@@ -26,7 +26,7 @@ use crate::miniscript::{Legacy, Miniscript, Segwitv0};
26
26
use crate :: prelude:: * ;
27
27
use crate :: {
28
28
expression, hash256, miniscript, BareCtx , Error , ForEachKey , MiniscriptKey , Satisfier ,
29
- ToPublicKey , TranslatePk , Translator ,
29
+ ToPublicKey , TranslateErr , TranslatePk , Translator ,
30
30
} ;
31
31
32
32
mod bare;
@@ -519,7 +519,7 @@ where
519
519
type Output = Descriptor < Q > ;
520
520
521
521
/// Converts a descriptor using abstract keys to one using specific keys.
522
- fn translate_pk < T , E > ( & self , t : & mut T ) -> Result < Self :: Output , E >
522
+ fn translate_pk < T , E > ( & self , t : & mut T ) -> Result < Self :: Output , TranslateErr < E > >
523
523
where
524
524
T : Translator < P , Q , E > ,
525
525
{
@@ -581,7 +581,10 @@ impl Descriptor<DescriptorPublicKey> {
581
581
582
582
translate_hash_clone ! ( DescriptorPublicKey , DescriptorPublicKey , ConversionError ) ;
583
583
}
584
- self . translate_pk ( & mut Derivator ( index) )
584
+ self . translate_pk ( & mut Derivator ( index) ) . map_err ( |e| {
585
+ e. try_into_translator_err ( )
586
+ . expect ( "No Context errors while translating" )
587
+ } )
585
588
}
586
589
587
590
#[ deprecated( note = "use at_derivation_index instead" ) ]
@@ -694,9 +697,13 @@ impl Descriptor<DescriptorPublicKey> {
694
697
}
695
698
696
699
let descriptor = Descriptor :: < String > :: from_str ( s) ?;
697
- let descriptor = descriptor
698
- . translate_pk ( & mut keymap_pk)
699
- . map_err ( |e| Error :: Unexpected ( e. to_string ( ) ) ) ?;
700
+ let descriptor = descriptor. translate_pk ( & mut keymap_pk) . map_err ( |e| {
701
+ Error :: Unexpected (
702
+ e. try_into_translator_err ( )
703
+ . expect ( "No Outer context errors" )
704
+ . to_string ( ) ,
705
+ )
706
+ } ) ?;
700
707
701
708
Ok ( ( descriptor, keymap_pk. 0 ) )
702
709
}
@@ -823,49 +830,16 @@ impl Descriptor<DescriptorPublicKey> {
823
830
824
831
for ( i, desc) in descriptors. iter_mut ( ) . enumerate ( ) {
825
832
let mut index_choser = IndexChoser ( i) ;
826
- * desc = desc. translate_pk ( & mut index_choser) ?;
833
+ * desc = desc. translate_pk ( & mut index_choser) . map_err ( |e| {
834
+ e. try_into_translator_err ( )
835
+ . expect ( "No Context errors possible" )
836
+ } ) ?;
827
837
}
828
838
829
839
Ok ( descriptors)
830
840
}
831
841
}
832
842
833
- impl < Pk : MiniscriptKey > Descriptor < Pk > {
834
- /// Whether this descriptor is a multipath descriptor that contains any 2 multipath keys
835
- /// with a different number of derivation paths.
836
- /// Such a descriptor is invalid according to BIP389.
837
- pub fn multipath_length_mismatch ( & self ) -> bool {
838
- // (Ab)use `for_each_key` to record the number of derivation paths a multipath key has.
839
- #[ derive( PartialEq ) ]
840
- enum MultipathLenChecker {
841
- SinglePath ,
842
- MultipathLen ( usize ) ,
843
- LenMismatch ,
844
- }
845
-
846
- let mut checker = MultipathLenChecker :: SinglePath ;
847
- self . for_each_key ( |key| {
848
- match key. num_der_paths ( ) {
849
- 0 | 1 => { }
850
- n => match checker {
851
- MultipathLenChecker :: SinglePath => {
852
- checker = MultipathLenChecker :: MultipathLen ( n) ;
853
- }
854
- MultipathLenChecker :: MultipathLen ( len) => {
855
- if len != n {
856
- checker = MultipathLenChecker :: LenMismatch ;
857
- }
858
- }
859
- MultipathLenChecker :: LenMismatch => { }
860
- } ,
861
- }
862
- true
863
- } ) ;
864
-
865
- checker == MultipathLenChecker :: LenMismatch
866
- }
867
- }
868
-
869
843
impl Descriptor < DefiniteDescriptorKey > {
870
844
/// Convert all the public keys in the descriptor to [`bitcoin::PublicKey`] by deriving them or
871
845
/// otherwise converting them. All [`bitcoin::secp256k1::XOnlyPublicKey`]s are converted to by adding a
@@ -909,8 +883,14 @@ impl Descriptor<DefiniteDescriptorKey> {
909
883
translate_hash_clone ! ( DefiniteDescriptorKey , bitcoin:: PublicKey , ConversionError ) ;
910
884
}
911
885
912
- let derived = self . translate_pk ( & mut Derivator ( secp) ) ?;
913
- Ok ( derived)
886
+ let derived = self . translate_pk ( & mut Derivator ( secp) ) ;
887
+ match derived {
888
+ Ok ( derived) => Ok ( derived) ,
889
+ Err ( e) => match e. try_into_translator_err ( ) {
890
+ Ok ( e) => Err ( e) ,
891
+ Err ( _) => unreachable ! ( "No Context errors when deriving keys" ) ,
892
+ } ,
893
+ }
914
894
}
915
895
}
916
896
@@ -944,10 +924,6 @@ impl_from_str!(
944
924
expression:: FromTree :: from_tree( & top)
945
925
} ?;
946
926
947
- if desc. multipath_length_mismatch( ) {
948
- return Err ( Error :: MultipathDescLenMismatch ) ;
949
- }
950
-
951
927
Ok ( desc)
952
928
}
953
929
) ;
@@ -1992,7 +1968,6 @@ pk(03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8))";
1992
1968
// We can parse a multipath descriptors, and make it into separate single-path descriptors.
1993
1969
let desc = Descriptor :: from_str ( "wsh(andor(pk(tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/0'/<7';8h;20>/*),older(10000),pk(tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/8/4567/<0;1;987>/*)))" ) . unwrap ( ) ;
1994
1970
assert ! ( desc. is_multipath( ) ) ;
1995
- assert ! ( !desc. multipath_length_mismatch( ) ) ;
1996
1971
assert_eq ! ( desc. into_single_descriptors( ) . unwrap( ) , vec![
1997
1972
Descriptor :: from_str( "wsh(andor(pk(tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/0'/7'/*),older(10000),pk(tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/8/4567/0/*)))" ) . unwrap( ) ,
1998
1973
Descriptor :: from_str( "wsh(andor(pk(tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/0'/8h/*),older(10000),pk(tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/8/4567/1/*)))" ) . unwrap( ) ,
@@ -2002,7 +1977,6 @@ pk(03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8))";
2002
1977
// Even if only one of the keys is multipath.
2003
1978
let desc = Descriptor :: from_str ( "wsh(andor(pk(tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/0'/<0;1>/*),older(10000),pk(tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/8/4567/*)))" ) . unwrap ( ) ;
2004
1979
assert ! ( desc. is_multipath( ) ) ;
2005
- assert ! ( !desc. multipath_length_mismatch( ) ) ;
2006
1980
assert_eq ! ( desc. into_single_descriptors( ) . unwrap( ) , vec![
2007
1981
Descriptor :: from_str( "wsh(andor(pk(tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/0'/0/*),older(10000),pk(tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/8/4567/*)))" ) . unwrap( ) ,
2008
1982
Descriptor :: from_str( "wsh(andor(pk(tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/0'/1/*),older(10000),pk(tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/8/4567/*)))" ) . unwrap( ) ,
@@ -2011,7 +1985,6 @@ pk(03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8))";
2011
1985
// We can detect regular single-path descriptors.
2012
1986
let notmulti_desc = Descriptor :: from_str ( "wsh(andor(pk(tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/0'/*),older(10000),pk(tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/8/4567/*)))" ) . unwrap ( ) ;
2013
1987
assert ! ( !notmulti_desc. is_multipath( ) ) ;
2014
- assert ! ( !notmulti_desc. multipath_length_mismatch( ) ) ;
2015
1988
assert_eq ! (
2016
1989
notmulti_desc. clone( ) . into_single_descriptors( ) . unwrap( ) ,
2017
1990
vec![ notmulti_desc]
0 commit comments