Skip to content

Commit a9ce6a3

Browse files
Add RPC proxy options to RpcConfig
Handles proxy options to create a socks5 stream through proxy to rpc host. Creates regular http connection otherwise.
1 parent 42b3df9 commit a9ce6a3

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/blockchain/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ pub use self::rpc::RpcBlockchain;
6060
#[cfg(feature = "rpc")]
6161
pub use self::rpc::RpcConfig;
6262

63+
#[cfg(feature = "rpc")]
64+
pub mod rpc_proxy;
65+
6366
#[cfg(feature = "esplora")]
6467
#[cfg_attr(docsrs, doc(cfg(feature = "esplora")))]
6568
pub mod esplora;

src/blockchain/rpc.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
//! auth: Auth::Cookie {
2525
//! file: "/home/user/.bitcoin/.cookie".into(),
2626
//! },
27+
//! proxy: None,
28+
//! proxy_auth: None,
2729
//! network: bdk::bitcoin::Network::Testnet,
2830
//! wallet_name: "wallet_name".to_string(),
2931
//! skip_blocks: None,
3032
//! };
3133
//! let blockchain = RpcBlockchain::from_config(&config);
3234
//! ```
3335
36+
use super::rpc_proxy::ProxyTransport;
3437
use crate::bitcoin::consensus::deserialize;
3538
use crate::bitcoin::{Address, Network, OutPoint, Transaction, TxOut, Txid};
3639
use crate::blockchain::{Blockchain, Capability, ConfigurableBlockchain, Progress};
@@ -60,7 +63,6 @@ pub struct RpcBlockchain {
6063
capabilities: HashSet<Capability>,
6164
/// Skip this many blocks of the blockchain at the first rescan, if None the rescan is done from the genesis block
6265
skip_blocks: Option<u32>,
63-
6466
/// This is a fixed Address used as a hack key to store information on the node
6567
_storage_address: Address,
6668
}
@@ -72,6 +74,10 @@ pub struct RpcConfig {
7274
pub url: String,
7375
/// The bitcoin node authentication mechanism
7476
pub auth: Auth,
77+
/// A proxy (like Tor) can be used to connect Bitcoin Core RPC
78+
pub proxy: Option<String>,
79+
/// Authentication for proxy server
80+
pub proxy_auth: Option<(String, String)>,
7581
/// The network we are using (it will be checked the bitcoin node network matches this)
7682
pub network: Network,
7783
/// The wallet name in the bitcoin node, consider using [crate::wallet::wallet_name_from_descriptor] for this
@@ -358,7 +364,20 @@ impl ConfigurableBlockchain for RpcBlockchain {
358364
let wallet_url = format!("{}/wallet/{}", config.url, &wallet_name);
359365
debug!("connecting to {} auth:{:?}", wallet_url, config.auth);
360366

361-
let client = Client::new(wallet_url.as_str(), config.auth.clone().into())?;
367+
let client = if let Some(proxy) = &config.proxy {
368+
let proxy_transport = ProxyTransport::new(
369+
proxy,
370+
wallet_url.as_str(),
371+
config.proxy_auth.clone(),
372+
&config.auth,
373+
)?;
374+
Client::from_jsonrpc(bitcoincore_rpc::jsonrpc::Client::with_transport(
375+
proxy_transport,
376+
))
377+
} else {
378+
Client::new(wallet_url.as_str(), config.auth.clone().into())?
379+
};
380+
362381
let loaded_wallets = client.list_wallets()?;
363382
if loaded_wallets.contains(&wallet_name) {
364383
debug!("wallet already loaded {:?}", wallet_name);
@@ -437,6 +456,8 @@ crate::bdk_blockchain_tests! {
437456
let config = RpcConfig {
438457
url: test_client.bitcoind.rpc_url(),
439458
auth: Auth::Cookie { file: test_client.bitcoind.params.cookie_file.clone() },
459+
proxy: None,
460+
proxy_auth: None,
440461
network: Network::Regtest,
441462
wallet_name: format!("client-wallet-test-{:?}", std::time::SystemTime::now() ),
442463
skip_blocks: None,

0 commit comments

Comments
 (0)