@@ -27,7 +27,7 @@ use crate::miniscript::{Legacy, Miniscript, Segwitv0};
27
27
use crate :: prelude:: * ;
28
28
use crate :: {
29
29
expression, hash256, miniscript, BareCtx , Error , ForEachKey , MiniscriptKey , Satisfier ,
30
- ToPublicKey , TranslatePk , Translator ,
30
+ ToPublicKey , TranslateErr , TranslatePk , Translator ,
31
31
} ;
32
32
33
33
mod bare;
@@ -520,7 +520,7 @@ where
520
520
type Output = Descriptor < Q > ;
521
521
522
522
/// Converts a descriptor using abstract keys to one using specific keys.
523
- fn translate_pk < T , E > ( & self , t : & mut T ) -> Result < Self :: Output , E >
523
+ fn translate_pk < T , E > ( & self , t : & mut T ) -> Result < Self :: Output , TranslateErr < E > >
524
524
where
525
525
T : Translator < P , Q , E > ,
526
526
{
@@ -585,7 +585,10 @@ impl Descriptor<DescriptorPublicKey> {
585
585
586
586
translate_hash_clone ! ( DescriptorPublicKey , DescriptorPublicKey , ConversionError ) ;
587
587
}
588
- self . translate_pk ( & mut Derivator ( index) )
588
+ self . translate_pk ( & mut Derivator ( index) ) . map_err ( |e| {
589
+ e. try_into_translator_err ( )
590
+ . expect ( "No Context errors while translating" )
591
+ } )
589
592
}
590
593
591
594
#[ deprecated( note = "use at_derivation_index instead" ) ]
@@ -698,9 +701,13 @@ impl Descriptor<DescriptorPublicKey> {
698
701
}
699
702
700
703
let descriptor = Descriptor :: < String > :: from_str ( s) ?;
701
- let descriptor = descriptor
702
- . translate_pk ( & mut keymap_pk)
703
- . map_err ( |e| Error :: Unexpected ( e. to_string ( ) ) ) ?;
704
+ let descriptor = descriptor. translate_pk ( & mut keymap_pk) . map_err ( |e| {
705
+ Error :: Unexpected (
706
+ e. try_into_translator_err ( )
707
+ . expect ( "No Outer context errors" )
708
+ . to_string ( ) ,
709
+ )
710
+ } ) ?;
704
711
705
712
Ok ( ( descriptor, keymap_pk. 0 ) )
706
713
}
@@ -827,49 +834,16 @@ impl Descriptor<DescriptorPublicKey> {
827
834
828
835
for ( i, desc) in descriptors. iter_mut ( ) . enumerate ( ) {
829
836
let mut index_choser = IndexChoser ( i) ;
830
- * desc = desc. translate_pk ( & mut index_choser) ?;
837
+ * desc = desc. translate_pk ( & mut index_choser) . map_err ( |e| {
838
+ e. try_into_translator_err ( )
839
+ . expect ( "No Context errors possible" )
840
+ } ) ?;
831
841
}
832
842
833
843
Ok ( descriptors)
834
844
}
835
845
}
836
846
837
- impl < Pk : MiniscriptKey > Descriptor < Pk > {
838
- /// Whether this descriptor is a multipath descriptor that contains any 2 multipath keys
839
- /// with a different number of derivation paths.
840
- /// Such a descriptor is invalid according to BIP389.
841
- pub fn multipath_length_mismatch ( & self ) -> bool {
842
- // (Ab)use `for_each_key` to record the number of derivation paths a multipath key has.
843
- #[ derive( PartialEq ) ]
844
- enum MultipathLenChecker {
845
- SinglePath ,
846
- MultipathLen ( usize ) ,
847
- LenMismatch ,
848
- }
849
-
850
- let mut checker = MultipathLenChecker :: SinglePath ;
851
- self . for_each_key ( |key| {
852
- match key. num_der_paths ( ) {
853
- 0 | 1 => { }
854
- n => match checker {
855
- MultipathLenChecker :: SinglePath => {
856
- checker = MultipathLenChecker :: MultipathLen ( n) ;
857
- }
858
- MultipathLenChecker :: MultipathLen ( len) => {
859
- if len != n {
860
- checker = MultipathLenChecker :: LenMismatch ;
861
- }
862
- }
863
- MultipathLenChecker :: LenMismatch => { }
864
- } ,
865
- }
866
- true
867
- } ) ;
868
-
869
- checker == MultipathLenChecker :: LenMismatch
870
- }
871
- }
872
-
873
847
impl Descriptor < DefiniteDescriptorKey > {
874
848
/// Convert all the public keys in the descriptor to [`bitcoin::PublicKey`] by deriving them or
875
849
/// otherwise converting them. All [`bitcoin::XOnlyPublicKey`]s are converted to by adding a
@@ -913,8 +887,14 @@ impl Descriptor<DefiniteDescriptorKey> {
913
887
translate_hash_clone ! ( DefiniteDescriptorKey , bitcoin:: PublicKey , ConversionError ) ;
914
888
}
915
889
916
- let derived = self . translate_pk ( & mut Derivator ( secp) ) ?;
917
- Ok ( derived)
890
+ let derived = self . translate_pk ( & mut Derivator ( secp) ) ;
891
+ match derived {
892
+ Ok ( derived) => Ok ( derived) ,
893
+ Err ( e) => match e. try_into_translator_err ( ) {
894
+ Ok ( e) => Err ( e) ,
895
+ Err ( _) => unreachable ! ( "No Context errors when deriving keys" ) ,
896
+ } ,
897
+ }
918
898
}
919
899
}
920
900
@@ -948,10 +928,6 @@ impl_from_str!(
948
928
expression:: FromTree :: from_tree( & top)
949
929
} ?;
950
930
951
- if desc. multipath_length_mismatch( ) {
952
- return Err ( Error :: MultipathDescLenMismatch ) ;
953
- }
954
-
955
931
Ok ( desc)
956
932
}
957
933
) ;
@@ -1977,7 +1953,6 @@ pk(03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8))";
1977
1953
// We can parse a multipath descriptors, and make it into separate single-path descriptors.
1978
1954
let desc = Descriptor :: from_str ( "wsh(andor(pk(tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/0'/<7';8h;20>/*),older(10000),pk(tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/8/4567/<0;1;987>/*)))" ) . unwrap ( ) ;
1979
1955
assert ! ( desc. is_multipath( ) ) ;
1980
- assert ! ( !desc. multipath_length_mismatch( ) ) ;
1981
1956
assert_eq ! ( desc. into_single_descriptors( ) . unwrap( ) , vec![
1982
1957
Descriptor :: from_str( "wsh(andor(pk(tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/0'/7'/*),older(10000),pk(tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/8/4567/0/*)))" ) . unwrap( ) ,
1983
1958
Descriptor :: from_str( "wsh(andor(pk(tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/0'/8h/*),older(10000),pk(tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/8/4567/1/*)))" ) . unwrap( ) ,
@@ -1987,7 +1962,6 @@ pk(03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8))";
1987
1962
// Even if only one of the keys is multipath.
1988
1963
let desc = Descriptor :: from_str ( "wsh(andor(pk(tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/0'/<0;1>/*),older(10000),pk(tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/8/4567/*)))" ) . unwrap ( ) ;
1989
1964
assert ! ( desc. is_multipath( ) ) ;
1990
- assert ! ( !desc. multipath_length_mismatch( ) ) ;
1991
1965
assert_eq ! ( desc. into_single_descriptors( ) . unwrap( ) , vec![
1992
1966
Descriptor :: from_str( "wsh(andor(pk(tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/0'/0/*),older(10000),pk(tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/8/4567/*)))" ) . unwrap( ) ,
1993
1967
Descriptor :: from_str( "wsh(andor(pk(tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/0'/1/*),older(10000),pk(tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/8/4567/*)))" ) . unwrap( ) ,
@@ -1996,7 +1970,6 @@ pk(03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8))";
1996
1970
// We can detect regular single-path descriptors.
1997
1971
let notmulti_desc = Descriptor :: from_str ( "wsh(andor(pk(tpubDEN9WSToTyy9ZQfaYqSKfmVqmq1VVLNtYfj3Vkqh67et57eJ5sTKZQBkHqSwPUsoSskJeaYnPttHe2VrkCsKA27kUaN9SDc5zhqeLzKa1rr/0'/*),older(10000),pk(tpubD8LYfn6njiA2inCoxwM7EuN3cuLVcaHAwLYeups13dpevd3nHLRdK9NdQksWXrhLQVxcUZRpnp5CkJ1FhE61WRAsHxDNAkvGkoQkAeWDYjV/8/4567/*)))" ) . unwrap ( ) ;
1998
1972
assert ! ( !notmulti_desc. is_multipath( ) ) ;
1999
- assert ! ( !notmulti_desc. multipath_length_mismatch( ) ) ;
2000
1973
assert_eq ! (
2001
1974
notmulti_desc. clone( ) . into_single_descriptors( ) . unwrap( ) ,
2002
1975
vec![ notmulti_desc]
0 commit comments