Skip to content

Commit

Permalink
fixup! Add plan capabilities to miniscript
Browse files Browse the repository at this point in the history
  • Loading branch information
afilini committed Nov 16, 2022
1 parent c68de8c commit 632a365
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
27 changes: 19 additions & 8 deletions src/descriptor/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,27 +486,38 @@ impl DescriptorPublicKey {
.expect("The key should not contain any wildcards at this point")
}

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

match (self, &definite_key.0) {
(DescriptorPublicKey::XPub(self_xkey), DescriptorPublicKey::XPub(definite_xkey))
if definite_xkey.derivation_path.len() > 0 =>
{
self_xkey.origin == definite_xkey.origin
let definite_path_len = definite_xkey.derivation_path.len();
if self_xkey.origin == definite_xkey.origin
&& self_xkey.xkey == definite_xkey.xkey
&& self_xkey.derivation_path.as_ref()
== &definite_xkey.derivation_path
[..(definite_xkey.derivation_path.len() - 1)]
== &definite_xkey.derivation_path[..(definite_path_len - 1)]
{
Some(vec![definite_xkey.derivation_path[definite_path_len - 1]].into())
} else {
None
}
}
_ => false,
_ => None,
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,13 @@ pub struct Assets {

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

pub(crate) fn has_ecdsa_sig(&self, pk: &DefiniteDescriptorKey) -> bool {
self.ecdsa_signatures.keys().any(|k| k.matches(pk))
self.ecdsa_signatures
.keys()
.any(|k| k.is_parent(pk).is_some())
}

pub(crate) fn has_schnorr_sig(
Expand All @@ -325,7 +327,7 @@ impl Assets {
) -> bool {
self.schnorr_signatures
.keys()
.any(|(k, lh)| tap_leaf_hash == lh && k.matches(pk))
.any(|(k, lh)| tap_leaf_hash == lh && k.is_parent(pk).is_some())
}
}

Expand Down

0 comments on commit 632a365

Please sign in to comment.