Skip to content

Commit

Permalink
feat: expose TxBuilder::add_data method
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Oct 22, 2024
1 parent b25d48f commit e06f2a9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ interface CreateTxError {
UnknownUtxo(string outpoint);
MissingNonWitnessUtxo(string outpoint);
MiniscriptPsbt(string error_message);
PushBytesError();
};

[Error]
Expand Down Expand Up @@ -474,6 +475,8 @@ interface TxBuilder {

TxBuilder set_exact_sequence(u32 nsequence);

TxBuilder add_data(sequence<u8> data);

[Throws=CreateTxError]
Psbt finish([ByRef] Wallet wallet);
};
Expand Down
10 changes: 10 additions & 0 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bitcoin_ffi::OutPoint;

use bdk_core::bitcoin::script::PushBytesError;
use bdk_electrum::electrum_client::Error as BdkElectrumError;
use bdk_esplora::esplora_client::{Error as BdkEsploraError, Error};
use bdk_wallet::bitcoin::address::ParseError as BdkParseError;
Expand Down Expand Up @@ -194,6 +195,9 @@ pub enum CreateTxError {

#[error("miniscript psbt error: {error_message}")]
MiniscriptPsbt { error_message: String },

#[error("attempt to prepare too many bytes to be pushed into script")]
PushBytesError,
}

#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -826,6 +830,12 @@ impl From<BdkCreateTxError> for CreateTxError {
}
}

impl From<PushBytesError> for CreateTxError {
fn from(_: PushBytesError) -> Self {
CreateTxError::PushBytesError
}
}

impl From<BdkCreateWithPersistError<chain::rusqlite::Error>> for CreateWithPersistError {
fn from(error: BdkCreateWithPersistError<chain::rusqlite::Error>) -> Self {
match error {
Expand Down
15 changes: 15 additions & 0 deletions bdk-ffi/src/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use bdk_wallet::bitcoin::ScriptBuf as BdkScriptBuf;
use bdk_wallet::bitcoin::{OutPoint, Sequence, Txid};
use bdk_wallet::ChangeSpendPolicy;

use bdk_wallet::bitcoin::script::PushBytesBuf;
use std::collections::HashSet;
use std::convert::TryFrom;
use std::str::FromStr;
use std::sync::Arc;

Expand All @@ -28,6 +30,7 @@ pub struct TxBuilder {
pub(crate) drain_wallet: bool,
pub(crate) drain_to: Option<BdkScriptBuf>,
pub(crate) sequence: Option<u32>,
pub(crate) data: Vec<u8>,
}

impl TxBuilder {
Expand All @@ -44,6 +47,7 @@ impl TxBuilder {
drain_wallet: false,
drain_to: None,
sequence: None,
data: Vec::new(),
}
}

Expand Down Expand Up @@ -167,6 +171,13 @@ impl TxBuilder {
})
}

pub(crate) fn add_data(&self, data: Vec<u8>) -> Arc<Self> {
Arc::new(TxBuilder {
data,
..self.clone()
})
}

pub(crate) fn finish(&self, wallet: &Arc<Wallet>) -> Result<Arc<Psbt>, 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();
Expand Down Expand Up @@ -205,6 +216,10 @@ impl TxBuilder {
if let Some(sequence) = self.sequence {
tx_builder.set_exact_sequence(Sequence(sequence));
}
if !&self.data.is_empty() {
let push_bytes = PushBytesBuf::try_from(self.data.clone())?;
tx_builder.add_data(&push_bytes);
}

let psbt = tx_builder.finish().map_err(CreateTxError::from)?;

Expand Down

0 comments on commit e06f2a9

Please sign in to comment.