@@ -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}
0 commit comments