1
- use std:: error;
2
- use std:: fs:: File ;
3
- use std:: io:: { Result , Write } ;
4
- use std:: path:: Path ;
1
+ use std:: {
2
+ error,
3
+ fs:: File ,
4
+ io:: { Result , Write } ,
5
+ path:: Path ,
6
+ } ;
5
7
6
8
use avalanche_types:: key;
7
9
use clap:: { Parser , Subcommand } ;
8
10
use jsonrpc_client_transports:: { transports, RpcError } ;
9
11
use jsonrpc_core:: futures;
10
12
use spacesvm:: {
11
- api:: { DecodeTxArgs , IssueTxArgs , ResolveArgs , ServiceClient as Client } ,
13
+ api:: {
14
+ DecodeTxArgs , IssueTxArgs , IssueTxResponse , PingResponse , ResolveArgs , ResolveResponse ,
15
+ ServiceClient as Client ,
16
+ } ,
12
17
chain:: tx:: { decoder, tx:: TransactionType , unsigned:: TransactionData } ,
13
18
} ;
14
19
@@ -46,6 +51,7 @@ enum Command {
46
51
space : String ,
47
52
key : String ,
48
53
} ,
54
+ Ping { } ,
49
55
}
50
56
51
57
#[ tokio:: main]
@@ -55,22 +61,36 @@ async fn main() -> std::result::Result<(), Box<dyn error::Error>> {
55
61
let secret_key = get_or_create_pk ( & cli. private_key_file ) ?;
56
62
let connection = transports:: http:: connect :: < Client > ( & cli. endpoint ) ;
57
63
let client = futures:: executor:: block_on ( connection) ?;
58
- ping ( & client) . await ?;
59
64
65
+ // prints the value returned if available.
60
66
if let Command :: Get { space, key } = & cli. command {
61
- futures:: executor:: block_on ( client. resolve ( ResolveArgs {
62
- space : space. as_bytes ( ) . to_vec ( ) ,
63
- key : key. as_bytes ( ) . to_vec ( ) ,
64
- } ) )
65
- . map_err ( |e| e. to_string ( ) ) ?;
67
+ let resp =
68
+ futures:: executor:: block_on ( get ( & client, space, key) ) . map_err ( |e| e. to_string ( ) ) ?;
69
+ log:: debug!( "{:?}" , resp) ;
70
+
71
+ println ! ( "{}" , String :: from_utf8_lossy( & resp. value) ) ;
72
+ return Ok ( ( ) ) ;
73
+ }
74
+
75
+ // returns on success and errors on failure
76
+ if let Command :: Ping { } = & cli. command {
77
+ let resp = futures:: executor:: block_on ( ping ( & client) ) . map_err ( |e| e. to_string ( ) ) ?;
78
+ log:: debug!( "{:?}" , resp) ;
79
+
80
+ return Ok ( ( ) ) ;
66
81
}
67
82
68
83
let tx = command_to_tx ( cli. command ) ?;
69
84
70
- futures:: executor:: block_on ( sign_and_submit ( & client, & secret_key, tx) )
71
- . map_err ( |e| e. to_string ( ) . into ( ) )
85
+ // prints the id of a successful transaction.
86
+ let resp = futures:: executor:: block_on ( sign_and_submit ( & client, & secret_key, tx) )
87
+ . map_err ( |e| e. to_string ( ) ) ?;
88
+ println ! ( "{}" , resp. tx_id) ;
89
+
90
+ Ok ( ( ) )
72
91
}
73
92
93
+ /// Takes a TX command and returns transaction data.
74
94
fn command_to_tx ( command : Command ) -> Result < TransactionData > {
75
95
match command {
76
96
Command :: Claim { space } => Ok ( claim_tx ( space) ) ,
@@ -83,6 +103,7 @@ fn command_to_tx(command: Command) -> Result<TransactionData> {
83
103
}
84
104
}
85
105
106
+ /// Returns a private key from a given path or creates new.
86
107
fn get_or_create_pk ( path : & str ) -> Result < key:: secp256k1:: private_key:: Key > {
87
108
if !Path :: new ( path) . try_exists ( ) ? {
88
109
let secret_key = key:: secp256k1:: private_key:: Key :: generate ( ) . unwrap ( ) ;
@@ -125,20 +146,18 @@ fn delete_tx(space: String, key: String) -> TransactionData {
125
146
}
126
147
}
127
148
128
- async fn ping ( client : & Client ) -> Result < ( ) > {
149
+ async fn ping ( client : & Client ) -> Result < PingResponse > {
129
150
let error_handling =
130
151
|e : RpcError | std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , e. to_string ( ) ) ;
131
- let resp = client. ping ( ) . await . map_err ( error_handling) ;
132
- dbg ! ( resp. is_ok( ) ) ;
133
- dbg ! ( & resp) ;
134
- Ok ( ( ) )
152
+ client. ping ( ) . await . map_err ( error_handling)
135
153
}
136
154
155
+ /// Decodes transaction signs the typed data ans issues tx returning IssueTxResponse.
137
156
async fn sign_and_submit (
138
157
client : & Client ,
139
158
pk : & key:: secp256k1:: private_key:: Key ,
140
159
tx_data : TransactionData ,
141
- ) -> Result < ( ) > {
160
+ ) -> Result < IssueTxResponse > {
142
161
let error_handling =
143
162
|e : RpcError | std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , dbg ! ( e) . to_string ( ) ) ;
144
163
let resp = client
@@ -151,13 +170,25 @@ async fn sign_and_submit(
151
170
let dh = decoder:: hash_structured_data ( typed_data) ?;
152
171
let sig = pk. sign_digest ( & dh. as_bytes ( ) ) ?;
153
172
154
- let resp = client
173
+ client
155
174
. issue_tx ( IssueTxArgs {
156
175
typed_data : resp. typed_data ,
157
176
signature : sig. to_bytes ( ) . to_vec ( ) ,
158
177
} )
159
178
. await
160
- . map_err ( error_handling) ?;
161
- println ! ( "response: {:?}" , resp) ;
162
- Ok ( ( ) )
179
+ . map_err ( error_handling)
180
+ }
181
+
182
+ /// Get returns a ResolveResponse.
183
+ async fn get ( client : & Client , space : & str , key : & str ) -> Result < ResolveResponse > {
184
+ let error_handling =
185
+ |e : RpcError | std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , dbg ! ( e) . to_string ( ) ) ;
186
+
187
+ client
188
+ . resolve ( ResolveArgs {
189
+ space : space. as_bytes ( ) . to_vec ( ) ,
190
+ key : key. as_bytes ( ) . to_vec ( ) ,
191
+ } )
192
+ . await
193
+ . map_err ( error_handling)
163
194
}
0 commit comments