From 909851228469bee153bd40252151e91a5f476734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Granh=C3=A3o?= Date: Thu, 21 Nov 2024 13:25:26 +0000 Subject: [PATCH] Use `BitcoinAddressData` for address parameters --- examples/node/cli.rs | 17 +++++++++++++++-- src/actions_required.rs | 12 +++++++++++- src/lib.rs | 19 +++++++++++++++++-- src/lipalightninglib.udl | 6 +++--- src/onchain/channel_closes.rs | 27 +++++++++++++++++++-------- src/onchain/reverse_swap.rs | 6 +++--- src/onchain/swap.rs | 22 +++++++++++++++++----- 7 files changed, 85 insertions(+), 24 deletions(-) diff --git a/examples/node/cli.rs b/examples/node/cli.rs index 4b802871..5ebd87cc 100644 --- a/examples/node/cli.rs +++ b/examples/node/cli.rs @@ -1021,6 +1021,7 @@ fn print_failed_swap(swap: &FailedSwapInfo) { fn refund_failed_swap(node: &LightningNode, words: &mut dyn Iterator) -> Result<()> { let swap_address = words.next().ok_or(anyhow!("Swap address is required"))?; let to_address = words.next().ok_or(anyhow!("To address is required"))?; + let address_data = get_bitcoin_address_data(node.util().decode_data(to_address.to_string())?)?; let failed_swaps = failed_swap_from_actions_required_list( &node @@ -1037,7 +1038,7 @@ fn refund_failed_swap(node: &LightningNode, words: &mut dyn Iterator } fn sweep(node: &LightningNode, address: String) -> Result { - let sweep_info = node.onchain().channel_close().prepare_sweep(address)?; + let address_data = get_bitcoin_address_data(node.util().decode_data(address)?)?; + let sweep_info = node.onchain().channel_close().prepare_sweep(address_data)?; Ok(node.onchain().channel_close().sweep(sweep_info)?) } @@ -1742,3 +1744,14 @@ fn failed_swap_from_actions_required_list(list: &[ActionRequiredItem]) -> Vec>() } + +fn get_bitcoin_address_data(decoded_data: DecodedData) -> Result { + if let DecodedData::OnchainAddress { + onchain_address_details, + } = decoded_data + { + Ok(onchain_address_details) + } else { + bail!("Not an onchain address") + } +} diff --git a/src/actions_required.rs b/src/actions_required.rs index 72ad54cb..dbaabba0 100644 --- a/src/actions_required.rs +++ b/src/actions_required.rs @@ -5,6 +5,7 @@ use crate::locker::Locker; use crate::onchain::Onchain; use crate::support::Support; use crate::{ActionRequiredItem, FailedSwapInfo, RuntimeErrorCode, CLN_DUST_LIMIT_SAT}; +use breez_sdk_core::{BitcoinAddressData, Network}; use perro::ResultTrait; use std::ops::Not; use std::sync::Arc; @@ -54,7 +55,16 @@ impl ActionsRequired { || self .onchain .swap() - .prepare_sweep(s.clone(), "1BitcoinEaterAddressDontSendf59kuE".to_string()) + .prepare_sweep( + s.clone(), + BitcoinAddressData { + address: "1BitcoinEaterAddressDontSendf59kuE".to_string(), + network: Network::Bitcoin, + amount_sat: None, + label: None, + message: None, + }, + ) .is_ok() }) .collect(); diff --git a/src/lib.rs b/src/lib.rs index b9dbb4b1..03581e4e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1226,7 +1226,13 @@ impl LightningNode { ) -> std::result::Result { self.onchain .channel_close() - .prepare_sweep(address) + .prepare_sweep(BitcoinAddressData { + address, + network: Network::Bitcoin, + amount_sat: None, + label: None, + message: None, + }) .map(SweepInfo::from) } @@ -1327,7 +1333,16 @@ impl LightningNode { ) -> Result { self.onchain .swap() - .prepare_sweep(failed_swap_info, to_address) + .prepare_sweep( + failed_swap_info, + BitcoinAddressData { + address: to_address, + network: Network::Bitcoin, + amount_sat: None, + label: None, + message: None, + }, + ) .map(ResolveFailedSwapInfo::from) } diff --git a/src/lipalightninglib.udl b/src/lipalightninglib.udl index e4ca8292..482baef9 100644 --- a/src/lipalightninglib.udl +++ b/src/lipalightninglib.udl @@ -330,7 +330,7 @@ interface Swap { OnchainResolvingFees? determine_resolving_fees(FailedSwapInfo failed_swap_info); [Throws=LnError] - SweepFailedSwapInfo prepare_sweep(FailedSwapInfo failed_swap_info, string to_address); + SweepFailedSwapInfo prepare_sweep(FailedSwapInfo failed_swap_info, BitcoinAddressData destination); [Throws=LnError] string sweep(SweepFailedSwapInfo sweep_failed_swap_info); @@ -347,7 +347,7 @@ interface ReverseSwap { ClearWalletInfo prepare_clear_wallet(); [Throws=LnError] - void clear_wallet(ClearWalletInfo clear_wallet_info, BitcoinAddressData destination_onchain_address_data); + void clear_wallet(ClearWalletInfo clear_wallet_info, BitcoinAddressData destination); }; interface ChannelClose { @@ -355,7 +355,7 @@ interface ChannelClose { OnchainResolvingFees? determine_resolving_fees(); [Throws=SweepError] - SweepChannelCloseInfo prepare_sweep(string address); + SweepChannelCloseInfo prepare_sweep(BitcoinAddressData destination); [Throws=LnError] string sweep(SweepChannelCloseInfo sweep_info); diff --git a/src/onchain/channel_closes.rs b/src/onchain/channel_closes.rs index 99e83dcf..3cdc0cab 100644 --- a/src/onchain/channel_closes.rs +++ b/src/onchain/channel_closes.rs @@ -6,7 +6,8 @@ use crate::support::Support; use crate::{Amount, OnchainResolvingFees, RuntimeErrorCode, SweepInfo, CLN_DUST_LIMIT_SAT}; use breez_sdk_core::error::RedeemOnchainError; use breez_sdk_core::{ - OpeningFeeParams, PrepareRedeemOnchainFundsRequest, RedeemOnchainFundsRequest, + BitcoinAddressData, Network, OpeningFeeParams, PrepareRedeemOnchainFundsRequest, + RedeemOnchainFundsRequest, }; use perro::{ensure, invalid_input, MapToError}; use std::sync::Arc; @@ -44,11 +45,19 @@ impl ChannelClose { invalid_input("No on-chain funds to resolve") ); - let prepare_onchain_tx = move |to_address: String| -> Result<(Sats, Sats, u32)> { - let sweep_info = self.prepare_sweep(to_address).map_to_runtime_error( - RuntimeErrorCode::NodeUnavailable, - "Failed to prepare sweep funds from channel closes", - )?; + let prepare_onchain_tx = move |address: String| -> Result<(Sats, Sats, u32)> { + let sweep_info = self + .prepare_sweep(BitcoinAddressData { + address, + network: Network::Bitcoin, + amount_sat: None, + label: None, + message: None, + }) + .map_to_runtime_error( + RuntimeErrorCode::NodeUnavailable, + "Failed to prepare sweep funds from channel closes", + )?; Ok(( sweep_info.amount.sats.as_sats(), @@ -63,7 +72,8 @@ impl ChannelClose { /// Prepares a sweep of all available on-chain funds to the provided on-chain address. /// /// Parameters: - /// * `address` - the funds will be sweeped to this address + /// * `destination` - the destination address to which funds will be sent. + /// Can be obtained using [`Util::decode_data`](crate::Util::decode_data) /// /// Returns information on the prepared sweep, including the exact fee that results from /// using the provided fee rate. The method [`ChannelClose::sweep`] can be used to broadcast @@ -72,8 +82,9 @@ impl ChannelClose { /// Requires network: **yes** pub fn prepare_sweep( &self, - address: String, + destination: BitcoinAddressData, ) -> std::result::Result { + let address = destination.address; let onchain_fee_rate = query_onchain_fee_rate(&self.support) .map_err(|e| RedeemOnchainError::ServiceConnectivity { err: e.to_string() })?; let res = diff --git a/src/onchain/reverse_swap.rs b/src/onchain/reverse_swap.rs index f1a6209c..1e41eb0f 100644 --- a/src/onchain/reverse_swap.rs +++ b/src/onchain/reverse_swap.rs @@ -126,20 +126,20 @@ impl ReverseSwap { /// Parameters: /// * `clear_wallet_info` - An instance of [`ClearWalletInfo`] obtained using /// [`ReverseSwap::prepare_clear_wallet`]. - /// * `destination_onchain_address_data` - An on-chain address data instance. Can be obtained + /// * `destination` - An on-chain address data instance. Can be obtained /// using [`LightningNode::decode_data`](crate::LightningNode::decode_data). /// /// Requires network: **yes** pub fn clear_wallet( &self, clear_wallet_info: ClearWalletInfo, - destination_onchain_address_data: BitcoinAddressData, + destination: BitcoinAddressData, ) -> Result<()> { self.support .rt .handle() .block_on(self.support.sdk.pay_onchain(PayOnchainRequest { - recipient_address: destination_onchain_address_data.address, + recipient_address: destination.address, prepare_res: clear_wallet_info.prepare_response, })) .map_to_runtime_error( diff --git a/src/onchain/swap.rs b/src/onchain/swap.rs index 128240ae..e7112a20 100644 --- a/src/onchain/swap.rs +++ b/src/onchain/swap.rs @@ -9,7 +9,8 @@ use crate::{ }; use breez_sdk_core::error::ReceiveOnchainError; use breez_sdk_core::{ - OpeningFeeParams, PrepareRefundRequest, ReceiveOnchainRequest, RefundRequest, + BitcoinAddressData, Network, OpeningFeeParams, PrepareRefundRequest, ReceiveOnchainRequest, + RefundRequest, }; use perro::{ensure, runtime_error, MapToError}; use std::sync::Arc; @@ -73,8 +74,17 @@ impl Swap { failed_swap_info: FailedSwapInfo, ) -> Result> { let failed_swap_closure = failed_swap_info.clone(); - let prepare_onchain_tx = move |to_address: String| -> Result<(Sats, Sats, u32)> { - let sweep_info = self.prepare_sweep(failed_swap_closure, to_address)?; + let prepare_onchain_tx = move |address: String| -> Result<(Sats, Sats, u32)> { + let sweep_info = self.prepare_sweep( + failed_swap_closure, + BitcoinAddressData { + address, + network: Network::Bitcoin, + amount_sat: None, + label: None, + message: None, + }, + )?; Ok(( sweep_info.recovered_amount.sats.as_sats(), @@ -94,14 +104,16 @@ impl Swap { /// /// Parameters: /// * `failed_swap_info` - the failed swap that will be prepared - /// * `to_address` - the destination address to which funds will be sent + /// * `destination` - the destination address to which funds will be sent. + /// Can be obtained using [`Util::decode_data`](crate::Util::decode_data) /// /// Requires network: **yes** pub fn prepare_sweep( &self, failed_swap_info: FailedSwapInfo, - to_address: String, + destination: BitcoinAddressData, ) -> Result { + let to_address = destination.address; let onchain_fee_rate = query_onchain_fee_rate(&self.support)?; let response = self .support