Remove hardcoded configuration

The hardcoded configuration was replaced with CLI
configuration options. CLI based config was chosen
over a config file as it does not access and clutter
the user's file system. By CLI options depend on whether
the program is run in Alice or Bob mode.
This commit is contained in:
rishflab 2020-10-29 14:45:45 +11:00
parent 62c9f22b64
commit 0ca511bf8a
2 changed files with 96 additions and 77 deletions

View File

@ -1,19 +1,44 @@
use libp2p::core::Multiaddr;
use url::Url;
#[derive(structopt::StructOpt, Debug)] #[derive(structopt::StructOpt, Debug)]
pub struct Options { pub enum Options {
/// Run the swap as Alice. Alice {
#[structopt(long = "as-alice")] /// Run the swap as Bob and try to swap this many BTC (in satoshi).
pub as_alice: bool, #[structopt(long = "sats")]
satoshis: u64,
/// Run the swap as Bob and try to swap this many XMR (in piconero). // /// Run the swap as Bob and try to swap this many XMR (in piconero).
#[structopt(long = "picos")] // #[structopt(long = "picos", conflicts_with = "sats"))]
pub piconeros: Option<u64>, // pub piconeros: u64,
#[structopt(default_value = "http://127.0.0.1:8332", long = "bitcoind")]
bitcoind_url: Url,
/// Run the swap as Bob and try to swap this many BTC (in satoshi). #[structopt(default_value = "127.0.0.1", long = "listen_addr")]
#[structopt(long = "sats")] listen_addr: String,
pub satoshis: Option<u64>,
/// Alice's onion multitaddr (only required for Bob, Alice will autogenerate #[structopt(default_value = 9876, long = "list_port")]
/// one) listen_port: u16,
#[structopt(long)] },
pub alice_address: Option<String>, Bob {
/// Alice's multitaddr (only required for Bob, Alice will autogenerate
/// one)
#[structopt(long = "alice_addr")]
alice_addr: Multiaddr,
/// Run the swap as Bob and try to swap this many BTC (in satoshi).
#[structopt(long = "sats")]
satoshis: u64,
// /// Run the swap as Bob and try to swap this many XMR (in piconero).
// #[structopt(long = "picos", conflicts_with = "sats"))]
// pub piconeros: u64,
#[structopt(default_value = "http://127.0.0.1:8332", long = "bitcoind")]
bitcoind_url: Url,
// #[structopt(default_value = "/ip4/127.0.0.1/tcp/9876", long = "dial")]
// alice_addr: String,
#[cfg(feature = "tor")]
#[structopt(long = "tor")]
tor_port: u16,
},
} }

View File

