@@ -3,11 +3,8 @@ use std::error;
3
3
use clap:: { Parser , Subcommand } ;
4
4
use jsonrpc_core:: futures;
5
5
use spacesvm:: {
6
- api:: {
7
- client:: { claim_tx, delete_tx, get_or_create_pk, set_tx, Client , Uri } ,
8
- DecodeTxArgs , IssueTxArgs , ResolveArgs ,
9
- } ,
10
- chain:: tx:: { decoder, unsigned:: TransactionData } ,
6
+ api:: client:: { claim_tx, delete_tx, get_or_create_pk, set_tx, Client , Uri } ,
7
+ chain:: tx:: unsigned:: TransactionData ,
11
8
} ;
12
9
13
10
#[ derive( Subcommand , Debug ) ]
@@ -46,51 +43,44 @@ struct Cli {
46
43
#[ command( subcommand) ]
47
44
command : Command ,
48
45
}
49
-
50
46
#[ tokio:: main]
51
47
async fn main ( ) -> Result < ( ) , Box < dyn error:: Error > > {
52
48
let cli = Cli :: parse ( ) ;
53
49
54
- let secret_key = get_or_create_pk ( & cli. private_key_file ) ?;
50
+ let private_key = get_or_create_pk ( & cli. private_key_file ) ?;
55
51
let uri = cli. endpoint . parse :: < Uri > ( ) ?;
56
- let mut client = Client :: new ( uri) ;
52
+ let client = Client :: new ( uri) ;
53
+ client. set_private_key ( private_key) . await ;
57
54
58
55
if let Command :: Get { space, key } = & cli. command {
59
- let resp = futures:: executor:: block_on ( client. resolve ( ResolveArgs {
60
- space : space. as_bytes ( ) . to_vec ( ) ,
61
- key : key. as_bytes ( ) . to_vec ( ) ,
62
- } ) )
63
- . map_err ( |e| e. to_string ( ) ) ?;
56
+ let resp = client
57
+ . resolve ( space, key)
58
+ . await
59
+ . map_err ( |e| e. to_string ( ) ) ?;
64
60
log:: debug!( "resolve response: {:?}" , resp) ;
65
61
66
62
println ! ( "{}" , serde_json:: to_string( & resp) ?) ;
67
63
return Ok ( ( ) ) ;
68
64
}
69
65
70
66
if let Command :: Ping { } = & cli. command {
71
- let resp = futures :: executor :: block_on ( client. ping ( ) ) . map_err ( |e| e. to_string ( ) ) ?;
67
+ let resp = client. ping ( ) . await . map_err ( |e| e. to_string ( ) ) ?;
72
68
73
69
println ! ( "{}" , serde_json:: to_string( & resp) ?) ;
74
70
return Ok ( ( ) ) ;
75
71
}
76
72
77
73
// decode tx
78
74
let tx_data = command_to_tx ( cli. command ) ?;
79
- let resp = futures:: executor:: block_on ( client. decode_tx ( DecodeTxArgs { tx_data } ) )
80
- . map_err ( |e| e. to_string ( ) ) ?;
75
+ let resp = futures:: executor:: block_on ( client. decode_tx ( tx_data) ) . map_err ( |e| e. to_string ( ) ) ?;
81
76
82
77
let typed_data = & resp. typed_data ;
83
78
84
- // create signature
85
- let dh = decoder:: hash_structured_data ( typed_data) ?;
86
- let sig = secret_key. sign_digest ( & dh. as_bytes ( ) ) ?;
87
-
88
79
// issue tx
89
- let resp = futures:: executor:: block_on ( client. issue_tx ( IssueTxArgs {
90
- typed_data : resp. typed_data ,
91
- signature : sig. to_bytes ( ) . to_vec ( ) ,
92
- } ) )
93
- . map_err ( |e| e. to_string ( ) ) ?;
80
+ let resp = client
81
+ . issue_tx ( typed_data)
82
+ . await
83
+ . map_err ( |e| e. to_string ( ) ) ?;
94
84
println ! ( "{}" , serde_json:: to_string( & resp) ?) ;
95
85
96
86
Ok ( ( ) )
@@ -99,9 +89,9 @@ async fn main() -> Result<(), Box<dyn error::Error>> {
99
89
/// Takes a TX command and returns transaction data.
100
90
fn command_to_tx ( command : Command ) -> std:: io:: Result < TransactionData > {
101
91
match command {
102
- Command :: Claim { space } => Ok ( claim_tx ( space) ) ,
103
- Command :: Set { space, key, value } => Ok ( set_tx ( space, key, value. as_bytes ( ) . to_vec ( ) ) ) ,
104
- Command :: Delete { space, key } => Ok ( delete_tx ( space, key) ) ,
92
+ Command :: Claim { space } => Ok ( claim_tx ( & space) ) ,
93
+ Command :: Set { space, key, value } => Ok ( set_tx ( & space, & key, & value) ) ,
94
+ Command :: Delete { space, key } => Ok ( delete_tx ( & space, & key) ) ,
105
95
_ => Err ( std:: io:: Error :: new (
106
96
std:: io:: ErrorKind :: Other ,
107
97
"not a supported tx" ,
0 commit comments