From 4115a452e303a64c223c512b1a90c783d73ae7ca Mon Sep 17 00:00:00 2001 From: Einliterflasche <81313171+Einliterflasche@users.noreply.github.com> Date: Thu, 11 Jul 2024 09:19:41 +0200 Subject: [PATCH] allow --change-address to be omitted and default to internal wallet address (#1709) allow --change-address to be omitted and default to internal wallet address (#1709) Co-authored-by: binarybaron <86064887+binarybaron@users.noreply.github.com> Co-authored-by: Byron Hambly --- CHANGELOG.md | 2 ++ docs/cli/README.md | 2 +- swap/src/api.rs | 4 ++++ swap/src/cli/command.rs | 46 +++++++++++++++++++++++++++++------------ 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a2baf45..1b4776b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- CLI: `--change-address` can now be omitted. In that case, any change is refunded to the internal bitcoin wallet. + ## [0.13.2] - 2024-07-02 - CLI: Buffer received transfer proofs for later processing if we're currently running a different swap diff --git a/docs/cli/README.md b/docs/cli/README.md index d95d90ef..eff9d072 100644 --- a/docs/cli/README.md +++ b/docs/cli/README.md @@ -75,7 +75,7 @@ OPTIONS: This command has three core options: -- `--change-address`: A Bitcoin address you control. Will be used for refunds of any kind. +- `--change-address`: A Bitcoin address you control. Will be used for refunds of any kind. You can also omit this flag which will refund any change to the internal wallet. - `--receive-address`: A Monero address you control. This is where you will receive the Monero after the swap. - `--seller`: The multiaddress of the seller you want to swap with. diff --git a/swap/src/api.rs b/swap/src/api.rs index 340c2e39..fb071cd4 100644 --- a/swap/src/api.rs +++ b/swap/src/api.rs @@ -267,6 +267,10 @@ impl Context { tasks: Arc::new(PendingTaskList::default()), } } + + pub fn bitcoin_wallet(&self) -> Option> { + self.bitcoin_wallet.clone() + } } impl fmt::Debug for Context { diff --git a/swap/src/cli/command.rs b/swap/src/cli/command.rs index af72df1b..f3d61c92 100644 --- a/swap/src/cli/command.rs +++ b/swap/src/cli/command.rs @@ -69,6 +69,37 @@ where monero_receive_address, tor, } => { + let context = Context::build( + Some(bitcoin), + Some(monero), + Some(tor), + data, + is_testnet, + debug, + json, + None, + ) + .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 = @@ -81,17 +112,6 @@ where swap_id: Uuid::new_v4(), }); - let context = Context::build( - Some(bitcoin), - Some(monero), - Some(tor), - data, - is_testnet, - debug, - json, - None, - ) - .await?; (context, request) } CliCommand::History => { @@ -300,10 +320,10 @@ enum CliCommand { #[structopt( long = "change-address", - help = "The bitcoin address where any form of change or excess funds should be sent to", + help = "The bitcoin address where any form of change or excess funds should be sent to. If omitted they will be sent to the internal wallet.", parse(try_from_str = bitcoin_address::parse) )] - bitcoin_change_address: bitcoin::Address, + bitcoin_change_address: Option, #[structopt(flatten)] monero: Monero,