22
33//! Async JSON-RPC clients for specific versions of Bitcoin Core.
44
5+ pub mod bdk_client;
56mod error;
6- pub mod v17;
7- pub mod v18;
8- pub mod v19;
9- pub mod v20;
10- pub mod v21;
11- pub mod v22;
12- pub mod v23;
13- pub mod v24;
14- pub mod v25;
15- pub mod v26;
16- pub mod v27;
17- pub mod v28;
18- pub mod v29;
19- pub mod v30;
207
8+ use std:: fmt;
219use std:: fs:: File ;
2210use std:: io:: { BufRead , BufReader } ;
2311use std:: path:: PathBuf ;
@@ -55,76 +43,63 @@ impl Auth {
5543 }
5644}
5745
58- /// Defines a async `jsonrpc::Client` using `bitreq`.
59- #[ macro_export]
60- macro_rules! define_jsonrpc_bitreq_async_client {
61- ( $version: literal) => {
62- use std:: fmt;
63- use $crate:: client_async:: { log_response, Auth , Result } ;
64- use $crate:: client_async:: error:: Error ;
65-
66- /// Client implements an async JSON-RPC client for the Bitcoin Core daemon or compatible APIs.
67- pub struct Client {
68- inner: jsonrpc:: client_async:: Client ,
69- }
70-
71- impl fmt:: Debug for Client {
72- fn fmt( & self , f: & mut fmt:: Formatter ) -> core:: fmt:: Result {
73- write!(
74- f,
75- "corepc_client::client_async::{}::Client({:?})" ,
76- $version, self . inner
77- )
78- }
79- }
46+ /// Client implements an async JSON-RPC client for the Bitcoin Core daemon or compatible APIs.
47+ pub struct Client {
48+ pub ( crate ) inner : jsonrpc:: client_async:: Client ,
49+ }
8050
81- impl Client {
82- /// Creates a client to a bitcoind JSON-RPC server without authentication.
83- pub fn new( url: & str ) -> Self {
84- let transport = jsonrpc:: bitreq_http_async:: Builder :: new( )
85- . url( url)
86- . expect( "jsonrpc v0.19, this function does not error" )
87- . timeout( std:: time:: Duration :: from_secs( 60 ) )
88- . build( ) ;
89- let inner = jsonrpc:: client_async:: Client :: with_transport( transport) ;
90-
91- Self { inner }
92- }
51+ impl fmt:: Debug for Client {
52+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> core:: fmt:: Result {
53+ write ! ( f, "corepc_client::client_async::Client({:?})" , self . inner)
54+ }
55+ }
9356
94- /// Creates a client to a bitcoind JSON-RPC server with authentication.
95- pub fn new_with_auth( url: & str , auth: Auth ) -> Result <Self > {
96- if matches!( auth, Auth :: None ) {
97- return Err ( Error :: MissingUserPassword ) ;
98- }
99- let ( user, pass) = auth. get_user_pass( ) ?;
100- let transport = jsonrpc:: bitreq_http_async:: Builder :: new( )
101- . url( url)
102- . expect( "jsonrpc v0.19, this function does not error" )
103- . timeout( std:: time:: Duration :: from_secs( 60 ) )
104- . basic_auth( user. unwrap( ) , pass)
105- . build( ) ;
106- let inner = jsonrpc:: client_async:: Client :: with_transport( transport) ;
107-
108- Ok ( Self { inner } )
109- }
57+ impl Client {
58+ /// Creates a client to a bitcoind JSON-RPC server without authentication.
59+ pub fn new ( url : & str ) -> Self {
60+ let transport = jsonrpc:: bitreq_http_async:: Builder :: new ( )
61+ . url ( url)
62+ . expect ( "jsonrpc v0.19, this function does not error" )
63+ . timeout ( std:: time:: Duration :: from_secs ( 60 ) )
64+ . build ( ) ;
65+ let inner = jsonrpc:: client_async:: Client :: with_transport ( transport) ;
66+
67+ Self { inner }
68+ }
11069
111- /// Call an RPC `method` with given `args` list.
112- pub async fn call<T : for <' a> serde:: de:: Deserialize <' a>>(
113- & self ,
114- method: & str ,
115- args: & [ serde_json:: Value ] ,
116- ) -> Result <T > {
117- let raw = serde_json:: value:: to_raw_value( args) ?;
118- let req = self . inner. build_request( & method, Some ( & * raw) ) ;
119- if log:: log_enabled!( log:: Level :: Debug ) {
120- log:: debug!( target: "corepc" , "request: {} {}" , method, serde_json:: Value :: from( args) ) ;
121- }
70+ /// Creates a client to a bitcoind JSON-RPC server with authentication.
71+ pub fn new_with_auth ( url : & str , auth : Auth ) -> Result < Self > {
72+ if matches ! ( auth, Auth :: None ) {
73+ return Err ( Error :: MissingUserPassword ) ;
74+ }
75+ let ( user, pass) = auth. get_user_pass ( ) ?;
76+ let user = user. ok_or ( Error :: MissingUserPassword ) ?;
77+ let transport = jsonrpc:: bitreq_http_async:: Builder :: new ( )
78+ . url ( url)
79+ . expect ( "jsonrpc v0.19, this function does not error" )
80+ . timeout ( std:: time:: Duration :: from_secs ( 60 ) )
81+ . basic_auth ( user, pass)
82+ . build ( ) ;
83+ let inner = jsonrpc:: client_async:: Client :: with_transport ( transport) ;
84+
85+ Ok ( Self { inner } )
86+ }
12287
123- let resp = self . inner. send_request( req) . await . map_err( Error :: from) ;
124- log_response( method, & resp) ;
125- Ok ( resp?. result( ) ?)
126- }
88+ /// Call an RPC `method` with given `args` list.
89+ pub async fn call < T : for < ' a > serde:: de:: Deserialize < ' a > > (
90+ & self ,
91+ method : & str ,
92+ args : & [ serde_json:: Value ] ,
93+ ) -> Result < T > {
94+ let raw = serde_json:: value:: to_raw_value ( args) ?;
95+ let req = self . inner . build_request ( method, Some ( & * raw) ) ;
96+ if log:: log_enabled!( log:: Level :: Debug ) {
97+ log:: debug!( target: "corepc" , "request: {} {}" , method, serde_json:: Value :: from( args) ) ;
12798 }
99+
100+ let resp = self . inner . send_request ( req) . await . map_err ( Error :: from) ;
101+ log_response ( method, & resp) ;
102+ Ok ( resp?. result ( ) ?)
128103 }
129104}
130105
@@ -156,7 +131,7 @@ macro_rules! impl_async_client_check_expected_server_version {
156131}
157132
158133/// Shorthand for converting a variable into a `serde_json::Value`.
159- fn into_json < T > ( val : T ) -> Result < serde_json:: Value >
134+ pub ( crate ) fn into_json < T > ( val : T ) -> Result < serde_json:: Value >
160135where
161136 T : serde:: ser:: Serialize ,
162137{
@@ -179,10 +154,12 @@ fn log_response(method: &str, resp: &Result<jsonrpc::Response>) {
179154 log:: debug!( target: "corepc" , "response error for {}: {:?}" , method, e) ;
180155 }
181156 } else if log:: log_enabled!( Trace ) {
182- let def =
183- serde_json:: value:: to_raw_value ( & serde_json:: value:: Value :: Null ) . unwrap ( ) ;
184- let result = resp. result . as_ref ( ) . unwrap_or ( & def) ;
185- log:: trace!( target: "corepc" , "response for {}: {}" , method, result) ;
157+ if let Ok ( def) =
158+ serde_json:: value:: to_raw_value ( & serde_json:: value:: Value :: Null )
159+ {
160+ let result = resp. result . as_ref ( ) . unwrap_or ( & def) ;
161+ log:: trace!( target: "corepc" , "response for {}: {}" , method, result) ;
162+ }
186163 } ,
187164 }
188165 }
0 commit comments