@@ -650,6 +650,28 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk> {
650650 }
651651}
652652
653+ /// Translates [`DescriptorPublicKey`]s into [`DefiniteDescriptorKey`]s.
654+ enum Definitor {
655+ /// Convert a non-wildcard key directly via [`DefiniteDescriptorKey::new`].
656+ FromPk ,
657+ /// Replace the wildcard with a specific derivation index.
658+ AtIndex ( u32 ) ,
659+ }
660+
661+ impl Translator < DescriptorPublicKey > for Definitor {
662+ type TargetPk = DefiniteDescriptorKey ;
663+ type Error = NonDefiniteKeyError ;
664+
665+ fn pk ( & mut self , pk : & DescriptorPublicKey ) -> Result < Self :: TargetPk , Self :: Error > {
666+ match self {
667+ Definitor :: FromPk => DefiniteDescriptorKey :: new ( pk. clone ( ) ) ,
668+ Definitor :: AtIndex ( idx) => pk. clone ( ) . at_derivation_index ( * idx) ,
669+ }
670+ }
671+
672+ translate_hash_clone ! ( DescriptorPublicKey ) ;
673+ }
674+
653675impl Descriptor < DescriptorPublicKey > {
654676 /// Whether or not the descriptor has any wildcards i.e. `/*`.
655677 pub fn has_wildcard ( & self ) -> bool { self . for_any_key ( |key| key. has_wildcard ( ) ) }
@@ -670,19 +692,7 @@ impl Descriptor<DescriptorPublicKey> {
670692 if self . has_wildcard ( ) {
671693 return Err ( NonDefiniteKeyError :: Wildcard ) ;
672694 }
673- struct Definitor ;
674-
675- impl Translator < DescriptorPublicKey > for Definitor {
676- type TargetPk = DefiniteDescriptorKey ;
677- type Error = NonDefiniteKeyError ;
678-
679- fn pk ( & mut self , pk : & DescriptorPublicKey ) -> Result < Self :: TargetPk , Self :: Error > {
680- DefiniteDescriptorKey :: new ( pk. clone ( ) )
681- }
682-
683- translate_hash_clone ! ( DescriptorPublicKey ) ;
684- }
685- self . translate_pk ( & mut Definitor )
695+ self . translate_pk ( & mut Definitor :: FromPk )
686696 . map_err ( |e| e. expect_translator_err ( "No Context errors while translating" ) )
687697 }
688698
@@ -700,19 +710,7 @@ impl Descriptor<DescriptorPublicKey> {
700710 if !self . has_wildcard ( ) {
701711 return Err ( NonDefiniteKeyError :: NoWildcard ) ;
702712 }
703- struct Derivator ( u32 ) ;
704-
705- impl Translator < DescriptorPublicKey > for Derivator {
706- type TargetPk = DefiniteDescriptorKey ;
707- type Error = NonDefiniteKeyError ;
708-
709- fn pk ( & mut self , pk : & DescriptorPublicKey ) -> Result < Self :: TargetPk , Self :: Error > {
710- pk. clone ( ) . at_derivation_index ( self . 0 )
711- }
712-
713- translate_hash_clone ! ( DescriptorPublicKey ) ;
714- }
715- self . translate_pk ( & mut Derivator ( index) )
713+ self . translate_pk ( & mut Definitor :: AtIndex ( index) )
716714 . map_err ( |e| e. expect_translator_err ( "No Context errors while translating" ) )
717715 }
718716
0 commit comments