Construct tor transport for Bob

This commit is contained in:
rishflab 2020-11-03 14:54:36 +11:00
parent 3b008c38a6
commit 7afd316210
3 changed files with 16 additions and 6 deletions

View File

@ -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,
},
}

View File

@ -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

View File

@ -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<SwapTransport> {
/// - 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<SwapTransport> {
use libp2p_tokio_socks5::Socks5TokioTcpConfig;
use std::collections::HashMap;
let map: HashMap<Multiaddr, u16> = [(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::<X25519Spec>::new().into_authentic(&id_keys)?;
let noise = NoiseConfig::xx(dh_keys).into_authenticated();