2021-03-14 21:02:42 -04:00
|
|
|
use crate::fs::default_data_dir;
|
2021-03-03 18:46:54 -05:00
|
|
|
use anyhow::{Context, Result};
|
2021-03-03 19:28:58 -05:00
|
|
|
use libp2p::core::Multiaddr;
|
|
|
|
use libp2p::PeerId;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::str::FromStr;
|
2021-03-14 21:02:42 -04:00
|
|
|
use url::Url;
|
2020-11-05 19:10:22 -05:00
|
|
|
use uuid::Uuid;
|
2020-10-28 23:45:45 -04:00
|
|
|
|
2021-02-25 23:24:13 -05:00
|
|
|
pub const DEFAULT_ALICE_MULTIADDR: &str = "/dns4/xmr-btc-asb.coblox.tech/tcp/9876";
|
|
|
|
pub const DEFAULT_ALICE_PEER_ID: &str = "12D3KooWCdMKjesXMJz1SiZ7HgotrxuqhQJbP5sgBm2BwP1cqThi";
|
2021-02-24 20:05:07 -05:00
|
|
|
|
2021-03-05 00:10:45 -05:00
|
|
|
// Port is assumed to be stagenet standard port 38081
|
|
|
|
pub const DEFAULT_STAGENET_MONERO_DAEMON_HOST: &str = "monero-stagenet.exan.tech";
|
|
|
|
|
2021-03-14 21:02:42 -04:00
|
|
|
pub const DEFAULT_ELECTRUM_HTTP_URL: &str = "https://blockstream.info/testnet/api/";
|
|
|
|
const DEFAULT_ELECTRUM_RPC_URL: &str = "ssl://electrum.blockstream.info:60002";
|
|
|
|
|
2020-12-15 05:26:02 -05:00
|
|
|
#[derive(structopt::StructOpt, Debug)]
|
2021-03-25 23:31:14 -04:00
|
|
|
#[structopt(name = "swap", about = "CLI for swapping BTC for XMR", author)]
|
2021-02-10 17:47:42 -05:00
|
|
|
pub struct Arguments {
|
2021-01-26 21:33:32 -05:00
|
|
|
#[structopt(
|
2021-03-14 21:02:42 -04:00
|
|
|
long = "--data-dir",
|
|
|
|
help = "Provide the data directory path to be used to store application data",
|
|
|
|
default_value
|
2021-01-26 21:33:32 -05:00
|
|
|
)]
|
2021-03-14 21:02:42 -04:00
|
|
|
pub data: Data,
|
2020-12-15 05:26:02 -05:00
|
|
|
|
2021-02-28 20:19:05 -05:00
|
|
|
#[structopt(long, help = "Activate debug logging.")]
|
|
|
|
pub debug: bool,
|
|
|
|
|
2020-12-15 05:26:02 -05:00
|
|
|
#[structopt(subcommand)]
|
2021-03-02 06:07:48 -05:00
|
|
|
pub cmd: Command,
|
2020-12-15 05:26:02 -05:00
|
|
|
}
|
|
|
|
|
2020-10-15 18:14:39 -04:00
|
|
|
#[derive(structopt::StructOpt, Debug)]
|
2020-12-15 05:26:02 -05:00
|
|
|
pub enum Command {
|
2021-03-05 00:16:18 -05:00
|
|
|
/// Start a XMR for BTC swap
|
2020-12-04 00:27:17 -05:00
|
|
|
BuyXmr {
|
2021-03-05 00:00:52 -05:00
|
|
|
#[structopt(flatten)]
|
2021-03-05 00:16:18 -05:00
|
|
|
connect_params: AliceConnectParams,
|
2021-03-02 06:07:48 -05:00
|
|
|
|
2021-03-11 02:16:00 -05:00
|
|
|
#[structopt(long = "electrum-rpc",
|
|
|
|
help = "Provide the Bitcoin Electrum RPC URL",
|
|
|
|
default_value = DEFAULT_ELECTRUM_RPC_URL
|
|
|
|
)]
|
|
|
|
electrum_rpc_url: Url,
|
2021-03-14 21:02:42 -04:00
|
|
|
|
2021-03-05 00:00:52 -05:00
|
|
|
#[structopt(flatten)]
|
|
|
|
monero_params: MoneroParams,
|
2020-10-28 23:45:45 -04:00
|
|
|
},
|
2021-03-05 00:16:18 -05:00
|
|
|
/// Show a list of past ongoing and completed swaps
|
2020-11-05 00:55:30 -05:00
|
|
|
History,
|
2021-03-05 00:16:18 -05:00
|
|
|
/// Resume a swap
|
2021-02-25 22:44:48 -05:00
|
|
|
Resume {
|
2021-03-05 00:16:18 -05:00
|
|
|
#[structopt(
|
|
|
|
long = "swap-id",
|
|
|
|
help = "The swap id can be retrieved using the history subcommand"
|
|
|
|
)]
|
2020-12-20 22:43:44 -05:00
|
|
|
swap_id: Uuid,
|
|
|
|
|
2021-03-05 00:00:52 -05:00
|
|
|
#[structopt(flatten)]
|
2021-03-05 00:16:18 -05:00
|
|
|
connect_params: AliceConnectParams,
|
2020-12-18 01:39:04 -05:00
|
|
|
|
2021-03-11 02:16:00 -05:00
|
|
|
#[structopt(long = "electrum-rpc",
|
|
|
|
help = "Provide the Bitcoin Electrum RPC URL",
|
|
|
|
default_value = DEFAULT_ELECTRUM_RPC_URL
|
|
|
|
)]
|
|
|
|
electrum_rpc_url: Url,
|
2021-03-14 21:02:42 -04:00
|
|
|
|
2021-03-05 00:00:52 -05:00
|
|
|
#[structopt(flatten)]
|
|
|
|
monero_params: MoneroParams,
|
2020-11-05 19:10:22 -05:00
|
|
|
},
|
2021-03-05 00:16:18 -05:00
|
|
|
/// Try to cancel an ongoing swap (expert users only)
|
2021-02-25 22:44:48 -05:00
|
|
|
Cancel {
|
2021-03-05 00:16:18 -05:00
|
|
|
#[structopt(
|
|
|
|
long = "swap-id",
|
|
|
|
help = "The swap id can be retrieved using the history subcommand"
|
|
|
|
)]
|
2021-02-01 00:25:33 -05:00
|
|
|
swap_id: Uuid,
|
|
|
|
|
2021-02-01 06:32:54 -05:00
|
|
|
#[structopt(short, long)]
|
|
|
|
force: bool,
|
2021-03-14 21:02:42 -04:00
|
|
|
|
2021-03-11 02:16:00 -05:00
|
|
|
#[structopt(long = "electrum-rpc",
|
|
|
|
help = "Provide the Bitcoin Electrum RPC URL",
|
|
|
|
default_value = DEFAULT_ELECTRUM_RPC_URL
|
|
|
|
)]
|
|
|
|
electrum_rpc_url: Url,
|
2021-02-01 00:25:33 -05:00
|
|
|
},
|
2021-03-05 00:16:18 -05:00
|
|
|
/// Try to cancel a swap and refund my BTC (expert users only)
|
2021-02-25 22:44:48 -05:00
|
|
|
Refund {
|
2021-03-05 00:16:18 -05:00
|
|
|
#[structopt(
|
|
|
|
long = "swap-id",
|
|
|
|
help = "The swap id can be retrieved using the history subcommand"
|
|
|
|
)]
|
2021-02-01 00:10:43 -05:00
|
|
|
swap_id: Uuid,
|
|
|
|
|
2021-02-01 06:32:54 -05:00
|
|
|
#[structopt(short, long)]
|
|
|
|
force: bool,
|
2021-03-14 21:02:42 -04:00
|
|
|
|
2021-03-11 02:16:00 -05:00
|
|
|
#[structopt(long = "electrum-rpc",
|
|
|
|
help = "Provide the Bitcoin Electrum RPC URL",
|
|
|
|
default_value = DEFAULT_ELECTRUM_RPC_URL
|
|
|
|
)]
|
|
|
|
electrum_rpc_url: Url,
|
2021-02-01 00:10:43 -05:00
|
|
|
},
|
|
|
|
}
|
2021-02-28 18:53:43 -05:00
|
|
|
|
2021-03-05 00:00:52 -05:00
|
|
|
#[derive(structopt::StructOpt, Debug)]
|
2021-03-05 00:16:18 -05:00
|
|
|
pub struct AliceConnectParams {
|
|
|
|
#[structopt(
|
|
|
|
long = "seller-peer-id",
|
|
|
|
default_value = DEFAULT_ALICE_PEER_ID,
|
|
|
|
help = "The peer id of a specific swap partner can be optionally provided"
|
|
|
|
)]
|
|
|
|
pub peer_id: PeerId,
|
2021-03-05 00:00:52 -05:00
|
|
|
|
|
|
|
#[structopt(
|
2021-03-05 00:16:18 -05:00
|
|
|
long = "seller-addr",
|
|
|
|
default_value = DEFAULT_ALICE_MULTIADDR,
|
|
|
|
help = "The multiaddr of a specific swap partner can be optionally provided"
|
2021-03-05 00:00:52 -05:00
|
|
|
)]
|
2021-03-05 00:16:18 -05:00
|
|
|
pub multiaddr: Multiaddr,
|
2021-03-05 00:00:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(structopt::StructOpt, Debug)]
|
|
|
|
pub struct MoneroParams {
|
2021-03-05 00:16:18 -05:00
|
|
|
#[structopt(long = "receive-address",
|
|
|
|
help = "Provide the monero address where you would like to receive monero",
|
|
|
|
parse(try_from_str = parse_monero_address)
|
|
|
|
)]
|
2021-03-05 00:00:52 -05:00
|
|
|
pub receive_monero_address: monero::Address,
|
2021-03-05 00:10:45 -05:00
|
|
|
|
|
|
|
#[structopt(
|
2021-03-05 00:16:18 -05:00
|
|
|
long = "monero-daemon-host",
|
|
|
|
help = "Specify to connect to a monero daemon of your choice",
|
|
|
|
default_value = DEFAULT_STAGENET_MONERO_DAEMON_HOST
|
2021-03-05 00:10:45 -05:00
|
|
|
)]
|
|
|
|
pub monero_daemon_host: String,
|
2021-03-05 00:00:52 -05:00
|
|
|
}
|
|
|
|
|
2021-03-14 21:02:42 -04:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Data(pub PathBuf);
|
|
|
|
|
|
|
|
impl Default for Data {
|
|
|
|
fn default() -> Self {
|
|
|
|
Data(default_data_dir().expect("computed valid path for data dir"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Data {
|
|
|
|
type Err = core::convert::Infallible;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
Ok(Data(PathBuf::from_str(s)?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for Data {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
self.0
|
|
|
|
.clone()
|
|
|
|
.into_os_string()
|
|
|
|
.into_string()
|
|
|
|
.expect("default datadir to be convertible to string")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 18:46:54 -05:00
|
|
|
fn parse_monero_address(s: &str) -> Result<monero::Address> {
|
|
|
|
monero::Address::from_str(s).with_context(|| {
|
|
|
|
format!(
|
|
|
|
"Failed to parse {} as a monero address, please make sure it is a valid address",
|
|
|
|
s
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-28 19:00:30 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-03-02 06:07:48 -05:00
|
|
|
use crate::cli::command::{DEFAULT_ALICE_MULTIADDR, DEFAULT_ALICE_PEER_ID};
|
2021-03-03 19:28:58 -05:00
|
|
|
use libp2p::core::Multiaddr;
|
|
|
|
use libp2p::PeerId;
|
2021-02-28 19:00:30 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_default_alice_peer_id_success() {
|
|
|
|
DEFAULT_ALICE_PEER_ID
|
|
|
|
.parse::<PeerId>()
|
|
|
|
.expect("default alice peer id str is a valid PeerId");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_default_alice_multiaddr_success() {
|
|
|
|
DEFAULT_ALICE_MULTIADDR
|
|
|
|
.parse::<Multiaddr>()
|
|
|
|
.expect("default alice multiaddr str is a valid Multiaddr>");
|
|
|
|
}
|
|
|
|
}
|