From 1d8b95df2848ce5d4c11f16a7075b5c0d4a9b903 Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Fri, 15 Nov 2024 11:31:25 -0500 Subject: [PATCH] feat: expose TxBuilder::current_height --- bdk-ffi/src/bdk.udl | 4 ++++ bdk-ffi/src/tx_builder.rs | 28 ++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index 121c766f..3a506f96 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -705,6 +705,8 @@ interface TxBuilder { TxBuilder add_data(sequence data); + TxBuilder current_height(u32 height); + [Throws=CreateTxError] Psbt finish([ByRef] Wallet wallet); }; @@ -714,6 +716,8 @@ interface BumpFeeTxBuilder { BumpFeeTxBuilder set_exact_sequence(u32 nsequence); + BumpFeeTxBuilder current_height(u32 height); + [Throws=CreateTxError] Psbt finish([ByRef] Wallet wallet); }; diff --git a/bdk-ffi/src/tx_builder.rs b/bdk-ffi/src/tx_builder.rs index dde2a6fe..6d4d7e94 100644 --- a/bdk-ffi/src/tx_builder.rs +++ b/bdk-ffi/src/tx_builder.rs @@ -3,18 +3,18 @@ use crate::error::CreateTxError; use crate::types::ScriptAmount; use crate::wallet::Wallet; -use bdk_wallet::KeychainKind; use bitcoin_ffi::{Amount, FeeRate, Script}; use bdk_wallet::bitcoin::amount::Amount as BdkAmount; +use bdk_wallet::bitcoin::script::PushBytesBuf; use bdk_wallet::bitcoin::Psbt as BdkPsbt; use bdk_wallet::bitcoin::ScriptBuf as BdkScriptBuf; use bdk_wallet::bitcoin::{OutPoint, Sequence, Txid}; use bdk_wallet::ChangeSpendPolicy; +use bdk_wallet::KeychainKind; use std::collections::BTreeMap; use std::collections::HashMap; -use bdk_wallet::bitcoin::script::PushBytesBuf; use std::collections::HashSet; use std::convert::TryFrom; use std::str::FromStr; @@ -36,6 +36,7 @@ pub struct TxBuilder { pub(crate) drain_to: Option, pub(crate) sequence: Option, pub(crate) data: Vec, + pub(crate) current_height: Option, } impl TxBuilder { @@ -55,6 +56,7 @@ impl TxBuilder { drain_to: None, sequence: None, data: Vec::new(), + current_height: None, } } @@ -204,6 +206,13 @@ impl TxBuilder { }) } + pub(crate) fn current_height(&self, height: u32) -> Arc { + Arc::new(TxBuilder { + current_height: Some(height), + ..self.clone() + }) + } + pub(crate) fn finish(&self, wallet: &Arc) -> Result, CreateTxError> { // TODO: I had to change the wallet here to be mutable. Why is that now required with the 1.0 API? let mut wallet = wallet.get_wallet(); @@ -252,6 +261,9 @@ impl TxBuilder { let push_bytes = PushBytesBuf::try_from(self.data.clone())?; tx_builder.add_data(&push_bytes); } + if let Some(height) = self.current_height { + tx_builder.current_height(height); + } let psbt = tx_builder.finish().map_err(CreateTxError::from)?; @@ -264,6 +276,7 @@ pub(crate) struct BumpFeeTxBuilder { pub(crate) txid: String, pub(crate) fee_rate: Arc, pub(crate) sequence: Option, + pub(crate) current_height: Option, } impl BumpFeeTxBuilder { @@ -272,6 +285,7 @@ impl BumpFeeTxBuilder { txid, fee_rate, sequence: None, + current_height: None, } } @@ -282,6 +296,13 @@ impl BumpFeeTxBuilder { }) } + pub(crate) fn current_height(&self, height: u32) -> Arc { + Arc::new(BumpFeeTxBuilder { + current_height: Some(height), + ..self.clone() + }) + } + pub(crate) fn finish(&self, wallet: &Arc) -> Result, CreateTxError> { let txid = Txid::from_str(self.txid.as_str()).map_err(|_| CreateTxError::UnknownUtxo { outpoint: self.txid.clone(), @@ -292,6 +313,9 @@ impl BumpFeeTxBuilder { if let Some(sequence) = self.sequence { tx_builder.set_exact_sequence(Sequence(sequence)); } + if let Some(height) = self.current_height { + tx_builder.current_height(height); + } let psbt: BdkPsbt = tx_builder.finish()?;