@@ -18,6 +18,7 @@ use crate::prelude::*;
18
18
use crate :: serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
19
19
use crate :: { hash256, MiniscriptKey , ToPublicKey } ;
20
20
21
+ type DescriptorSinglePublicKey = SinglePub ;
21
22
type DescriptorExtendedPublicKey = DescriptorXKey < bip32:: ExtendedPubKey > ;
22
23
23
24
/// The descriptor pubkey, either a single pubkey or an xpub.
@@ -285,6 +286,16 @@ impl error::Error for DescriptorKeyParseError {
285
286
}
286
287
}
287
288
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
+
288
299
impl fmt:: Display for DescriptorExtendedPublicKey {
289
300
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
290
301
maybe_fmt_master_id ( f, & self . origin ) ?;
@@ -302,14 +313,7 @@ impl fmt::Display for DescriptorExtendedPublicKey {
302
313
impl fmt:: Display for DescriptorPublicKey {
303
314
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
304
315
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) ,
313
317
DescriptorPublicKey :: XPub ( ref xpub) => xpub. fmt ( f) ,
314
318
DescriptorPublicKey :: MultiXPub ( ref xpub) => {
315
319
maybe_fmt_master_id ( f, & xpub. origin ) ?;
@@ -565,6 +569,50 @@ pub trait DescriptorInnerKey {
565
569
fn is_multipath ( & self ) -> bool ;
566
570
}
567
571
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
+
568
616
impl DescriptorInnerKey for DescriptorExtendedPublicKey {
569
617
/// The fingerprint of the master key associated with this key, `0x00000000` if none.
570
618
fn master_fingerprint ( & self ) -> bip32:: Fingerprint {
@@ -651,24 +699,7 @@ impl DescriptorPublicKey {
651
699
xpub. xkey . fingerprint ( )
652
700
}
653
701
}
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 ( ) ,
672
703
}
673
704
}
674
705
@@ -682,13 +713,7 @@ impl DescriptorPublicKey {
682
713
pub fn full_derivation_path ( & self ) -> Option < bip32:: DerivationPath > {
683
714
match * self {
684
715
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 ( ) ,
692
717
DescriptorPublicKey :: MultiXPub ( _) => None ,
693
718
}
694
719
}
@@ -702,7 +727,7 @@ impl DescriptorPublicKey {
702
727
/// Whether or not the key has a wildcard
703
728
pub fn has_wildcard ( & self ) -> bool {
704
729
match * self {
705
- DescriptorPublicKey :: Single ( .. ) => false ,
730
+ DescriptorPublicKey :: Single ( ref single ) => single . has_wildcard ( ) ,
706
731
DescriptorPublicKey :: XPub ( ref xpub) => xpub. has_wildcard ( ) ,
707
732
DescriptorPublicKey :: MultiXPub ( ref xpub) => xpub. wildcard != Wildcard :: None ,
708
733
}
@@ -728,8 +753,7 @@ impl DescriptorPublicKey {
728
753
/// - If `index` is hardened.
729
754
pub fn at_derivation_index ( self , index : u32 ) -> Result < DefiniteDescriptorKey , ConversionError > {
730
755
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) ,
733
757
DescriptorPublicKey :: XPub ( xpub) => xpub. at_derivation_index ( index) ,
734
758
DescriptorPublicKey :: MultiXPub ( _) => Err ( ConversionError :: MultiKey ) ,
735
759
}
@@ -738,7 +762,7 @@ impl DescriptorPublicKey {
738
762
/// Whether or not this key has multiple derivation paths.
739
763
pub fn is_multipath ( & self ) -> bool {
740
764
match self {
741
- DescriptorPublicKey :: Single ( .. ) => false ,
765
+ DescriptorPublicKey :: Single ( single ) => single . is_multipath ( ) ,
742
766
DescriptorPublicKey :: XPub ( xpub) => xpub. is_multipath ( ) ,
743
767
DescriptorPublicKey :: MultiXPub ( _) => true ,
744
768
}
0 commit comments