Skip to content

Commit

Permalink
feat: expose TxBuilder::policy_path
Browse files Browse the repository at this point in the history
  • Loading branch information
BitcoinZavior committed Oct 22, 2024
1 parent 61104c6 commit c6c5eb9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,8 @@ interface TxBuilder {

TxBuilder add_utxo(OutPoint outpoint);

TxBuilder policy_path(record<string, sequence<u64>> policy_path, KeychainKind keychain);

TxBuilder change_policy(ChangeSpendPolicy change_policy);

TxBuilder do_not_spend_change();
Expand Down
25 changes: 25 additions & 0 deletions bdk-ffi/src/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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;
Expand All @@ -11,6 +12,8 @@ use bdk_wallet::bitcoin::ScriptBuf as BdkScriptBuf;
use bdk_wallet::bitcoin::{OutPoint, Sequence, Txid};
use bdk_wallet::ChangeSpendPolicy;

use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::str::FromStr;
use std::sync::Arc;
Expand All @@ -21,6 +24,7 @@ pub struct TxBuilder {
pub(crate) recipients: Vec<(BdkScriptBuf, BdkAmount)>,
pub(crate) utxos: Vec<OutPoint>,
pub(crate) unspendable: HashSet<OutPoint>,
pub(crate) policy_path: (HashMap<String, Vec<u64>>, KeychainKind),
pub(crate) change_policy: ChangeSpendPolicy,
pub(crate) manually_selected_only: bool,
pub(crate) fee_rate: Option<FeeRate>,
Expand All @@ -37,6 +41,7 @@ impl TxBuilder {
recipients: Vec::new(),
utxos: Vec::new(),
unspendable: HashSet::new(),
policy_path: (HashMap::new(), KeychainKind::External),
change_policy: ChangeSpendPolicy::ChangeAllowed,
manually_selected_only: false,
fee_rate: None,
Expand Down Expand Up @@ -104,6 +109,17 @@ impl TxBuilder {
})
}

pub(crate) fn policy_path(
&self,
policy_path: HashMap<String, Vec<u64>>,
keychain: KeychainKind,
) -> Arc<Self> {
Arc::new(TxBuilder {
policy_path: (policy_path, keychain),
..self.clone()
})
}

pub(crate) fn change_policy(&self, change_policy: ChangeSpendPolicy) -> Arc<Self> {
Arc::new(TxBuilder {
change_policy,
Expand Down Expand Up @@ -177,6 +193,15 @@ impl TxBuilder {
for (script, amount) in &self.recipients {
tx_builder.add_recipient(script.clone(), *amount);
}
tx_builder.policy_path(
self.policy_path
.0
.clone()
.into_iter()
.map(|(key, value)| (key, value.into_iter().map(|x| x as usize).collect()))
.collect::<BTreeMap<String, Vec<usize>>>(),
self.policy_path.1,
);
tx_builder.change_policy(self.change_policy);
if !self.utxos.is_empty() {
tx_builder
Expand Down

0 comments on commit c6c5eb9

Please sign in to comment.