@@ -19,7 +19,7 @@ use std::time::Duration;
19
19
#[ allow( unused_imports) ]
20
20
use log:: { debug, error, info, trace} ;
21
21
22
- use ureq:: { Agent , Response } ;
22
+ use ureq:: { Agent , Proxy , Response } ;
23
23
24
24
use bitcoin:: consensus:: { deserialize, serialize} ;
25
25
use bitcoin:: hashes:: hex:: { FromHex , ToHex } ;
@@ -59,7 +59,7 @@ impl std::convert::From<UrlClient> for EsploraBlockchain {
59
59
}
60
60
61
61
impl 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`.
63
63
pub fn new ( base_url : & str , stop_gap : usize ) -> Self {
64
64
EsploraBlockchain {
65
65
url_client : UrlClient {
@@ -358,6 +358,17 @@ impl ElectrumLikeSync for UrlClient {
358
358
pub struct EsploraBlockchainConfig {
359
359
/// Base URL of the esplora service eg. `https://blockstream.info/api/`
360
360
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 > ,
361
372
/// Socket read timeout.
362
373
pub timeout_read : u64 ,
363
374
/// Socket write timeout.
@@ -370,10 +381,18 @@ impl ConfigurableBlockchain for EsploraBlockchain {
370
381
type Config = EsploraBlockchainConfig ;
371
382
372
383
fn from_config ( config : & Self :: Config ) -> Result < Self , Error > {
373
- let agent : Agent = ureq:: AgentBuilder :: new ( )
384
+ let mut agent_builder = ureq:: AgentBuilder :: new ( )
374
385
. 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
+ )
378
397
}
379
398
}
0 commit comments