Skip to content

Commit a222d72

Browse files
committed
psbt_params: Add optional plan Assets to Params
1 parent eaadf93 commit a222d72

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

wallet/src/wallet/psbt_params.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ use alloc::vec::Vec;
44

55
use bdk_tx::DefiniteDescriptor;
66
use bitcoin::{absolute, transaction::Version, Amount, FeeRate, OutPoint, ScriptBuf, Sequence};
7+
use miniscript::plan::Assets;
78

89
/// Parameters to create a PSBT.
9-
#[derive(Debug, Clone)]
10+
#[derive(Debug)]
1011
pub struct Params {
1112
// Inputs
1213
pub(crate) utxos: Vec<OutPoint>,
13-
// TODO: miniscript plan Assets?
14-
// pub(crate) assets: Assets,
14+
pub(crate) assets: Option<Assets>,
1515

1616
// Outputs
1717
pub(crate) recipients: Vec<(ScriptBuf, Amount)>,
@@ -33,6 +33,7 @@ impl Default for Params {
3333
fn default() -> Self {
3434
Self {
3535
utxos: Default::default(),
36+
assets: Default::default(),
3637
recipients: Default::default(),
3738
change_descriptor: Default::default(),
3839
feerate: bitcoin::FeeRate::BROADCAST_MIN,
@@ -48,6 +49,28 @@ impl Default for Params {
4849

4950
// TODO: more setters for Params
5051
impl Params {
52+
/// Add the spend [`Assets`].
53+
///
54+
/// Assets are required to create a spending plan for an output controlled by the wallet's
55+
/// descriptors. If none are provided here, then we assume all of the keys are equally likely
56+
/// to sign.
57+
///
58+
/// This may be called multiple times to add additional assets, however only the last
59+
/// absolute or relative timelock is retained. See also `AssetsExt`.
60+
pub fn add_assets<I, S>(&mut self, assets: Assets) -> &mut Self {
61+
let mut new = match self.assets {
62+
Some(ref existing) => {
63+
let mut new = Assets::new();
64+
new.extend(existing);
65+
new
66+
}
67+
None => Assets::new(),
68+
};
69+
new.extend(&assets);
70+
self.assets = Some(new);
71+
self
72+
}
73+
5174
/// Add recipients.
5275
///
5376
/// - `recipients`: An iterator of `(S, Amount)` tuples where `S` can be a bitcoin [`Address`],
@@ -86,3 +109,27 @@ pub enum SelectionStrategy {
86109
/// [`LowestFee`](bdk_coin_select::metrics::LowestFee) metric for more.
87110
LowestFee,
88111
}
112+
113+
/// Trait to extend the functionality of [`Assets`].
114+
pub(crate) trait AssetsExt {
115+
/// Extend `self` with the contents of `other`.
116+
fn extend(&mut self, other: &Self);
117+
}
118+
119+
impl AssetsExt for Assets {
120+
/// Extend `self` with the contents of `other`. Note that if present this preferentially
121+
/// uses the absolute and relative timelocks of `other`.
122+
fn extend(&mut self, other: &Self) {
123+
self.keys.extend(other.keys.clone());
124+
self.sha256_preimages.extend(other.sha256_preimages.clone());
125+
self.hash256_preimages
126+
.extend(other.hash256_preimages.clone());
127+
self.ripemd160_preimages
128+
.extend(other.ripemd160_preimages.clone());
129+
self.hash160_preimages
130+
.extend(other.hash160_preimages.clone());
131+
132+
self.absolute_timelock = other.absolute_timelock.or(self.absolute_timelock);
133+
self.relative_timelock = other.relative_timelock.or(self.relative_timelock);
134+
}
135+
}

0 commit comments

Comments
 (0)