@@ -486,27 +486,38 @@ impl DescriptorPublicKey {
486
486
. expect ( "The key should not contain any wildcards at this point" )
487
487
}
488
488
489
- /// Whether this key matches a [`DefiniteDescriptorKey`]
489
+ /// Whether this key is the "parent" of a [`DefiniteDescriptorKey`]
490
490
///
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 > {
493
499
// If the key is `Single` or it's an `XPub` with no wildcard it will match the definite key
494
500
// exactly, so we try this check first
495
501
if self == & definite_key. 0 {
496
- return true ;
502
+ return Some ( bip32 :: DerivationPath :: default ( ) ) ;
497
503
}
498
504
499
505
match ( self , & definite_key. 0 ) {
500
506
( DescriptorPublicKey :: XPub ( self_xkey) , DescriptorPublicKey :: XPub ( definite_xkey) )
501
507
if definite_xkey. derivation_path . len ( ) > 0 =>
502
508
{
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
504
511
&& self_xkey. xkey == definite_xkey. xkey
505
512
&& 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
+ }
508
519
}
509
- _ => false ,
520
+ _ => None ,
510
521
}
511
522
}
512
523
}
0 commit comments