@@ -28,6 +28,7 @@ use core::ops::Range;
28
28
use core:: str:: { self , FromStr } ;
29
29
30
30
use bitcoin:: blockdata:: witness:: Witness ;
31
+ use bitcoin:: hashes:: sha256;
31
32
use bitcoin:: util:: address:: WitnessVersion ;
32
33
use bitcoin:: { self , secp256k1, Address , Network , Script , TxIn } ;
33
34
use sync:: Arc ;
@@ -36,8 +37,8 @@ use self::checksum::verify_checksum;
36
37
use crate :: miniscript:: { Legacy , Miniscript , Segwitv0 } ;
37
38
use crate :: prelude:: * ;
38
39
use crate :: {
39
- expression, miniscript, BareCtx , Error , ForEach , ForEachKey , MiniscriptKey , Satisfier ,
40
- ToPublicKey , TranslatePk , TranslatePk2 ,
40
+ expression, miniscript, BareCtx , Error , ForEach , ForEachKey , MiniscriptKey , PkTranslator ,
41
+ Satisfier , ToPublicKey , TranslatePk , Translator ,
41
42
} ;
42
43
43
44
mod bare;
@@ -475,25 +476,19 @@ where
475
476
Q : MiniscriptKey ,
476
477
{
477
478
type Output = Descriptor < Q > ;
479
+
478
480
/// Converts a descriptor using abstract keys to one using specific keys.
479
- ///
480
- /// # Panics
481
- ///
482
- /// If `fpk` returns an uncompressed key when converting to a Segwit descriptor.
483
- /// To prevent this panic, ensure `fpk` returns an error in this case instead.
484
- fn translate_pk < Fpk , Fpkh , E > ( & self , mut fpk : Fpk , mut fpkh : Fpkh ) -> Result < Descriptor < Q > , E >
481
+ fn translate_pk < T , E > ( & self , t : & mut T ) -> Result < Self :: Output , E >
485
482
where
486
- Fpk : FnMut ( & P ) -> Result < Q , E > ,
487
- Fpkh : FnMut ( & P :: Hash ) -> Result < Q :: Hash , E > ,
488
- Q : MiniscriptKey ,
483
+ T : Translator < P , Q , E > ,
489
484
{
490
485
let desc = match * self {
491
- Descriptor :: Bare ( ref bare) => Descriptor :: Bare ( bare. translate_pk ( & mut fpk , & mut fpkh ) ?) ,
492
- Descriptor :: Pkh ( ref pk) => Descriptor :: Pkh ( pk. translate_pk ( & mut fpk , & mut fpkh ) ?) ,
493
- Descriptor :: Wpkh ( ref pk) => Descriptor :: Wpkh ( pk. translate_pk ( & mut fpk , & mut fpkh ) ?) ,
494
- Descriptor :: Sh ( ref sh) => Descriptor :: Sh ( sh. translate_pk ( & mut fpk , & mut fpkh ) ?) ,
495
- Descriptor :: Wsh ( ref wsh) => Descriptor :: Wsh ( wsh. translate_pk ( & mut fpk , & mut fpkh ) ?) ,
496
- Descriptor :: Tr ( ref tr) => Descriptor :: Tr ( tr. translate_pk ( & mut fpk , & mut fpkh ) ?) ,
486
+ Descriptor :: Bare ( ref bare) => Descriptor :: Bare ( bare. translate_pk ( t ) ?) ,
487
+ Descriptor :: Pkh ( ref pk) => Descriptor :: Pkh ( pk. translate_pk ( t ) ?) ,
488
+ Descriptor :: Wpkh ( ref pk) => Descriptor :: Wpkh ( pk. translate_pk ( t ) ?) ,
489
+ Descriptor :: Sh ( ref sh) => Descriptor :: Sh ( sh. translate_pk ( t ) ?) ,
490
+ Descriptor :: Wsh ( ref wsh) => Descriptor :: Wsh ( wsh. translate_pk ( t ) ?) ,
491
+ Descriptor :: Tr ( ref tr) => Descriptor :: Tr ( tr. translate_pk ( t ) ?) ,
497
492
} ;
498
493
Ok ( desc)
499
494
}
@@ -529,7 +524,19 @@ impl Descriptor<DescriptorPublicKey> {
529
524
/// In most cases, you would want to use [`Self::derived_descriptor`] directly to obtain
530
525
/// a [`Descriptor<bitcoin::PublicKey>`]
531
526
pub fn derive ( & self , index : u32 ) -> Descriptor < DerivedDescriptorKey > {
532
- self . translate_pk2_infallible ( |pk| pk. clone ( ) . derive ( index) )
527
+ struct Derivator ( u32 ) ;
528
+
529
+ impl PkTranslator < DescriptorPublicKey , DerivedDescriptorKey , ( ) > for Derivator {
530
+ fn pk ( & mut self , pk : & DescriptorPublicKey ) -> Result < DerivedDescriptorKey , ( ) > {
531
+ Ok ( pk. clone ( ) . derive ( self . 0 ) )
532
+ }
533
+
534
+ fn pkh ( & mut self , pkh : & DescriptorPublicKey ) -> Result < DerivedDescriptorKey , ( ) > {
535
+ Ok ( pkh. clone ( ) . derive ( self . 0 ) )
536
+ }
537
+ }
538
+ self . translate_pk ( & mut Derivator ( index) )
539
+ . expect ( "BIP 32 key index substitution cannot fail" )
533
540
}
534
541
535
542
/// Derive a [`Descriptor`] with a concrete [`bitcoin::PublicKey`] at a given index
@@ -561,9 +568,28 @@ impl Descriptor<DescriptorPublicKey> {
561
568
secp : & secp256k1:: Secp256k1 < C > ,
562
569
index : u32 ,
563
570
) -> Result < Descriptor < bitcoin:: PublicKey > , ConversionError > {
564
- let derived = self
565
- . derive ( index)
566
- . translate_pk2 ( |xpk| xpk. derive_public_key ( secp) ) ?;
571
+ struct Derivator < ' a , C : secp256k1:: Verification > ( & ' a secp256k1:: Secp256k1 < C > ) ;
572
+
573
+ impl < ' a , C : secp256k1:: Verification >
574
+ PkTranslator < DerivedDescriptorKey , bitcoin:: PublicKey , ConversionError >
575
+ for Derivator < ' a , C >
576
+ {
577
+ fn pk (
578
+ & mut self ,
579
+ pk : & DerivedDescriptorKey ,
580
+ ) -> Result < bitcoin:: PublicKey , ConversionError > {
581
+ pk. derive_public_key ( & self . 0 )
582
+ }
583
+
584
+ fn pkh (
585
+ & mut self ,
586
+ pkh : & DerivedDescriptorKey ,
587
+ ) -> Result < bitcoin:: hashes:: hash160:: Hash , ConversionError > {
588
+ Ok ( pkh. derive_public_key ( & self . 0 ) ?. to_pubkeyhash ( ) )
589
+ }
590
+ }
591
+
592
+ let derived = self . derive ( index) . translate_pk ( & mut Derivator ( secp) ) ?;
567
593
Ok ( derived)
568
594
}
569
595
@@ -575,39 +601,79 @@ impl Descriptor<DescriptorPublicKey> {
575
601
secp : & secp256k1:: Secp256k1 < C > ,
576
602
s : & str ,
577
603
) -> Result < ( Descriptor < DescriptorPublicKey > , KeyMap ) , Error > {
578
- let parse_key = |s : & String ,
579
- key_map : & mut KeyMap |
580
- -> Result < DescriptorPublicKey , DescriptorKeyParseError > {
604
+ fn parse_key < C : secp256k1:: Signing > (
605
+ s : & String ,
606
+ key_map : & mut KeyMap ,
607
+ secp : & secp256k1:: Secp256k1 < C > ,
608
+ ) -> Result < DescriptorPublicKey , Error > {
581
609
let ( public_key, secret_key) = match DescriptorSecretKey :: from_str ( s) {
582
- Ok ( sk) => ( sk. to_public ( secp) ?, Some ( sk) ) ,
583
- Err ( _) => ( DescriptorPublicKey :: from_str ( s) ?, None ) ,
610
+ Ok ( sk) => (
611
+ sk. to_public ( secp)
612
+ . map_err ( |e| Error :: Unexpected ( e. to_string ( ) ) ) ?,
613
+ Some ( sk) ,
614
+ ) ,
615
+ Err ( _) => (
616
+ DescriptorPublicKey :: from_str ( s)
617
+ . map_err ( |e| Error :: Unexpected ( e. to_string ( ) ) ) ?,
618
+ None ,
619
+ ) ,
584
620
} ;
585
621
586
622
if let Some ( secret_key) = secret_key {
587
623
key_map. insert ( public_key. clone ( ) , secret_key) ;
588
624
}
589
625
590
626
Ok ( public_key)
591
- } ;
627
+ }
628
+
629
+ let mut keymap_pk = KeyMapWrapper ( HashMap :: new ( ) , secp) ;
592
630
593
- let mut keymap_pk = KeyMap :: new ( ) ;
594
- let mut keymap_pkh = KeyMap :: new ( ) ;
631
+ struct KeyMapWrapper < ' a , C : secp256k1:: Signing > ( KeyMap , & ' a secp256k1:: Secp256k1 < C > ) ;
632
+
633
+ impl < ' a , C : secp256k1:: Signing > Translator < String , DescriptorPublicKey , Error >
634
+ for KeyMapWrapper < ' a , C >
635
+ {
636
+ fn pk ( & mut self , pk : & String ) -> Result < DescriptorPublicKey , Error > {
637
+ parse_key ( pk, & mut self . 0 , self . 1 )
638
+ }
639
+
640
+ fn pkh ( & mut self , pkh : & String ) -> Result < DescriptorPublicKey , Error > {
641
+ parse_key ( pkh, & mut self . 0 , self . 1 )
642
+ }
643
+
644
+ fn sha256 ( & mut self , sha256 : & String ) -> Result < sha256:: Hash , Error > {
645
+ let hash =
646
+ sha256:: Hash :: from_str ( sha256) . map_err ( |e| Error :: Unexpected ( e. to_string ( ) ) ) ?;
647
+ Ok ( hash)
648
+ }
649
+ }
595
650
596
651
let descriptor = Descriptor :: < String > :: from_str ( s) ?;
597
652
let descriptor = descriptor
598
- . translate_pk (
599
- |pk| parse_key ( pk, & mut keymap_pk) ,
600
- |pkh| parse_key ( pkh, & mut keymap_pkh) ,
601
- )
653
+ . translate_pk ( & mut keymap_pk)
602
654
. map_err ( |e| Error :: Unexpected ( e. to_string ( ) ) ) ?;
603
655
604
- keymap_pk. extend ( keymap_pkh. into_iter ( ) ) ;
605
-
606
- Ok ( ( descriptor, keymap_pk) )
656
+ Ok ( ( descriptor, keymap_pk. 0 ) )
607
657
}
608
658
609
659
/// Serialize a descriptor to string with its secret keys
610
660
pub fn to_string_with_secret ( & self , key_map : & KeyMap ) -> String {
661
+ struct KeyMapLookUp < ' a > ( & ' a KeyMap ) ;
662
+
663
+ impl < ' a > Translator < DescriptorPublicKey , String , ( ) > for KeyMapLookUp < ' a > {
664
+ fn pk ( & mut self , pk : & DescriptorPublicKey ) -> Result < String , ( ) > {
665
+ key_to_string ( pk, self . 0 )
666
+ }
667
+
668
+ fn pkh ( & mut self , pkh : & DescriptorPublicKey ) -> Result < String , ( ) > {
669
+ key_to_string ( pkh, self . 0 )
670
+ }
671
+
672
+ fn sha256 ( & mut self , sha256 : & sha256:: Hash ) -> Result < String , ( ) > {
673
+ Ok ( sha256. to_string ( ) )
674
+ }
675
+ }
676
+
611
677
fn key_to_string ( pk : & DescriptorPublicKey , key_map : & KeyMap ) -> Result < String , ( ) > {
612
678
Ok ( match key_map. get ( pk) {
613
679
Some ( secret) => secret. to_string ( ) ,
@@ -616,10 +682,7 @@ impl Descriptor<DescriptorPublicKey> {
616
682
}
617
683
618
684
let descriptor = self
619
- . translate_pk :: < _ , _ , ( ) > (
620
- |pk| key_to_string ( pk, key_map) ,
621
- |pkh| key_to_string ( pkh, key_map) ,
622
- )
685
+ . translate_pk ( & mut KeyMapLookUp ( key_map) )
623
686
. expect ( "Translation to string cannot fail" ) ;
624
687
625
688
descriptor. to_string ( )
@@ -731,7 +794,7 @@ mod tests {
731
794
use crate :: descriptor:: { DescriptorPublicKey , DescriptorSecretKey , DescriptorXKey , SinglePub } ;
732
795
#[ cfg( feature = "compiler" ) ]
733
796
use crate :: policy;
734
- use crate :: { hex_script, Descriptor , DummyKey , Error , Miniscript , Satisfier , TranslatePk2 } ;
797
+ use crate :: { hex_script, Descriptor , DummyKey , Error , Miniscript , Satisfier } ;
735
798
736
799
type StdDescriptor = Descriptor < PublicKey > ;
737
800
const TEST_PK : & ' static str =
@@ -1468,19 +1531,14 @@ mod tests {
1468
1531
assert_eq ! ( desc_one. to_string( ) , raw_desc_one) ;
1469
1532
assert_eq ! ( desc_two. to_string( ) , raw_desc_two) ;
1470
1533
1471
- // Derive a child in case the descriptor is ranged. If it's not this won't have any
1472
- // effect
1473
- let desc_one = desc_one. derive ( index) ;
1474
- let desc_two = desc_two. derive ( index) ;
1475
-
1476
1534
// Same address
1477
1535
let addr_one = desc_one
1478
- . translate_pk2 ( |xpk| xpk . derive_public_key ( & secp_ctx) )
1536
+ . derived_descriptor ( & secp_ctx, index )
1479
1537
. unwrap ( )
1480
1538
. address ( bitcoin:: Network :: Bitcoin )
1481
1539
. unwrap ( ) ;
1482
1540
let addr_two = desc_two
1483
- . translate_pk2 ( |xpk| xpk . derive_public_key ( & secp_ctx) )
1541
+ . derived_descriptor ( & secp_ctx, index )
1484
1542
. unwrap ( )
1485
1543
. address ( bitcoin:: Network :: Bitcoin )
1486
1544
. unwrap ( ) ;
0 commit comments