@@ -18,6 +18,7 @@ use crate::prelude::*;
1818use crate :: serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
1919use crate :: { hash256, MiniscriptKey , ToPublicKey } ;
2020
21+ type DescriptorSinglePublicKey = SinglePub ;
2122type DescriptorExtendedPublicKey = DescriptorXKey < bip32:: ExtendedPubKey > ;
2223
2324/// The descriptor pubkey, either a single pubkey or an xpub.
@@ -285,6 +286,16 @@ impl error::Error for DescriptorKeyParseError {
285286 }
286287}
287288
289+ impl fmt:: Display for DescriptorSinglePublicKey {
290+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
291+ maybe_fmt_master_id ( f, & self . origin ) ?;
292+ match self . key {
293+ SinglePubKey :: FullKey ( full_key) => full_key. fmt ( f) ,
294+ SinglePubKey :: XOnly ( x_only_key) => x_only_key. fmt ( f) ,
295+ }
296+ }
297+ }
298+
288299impl fmt:: Display for DescriptorExtendedPublicKey {
289300 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
290301 maybe_fmt_master_id ( f, & self . origin ) ?;
@@ -302,14 +313,7 @@ impl fmt::Display for DescriptorExtendedPublicKey {
302313impl fmt:: Display for DescriptorPublicKey {
303314 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
304315 match * self {
305- DescriptorPublicKey :: Single ( ref pk) => {
306- maybe_fmt_master_id ( f, & pk. origin ) ?;
307- match pk. key {
308- SinglePubKey :: FullKey ( full_key) => full_key. fmt ( f) ,
309- SinglePubKey :: XOnly ( x_only_key) => x_only_key. fmt ( f) ,
310- } ?;
311- Ok ( ( ) )
312- }
316+ DescriptorPublicKey :: Single ( ref pk) => pk. fmt ( f) ,
313317 DescriptorPublicKey :: XPub ( ref xpub) => xpub. fmt ( f) ,
314318 DescriptorPublicKey :: MultiXPub ( ref xpub) => {
315319 maybe_fmt_master_id ( f, & xpub. origin ) ?;
@@ -565,6 +569,50 @@ pub trait DescriptorInnerKey {
565569 fn is_multipath ( & self ) -> bool ;
566570}
567571
572+ impl DescriptorInnerKey for DescriptorSinglePublicKey {
573+ fn master_fingerprint ( & self ) -> bip32:: Fingerprint {
574+ if let Some ( ( fingerprint, _) ) = self . origin {
575+ fingerprint
576+ } else {
577+ let mut engine = XpubIdentifier :: engine ( ) ;
578+ match self . key {
579+ SinglePubKey :: FullKey ( pk) => {
580+ pk. write_into ( & mut engine) . expect ( "engines don't error" )
581+ }
582+ SinglePubKey :: XOnly ( x_only_pk) => engine. input ( & x_only_pk. serialize ( ) ) ,
583+ } ;
584+ bip32:: Fingerprint :: from (
585+ & XpubIdentifier :: from_engine ( engine) [ ..4 ]
586+ . try_into ( )
587+ . expect ( "4 byte slice" ) ,
588+ )
589+ }
590+ }
591+
592+ fn full_derivation_path ( & self ) -> Option < bip32:: DerivationPath > {
593+ Some ( if let Some ( ( _, ref path) ) = self . origin {
594+ path. clone ( )
595+ } else {
596+ bip32:: DerivationPath :: from ( vec ! [ ] )
597+ } )
598+ }
599+
600+ fn has_wildcard ( & self ) -> bool {
601+ false
602+ }
603+
604+ fn at_derivation_index ( self , _index : u32 ) -> Result < DefiniteDescriptorKey , ConversionError > {
605+ Ok (
606+ DefiniteDescriptorKey :: new ( DescriptorPublicKey :: Single ( self ) )
607+ . expect ( "The key should not contain any wildcards at this point" ) ,
608+ )
609+ }
610+
611+ fn is_multipath ( & self ) -> bool {
612+ false
613+ }
614+ }
615+
568616impl DescriptorInnerKey for DescriptorExtendedPublicKey {
569617 /// The fingerprint of the master key associated with this key, `0x00000000` if none.
570618 fn master_fingerprint ( & self ) -> bip32:: Fingerprint {
@@ -651,24 +699,7 @@ impl DescriptorPublicKey {
651699 xpub. xkey . fingerprint ( )
652700 }
653701 }
654- DescriptorPublicKey :: Single ( ref single) => {
655- if let Some ( ( fingerprint, _) ) = single. origin {
656- fingerprint
657- } else {
658- let mut engine = XpubIdentifier :: engine ( ) ;
659- match single. key {
660- SinglePubKey :: FullKey ( pk) => {
661- pk. write_into ( & mut engine) . expect ( "engines don't error" )
662- }
663- SinglePubKey :: XOnly ( x_only_pk) => engine. input ( & x_only_pk. serialize ( ) ) ,
664- } ;
665- bip32:: Fingerprint :: from (
666- & XpubIdentifier :: from_engine ( engine) [ ..4 ]
667- . try_into ( )
668- . expect ( "4 byte slice" ) ,
669- )
670- }
671- }
702+ DescriptorPublicKey :: Single ( ref single) => single. master_fingerprint ( ) ,
672703 }
673704 }
674705
@@ -682,13 +713,7 @@ impl DescriptorPublicKey {
682713 pub fn full_derivation_path ( & self ) -> Option < bip32:: DerivationPath > {
683714 match * self {
684715 DescriptorPublicKey :: XPub ( ref xpub) => xpub. full_derivation_path ( ) ,
685- DescriptorPublicKey :: Single ( ref single) => {
686- Some ( if let Some ( ( _, ref path) ) = single. origin {
687- path. clone ( )
688- } else {
689- bip32:: DerivationPath :: from ( vec ! [ ] )
690- } )
691- }
716+ DescriptorPublicKey :: Single ( ref single) => single. full_derivation_path ( ) ,
692717 DescriptorPublicKey :: MultiXPub ( _) => None ,
693718 }
694719 }
@@ -702,7 +727,7 @@ impl DescriptorPublicKey {
702727 /// Whether or not the key has a wildcard
703728 pub fn has_wildcard ( & self ) -> bool {
704729 match * self {
705- DescriptorPublicKey :: Single ( .. ) => false ,
730+ DescriptorPublicKey :: Single ( ref single ) => single . has_wildcard ( ) ,
706731 DescriptorPublicKey :: XPub ( ref xpub) => xpub. has_wildcard ( ) ,
707732 DescriptorPublicKey :: MultiXPub ( ref xpub) => xpub. wildcard != Wildcard :: None ,
708733 }
@@ -728,8 +753,7 @@ impl DescriptorPublicKey {
728753 /// - If `index` is hardened.
729754 pub fn at_derivation_index ( self , index : u32 ) -> Result < DefiniteDescriptorKey , ConversionError > {
730755 match self {
731- DescriptorPublicKey :: Single ( _) => Ok ( DefiniteDescriptorKey :: new ( self )
732- . expect ( "The key should not contain any wildcards at this point" ) ) ,
756+ DescriptorPublicKey :: Single ( single) => single. at_derivation_index ( index) ,
733757 DescriptorPublicKey :: XPub ( xpub) => xpub. at_derivation_index ( index) ,
734758 DescriptorPublicKey :: MultiXPub ( _) => Err ( ConversionError :: MultiKey ) ,
735759 }
@@ -738,7 +762,7 @@ impl DescriptorPublicKey {
738762 /// Whether or not this key has multiple derivation paths.
739763 pub fn is_multipath ( & self ) -> bool {
740764 match self {
741- DescriptorPublicKey :: Single ( .. ) => false ,
765+ DescriptorPublicKey :: Single ( single ) => single . is_multipath ( ) ,
742766 DescriptorPublicKey :: XPub ( xpub) => xpub. is_multipath ( ) ,
743767 DescriptorPublicKey :: MultiXPub ( _) => true ,
744768 }
0 commit comments