Skip to content

Commit

Permalink
Merge pull request #1275 from getlipa/feature/use-bitcoin-address-dat…
Browse files Browse the repository at this point in the history
…a-for-address-params

Use `BitcoinAddressData` for address parameters
  • Loading branch information
danielgranhao authored Nov 21, 2024
2 parents bd4fdef + 9098512 commit ddf7b3f
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 24 deletions.
17 changes: 15 additions & 2 deletions examples/node/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@ fn print_failed_swap(swap: &FailedSwapInfo) {
fn refund_failed_swap(node: &LightningNode, words: &mut dyn Iterator<Item = &str>) -> 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
Expand All @@ -1037,7 +1038,7 @@ fn refund_failed_swap(node: &LightningNode, words: &mut dyn Iterator<Item = &str
let resolve_failed_swap_info = node
.onchain()
.swap()
.prepare_sweep(failed_swap, to_address.into())
.prepare_sweep(failed_swap, address_data)
.map_err(|e| anyhow!("Failed to prepare the resolution of the failed swap: {e}"))?;
let txid = node
.onchain()
Expand Down Expand Up @@ -1420,7 +1421,8 @@ fn set_personal_note(node: &LightningNode, words: &mut dyn Iterator<Item = &str>
}

fn sweep(node: &LightningNode, address: String) -> Result<String> {
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)?)
}

Expand Down Expand Up @@ -1742,3 +1744,14 @@ fn failed_swap_from_actions_required_list(list: &[ActionRequiredItem]) -> Vec<Fa
})
.collect::<Vec<_>>()
}

fn get_bitcoin_address_data(decoded_data: DecodedData) -> Result<BitcoinAddressData> {
if let DecodedData::OnchainAddress {
onchain_address_details,
} = decoded_data
{
Ok(onchain_address_details)
} else {
bail!("Not an onchain address")
}
}
12 changes: 11 additions & 1 deletion src/actions_required.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
19 changes: 17 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,13 @@ impl LightningNode {
) -> std::result::Result<SweepInfo, RedeemOnchainError> {
self.onchain
.channel_close()
.prepare_sweep(address)
.prepare_sweep(BitcoinAddressData {
address,
network: Network::Bitcoin,
amount_sat: None,
label: None,
message: None,
})
.map(SweepInfo::from)
}

Expand Down Expand Up @@ -1327,7 +1333,16 @@ impl LightningNode {
) -> Result<ResolveFailedSwapInfo> {
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)
}

Expand Down
6 changes: 3 additions & 3 deletions src/lipalightninglib.udl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -347,15 +347,15 @@ 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 {
[Throws=LnError]
OnchainResolvingFees? determine_resolving_fees();

[Throws=SweepError]
SweepChannelCloseInfo prepare_sweep(string address);
SweepChannelCloseInfo prepare_sweep(BitcoinAddressData destination);

[Throws=LnError]
string sweep(SweepChannelCloseInfo sweep_info);
Expand Down
27 changes: 19 additions & 8 deletions src/onchain/channel_closes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand All @@ -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
Expand All @@ -72,8 +82,9 @@ impl ChannelClose {
/// Requires network: **yes**
pub fn prepare_sweep(
&self,
address: String,
destination: BitcoinAddressData,
) -> std::result::Result<SweepChannelCloseInfo, RedeemOnchainError> {
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 =
Expand Down
6 changes: 3 additions & 3 deletions src/onchain/reverse_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
22 changes: 17 additions & 5 deletions src/onchain/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,8 +74,17 @@ impl Swap {
failed_swap_info: FailedSwapInfo,
) -> Result<Option<OnchainResolvingFees>> {
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(),
Expand All @@ -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<SweepFailedSwapInfo> {
let to_address = destination.address;
let onchain_fee_rate = query_onchain_fee_rate(&self.support)?;
let response = self
.support
Expand Down

0 comments on commit ddf7b3f

Please sign in to comment.