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 @@
#[derive(structopt::StructOpt, Debug)] use libp2p::core::Multiaddr;
pub struct Options { use url::Url;
/// Run the swap as Alice.
#[structopt(long = "as-alice")]
pub as_alice: bool,
/// Run the swap as Bob and try to swap this many XMR (in piconero). #[derive(structopt::StructOpt, Debug)]
#[structopt(long = "picos")] pub enum Options {
pub piconeros: Option<u64>, Alice {
/// 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 = "127.0.0.1", long = "listen_addr")]
listen_addr: String,
#[structopt(default_value = 9876, long = "list_port")]
listen_port: u16,
},
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). /// Run the swap as Bob and try to swap this many BTC (in satoshi).
#[structopt(long = "sats")] #[structopt(long = "sats")]
pub satoshis: Option<u64>, satoshis: u64,
/// Alice's onion multitaddr (only required for Bob, Alice will autogenerate // /// Run the swap as Bob and try to swap this many XMR (in piconero).
/// one) // #[structopt(long = "picos", conflicts_with = "sats"))]
#[structopt(long)] // pub piconeros: u64,
pub alice_address: Option<String>, #[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,47 +29,50 @@ 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)?;
match opt {
Options::Alice {
bitcoind_url: url,
listen_addr,
listen_port,
..
} => {
info!("running swap node as Alice ...");
#[cfg(feature = "tor")] #[cfg(feature = "tor")]
let (addr, _ac) = { let (alice, _ac) = {
let tor_secret_key = torut::onion::TorSecretKeyV3::generate(); let tor_secret_key = torut::onion::TorSecretKeyV3::generate();
let onion_address = tor_secret_key let onion_address = tor_secret_key
.public() .public()
.get_onion_address() .get_onion_address()
.get_address_without_dot_onion(); .get_address_without_dot_onion();
( let onion_address_string = format!("/onion3/{}:{}", onion_address, TOR_PORT);
format!("/onion3/{}:{}", onion_address, TOR_PORT), let addr: Multiaddr = onion_address_string.parse()?;
create_tor_service(tor_secret_key).await?,
) (addr, create_tor_service(tor_secret_key).await?)
}; };
#[cfg(not(feature = "tor"))] #[cfg(not(feature = "tor"))]
let addr = format!("/ip4/{}/tcp/{}", ADDR, PORT); let alice = {
let alice: Multiaddr = listen_addr
.parse()
.expect("failed to parse Alice's address");
};
let alice: Multiaddr = addr.parse().expect("failed to parse Alice's address");
if opt.as_alice {
info!("running swap node as Alice ...");
if opt.piconeros.is_some() || opt.satoshis.is_some() {
bail!("Alice cannot set the amount to swap via the cli");
}
let url = Url::parse(BITCOIND_JSON_RPC_URL).expect("failed to parse url");
let bitcoin_wallet = bitcoin::Wallet::new("alice", &url) let bitcoin_wallet = bitcoin::Wallet::new("alice", &url)
.await .await
.expect("failed to create bitcoin wallet"); .expect("failed to create bitcoin wallet");
@ -78,31 +81,22 @@ async fn main() -> Result<()> {
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_alice(bitcoin_wallet, monero_wallet, alice.clone()).await?;
} else { }
Options::Bob {
alice_addr: alice_address,
satoshis,
bitcoind_url: url,
} => {
info!("running swap node as Bob ..."); info!("running swap node as Bob ...");
let alice = match opt.alice_address { let bitcoin_wallet = Wallet::new("bob", &url)
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 .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));
match (opt.piconeros, opt.satoshis) { swap_as_bob(satoshis, alice_address, refund, bitcoin_wallet).await?;
(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
} }
} }