From 78cf4fd47cea46463911fd24d1029f2fe4cfec3a Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 26 Mar 2025 09:47:22 -0500 Subject: [PATCH] feat: add proxy to electrum and esplora --- bdk-ffi/src/bdk.udl | 6 ++++-- bdk-ffi/src/electrum.rs | 12 +++++++++--- bdk-ffi/src/esplora.rs | 9 ++++++--- bdk-ffi/src/tx_builder.rs | 2 +- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index 52d22783..f6c6fab9 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -921,7 +921,8 @@ interface Descriptor { /// cache to avoid re-fetching already downloaded transactions. interface EsploraClient { /// Creates a new bdk client from a esplora_client::BlockingClient - constructor(string url); + /// Optional: Set the proxy of the builder + constructor(string url, optional string? proxy = null); /// Scan keychain scripts for transactions against Esplora, returning an update that can be /// applied to the receiving structures. @@ -979,8 +980,9 @@ interface EsploraClient { /// cache to avoid re-fetching already downloaded transactions. interface ElectrumClient { /// Creates a new bdk client from a electrum_client::ElectrumApi + /// Optional: Set the proxy of the builder [Throws=ElectrumError] - constructor(string url); + constructor(string url, optional string? socks5 = null); /// Full scan the keychain scripts specified with the blockchain (via an Electrum client) and /// returns updates for bdk_chain data structures. diff --git a/bdk-ffi/src/electrum.rs b/bdk-ffi/src/electrum.rs index c5dcef4e..b26ed212 100644 --- a/bdk-ffi/src/electrum.rs +++ b/bdk-ffi/src/electrum.rs @@ -25,9 +25,15 @@ use std::sync::Arc; pub struct ElectrumClient(BdkBdkElectrumClient); impl ElectrumClient { - pub fn new(url: String) -> Result { - let inner_client: bdk_electrum::electrum_client::Client = - bdk_electrum::electrum_client::Client::new(url.as_str())?; + pub fn new(url: String, socks5: Option) -> Result { + let mut config = bdk_electrum::electrum_client::ConfigBuilder::new(); + if let Some(socks5) = socks5 { + config = config.socks5(Some(bdk_electrum::electrum_client::Socks5Config::new( + socks5.as_str(), + ))); + } + let inner_client = + bdk_electrum::electrum_client::Client::from_config(url.as_str(), config.build())?; let client = BdkBdkElectrumClient::new(inner_client); Ok(Self(client)) } diff --git a/bdk-ffi/src/esplora.rs b/bdk-ffi/src/esplora.rs index c00e7812..956deb24 100644 --- a/bdk-ffi/src/esplora.rs +++ b/bdk-ffi/src/esplora.rs @@ -23,9 +23,12 @@ use std::sync::Arc; pub struct EsploraClient(BlockingClient); impl EsploraClient { - pub fn new(url: String) -> Self { - let client = Builder::new(url.as_str()).build_blocking(); - Self(client) + pub fn new(url: String, proxy: Option) -> Self { + let mut builder = Builder::new(url.as_str()); + if let Some(proxy) = proxy { + builder = builder.proxy(proxy.as_str()); + } + Self(builder.build_blocking()) } pub fn full_scan( diff --git a/bdk-ffi/src/tx_builder.rs b/bdk-ffi/src/tx_builder.rs index 8ec3f053..7e3b0522 100644 --- a/bdk-ffi/src/tx_builder.rs +++ b/bdk-ffi/src/tx_builder.rs @@ -608,7 +608,7 @@ mod tests { Arc::new(Connection::new_in_memory().unwrap()), ) .unwrap(); - let client = EsploraClient::new("https://mutinynet.com/api/".to_string()); + let client = EsploraClient::new("https://mutinynet.com/api/".to_string(), None); let full_scan_builder = wallet.start_full_scan(); let full_scan_request = full_scan_builder .inspect_spks_for_all_keychains(Arc::new(FullScanInspector))