Skip to content

Commit 632a365

Browse files
committed
fixup! Add plan capabilities to miniscript
1 parent c68de8c commit 632a365

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/descriptor/key.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -486,27 +486,38 @@ impl DescriptorPublicKey {
486486
.expect("The key should not contain any wildcards at this point")
487487
}
488488

489-
/// Whether this key matches a [`DefiniteDescriptorKey`]
489+
/// Whether this key is the "parent" of a [`DefiniteDescriptorKey`]
490490
///
491-
/// Returns `true` if this key is the un-derived version of the definite key passed in.
492-
pub fn matches(&self, definite_key: &DefiniteDescriptorKey) -> bool {
491+
/// The key is considered "parent" if it represents the non-derived version of a definite key,
492+
/// meaning it contains a wildcard where the definite key has a definite derivation number.
493+
///
494+
/// If `self` is a single key or doesn't contain any wildcards, the definite key will have to
495+
/// be exactly the same.
496+
///
497+
/// Returns the derivation path to apply to `self` to obtain the definite key.
498+
pub fn is_parent(&self, definite_key: &DefiniteDescriptorKey) -> Option<bip32::DerivationPath> {
493499
// If the key is `Single` or it's an `XPub` with no wildcard it will match the definite key
494500
// exactly, so we try this check first
495501
if self == &definite_key.0 {
496-
return true;
502+
return Some(bip32::DerivationPath::default());
497503
}
498504

499505
match (self, &definite_key.0) {
500506
(DescriptorPublicKey::XPub(self_xkey), DescriptorPublicKey::XPub(definite_xkey))
501507
if definite_xkey.derivation_path.len() > 0 =>
502508
{
503-
self_xkey.origin == definite_xkey.origin
509+
let definite_path_len = definite_xkey.derivation_path.len();
510+
if self_xkey.origin == definite_xkey.origin
504511
&& self_xkey.xkey == definite_xkey.xkey
505512
&& self_xkey.derivation_path.as_ref()
506-
== &definite_xkey.derivation_path
507-
[..(definite_xkey.derivation_path.len() - 1)]
513+
== &definite_xkey.derivation_path[..(definite_path_len - 1)]
514+
{
515+
Some(vec![definite_xkey.derivation_path[definite_path_len - 1]].into())
516+
} else {
517+
None
518+
}
508519
}
509-
_ => false,
520+
_ => None,
510521
}
511522
}
512523
}

src/plan.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,13 @@ pub struct Assets {
311311

312312
impl Assets {
313313
pub(crate) fn has_key(&self, pk: &DefiniteDescriptorKey) -> bool {
314-
self.keys.values().any(|k| k.matches(pk))
314+
self.keys.values().any(|k| k.is_parent(pk).is_some())
315315
}
316316

317317
pub(crate) fn has_ecdsa_sig(&self, pk: &DefiniteDescriptorKey) -> bool {
318-
self.ecdsa_signatures.keys().any(|k| k.matches(pk))
318+
self.ecdsa_signatures
319+
.keys()
320+
.any(|k| k.is_parent(pk).is_some())
319321
}
320322

321323
pub(crate) fn has_schnorr_sig(
@@ -325,7 +327,7 @@ impl Assets {
325327
) -> bool {
326328
self.schnorr_signatures
327329
.keys()
328-
.any(|(k, lh)| tap_leaf_hash == lh && k.matches(pk))
330+
.any(|(k, lh)| tap_leaf_hash == lh && k.is_parent(pk).is_some())
329331
}
330332
}
331333

0 commit comments

Comments
 (0)