Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ env_logger = "0.7"
electrsd = "0.21"
# Move back to importing from rust-bitcoin once https://github.com/rust-bitcoin/rust-bitcoin/pull/1342 is released
base64 = "^0.13"
libtor = "47.8.0+0.4.7.x"
ureq = {version = "*", features = ["json", "socks-proxy"]}

[[example]]
name = "compact_filters_balance"
Expand Down Expand Up @@ -146,12 +148,17 @@ required-features = ["electrum"]
[[example]]
name = "esplora_backend_synchronous"
path = "examples/esplora_backend_synchronous.rs"
required-features = ["use-esplora-ureq"]
required-features = ["use-esplora-blocking"]

[[example]]
name = "esplora_backend_asynchronous"
path = "examples/esplora_backend_asynchronous.rs"
required-features = ["use-esplora-reqwest", "reqwest-default-tls", "async-interface"]
required-features = ["use-esplora-async", "reqwest-default-tls", "async-interface"]

[[example]]
name = "esplora_backend_with_tor"
path = "examples/esplora_backend_with_tor.rs"
required-features = ["use-esplora-blocking"]

[[example]]
name = "mnemonic_to_descriptors"
Expand Down
2 changes: 1 addition & 1 deletion examples/esplora_backend_asynchronous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::utils::tx::build_signed_tx;
/// If enough amount is available, this will send a transaction to an address.
/// Otherwise, this will display a wallet address to receive funds.
///
/// This can be run with `cargo run --no-default-features --features="use-esplora-reqwest, reqwest-default-tls, async-interface" --example esplora_backend_asynchronous`
/// This can be run with `cargo run --no-default-features --features="use-esplora-async reqwest-default-tls async-interface" --example esplora_backend_asynchronous`
/// in the root folder.
#[tokio::main(flavor = "current_thread")]
async fn main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/esplora_backend_synchronous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::utils::tx::build_signed_tx;
/// If enough amount is available, this will send a transaction to an address.
/// Otherwise, this will display a wallet address to receive funds.
///
/// This can be run with `cargo run --features=use-esplora-ureq --example esplora_backend_synchronous`
/// This can be run with `cargo run --features=use-esplora-blocking --example esplora_backend_synchronous`
/// in the root folder.
fn main() {
let network = Network::Signet;
Expand Down
126 changes: 126 additions & 0 deletions examples/esplora_backend_with_tor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
use std::fs::OpenOptions;
use std::io::Read;
use std::str::FromStr;
use std::time::Duration;
use std::{env, thread};

use bitcoin::{util::bip32, Network};
use esplora_client::Builder;
use libtor::{LogDestination, LogLevel, Tor, TorFlag};

use bdk::{
blockchain::esplora::EsploraBlockchain, database::MemoryDatabase, template::Bip84,
KeychainKind, SyncOptions, Wallet,
};
/// This will create a wallet from an xpriv and sync it by connecting to an Esplora server
/// over Tor network, using blocking calls with `ureq`.
///
/// This can be run with `cargo run --features="use-esplora-blocking" --example esplora_backend_with_tor`
/// in the root folder.
fn main() {
let network = Network::Testnet;

let xpriv_str =
"tprv8ZgxMBicQKsPcx5nBGsR63Pe8KnRUqmbJNENAfGftF3yuXoMMoVJJcYeUw5eVkm9WBPjWYt6HMWYJNesB5HaNVBaFc1M6dRjWSYnmewUMYy";

let esplora_url =
"http://explorerzydxu5ecjrkwceayqybizmpjjznk5izmitf2modhcusuqlid.onion/testnet/api";

let xpriv = bip32::ExtendedPrivKey::from_str(xpriv_str).unwrap();

let socks_addr = start_tor();

let client = Builder::new(esplora_url)
.proxy(&format!("socks5://{}", socks_addr))
.timeout(120) // seconds
.build_blocking()
.expect("Should never fail with no proxy and timeout");

println!(
"Connecting to {} via SOCKS5 proxy {}",
&esplora_url, &socks_addr
);

let blockchain = EsploraBlockchain::from_client(client, 20);

let wallet = Wallet::new(
Bip84(xpriv, KeychainKind::External),
Some(Bip84(xpriv, KeychainKind::Internal)),
network,
MemoryDatabase::default(),
)
.unwrap();

println!("Syncing the wallet");

wallet.sync(&blockchain, SyncOptions::default()).unwrap();

println!("Done!");
}

pub fn start_tor() -> String {
let socks_port = 19050;

let data_dir = format!("{}/{}", env::temp_dir().display(), "bdk-tor-example");
let log_file_name = format!("{}/{}", &data_dir, "log");

println!("Staring Tor in {}", &data_dir);

truncate_log(&log_file_name);

Tor::new()
.flag(TorFlag::DataDirectory(data_dir))
.flag(TorFlag::LogTo(
LogLevel::Notice,
LogDestination::File(log_file_name.as_str().into()),
))
.flag(TorFlag::SocksPort(socks_port))
.flag(TorFlag::Custom("ExitPolicy reject *:*".into()))
.flag(TorFlag::Custom("BridgeRelay 0".into()))
.start_background();

let mut started = false;
let mut tries = 0;
while !started {
tries += 1;
if tries > 120 {
panic!(
"It took too long to start Tor. See {} for details",
&log_file_name
);
}

thread::sleep(Duration::from_millis(1000));
started = find_string_in_log(&log_file_name, &"Bootstrapped 100%".into());
}

println!("Tor started");

format!("127.0.0.1:{}", socks_port)
}

fn truncate_log(filename: &String) {
let path = std::path::Path::new(filename);
if path.exists() {
let file = OpenOptions::new()
.write(true)
.open(path)
.expect("no such file");
file.set_len(0).expect("cannot set size to 0");
}
}

fn find_string_in_log(filename: &String, str: &String) -> bool {
let path = std::path::Path::new(filename);
if path.exists() {
let mut file = OpenOptions::new()
.read(true)
.open(path)
.expect("no such file");
let mut buf = String::new();
file.read_to_string(&mut buf).expect("cannot read log file");
buf.contains(str)
} else {
false
}
}