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+ } ;
57
68use avalanche_types:: key;
79use clap:: { Parser , Subcommand } ;
810use jsonrpc_client_transports:: { transports, RpcError } ;
911use jsonrpc_core:: futures;
1012use spacesvm:: {
11- api:: { DecodeTxArgs , IssueTxArgs , ResolveArgs , ServiceClient as Client } ,
13+ api:: {
14+ DecodeTxArgs , IssueTxArgs , IssueTxResponse , PingResponse , ResolveArgs , ResolveResponse ,
15+ ServiceClient as Client ,
16+ } ,
1217 chain:: tx:: { decoder, tx:: TransactionType , unsigned:: TransactionData } ,
1318} ;
1419
@@ -46,6 +51,7 @@ enum Command {
4651 space : String ,
4752 key : String ,
4853 } ,
54+ Ping { } ,
4955}
5056
5157#[ tokio:: main]
@@ -55,22 +61,36 @@ async fn main() -> std::result::Result<(), Box<dyn error::Error>> {
5561 let secret_key = get_or_create_pk ( & cli. private_key_file ) ?;
5662 let connection = transports:: http:: connect :: < Client > ( & cli. endpoint ) ;
5763 let client = futures:: executor:: block_on ( connection) ?;
58- ping ( & client) . await ?;
5964
65+ // prints the value returned if available.
6066 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 ( ( ) ) ;
6681 }
6782
6883 let tx = command_to_tx ( cli. command ) ?;
6984
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 ( ( ) )
7291}
7392
93+ /// Takes a TX command and returns transaction data.
7494fn command_to_tx ( command : Command ) -> Result < TransactionData > {
7595 match command {
7696 Command :: Claim { space } => Ok ( claim_tx ( space) ) ,
@@ -83,6 +103,7 @@ fn command_to_tx(command: Command) -> Result<TransactionData> {
83103 }
84104}
85105
106+ /// Returns a private key from a given path or creates new.
86107fn get_or_create_pk ( path : & str ) -> Result < key:: secp256k1:: private_key:: Key > {
87108 if !Path :: new ( path) . try_exists ( ) ? {
88109 let secret_key = key:: secp256k1:: private_key:: Key :: generate ( ) . unwrap ( ) ;
@@ -125,20 +146,18 @@ fn delete_tx(space: String, key: String) -> TransactionData {
125146 }
126147}
127148
128- async fn ping ( client : & Client ) -> Result < ( ) > {
149+ async fn ping ( client : & Client ) -> Result < PingResponse > {
129150 let error_handling =
130151 |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)
135153}
136154
155+ /// Decodes transaction signs the typed data ans issues tx returning IssueTxResponse.
137156async fn sign_and_submit (
138157 client : & Client ,
139158 pk : & key:: secp256k1:: private_key:: Key ,
140159 tx_data : TransactionData ,
141- ) -> Result < ( ) > {
160+ ) -> Result < IssueTxResponse > {
142161 let error_handling =
143162 |e : RpcError | std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , dbg ! ( e) . to_string ( ) ) ;
144163 let resp = client
@@ -151,13 +170,25 @@ async fn sign_and_submit(
151170 let dh = decoder:: hash_structured_data ( typed_data) ?;
152171 let sig = pk. sign_digest ( & dh. as_bytes ( ) ) ?;
153172
154- let resp = client
173+ client
155174 . issue_tx ( IssueTxArgs {
156175 typed_data : resp. typed_data ,
157176 signature : sig. to_bytes ( ) . to_vec ( ) ,
158177 } )
159178 . 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)
163194}
0 commit comments