From 7afd316210feb2614e14abf376838f894d7174c5 Mon Sep 17 00:00:00 2001 From: rishflab Date: Tue, 3 Nov 2020 14:54:36 +1100 Subject: [PATCH] Construct tor transport for Bob --- swap/src/cli.rs | 4 ++++ swap/src/main.rs | 8 ++++++-- swap/src/network/transport.rs | 10 ++++++---- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/swap/src/cli.rs b/swap/src/cli.rs index 832b10c3..24e0041d 100644 --- a/swap/src/cli.rs +++ b/swap/src/cli.rs @@ -2,6 +2,7 @@ use libp2p::core::Multiaddr; use url::Url; #[derive(structopt::StructOpt, Debug)] +#[structopt(name = "xmr-btc-swap", about = "Trustless XMR BTC swaps")] pub enum Options { Alice { #[structopt(default_value = "http://127.0.0.1:8332", long = "bitcoind")] @@ -22,5 +23,8 @@ pub enum Options { #[structopt(default_value = "http://127.0.0.1:8332", long = "bitcoind")] bitcoind_url: Url, + + #[structopt(long = "tor")] + tor: bool, }, } diff --git a/swap/src/main.rs b/swap/src/main.rs index e52f9bc5..d55ce097 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -66,7 +66,7 @@ async fn main() -> Result<()> { let onion_address_string = format!("/onion3/{}:{}", onion_address, tor_port); let addr: Multiaddr = onion_address_string.parse()?; let ac = create_tor_service(tor_secret_key, tor_port).await?; - let transport = build_tor(local_key_pair, addr.clone(), tor_port)?; + let transport = build_tor(local_key_pair, Some((addr.clone(), tor_port)))?; (addr, Some(ac), transport) } None => { @@ -88,13 +88,17 @@ async fn main() -> Result<()> { alice_addr, satoshis, bitcoind_url: url, + tor, } => { info!("running swap node as Bob ..."); let behaviour = Bob::default(); let local_key_pair = behaviour.identity(); - let transport = build(local_key_pair)?; + let transport = match tor { + true => build_tor(local_key_pair, None)?, + false => build(local_key_pair)?, + }; let bitcoin_wallet = Wallet::new("bob", &url) .await diff --git a/swap/src/network/transport.rs b/swap/src/network/transport.rs index b937a516..5fa715fa 100644 --- a/swap/src/network/transport.rs +++ b/swap/src/network/transport.rs @@ -5,7 +5,7 @@ use libp2p::{ muxing::StreamMuxerBox, transport::Boxed, upgrade::{SelectUpgrade, Version}, - Multiaddr, Transport, + Transport, }, dns::DnsConfig, mplex::MplexConfig, @@ -46,13 +46,15 @@ pub fn build(id_keys: identity::Keypair) -> Result { /// - multiplexing via yamux or mplex pub fn build_tor( id_keys: identity::Keypair, - addr: libp2p::core::Multiaddr, - port: u16, + address_port_pair: Option<(libp2p::core::Multiaddr, u16)>, ) -> Result { use libp2p_tokio_socks5::Socks5TokioTcpConfig; use std::collections::HashMap; - let map: HashMap = [(addr, port)].iter().cloned().collect(); + let mut map = HashMap::new(); + if let Some((addr, port)) = address_port_pair { + map.insert(addr, port); + } let dh_keys = noise::Keypair::::new().into_authentic(&id_keys)?; let noise = NoiseConfig::xx(dh_keys).into_authenticated();