diff --git a/swap/src/bin/swap_cli.rs b/swap/src/bin/swap_cli.rs index 36f1326a..01f23f05 100644 --- a/swap/src/bin/swap_cli.rs +++ b/swap/src/bin/swap_cli.rs @@ -12,7 +12,7 @@ #![forbid(unsafe_code)] #![allow(non_snake_case)] -use anyhow::{Context, Result}; +use anyhow::{bail, Context, Result}; use prettytable::{row, Table}; use reqwest::Url; use std::{path::Path, sync::Arc, time::Duration}; @@ -107,6 +107,14 @@ async fn main() -> Result<()> { alice_peer_id, alice_addr, } => { + if receive_monero_address.network != monero_network { + bail!( + "Given monero address is on network {:?}, expected address on network {:?}", + receive_monero_address.network, + monero_network + ) + } + let bitcoin_wallet = init_bitcoin_wallet(config, bitcoin_network, &wallet_data_dir, seed).await?; let monero_wallet = @@ -186,6 +194,10 @@ async fn main() -> Result<()> { alice_peer_id, alice_addr, } => { + if receive_monero_address.network != monero_network { + bail!("The given monero address is on network {:?}, expected address of network {:?}.", receive_monero_address.network, monero_network) + } + let bitcoin_wallet = init_bitcoin_wallet(config, bitcoin_network, &wallet_data_dir, seed).await?; let monero_wallet = diff --git a/swap/src/cli/command.rs b/swap/src/cli/command.rs index 0231f033..30a675e0 100644 --- a/swap/src/cli/command.rs +++ b/swap/src/cli/command.rs @@ -1,5 +1,6 @@ +use anyhow::{Context, Result}; use libp2p::{core::Multiaddr, PeerId}; -use std::path::PathBuf; +use std::{path::PathBuf, str::FromStr}; use uuid::Uuid; pub const DEFAULT_ALICE_MULTIADDR: &str = "/dns4/xmr-btc-asb.coblox.tech/tcp/9876"; @@ -25,7 +26,7 @@ pub struct Arguments { #[structopt(name = "xmr_btc-swap", about = "XMR BTC atomic swap")] pub enum Command { BuyXmr { - #[structopt(long = "receive-address")] + #[structopt(long = "receive-address", parse(try_from_str = parse_monero_address))] receive_monero_address: monero::Address, #[structopt(long = "connect-peer-id", default_value = DEFAULT_ALICE_PEER_ID)] @@ -39,7 +40,7 @@ pub enum Command { }, History, Resume { - #[structopt(long = "receive-address")] + #[structopt(long = "receive-address", parse(try_from_str = parse_monero_address))] receive_monero_address: monero::Address, #[structopt(long = "swap-id")] @@ -72,6 +73,15 @@ pub enum Command { }, } +fn parse_monero_address(s: &str) -> Result { + monero::Address::from_str(s).with_context(|| { + format!( + "Failed to parse {} as a monero address, please make sure it is a valid address", + s + ) + }) +} + #[cfg(test)] mod tests { use crate::cli::command::{DEFAULT_ALICE_MULTIADDR, DEFAULT_ALICE_PEER_ID};