11use std:: {
22 fs:: File ,
3- io:: { Result , Write } ,
3+ io:: { Error , ErrorKind , Result , Write } ,
44 path:: Path ,
55} ;
66
@@ -9,12 +9,19 @@ use crate::{
99 DecodeTxArgs , DecodeTxResponse , IssueTxArgs , IssueTxResponse , PingResponse , ResolveArgs ,
1010 ResolveResponse ,
1111 } ,
12- chain:: tx:: { tx:: TransactionType , unsigned:: TransactionData } ,
12+ chain:: tx:: {
13+ decoder:: { self , TypedData } ,
14+ tx:: TransactionType ,
15+ unsigned:: TransactionData ,
16+ } ,
17+ } ;
18+ use avalanche_types:: key:: {
19+ self ,
20+ secp256k1:: { private_key:: Key , signature:: Sig } ,
1321} ;
14- use avalanche_types:: key;
1522use http:: { Method , Request } ;
1623use hyper:: { body, client:: HttpConnector , Body , Client as HyperClient } ;
17- use jsonrpc_core:: { Call , Id , MethodCall , Params , Version } ;
24+ use jsonrpc_core:: { Call , Id , MethodCall , Params , Value , Version } ;
1825use serde:: de;
1926
2027pub use http:: Uri ;
@@ -23,13 +30,19 @@ pub use http::Uri;
2330pub struct Client < C > {
2431 id : u64 ,
2532 client : HyperClient < C > ,
26- pub uri : Uri ,
33+ endpoint : Uri ,
34+ private_key : Option < Key > ,
2735}
2836
2937impl Client < HttpConnector > {
30- pub fn new ( uri : Uri ) -> Self {
38+ pub fn new ( endpoint : Uri ) -> Self {
3139 let client = HyperClient :: new ( ) ;
32- Self { id : 0 , client, uri }
40+ Self {
41+ id : 0 ,
42+ client,
43+ endpoint,
44+ private_key : None ,
45+ }
3346 }
3447}
3548
@@ -40,6 +53,16 @@ impl Client<HttpConnector> {
4053 Id :: Num ( id)
4154 }
4255
56+ pub fn set_endpoint ( mut self , endpoint : Uri ) -> Self {
57+ self . endpoint = endpoint;
58+ self
59+ }
60+
61+ pub fn set_private_key ( mut self , private_key : Key ) -> Self {
62+ self . private_key = Some ( private_key) ;
63+ self
64+ }
65+
4366 /// Returns a serialized json request as string and the request id.
4467 pub fn raw_request ( & mut self , method : & str , params : & Params ) -> ( Id , String ) {
4568 let id = self . next_id ( ) ;
@@ -55,6 +78,14 @@ impl Client<HttpConnector> {
5578 )
5679 }
5780
81+ /// Returns a recoverable signature from bytes.
82+ pub fn sign_digest ( & self , dh : & [ u8 ] ) -> Result < Sig > {
83+ if let Some ( pk) = & self . private_key {
84+ pk. sign_digest ( dh) ?;
85+ }
86+ Err ( Error :: new ( ErrorKind :: Other , "private key not set" ) )
87+ }
88+
5889 /// Returns a PingResponse from client request.
5990 pub async fn ping ( & mut self ) -> Result < PingResponse > {
6091 let ( _id, json_request) = self . raw_request ( "ping" , & Params :: None ) ;
@@ -64,40 +95,48 @@ impl Client<HttpConnector> {
6495 }
6596
6697 /// Returns a DecodeTxResponse from client request.
67- pub async fn decode_tx ( & mut self , args : DecodeTxArgs ) -> Result < DecodeTxResponse > {
68- let arg_bytes = serde_json:: to_vec ( & args) ?;
69- let params: Params = serde_json:: from_slice ( & arg_bytes) ?;
70- let ( _id, json_request) = self . raw_request ( "decodeTx" , & params) ;
98+ pub async fn decode_tx ( & mut self , tx_data : TransactionData ) -> Result < DecodeTxResponse > {
99+ let arg_value = serde_json:: to_value ( & DecodeTxArgs { tx_data } ) ?;
100+ let ( _id, json_request) = self . raw_request ( "decodeTx" , & Params :: Array ( vec ! [ arg_value] ) ) ;
71101 let resp = self . post_de :: < DecodeTxResponse > ( & json_request) . await ?;
72102
73103 Ok ( resp)
74104 }
75105
76106 /// Returns a IssueTxResponse from client request.
77- pub async fn issue_tx ( & mut self , args : IssueTxArgs ) -> Result < IssueTxResponse > {
78- let arg_bytes = serde_json:: to_vec ( & args) ?;
79- let params: Params = serde_json:: from_slice ( & arg_bytes) ?;
80- let ( _id, json_request) = self . raw_request ( "issueTx" , & params) ;
107+ pub async fn issue_tx ( & mut self , typed_data : & TypedData ) -> Result < IssueTxResponse > {
108+ let dh = decoder:: hash_structured_data ( typed_data) ?;
109+ let sig = self . sign_digest ( & dh. as_bytes ( ) ) ?. to_bytes ( ) . to_vec ( ) ;
110+ log:: debug!( "signature: {:?}" , sig) ;
111+
112+ let arg_value = serde_json:: to_value ( & IssueTxArgs {
113+ typed_data : typed_data. to_owned ( ) ,
114+ signature : sig,
115+ } ) ?;
116+ let ( _id, json_request) = self . raw_request ( "issueTx" , & Params :: Array ( vec ! [ arg_value] ) ) ;
81117 let resp = self . post_de :: < IssueTxResponse > ( & json_request) . await ?;
82118
83119 Ok ( resp)
84120 }
85121
86122 /// Returns a ResolveResponse from client request.
87- pub async fn resolve ( & mut self , args : ResolveArgs ) -> Result < ResolveResponse > {
88- let arg_bytes = serde_json:: to_vec ( & args) ?;
89- let params: Params = serde_json:: from_slice ( & arg_bytes) ?;
90- let ( _id, json_request) = self . raw_request ( "resolve" , & params) ;
123+ pub async fn resolve ( & mut self , space : & str , key : & str ) -> Result < ResolveResponse > {
124+ let arg_value = serde_json:: to_value ( & ResolveArgs {
125+ space : space. as_bytes ( ) . to_vec ( ) ,
126+ key : key. as_bytes ( ) . to_vec ( ) ,
127+ } ) ?;
128+ let ( _id, json_request) = self . raw_request ( "issueTx" , & Params :: Array ( vec ! [ arg_value] ) ) ;
91129 let resp = self . post_de :: < ResolveResponse > ( & json_request) . await ?;
92130
93131 Ok ( resp)
94132 }
95133
96134 /// Returns a deserialized response from client request.
97135 pub async fn post_de < T : de:: DeserializeOwned > ( & self , json : & str ) -> Result < T > {
136+ println ! ( "json: {}" , json) ;
98137 let req = Request :: builder ( )
99138 . method ( Method :: POST )
100- . uri ( self . uri . to_string ( ) )
139+ . uri ( self . endpoint . to_string ( ) )
101140 . header ( "content-type" , "application/json-rpc" )
102141 . body ( Body :: from ( json. to_owned ( ) ) )
103142 . map_err ( |e| {
@@ -107,20 +146,30 @@ impl Client<HttpConnector> {
107146 )
108147 } ) ?;
109148
110- let resp = self . client . request ( req) . await . map_err ( |e| {
149+ let mut resp = self . client . request ( req) . await . map_err ( |e| {
111150 std:: io:: Error :: new (
112151 std:: io:: ErrorKind :: Other ,
113152 format ! ( "client post request failed: {}" , e) ,
114153 )
115154 } ) ?;
116155
117- let bytes = body:: to_bytes ( resp. into_body ( ) )
156+ let bytes = body:: to_bytes ( resp. body_mut ( ) )
118157 . await
119158 . map_err ( |e| std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , e. to_string ( ) ) ) ?;
120- let resp = serde_json:: from_slice ( & bytes) . map_err ( |e| {
159+
160+ // deserialize bytes to value
161+ let v: Value = serde_json:: from_slice ( & bytes) . map_err ( |e| {
162+ std:: io:: Error :: new (
163+ std:: io:: ErrorKind :: Other ,
164+ format ! ( "failed to deserialize response to value: {}" , e) ,
165+ )
166+ } ) ?;
167+
168+ // deserialize result to T
169+ let resp = serde_json:: from_value ( v[ "result" ] . to_owned ( ) ) . map_err ( |e| {
121170 std:: io:: Error :: new (
122171 std:: io:: ErrorKind :: Other ,
123- format ! ( "failed to create client request : {}" , e) ,
172+ format ! ( "failed to deserialize response : {}" , e) ,
124173 )
125174 } ) ?;
126175
0 commit comments