From f70696e70c16783e4533f378e2b90bcd1adeee71 Mon Sep 17 00:00:00 2001 From: binarybaron Date: Wed, 24 Jul 2024 19:18:32 +0200 Subject: [PATCH] fix: Allow bitcoin change address to be omitted when making a request to RPC server --- swap/src/api/request.rs | 21 ++++++++++++++++++++- swap/src/cli/command.rs | 23 +++-------------------- swap/src/rpc/methods.rs | 23 +++++++++++++---------- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/swap/src/api/request.rs b/swap/src/api/request.rs index 50274ab1..b1e9c68c 100644 --- a/swap/src/api/request.rs +++ b/swap/src/api/request.rs @@ -31,7 +31,7 @@ pub struct Request { pub enum Method { BuyXmr { seller: Multiaddr, - bitcoin_change_address: bitcoin::Address, + bitcoin_change_address: Option, monero_receive_address: monero::Address, swap_id: Uuid, }, @@ -335,6 +335,25 @@ impl Request { let env_config = context.config.env_config; let seed = context.config.seed.clone().context("Could not get seed")?; + // When no change address was provided we default to the internal wallet + let bitcoin_change_address = match bitcoin_change_address { + Some(addr) => addr, + None => { + let internal_wallet_address = context + .bitcoin_wallet() + .expect("bitcoin wallet should exist") + .new_address() + .await?; + + tracing::info!( + internal_wallet_address=%internal_wallet_address, + "No --change-address supplied. Any change will be received to the internal wallet." + ); + + internal_wallet_address + } + }; + let seller_peer_id = seller .extract_peer_id() .context("Seller address must contain peer ID")?; diff --git a/swap/src/cli/command.rs b/swap/src/cli/command.rs index f3d61c92..36fe4c8a 100644 --- a/swap/src/cli/command.rs +++ b/swap/src/cli/command.rs @@ -81,29 +81,12 @@ where ) .await?; - // when no refund address was provided we default to the internal wallet - let bitcoin_change_address = match bitcoin_change_address { - Some(addr) => addr, - None => { - let internal_wallet_address = context - .bitcoin_wallet() - .expect("bitcoin wallet should exist") - .new_address() - .await?; - - tracing::info!( - internal_wallet_address=%internal_wallet_address, - "No --change-address supplied. Any change will be received to the internal wallet." - ); - - internal_wallet_address - } - }; let monero_receive_address = monero_address::validate_is_testnet(monero_receive_address, is_testnet)?; - let bitcoin_change_address = - bitcoin_address::validate_is_testnet(bitcoin_change_address, is_testnet)?; + let bitcoin_change_address = bitcoin_change_address.map(|address| { + bitcoin_address::validate_is_testnet(address, is_testnet) + }).transpose()?; let request = Request::new(Method::BuyXmr { seller, diff --git a/swap/src/rpc/methods.rs b/swap/src/rpc/methods.rs index 83100d4a..2627ae86 100644 --- a/swap/src/rpc/methods.rs +++ b/swap/src/rpc/methods.rs @@ -135,16 +135,19 @@ pub fn register_modules(context: Arc) -> Result> module.register_async_method("buy_xmr", |params_raw, context| async move { let params: HashMap = params_raw.parse()?; - let bitcoin_change_address = - bitcoin::Address::from_str(params.get("bitcoin_change_address").ok_or_else(|| { - jsonrpsee_core::Error::Custom("Does not contain bitcoin_change_address".to_string()) - })?) - .map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?; - - let bitcoin_change_address = bitcoin_address::validate( - bitcoin_change_address, - context.config.env_config.bitcoin_network, - )?; + let bitcoin_change_address = params + .get("bitcoin_change_address") + .map(|addr_str| { + bitcoin::Address::from_str(addr_str) + .map_err(|err| { + jsonrpsee_core::Error::Custom(format!("Could not parse bitcoin address: {}", err)) + }) + .and_then(|address| { + bitcoin_address::validate(address, context.config.env_config.bitcoin_network) + .map_err(|err| jsonrpsee_core::Error::Custom(err.to_string())) + }) + }) + .transpose()?; let monero_receive_address = monero::Address::from_str(params.get("monero_receive_address").ok_or_else(|| {