@@ -19,7 +19,7 @@ use std::time::Duration;
1919#[ allow( unused_imports) ]
2020use log:: { debug, error, info, trace} ;
2121
22- use ureq:: { Agent , Response } ;
22+ use ureq:: { Agent , Proxy , Response } ;
2323
2424use bitcoin:: consensus:: { deserialize, serialize} ;
2525use bitcoin:: hashes:: hex:: { FromHex , ToHex } ;
@@ -59,7 +59,7 @@ impl std::convert::From<UrlClient> for EsploraBlockchain {
5959}
6060
6161impl EsploraBlockchain {
62- /// Create a new instance of the client from a base URL and `stop_gap`.
62+ /// Create a new instance of the client from a base URL and the `stop_gap`.
6363 pub fn new ( base_url : & str , stop_gap : usize ) -> Self {
6464 EsploraBlockchain {
6565 url_client : UrlClient {
@@ -358,6 +358,17 @@ impl ElectrumLikeSync for UrlClient {
358358pub struct EsploraBlockchainConfig {
359359 /// Base URL of the esplora service eg. `https://blockstream.info/api/`
360360 pub base_url : String ,
361+ /// Optional URL of the proxy to use to make requests to the Esplora server
362+ ///
363+ /// The string should be formatted as: `<protocol>://<user>:<password>@host:<port>`.
364+ ///
365+ /// Note that the format of this value and the supported protocols change slightly between the
366+ /// sync version of esplora (using `ureq`) and the async version (using `reqwest`). For more
367+ /// details check with the documentation of the two crates. Both of them are compiled with
368+ /// the `socks` feature enabled.
369+ ///
370+ /// The proxy is ignored when targeting `wasm32`.
371+ pub proxy : Option < String > ,
361372 /// Socket read timeout.
362373 pub timeout_read : u64 ,
363374 /// Socket write timeout.
@@ -370,10 +381,18 @@ impl ConfigurableBlockchain for EsploraBlockchain {
370381 type Config = EsploraBlockchainConfig ;
371382
372383 fn from_config ( config : & Self :: Config ) -> Result < Self , Error > {
373- let agent : Agent = ureq:: AgentBuilder :: new ( )
384+ let mut agent_builder = ureq:: AgentBuilder :: new ( )
374385 . timeout_read ( Duration :: from_secs ( config. timeout_read ) )
375- . timeout_write ( Duration :: from_secs ( config. timeout_write ) )
376- . build ( ) ;
377- Ok ( EsploraBlockchain :: new ( config. base_url . as_str ( ) , config. stop_gap ) . with_agent ( agent) )
386+ . timeout_write ( Duration :: from_secs ( config. timeout_write ) ) ;
387+
388+ if let Some ( proxy) = & config. proxy {
389+ agent_builder = agent_builder
390+ . proxy ( Proxy :: new ( proxy) . map_err ( |e| Error :: Esplora ( Box :: new ( e. into ( ) ) ) ) ?) ;
391+ }
392+
393+ Ok (
394+ EsploraBlockchain :: new ( config. base_url . as_str ( ) , config. stop_gap )
395+ . with_agent ( agent_builder. build ( ) ) ,
396+ )
378397 }
379398}
0 commit comments