@@ -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,17 @@ 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
+ Ok ( ( ) )
297
+ }
298
+ }
299
+
288
300
impl fmt:: Display for DescriptorExtendedPublicKey {
289
301
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
290
302
maybe_fmt_master_id ( f, & self . origin ) ?;
@@ -302,14 +314,7 @@ impl fmt::Display for DescriptorExtendedPublicKey {
302
314
impl fmt:: Display for DescriptorPublicKey {
303
315
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
304
316
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
- }
317
+ DescriptorPublicKey :: Single ( ref pk) => pk. fmt ( f) ,
313
318
DescriptorPublicKey :: XPub ( ref xpub) => xpub. fmt ( f) ,
314
319
DescriptorPublicKey :: MultiXPub ( ref xpub) => {
315
320
maybe_fmt_master_id ( f, & xpub. origin ) ?;
@@ -565,6 +570,48 @@ pub trait DescriptorInnerKey {
565
570
fn is_multipath ( & self ) -> bool ;
566
571
}
567
572
573
+ impl DescriptorInnerKey for DescriptorSinglePublicKey {
574
+ fn master_fingerprint ( & self ) -> bip32:: Fingerprint {
575
+ if let Some ( ( fingerprint, _) ) = self . origin {
576
+ fingerprint
577
+ } else {
578
+ let mut engine = XpubIdentifier :: engine ( ) ;
579
+ match self . key {
580
+ SinglePubKey :: FullKey ( pk) => {
581
+ pk. write_into ( & mut engine) . expect ( "engines don't error" )
582
+ }
583
+ SinglePubKey :: XOnly ( x_only_pk) => engine. input ( & x_only_pk. serialize ( ) ) ,
584
+ } ;
585
+ bip32:: Fingerprint :: from (
586
+ & XpubIdentifier :: from_engine ( engine) [ ..4 ]
587
+ . try_into ( )
588
+ . expect ( "4 byte slice" ) ,
589
+ )
590
+ }
591
+ }
592
+
593
+ fn full_derivation_path ( & self ) -> Option < bip32:: DerivationPath > {
594
+ Some ( if let Some ( ( _, ref path) ) = self . origin {
595
+ path. clone ( )
596
+ } else {
597
+ bip32:: DerivationPath :: from ( vec ! [ ] )
598
+ } )
599
+ }
600
+
601
+ fn has_wildcard ( & self ) -> bool {
602
+ false
603
+ }
604
+
605
+ fn at_derivation_index ( self , _index : u32 ) -> Result < DefiniteDescriptorKey , ConversionError > {
606
+ Ok ( DefiniteDescriptorKey :: new ( DescriptorPublicKey :: Single ( self ) )
607
+ . expect ( "The key should not contain any wildcards at this point" ) )
608
+ }
609
+
610
+ fn is_multipath ( & self ) -> bool {
611
+ false
612
+ }
613
+ }
614
+
568
615
impl DescriptorInnerKey for DescriptorExtendedPublicKey {
569
616
fn master_fingerprint ( & self ) -> bip32:: Fingerprint {
570
617
if let Some ( ( fingerprint, _) ) = self . origin {
@@ -629,24 +676,7 @@ impl DescriptorPublicKey {
629
676
xpub. xkey . fingerprint ( )
630
677
}
631
678
}
632
- DescriptorPublicKey :: Single ( ref single) => {
633
- if let Some ( ( fingerprint, _) ) = single. origin {
634
- fingerprint
635
- } else {
636
- let mut engine = XpubIdentifier :: engine ( ) ;
637
- match single. key {
638
- SinglePubKey :: FullKey ( pk) => {
639
- pk. write_into ( & mut engine) . expect ( "engines don't error" )
640
- }
641
- SinglePubKey :: XOnly ( x_only_pk) => engine. input ( & x_only_pk. serialize ( ) ) ,
642
- } ;
643
- bip32:: Fingerprint :: from (
644
- & XpubIdentifier :: from_engine ( engine) [ ..4 ]
645
- . try_into ( )
646
- . expect ( "4 byte slice" ) ,
647
- )
648
- }
649
- }
679
+ DescriptorPublicKey :: Single ( ref single) => single. master_fingerprint ( ) ,
650
680
}
651
681
}
652
682
@@ -660,13 +690,7 @@ impl DescriptorPublicKey {
660
690
pub fn full_derivation_path ( & self ) -> Option < bip32:: DerivationPath > {
661
691
match * self {
662
692
DescriptorPublicKey :: XPub ( ref xpub) => xpub. full_derivation_path ( ) ,
663
- DescriptorPublicKey :: Single ( ref single) => {
664
- Some ( if let Some ( ( _, ref path) ) = single. origin {
665
- path. clone ( )
666
- } else {
667
- bip32:: DerivationPath :: from ( vec ! [ ] )
668
- } )
669
- }
693
+ DescriptorPublicKey :: Single ( ref single) => single. full_derivation_path ( ) ,
670
694
DescriptorPublicKey :: MultiXPub ( _) => None ,
671
695
}
672
696
}
@@ -680,7 +704,7 @@ impl DescriptorPublicKey {
680
704
/// Whether or not the key has a wildcard
681
705
pub fn has_wildcard ( & self ) -> bool {
682
706
match * self {
683
- DescriptorPublicKey :: Single ( .. ) => false ,
707
+ DescriptorPublicKey :: Single ( ref single ) => single . has_wildcard ( ) ,
684
708
DescriptorPublicKey :: XPub ( ref xpub) => xpub. has_wildcard ( ) ,
685
709
DescriptorPublicKey :: MultiXPub ( ref xpub) => xpub. wildcard != Wildcard :: None ,
686
710
}
@@ -706,8 +730,7 @@ impl DescriptorPublicKey {
706
730
/// - If `index` is hardened.
707
731
pub fn at_derivation_index ( self , index : u32 ) -> Result < DefiniteDescriptorKey , ConversionError > {
708
732
match self {
709
- DescriptorPublicKey :: Single ( _) => Ok ( DefiniteDescriptorKey :: new ( self )
710
- . expect ( "The key should not contain any wildcards at this point" ) ) ,
733
+ DescriptorPublicKey :: Single ( single) => single. at_derivation_index ( index) ,
711
734
DescriptorPublicKey :: XPub ( xpub) => xpub. at_derivation_index ( index) ,
712
735
DescriptorPublicKey :: MultiXPub ( _) => Err ( ConversionError :: MultiKey ) ,
713
736
}
@@ -716,8 +739,7 @@ impl DescriptorPublicKey {
716
739
/// Whether or not this key has multiple derivation paths.
717
740
pub fn is_multipath ( & self ) -> bool {
718
741
match * self {
719
- DescriptorPublicKey :: Single ( ..) => false ,
720
- DescriptorPublicKey :: XPub ( ..) => self . is_multipath ( ) ,
742
+ DescriptorPublicKey :: Single ( ..) | DescriptorPublicKey :: XPub ( ..) => self . is_multipath ( ) ,
721
743
DescriptorPublicKey :: MultiXPub ( _) => true ,
722
744
}
723
745
}
0 commit comments