Skip to content

Commit e9d9965

Browse files
committed
rust: use proxy for electrum requests
1 parent 434f439 commit e9d9965

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

subprojects/gdk_rust/gdk_electrum/src/account.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,12 +674,13 @@ pub fn discover_accounts(
674674
master_xprv: &ExtendedPrivKey,
675675
network_id: NetworkId,
676676
electrum_url: &ElectrumUrl,
677+
proxy: Option<&str>,
677678
master_blinding: Option<&MasterBlindingKey>,
678679
) -> Result<Vec<u32>, Error> {
679680
use electrum_client::ElectrumApi;
680681

681682
// build our own client so that the subscriptions are dropped at the end
682-
let client = electrum_url.build_client()?;
683+
let client = electrum_url.build_client(proxy)?;
683684

684685
// the batch size is the effective gap limit for our purposes. in reality it is a lower bound.
685686
let gap_limit = BATCH_SIZE;

subprojects/gdk_rust/gdk_electrum/src/headers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn spv_verify_tx(input: &SPVVerifyTx) -> Result<SPVVerifyResult, Error> {
7373
}
7474

7575
let url = determine_electrum_url_from_net(&input.network)?;
76-
let client = url.build_client()?;
76+
let client = url.build_client(input.tor_proxy.as_deref())?;
7777

7878
match input.network.id() {
7979
NetworkId::Bitcoin(bitcoin_network) => {

subprojects/gdk_rust/gdk_electrum/src/interface.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ pub enum ElectrumUrl {
4242
}
4343

4444
impl ElectrumUrl {
45-
pub fn build_client(&self) -> Result<Client, Error> {
46-
self.build_config(ConfigBuilder::new())
45+
pub fn build_client(&self, proxy: Option<&str>) -> Result<Client, Error> {
46+
let mut config = ConfigBuilder::new();
47+
if let Some(proxy) = proxy {
48+
// TODO: add support for credentials?
49+
config = config.socks5(Some(electrum_client::Socks5Config::new(proxy)))?;
50+
}
51+
self.build_config(config)
4752
}
4853

4954
pub fn build_config(&self, config: ConfigBuilder) -> Result<Client, Error> {
@@ -163,11 +168,16 @@ impl WalletCtx {
163168
self.get_account(opt.subaccount)?.set_settings(opt)
164169
}
165170

166-
pub fn recover_accounts(&mut self, electrum_url: &ElectrumUrl) -> Result<(), Error> {
171+
pub fn recover_accounts(
172+
&mut self,
173+
electrum_url: &ElectrumUrl,
174+
proxy: Option<&str>,
175+
) -> Result<(), Error> {
167176
let account_nums = discover_accounts(
168177
&self.master_xprv,
169178
self.network.id(),
170179
electrum_url,
180+
proxy,
171181
self.master_blinding.as_ref(),
172182
)?;
173183
for account_num in account_nums {

subprojects/gdk_rust/gdk_electrum/src/lib.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ impl Session<Error> for ElectrumSession {
455455
notify_block(self.notify.clone(), tip_height);
456456

457457
info!("building client");
458-
if let Ok(fee_client) = self.url.build_client() {
458+
if let Ok(fee_client) = self.url.build_client(self.proxy.as_deref()) {
459459
info!("building built end");
460460
let fee_store = store.clone();
461461
thread::spawn(move || {
@@ -492,6 +492,7 @@ impl Session<Error> for ElectrumSession {
492492
};
493493

494494
let headers_url = self.url.clone();
495+
let proxy = self.proxy.clone();
495496
let (close_headers, r) = channel();
496497
self.closer.senders.push(close_headers);
497498
let notify_headers = self.notify.clone();
@@ -506,7 +507,7 @@ impl Session<Error> for ElectrumSession {
506507
break;
507508
}
508509

509-
if let Ok(client) = headers_url.build_client() {
510+
if let Ok(client) = headers_url.build_client(proxy.as_deref()) {
510511
loop {
511512
if r.try_recv().is_ok() {
512513
info!("closing headers thread");
@@ -579,7 +580,7 @@ impl Session<Error> for ElectrumSession {
579580

580581
// Recover BIP 44 accounts on the first login
581582
if !store.read().unwrap().cache.accounts_recovered {
582-
wallet.write().unwrap().recover_accounts(&self.url)?;
583+
wallet.write().unwrap().recover_accounts(&self.url, self.proxy.as_deref())?;
583584
store.write().unwrap().cache.accounts_recovered = true;
584585
}
585586

@@ -602,10 +603,11 @@ impl Session<Error> for ElectrumSession {
602603
let (close_tipper, r) = channel();
603604
self.closer.senders.push(close_tipper);
604605
let tipper_url = self.url.clone();
606+
let proxy = self.proxy.clone();
605607
let tipper_handle = thread::spawn(move || {
606608
info!("starting tipper thread");
607609
loop {
608-
if let Ok(client) = tipper_url.build_client() {
610+
if let Ok(client) = tipper_url.build_client(proxy.as_deref()) {
609611
match tipper.tip(&client) {
610612
Ok(current_tip) => {
611613
if tip_height != current_tip {
@@ -631,10 +633,11 @@ impl Session<Error> for ElectrumSession {
631633
self.closer.senders.push(close_syncer);
632634
let notify_txs = self.notify.clone();
633635
let syncer_url = self.url.clone();
636+
let proxy = self.proxy.clone();
634637
let syncer_handle = thread::spawn(move || {
635638
info!("starting syncer thread");
636639
loop {
637-
match syncer_url.build_client() {
640+
match syncer_url.build_client(proxy.as_deref()) {
638641
Ok(client) => match syncer.sync(&client) {
639642
Ok(updated_accounts) => {
640643
for account_num in updated_accounts {
@@ -764,7 +767,7 @@ impl Session<Error> for ElectrumSession {
764767

765768
fn send_transaction(&mut self, tx: &TransactionMeta) -> Result<String, Error> {
766769
info!("electrum send_transaction {:#?}", tx);
767-
let client = self.url.build_client()?;
770+
let client = self.url.build_client(self.proxy.as_deref())?;
768771
let tx_bytes = hex::decode(&tx.hex)?;
769772
let txid = client.transaction_broadcast_raw(&tx_bytes)?;
770773
Ok(format!("{}", txid))
@@ -774,7 +777,7 @@ impl Session<Error> for ElectrumSession {
774777
let transaction = BETransaction::from_hex(&tx_hex, self.network.id())?;
775778

776779
info!("broadcast_transaction {:#?}", transaction.txid());
777-
let client = self.url.build_client()?;
780+
let client = self.url.build_client(self.proxy.as_deref())?;
778781
let hex = hex::decode(tx_hex)?;
779782
let txid = client.transaction_broadcast_raw(&hex)?;
780783
Ok(format!("{}", txid))
@@ -790,7 +793,7 @@ impl Session<Error> for ElectrumSession {
790793
NetworkId::Bitcoin(_) => 1000,
791794
NetworkId::Elements(_) => 100,
792795
};
793-
let fee_estimates = try_get_fee_estimates(&self.url.build_client()?)
796+
let fee_estimates = try_get_fee_estimates(&self.url.build_client(self.proxy.as_deref())?)
794797
.unwrap_or_else(|_| vec![FeeEstimate(min_fee); 25]);
795798
self.get_wallet()?.store.write()?.cache.fee_estimates = fee_estimates.clone();
796799
Ok(fee_estimates)

0 commit comments

Comments
 (0)