Merge bitcoindevkit/bdk-ffi#150: Add new BumpFeeTxBuilder interface

0787d9c446a3390f0e020dcabd5f30a9d760391d Fix order of BumpFeeTxBuilder parameters (Steve Myers)
390d12703eb12fc3b58a14f62a64f8582ebbeaf2 Change TxBuilder and BumpFeeTxBuilder build() to finish() (Steve Myers)
9f903932dc59e1cc955112492b699330b8806d93 Add BumpFeeTxBuilder (Steve Myers)

Pull request description:

  Add BumpFeeTxBuilder to bump the fee on an unconfirmed tx created by the Wallet. The structure of the new interface is:

  ```udl
  interface BumpFeeTxBuilder {
    constructor(string txid, float new_fee_rate);
    BumpFeeTxBuilder allow_shrinking(string address);
    BumpFeeTxBuilder enable_rbf();
    BumpFeeTxBuilder enable_rbf_with_sequence(u32 nsequence);
    [Throws=BdkError]
    PartiallySignedBitcoinTransaction build([ByRef] Wallet wallet);
  };
  ```
  Fixes #150

Top commit has no ACKs.

Tree-SHA512: a24ed41f7b897a0e091ef55d4c1347b3973cbe628b7d1bd69c95e663bc6f049de8f7e3b019115d763cff3fa6d5cb4d31ed474022c3087e8404a2af37f380d56d
This commit is contained in:
Steve Myers 2022-05-10 21:53:31 -07:00
commit 71583eca7f
No known key found for this signature in database
GPG Key ID: 8105A46B22C2D051
3 changed files with 88 additions and 3 deletions

View File

@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
- Update BDK to version 0.18.0 - Update BDK to version 0.18.0
- Add BumpFeeTxBuilder to bump the fee on an unconfirmed tx created by the Wallet
- Change TxBuilder.build() to TxBuilder.finish() to align with bdk function name
## [v0.5.0] ## [v0.5.0]

View File

@ -155,7 +155,16 @@ interface TxBuilder {
TxBuilder enable_rbf(); TxBuilder enable_rbf();
TxBuilder enable_rbf_with_sequence(u32 nsequence); TxBuilder enable_rbf_with_sequence(u32 nsequence);
[Throws=BdkError] [Throws=BdkError]
PartiallySignedBitcoinTransaction build([ByRef] Wallet wallet); PartiallySignedBitcoinTransaction finish([ByRef] Wallet wallet);
};
interface BumpFeeTxBuilder {
constructor(string txid, float new_fee_rate);
BumpFeeTxBuilder allow_shrinking(string address);
BumpFeeTxBuilder enable_rbf();
BumpFeeTxBuilder enable_rbf_with_sequence(u32 nsequence);
[Throws=BdkError]
PartiallySignedBitcoinTransaction finish([ByRef] Wallet wallet);
}; };
dictionary ExtendedKeyInfo { dictionary ExtendedKeyInfo {

View File

@ -1,7 +1,7 @@
use bdk::bitcoin::hashes::hex::ToHex; use bdk::bitcoin::hashes::hex::ToHex;
use bdk::bitcoin::secp256k1::Secp256k1; use bdk::bitcoin::secp256k1::Secp256k1;
use bdk::bitcoin::util::psbt::PartiallySignedTransaction; use bdk::bitcoin::util::psbt::PartiallySignedTransaction;
use bdk::bitcoin::{Address, Network, Script}; use bdk::bitcoin::{Address, Network, Script, Txid};
use bdk::blockchain::any::{AnyBlockchain, AnyBlockchainConfig}; use bdk::blockchain::any::{AnyBlockchain, AnyBlockchainConfig};
use bdk::blockchain::{ use bdk::blockchain::{
electrum::ElectrumBlockchainConfig, esplora::EsploraBlockchainConfig, ConfigurableBlockchain, electrum::ElectrumBlockchainConfig, esplora::EsploraBlockchainConfig, ConfigurableBlockchain,
@ -406,7 +406,7 @@ impl TxBuilder {
}) })
} }
fn build(&self, wallet: &Wallet) -> Result<Arc<PartiallySignedBitcoinTransaction>, Error> { fn finish(&self, wallet: &Wallet) -> Result<Arc<PartiallySignedBitcoinTransaction>, Error> {
let wallet = wallet.get_wallet(); let wallet = wallet.get_wallet();
let mut tx_builder = wallet.build_tx(); let mut tx_builder = wallet.build_tx();
for (address, amount) in &self.recipients { for (address, amount) in &self.recipients {
@ -440,4 +440,78 @@ impl TxBuilder {
} }
} }
struct BumpFeeTxBuilder {
txid: String,
fee_rate: f32,
allow_shrinking: Option<String>,
rbf: Option<RbfValue>,
}
impl BumpFeeTxBuilder {
fn new(txid: String, fee_rate: f32) -> Self {
Self {
txid,
fee_rate,
allow_shrinking: None,
rbf: None,
}
}
fn allow_shrinking(&self, address: String) -> Arc<Self> {
Arc::new(Self {
txid: self.txid.clone(),
fee_rate: self.fee_rate,
allow_shrinking: Some(address),
rbf: self.rbf.clone(),
})
}
fn enable_rbf(&self) -> Arc<Self> {
Arc::new(Self {
txid: self.txid.clone(),
fee_rate: self.fee_rate,
allow_shrinking: self.allow_shrinking.clone(),
rbf: Some(RbfValue::Default),
})
}
fn enable_rbf_with_sequence(&self, nsequence: u32) -> Arc<Self> {
Arc::new(Self {
txid: self.txid.clone(),
fee_rate: self.fee_rate,
allow_shrinking: self.allow_shrinking.clone(),
rbf: Some(RbfValue::Value(nsequence)),
})
}
fn finish(&self, wallet: &Wallet) -> Result<Arc<PartiallySignedBitcoinTransaction>, Error> {
let wallet = wallet.get_wallet();
let txid = Txid::from_str(self.txid.as_str())?;
let mut tx_builder = wallet.build_fee_bump(txid)?;
tx_builder.fee_rate(FeeRate::from_sat_per_vb(self.fee_rate));
if let Some(allow_shrinking) = &self.allow_shrinking {
let address =
Address::from_str(allow_shrinking).map_err(|e| Error::Generic(e.to_string()))?;
let script = address.script_pubkey();
tx_builder.allow_shrinking(script)?;
}
if let Some(rbf) = &self.rbf {
match *rbf {
RbfValue::Default => {
tx_builder.enable_rbf();
}
RbfValue::Value(nsequence) => {
tx_builder.enable_rbf_with_sequence(nsequence);
}
}
}
tx_builder
.finish()
.map(|(psbt, _)| PartiallySignedBitcoinTransaction {
internal: Mutex::new(psbt),
})
.map(Arc::new)
}
}
uniffi::deps::static_assertions::assert_impl_all!(Wallet: Sync, Send); uniffi::deps::static_assertions::assert_impl_all!(Wallet: Sync, Send);