@@ -525,26 +525,30 @@ impl Descriptor<DescriptorPublicKey> {
525
525
/// Replaces all wildcards (i.e. `/*`) in the descriptor with a particular derivation index,
526
526
/// turning it into a *definite* descriptor.
527
527
///
528
- /// # Panics
529
- ///
530
- /// If index ≥ 2^31
531
- pub fn at_derivation_index ( & self , index : u32 ) -> Descriptor < DefiniteDescriptorKey > {
528
+ /// # Errors
529
+ /// - If index ≥ 2^31
530
+ pub fn at_derivation_index (
531
+ & self ,
532
+ index : u32 ,
533
+ ) -> Result < Descriptor < DefiniteDescriptorKey > , ConversionError > {
532
534
struct Derivator ( u32 ) ;
533
535
534
- impl Translator < DescriptorPublicKey , DefiniteDescriptorKey , ( ) > for Derivator {
535
- fn pk ( & mut self , pk : & DescriptorPublicKey ) -> Result < DefiniteDescriptorKey , ( ) > {
536
- Ok ( pk. clone ( ) . at_derivation_index ( self . 0 ) )
536
+ impl Translator < DescriptorPublicKey , DefiniteDescriptorKey , ConversionError > for Derivator {
537
+ fn pk (
538
+ & mut self ,
539
+ pk : & DescriptorPublicKey ,
540
+ ) -> Result < DefiniteDescriptorKey , ConversionError > {
541
+ pk. clone ( ) . at_derivation_index ( self . 0 )
537
542
}
538
543
539
- translate_hash_clone ! ( DescriptorPublicKey , DescriptorPublicKey , ( ) ) ;
544
+ translate_hash_clone ! ( DescriptorPublicKey , DescriptorPublicKey , ConversionError ) ;
540
545
}
541
546
self . translate_pk ( & mut Derivator ( index) )
542
- . expect ( "BIP 32 key index substitution cannot fail" )
543
547
}
544
548
545
549
#[ deprecated( note = "use at_derivation_index instead" ) ]
546
550
/// Deprecated name for [`at_derivation_index`].
547
- pub fn derive ( & self , index : u32 ) -> Descriptor < DefiniteDescriptorKey > {
551
+ pub fn derive ( & self , index : u32 ) -> Result < Descriptor < DefiniteDescriptorKey > , ConversionError > {
548
552
self . at_derivation_index ( index)
549
553
}
550
554
@@ -561,8 +565,8 @@ impl Descriptor<DescriptorPublicKey> {
561
565
/// .expect("Valid ranged descriptor");
562
566
/// # let index = 42;
563
567
/// # let secp = Secp256k1::verification_only();
564
- /// let derived_descriptor = descriptor.at_derivation_index(index).derived_descriptor(&secp);
565
- /// # assert_eq!(descriptor.derived_descriptor(&secp, index), derived_descriptor);
568
+ /// let derived_descriptor = descriptor.at_derivation_index(index).unwrap(). derived_descriptor(&secp).unwrap( );
569
+ /// # assert_eq!(descriptor.derived_descriptor(&secp, index).unwrap() , derived_descriptor);
566
570
/// ```
567
571
///
568
572
/// and is only here really here for backwards compatbility.
@@ -579,7 +583,7 @@ impl Descriptor<DescriptorPublicKey> {
579
583
secp : & secp256k1:: Secp256k1 < C > ,
580
584
index : u32 ,
581
585
) -> Result < Descriptor < bitcoin:: PublicKey > , ConversionError > {
582
- self . at_derivation_index ( index) . derived_descriptor ( & secp)
586
+ self . at_derivation_index ( index) ? . derived_descriptor ( & secp)
583
587
}
584
588
585
589
/// Parse a descriptor that may contain secret keys
@@ -741,7 +745,7 @@ impl Descriptor<DefiniteDescriptorKey> {
741
745
/// let secp = secp256k1::Secp256k1::verification_only();
742
746
/// let descriptor = Descriptor::<DescriptorPublicKey>::from_str("tr(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*)")
743
747
/// .expect("Valid ranged descriptor");
744
- /// let result = descriptor.at_derivation_index(0).derived_descriptor(&secp).expect("Non-hardened derivation");
748
+ /// let result = descriptor.at_derivation_index(0).unwrap(). derived_descriptor(&secp).expect("Non-hardened derivation");
745
749
/// assert_eq!(result.to_string(), "tr(03cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115)#6qm9h8ym");
746
750
/// ```
747
751
///
@@ -1594,12 +1598,14 @@ mod tests {
1594
1598
// Same address
1595
1599
let addr_one = desc_one
1596
1600
. at_derivation_index ( index)
1601
+ . unwrap ( )
1597
1602
. derived_descriptor ( & secp_ctx)
1598
1603
. unwrap ( )
1599
1604
. address ( bitcoin:: Network :: Bitcoin )
1600
1605
. unwrap ( ) ;
1601
1606
let addr_two = desc_two
1602
1607
. at_derivation_index ( index)
1608
+ . unwrap ( )
1603
1609
. derived_descriptor ( & secp_ctx)
1604
1610
. unwrap ( )
1605
1611
. address ( bitcoin:: Network :: Bitcoin )
@@ -1677,7 +1683,7 @@ pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHW
1677
1683
pk(03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8))";
1678
1684
let policy: policy:: concrete:: Policy < DescriptorPublicKey > = descriptor_str. parse ( ) . unwrap ( ) ;
1679
1685
let descriptor = Descriptor :: new_sh ( policy. compile ( ) . unwrap ( ) ) . unwrap ( ) ;
1680
- let definite_descriptor = descriptor. at_derivation_index ( 42 ) ;
1686
+ let definite_descriptor = descriptor. at_derivation_index ( 42 ) . unwrap ( ) ;
1681
1687
1682
1688
let res_descriptor_str = "thresh(2,\
1683
1689
pk([d34db33f/44'/0'/0']xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/1/42),\
0 commit comments