Skip to content

Commit 2e4b89e

Browse files
committed
refactor: make Descriptor plan API available for generic Pk
- Migrate `plan` and `plan_mall` methods from `DefiniteDescriptorKey` to generic `Pk: MiniscriptKey + ToPublicKey`. - Made the `Plan` struct generic over `Pk` (excluding `update_psbt_input` since it needs descriptor key data).
1 parent 7243b45 commit 2e4b89e

6 files changed

Lines changed: 41 additions & 86 deletions

File tree

src/descriptor/bare.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use core::fmt;
1212
use bitcoin::script::{self, PushBytes};
1313
use bitcoin::{Address, Network, ScriptBuf, Weight};
1414

15-
use crate::descriptor::{write_descriptor, DefiniteDescriptorKey};
15+
use crate::descriptor::write_descriptor;
1616
use crate::expression::{self, FromTree};
1717
use crate::miniscript::context::{ScriptContext, ScriptContextError};
1818
use crate::miniscript::satisfy::{Placeholder, Satisfaction, Witness};
@@ -138,25 +138,19 @@ impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
138138
}
139139
}
140140

141-
impl Bare<DefiniteDescriptorKey> {
141+
impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
142142
/// Returns a plan if the provided assets are sufficient to produce a non-malleable satisfaction
143-
pub fn plan_satisfaction<P>(
144-
&self,
145-
provider: &P,
146-
) -> Satisfaction<Placeholder<DefiniteDescriptorKey>>
143+
pub fn plan_satisfaction<P>(&self, provider: &P) -> Satisfaction<Placeholder<Pk>>
147144
where
148-
P: AssetProvider<DefiniteDescriptorKey>,
145+
P: AssetProvider<Pk>,
149146
{
150147
self.ms.build_template(provider)
151148
}
152149

153150
/// Returns a plan if the provided assets are sufficient to produce a malleable satisfaction
154-
pub fn plan_satisfaction_mall<P>(
155-
&self,
156-
provider: &P,
157-
) -> Satisfaction<Placeholder<DefiniteDescriptorKey>>
151+
pub fn plan_satisfaction_mall<P>(&self, provider: &P) -> Satisfaction<Placeholder<Pk>>
158152
where
159-
P: AssetProvider<DefiniteDescriptorKey>,
153+
P: AssetProvider<Pk>,
160154
{
161155
self.ms.build_template_mall(provider)
162156
}
@@ -316,16 +310,11 @@ impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
316310
{
317311
self.get_satisfaction(satisfier)
318312
}
319-
}
320313

