From 35d0e246d89810fa00ec3135b764a49472d68ccd Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Thu, 4 Mar 2021 10:46:12 +1100 Subject: [PATCH 1/2] Monero address network check Add a network check to ensure the given monero address is on the configured network. --- swap/src/bin/swap_cli.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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 = From 7504c6ceeedda6449cc7d80c776876538ebfc2c1 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Thu, 4 Mar 2021 10:46:54 +1100 Subject: [PATCH 2/2] Context for monero address parsing errors Most of the errors are not user friendly, thus added context to tell the user the given address is incorrect. --- swap/src/cli/command.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) 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};