From 590834ca2463b275d2cd1fcc3a603a62f97cefd3 Mon Sep 17 00:00:00 2001 From: Philipp Hoenisch Date: Thu, 22 Oct 2020 11:44:13 +1100 Subject: [PATCH] A simple clie proposal --- README.md | 24 +++++++++++++++++ xmr-btc/Cargo.toml | 2 ++ xmr-btc/src/cli.rs | 17 ++++++++++++ xmr-btc/src/lib.rs | 1 + xmr-btc/src/main.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 xmr-btc/src/cli.rs create mode 100644 xmr-btc/src/main.rs diff --git a/README.md b/README.md index e9a2a1dc..ae57e301 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,30 @@ XMR to BTC Atomic Swap ====================== +## How to start + +### As a maker + +To start the application as a maker type the following into your terminal: +```bash +xmr_btc --maker +``` +The application will then print your listening address and waits for a taker to connect to you. +Once connected, it will perform the atomic swap automatically. + +### As a taker + +To start the application as a taker type the following into your terminal: + +```bash +xmr-btc --taker --address +``` + +You will connect to the maker as defined by `` and retrieve his latest rate. +If you are happy with this rate just hit `ENTER` and the application will perform the atomic swap for you. + +----- + This repository is a proof of concept for atomically swapping XMR for BTC. We define: diff --git a/xmr-btc/Cargo.toml b/xmr-btc/Cargo.toml index b8afd051..2dc57543 100644 --- a/xmr-btc/Cargo.toml +++ b/xmr-btc/Cargo.toml @@ -17,8 +17,10 @@ miniscript = "1" monero = "0.9" rand = "0.7" reqwest = { version = "0.10", default-features = false, features = ["socks"] } +rustyline = "6.3" serde = { version = "1", features = ["derive"] } serde_json = "1" +structopt = "0.3" sha2 = "0.9" thiserror = "1" tokio = { version = "0.2", default-features = false, features = ["blocking", "macros", "rt-core", "time", "rt-threaded"] } diff --git a/xmr-btc/src/cli.rs b/xmr-btc/src/cli.rs new file mode 100644 index 00000000..5fc10fcd --- /dev/null +++ b/xmr-btc/src/cli.rs @@ -0,0 +1,17 @@ +use structopt::StructOpt; + +#[derive(Debug, StructOpt)] +#[structopt(name = "xmr-btc", about = "A simple BTC/XMR atomic swap tool.")] +pub struct Opt { + /// Run as the maker i.e., wait for a taker to connect and trade + #[structopt(short, long)] + pub maker: bool, + + /// Request a rate from a maker + #[structopt(short, long)] + pub taker: bool, + + /// Onion/Ipv4 mulitaddr of a maker + #[structopt(long)] + pub address: Option, +} diff --git a/xmr-btc/src/lib.rs b/xmr-btc/src/lib.rs index 9526bad4..93f920b2 100644 --- a/xmr-btc/src/lib.rs +++ b/xmr-btc/src/lib.rs @@ -48,6 +48,7 @@ mod utils { pub mod alice; pub mod bitcoin; pub mod bob; +pub mod cli; pub mod monero; #[cfg(feature = "tor")] pub mod tor; diff --git a/xmr-btc/src/main.rs b/xmr-btc/src/main.rs new file mode 100644 index 00000000..38a17bf3 --- /dev/null +++ b/xmr-btc/src/main.rs @@ -0,0 +1,63 @@ +use anyhow::{bail, Result}; +use rustyline::{error::ReadlineError, Editor}; +use structopt::StructOpt; +use xmr_btc::cli::Opt; + +fn run_app() -> Result<()> { + let opt = Opt::from_args(); + + if opt.maker { + run_maker()? + } else if opt.taker { + let addr = match opt.address { + Some(addr) => addr, + None => bail!("Maker address is required"), + }; + + run_taker(addr)? + } else { + bail!("Invalid argument") + } + + Ok(()) +} + +fn main() { + std::process::exit(match run_app() { + Ok(_) => 0, + Err(err) => { + eprintln!("error: {:?}", err); + 1 + } + }); +} + +fn run_maker() -> Result<()> { + // wait for a taker to connect and auto trade + println!("Usually we would now wait for takers to connect"); + Ok(()) +} + +fn run_taker(_addr: String) -> Result<()> { + let mut rl = Editor::<()>::new(); + + // connect to maker and get rate + + let readline = rl.readline("Received order from maker: 1 BTC for 42 XMR. Take it or leave it? Hit to accept or CTRL-C to decline and quit.\n "); + match readline { + Ok(line) => { + rl.add_history_entry(line.as_str()); + println!("Accepting trade doing the swap"); + Ok(()) + } + Err(ReadlineError::Interrupted) => { + println!("Trade canceled. Terminating application"); + Ok(()) + } + Err(ReadlineError::Eof) => { + println!("Trade canceled. Terminating application"); + Ok(()) + } + Err(err) => bail!("Error: {:?}", err), + } +}