321-
impl Pkh<DefiniteDescriptorKey> {
322314
/// Returns a plan if the provided assets are sufficient to produce a non-malleable satisfaction
323-
pub fn plan_satisfaction<P>(
324-
&self,
325-
provider: &P,
326-
) -> Satisfaction<Placeholder<DefiniteDescriptorKey>>
315+
pub fn plan_satisfaction<P>(&self, provider: &P) -> Satisfaction<Placeholder<Pk>>
327316
where
328-
P: AssetProvider<DefiniteDescriptorKey>,
317+
P: AssetProvider<Pk>,
329318
{
330319
let stack = if provider.provider_lookup_ecdsa_sig(&self.pk) {
331320
let stack = vec![
@@ -341,12 +330,9 @@ impl Pkh<DefiniteDescriptorKey> {
341330
}
342331

343332
/// Returns a plan if the provided assets are sufficient to produce a malleable satisfaction
344-
pub fn plan_satisfaction_mall<P>(
345-
&self,
346-
provider: &P,
347-
) -> Satisfaction<Placeholder<DefiniteDescriptorKey>>
333+
pub fn plan_satisfaction_mall<P>(&self, provider: &P) -> Satisfaction<Placeholder<Pk>>
348334
where
349-
P: AssetProvider<DefiniteDescriptorKey>,
335+
P: AssetProvider<Pk>,
350336
{
351337
self.plan_satisfaction(provider)
352338
}

src/descriptor/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,14 +552,14 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
552552
}
553553
}
554554

555-
impl Descriptor<DefiniteDescriptorKey> {
555+
impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
556556
/// Returns a plan if the provided assets are sufficient to produce a non-malleable satisfaction
557557
///
558558
/// If the assets aren't sufficient for generating a Plan, the descriptor is returned
559559
#[allow(clippy::result_large_err)] // our "error type" is the original descriptor
560-
pub fn plan<P>(self, provider: &P) -> Result<Plan, Self>
560+
pub fn plan<P>(self, provider: &P) -> Result<Plan<Pk>, Self>
561561
where
562-
P: AssetProvider<DefiniteDescriptorKey>,
562+
P: AssetProvider<Pk>,
563563
{
564564
let satisfaction = match self {
565565
Descriptor::Bare(ref bare) => bare.plan_satisfaction(provider),
@@ -586,9 +586,9 @@ impl Descriptor<DefiniteDescriptorKey> {
586586
///
587587
/// If the assets aren't sufficient for generating a Plan, the descriptor is returned
588588
#[allow(clippy::result_large_err)] // our "error type" is the original descriptor
589-
pub fn plan_mall<P>(self, provider: &P) -> Result<Plan, Self>
589+
pub fn plan_mall<P>(self, provider: &P) -> Result<Plan<Pk>, Self>
590590
where
591-
P: AssetProvider<DefiniteDescriptorKey>,
591+
P: AssetProvider<Pk>,
592592
{
593593
let satisfaction = match self {
594594
Descriptor::Bare(ref bare) => bare.plan_satisfaction_mall(provider),

src/descriptor/segwitv0.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use core::fmt;
1010

1111
use bitcoin::{Address, Network, ScriptBuf, Weight};
1212

13-
use crate::descriptor::{write_descriptor, DefiniteDescriptorKey};
13+
use crate::descriptor::write_descriptor;
1414
use crate::expression::{self, FromTree};
1515
use crate::miniscript::context::{ScriptContext, ScriptContextError};
1616
use crate::miniscript::limits::MAX_PUBKEYS_PER_MULTISIG;
@@ -159,27 +159,19 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wsh<Pk> {
159159
let script_sig = ScriptBuf::new();
160160
Ok((witness, script_sig))
161161
}
162-
}
163162

164-
impl Wsh<DefiniteDescriptorKey> {
165163
/// Returns a plan if the provided assets are sufficient to produce a non-malleable satisfaction
166-
pub fn plan_satisfaction<P>(
167-
&self,
168-
provider: &P,
169-
) -> Satisfaction<Placeholder<DefiniteDescriptorKey>>
164+
pub fn plan_satisfaction<P>(&self, provider: &P) -> Satisfaction<Placeholder<Pk>>
170165
where
171-
P: AssetProvider<DefiniteDescriptorKey>,
166+
P: AssetProvider<Pk>,
172167
{
173168
self.ms.build_template(provider)
174169
}
175170

176171
/// Returns a plan if the provided assets are sufficient to produce a malleable satisfaction
177-
pub fn plan_satisfaction_mall<P>(
178-
&self,
179-
provider: &P,
180-
) -> Satisfaction<Placeholder<DefiniteDescriptorKey>>
172+
pub fn plan_satisfaction_mall<P>(&self, provider: &P) -> Satisfaction<Placeholder<Pk>>
181173
where
182-
P: AssetProvider<DefiniteDescriptorKey>,
174+
P: AssetProvider<Pk>,
183175
{
184176
if let Terminal::SortedMulti(..) = self.ms.node {
185177
self.ms.build_template(provider)
@@ -363,16 +355,11 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
363355
{
364356
self.get_satisfaction(satisfier)
365357
}
366-
}
367358

368-
impl Wpkh<DefiniteDescriptorKey> {
369359
/// Returns a plan if the provided assets are sufficient to produce a non-malleable satisfaction
370-
pub fn plan_satisfaction<P>(
371-
&self,
372-
provider: &P,
373-
) -> Satisfaction<Placeholder<DefiniteDescriptorKey>>
360+
pub fn plan_satisfaction<P>(&self, provider: &P) -> Satisfaction<Placeholder<Pk>>
374361
where
375-
P: AssetProvider<DefiniteDescriptorKey>,
362+
P: AssetProvider<Pk>,
376363
{
377364
let stack = if provider.provider_lookup_ecdsa_sig(&self.pk) {
378365
let stack = vec![
@@ -388,12 +375,9 @@ impl Wpkh<DefiniteDescriptorKey> {
388375
}
389376

390377
/// Returns a plan if the provided assets are sufficient to produce a malleable satisfaction
391-
pub fn plan_satisfaction_mall<P>(
392-
&self,
393-
provider: &P,
394-
) -> Satisfaction<Placeholder<DefiniteDescriptorKey>>
378+
pub fn plan_satisfaction_mall<P>(&self, provider: &P) -> Satisfaction<Placeholder<Pk>>
395379
where
396-
P: AssetProvider<DefiniteDescriptorKey>,
380+
P: AssetProvider<Pk>,
397381
{
398382
self.plan_satisfaction(provider)
399383
}

src/descriptor/sh.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use bitcoin::script::PushBytes;
1414
use bitcoin::{script, Address, Network, ScriptBuf, Weight};
1515

1616
use super::{Wpkh, Wsh};
17-
use crate::descriptor::{write_descriptor, DefiniteDescriptorKey};
17+
use crate::descriptor::write_descriptor;
1818
use crate::expression::{self, FromTree};
1919
use crate::miniscript::context::ScriptContext;
2020
use crate::miniscript::limits::MAX_PUBKEYS_PER_MULTISIG;
@@ -377,16 +377,11 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
377377
_ => self.get_satisfaction(satisfier),
378378
}
379379
}
380-
}
381380

382-
impl Sh<DefiniteDescriptorKey> {
383381
/// Returns a plan if the provided assets are sufficient to produce a non-malleable satisfaction
384-
pub fn plan_satisfaction<P>(
385-
&self,
386-
provider: &P,
387-
) -> Satisfaction<Placeholder<DefiniteDescriptorKey>>
382+
pub fn plan_satisfaction<P>(&self, provider: &P) -> Satisfaction<Placeholder<Pk>>
388383
where
389-
P: AssetProvider<DefiniteDescriptorKey>,
384+
P: AssetProvider<Pk>,
390385
{
391386
match &self.inner {
392387
ShInner::Wsh(ref wsh) => wsh.plan_satisfaction(provider),
@@ -396,12 +391,9 @@ impl Sh<DefiniteDescriptorKey> {
396391
}
397392

398393
/// Returns a plan if the provided assets are sufficient to produce a malleable satisfaction
399-
pub fn plan_satisfaction_mall<P>(
400-
&self,
401-
provider: &P,
402-
) -> Satisfaction<Placeholder<DefiniteDescriptorKey>>
394+
pub fn plan_satisfaction_mall<P>(&self, provider: &P) -> Satisfaction<Placeholder<Pk>>
403395
where
404-
P: AssetProvider<DefiniteDescriptorKey>,
396+
P: AssetProvider<Pk>,
405397
{
406398
match &self.inner {
407399
ShInner::Wsh(ref wsh) => wsh.plan_satisfaction_mall(provider),

src/descriptor/tr/mod.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use bitcoin::{opcodes, Address, Network, ScriptBuf, Weight};
77
use sync::Arc;
88

99
use super::checksum;
10-
use crate::descriptor::DefiniteDescriptorKey;
1110
use crate::expression::{self, FromTree};
1211
use crate::miniscript::satisfy::{Placeholder, Satisfaction, SchnorrSigType, Witness};
1312
use crate::miniscript::Miniscript;
@@ -318,27 +317,19 @@ impl<Pk: MiniscriptKey + ToPublicKey> Tr<Pk> {
318317
Err(Error::CouldNotSatisfy)
319318
}
320319
}
321-
}
322320

323-
impl Tr<DefiniteDescriptorKey> {
324321
/// Returns a plan if the provided assets are sufficient to produce a non-malleable satisfaction
325-
pub fn plan_satisfaction<P>(
326-
&self,
327-
provider: &P,
328-
) -> Satisfaction<Placeholder<DefiniteDescriptorKey>>
322+
pub fn plan_satisfaction<P>(&self, provider: &P) -> Satisfaction<Placeholder<Pk>>
329323
where
330-
P: AssetProvider<DefiniteDescriptorKey>,
324+
P: AssetProvider<Pk>,
331325
{
332326
best_tap_spend(self, provider, false /* allow_mall */)
333327
}
334328

335329
/// Returns a plan if the provided assets are sufficient to produce a malleable satisfaction
336-
pub fn plan_satisfaction_mall<P>(
337-
&self,
338-
provider: &P,
339-
) -> Satisfaction<Placeholder<DefiniteDescriptorKey>>
330+
pub fn plan_satisfaction_mall<P>(&self, provider: &P) -> Satisfaction<Placeholder<Pk>>
340331
where
341-
P: AssetProvider<DefiniteDescriptorKey>,
332+
P: AssetProvider<Pk>,
342333
{
343334
best_tap_spend(self, provider, true /* allow_mall */)
344335
}

src/plan.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,20 @@ where
210210
/// Calling `plan` on a Descriptor will return this structure,
211211
/// containing the cheapest spending path possible (considering the `Assets` given)
212212
#[derive(Debug, Clone)]
213-
pub struct Plan {
213+
pub struct Plan<Pk: MiniscriptKey> {
214214
/// This plan's witness template
215-
pub(crate) template: Vec<Placeholder<DefiniteDescriptorKey>>,
215+
pub(crate) template: Vec<Placeholder<Pk>>,
216216
/// The absolute timelock this plan uses
217217
pub absolute_timelock: Option<absolute::LockTime>,
218218
/// The relative timelock this plan uses
219219
pub relative_timelock: Option<relative::LockTime>,
220220

221-
pub(crate) descriptor: Descriptor<DefiniteDescriptorKey>,
221+
pub(crate) descriptor: Descriptor<Pk>,
222222
}
223223

224-
impl Plan {
224+
impl<Pk: MiniscriptKey + ToPublicKey> Plan<Pk> {
225225
/// Returns the witness template
226-
pub fn witness_template(&self) -> &Vec<Placeholder<DefiniteDescriptorKey>> { &self.template }
226+
pub fn witness_template(&self) -> &Vec<Placeholder<Pk>> { &self.template }
227227

228228
/// Returns the witness version
229229
pub fn witness_version(&self) -> Option<WitnessVersion> {
@@ -264,7 +264,7 @@ impl Plan {
264264
}
265265

266266
/// Try creating the final script_sig and witness using a [`Satisfier`]
267-
pub fn satisfy<Sat: Satisfier<DefiniteDescriptorKey>>(
267+
pub fn satisfy<Sat: Satisfier<Pk>>(
268268
&self,
269269
stfr: &Sat,
270270
) -> Result<(Vec<Vec<u8>>, ScriptBuf), Error> {
@@ -302,7 +302,9 @@ impl Plan {
302302
}
303303
})
304304
}
305+
}
305306

307+
impl Plan<DefiniteDescriptorKey> {
306308
/// Update a PSBT input with the metadata required to complete this plan
307309
///
308310
/// This will only add the metadata for items required to complete this plan. For example, if

0 commit comments

Comments
 (0)