@ -14,7 +14,7 @@
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use futures::{channel::mpsc, StreamExt}; use futures::{channel::mpsc, StreamExt};
use libp2p::Multiaddr; use libp2p::core::Multiaddr;
use log::LevelFilter; use log::LevelFilter;
use std::{io, io::Write, process, sync::Arc}; use std::{io, io::Write, process, sync::Arc};
use structopt::StructOpt; use structopt::StructOpt;
@ -29,80 +29,74 @@ use swap::{alice, bitcoin, bob, monero, Cmd, Rsp, SwapAmounts};
// TODO: Add root seed file instead of generating new seed each run. // TODO: Add root seed file instead of generating new seed each run.
// TODO: Add a config file with these in it. // // TODO: Add a config file with these in it.
// Alice's address and port until we have a config file. // // Alice's address and port until we have a config file.
pub const PORT: u16 = 9876; // Arbitrarily chosen. // pub const LISTEN_PORT: u16 = 9876; // Arbitrarily chosen.
pub const ADDR: &str = "127.0.0.1"; // pub const LISTEN_ADDR: &str = "127.0.0.1";
pub const BITCOIND_JSON_RPC_URL: &str = "http://127.0.0.1:8332"; // pub const BITCOIND_JSON_RPC_URL: &str = "http://127.0.0.1:8332";
pub const MONERO_WALLET_RPC_PORT: u16 = 18083; //
// #[cfg(feature = "tor")]
#[cfg(feature = "tor")] // pub const TOR_PORT: u16 = LISTEN_PORT + 1;
pub const TOR_PORT: u16 = PORT + 1;
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
let opt = Options::from_args(); let opt: Options = Options::from_args();
trace::init_tracing(LevelFilter::Debug)?; trace::init_tracing(LevelFilter::Debug)?;
#[cfg(feature = "tor")] match opt {
let (addr, _ac) = { Options::Alice {
let tor_secret_key = torut::onion::TorSecretKeyV3::generate(); bitcoind_url: url,
let onion_address = tor_secret_key listen_addr,
.public() listen_port,
.get_onion_address() ..
.get_address_without_dot_onion(); } => {
( info!("running swap node as Alice ...");
format!("/onion3/{}:{}", onion_address, TOR_PORT),
create_tor_service(tor_secret_key).await?,
)
};
#[cfg(not(feature = "tor"))]
let addr = format!("/ip4/{}/tcp/{}", ADDR, PORT);
let alice: Multiaddr = addr.parse().expect("failed to parse Alice's address"); #[cfg(feature = "tor")]
let (alice, _ac) = {
let tor_secret_key = torut::onion::TorSecretKeyV3::generate();
let onion_address = tor_secret_key
.public()
.get_onion_address()
.get_address_without_dot_onion();
let onion_address_string = format!("/onion3/{}:{}", onion_address, TOR_PORT);
let addr: Multiaddr = onion_address_string.parse()?;
if opt.as_alice { (addr, create_tor_service(tor_secret_key).await?)
info!("running swap node as Alice ..."); };
if opt.piconeros.is_some() || opt.satoshis.is_some() { #[cfg(not(feature = "tor"))]
bail!("Alice cannot set the amount to swap via the cli"); let alice = {
let alice: Multiaddr = listen_addr
.parse()
.expect("failed to parse Alice's address");
};
let bitcoin_wallet = bitcoin::Wallet::new("alice", &url)
.await
.expect("failed to create bitcoin wallet");
let bitcoin_wallet = Arc::new(bitcoin_wallet);
let monero_wallet = Arc::new(monero::Wallet::localhost(MONERO_WALLET_RPC_PORT));
swap_as_alice(bitcoin_wallet, monero_wallet, alice.clone()).await?;
} }
Options::Bob {
alice_addr: alice_address,
satoshis,
bitcoind_url: url,
} => {
info!("running swap node as Bob ...");
let url = Url::parse(BITCOIND_JSON_RPC_URL).expect("failed to parse url"); let bitcoin_wallet = Wallet::new("bob", &url)
let bitcoin_wallet = bitcoin::Wallet::new("alice", &url) .await
.await .expect("failed to create bitcoin wallet");
.expect("failed to create bitcoin wallet");
let bitcoin_wallet = Arc::new(bitcoin_wallet);
let monero_wallet = Arc::new(monero::Wallet::localhost(MONERO_WALLET_RPC_PORT)); let monero_wallet = Arc::new(monero::Wallet::localhost(MONERO_WALLET_RPC_PORT));
swap_as_alice(bitcoin_wallet, monero_wallet, alice.clone()).await?; swap_as_bob(satoshis, alice_address, refund, bitcoin_wallet).await?;
} else { }
info!("running swap node as Bob ...");
let alice = match opt.alice_address {
Some(addr) => addr,
None => bail!("Address required to dial"),
};
let alice = multiaddr(&alice)?;
let url = Url::parse(BITCOIND_JSON_RPC_URL).expect("failed to parse url");
let bitcoin_wallet = bitcoin::Wallet::new("bob", &url)
.await
.expect("failed to create bitcoin wallet");
let bitcoin_wallet = Arc::new(bitcoin_wallet);
let monero_wallet = Arc::new(monero::Wallet::localhost(MONERO_WALLET_RPC_PORT));
match (opt.piconeros, opt.satoshis) {
(Some(_), Some(_)) => bail!("Please supply only a single amount to swap"),
(None, None) => bail!("Please supply an amount to swap"),
(Some(_picos), _) => todo!("support starting with picos"),
(None, Some(sats)) => {
swap_as_bob(bitcoin_wallet, monero_wallet, sats, alice).await?;
}
};
} }
Ok(()) Ok(())
@ -137,7 +131,7 @@ async fn swap_as_alice(
} }
#[cfg(feature = "tor")] #[cfg(feature = "tor")]
{ {
alice::swap(bitcoin_wallet, monero_wallet, addr, Some(PORT)).await alice::swap(bitcoin_wallet, monero_wallet, addr, Some(LISTEN_PORT)).await
} }
} }