@@ -223,7 +223,8 @@ impl DescriptorXKey<bip32::Xpriv> {
223
223
let xprv = self
224
224
. xkey
225
225
. derive_priv ( secp, & hardened_path)
226
- . map_err ( |_| DescriptorKeyParseError ( "Unable to derive the hardened steps" ) ) ?;
226
+ . map_err ( DescriptorKeyParseError :: DeriveHardenedKey ) ?;
227
+
227
228
let xpub = bip32:: Xpub :: from_priv ( secp, & xprv) ;
228
229
229
230
let origin = match & self . origin {
@@ -276,7 +277,9 @@ impl DescriptorMultiXKey<bip32::Xpriv> {
276
277
if child_num. is_normal ( ) {
277
278
Ok ( * child_num)
278
279
} else {
279
- Err ( DescriptorKeyParseError ( "Can't make a multi-xpriv with hardened derivation steps that are not shared among all paths into a public key." ) )
280
+ Err ( DescriptorKeyParseError :: MalformedKeyData (
281
+ MalformedKeyDataKind :: InvalidMultiXKeyDerivation ,
282
+ ) )
280
283
}
281
284
} )
282
285
. collect ( )
@@ -295,7 +298,7 @@ impl DescriptorMultiXKey<bip32::Xpriv> {
295
298
let xprv = self
296
299
. xkey
297
300
. derive_priv ( secp, & hardened_path)
298
- . map_err ( |_| DescriptorKeyParseError ( "Unable to derive the hardened steps" ) ) ?;
301
+ . map_err ( DescriptorKeyParseError :: DeriveHardenedKey ) ?;
299
302
let xpub = bip32:: Xpub :: from_priv ( secp, & xprv) ;
300
303
301
304
let origin = match & self . origin {
@@ -324,18 +327,145 @@ impl DescriptorMultiXKey<bip32::Xpriv> {
324
327
}
325
328
}
326
329
330
+ /// Kinds of malformed key data
331
+ #[ derive( Debug , PartialEq , Clone ) ]
332
+ #[ non_exhaustive]
333
+ #[ allow( missing_docs) ]
334
+ pub enum MalformedKeyDataKind {
335
+ EmptyKey ,
336
+ EncounteredUnprintableCharacter ,
337
+ InvalidFullPublicKeyPrefix ,
338
+ InvalidMasterFingerprintLength ,
339
+ InvalidMultiIndexStep ,
340
+ InvalidMultiXKeyDerivation ,
341
+ InvalidPublicKeyLength ,
342
+ InvalidWildcardInDerivationPath ,
343
+ KeyTooShort ,
344
+ MultipleFingerprintsInPublicKey ,
345
+ MultipleDerivationPathIndexSteps ,
346
+ NoKeyAfterOrigin ,
347
+ NoMasterFingerprintFound ,
348
+ UnclosedSquareBracket ,
349
+ WildcardAsDerivedDescriptorKey ,
350
+ }
351
+
352
+ impl fmt:: Display for MalformedKeyDataKind {
353
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
354
+ use MalformedKeyDataKind :: * ;
355
+
356
+ let err = match self {
357
+ EmptyKey => "empty key" ,
358
+ EncounteredUnprintableCharacter => "encountered an unprintable character" ,
359
+ InvalidFullPublicKeyPrefix => "only full public keys with prefixes '02', '03' or '04' are allowed" ,
360
+ InvalidMasterFingerprintLength => "master fingerprint should be 8 characters long" ,
361
+ InvalidMultiIndexStep => "invalid multi index step in multipath descriptor" ,
362
+ InvalidMultiXKeyDerivation => "can't make a multi-xpriv with hardened derivation steps that are not shared among all paths into a public key" ,
363
+ InvalidPublicKeyLength => "public keys must be 64, 66 or 130 characters in size" ,
364
+ InvalidWildcardInDerivationPath => "'*' may only appear as last element in a derivation path" ,
365
+ KeyTooShort => "key too short" ,
366
+ MultipleFingerprintsInPublicKey => "multiple ']' in Descriptor Public Key" ,
367
+ MultipleDerivationPathIndexSteps => "'<' may only appear once in a derivation path" ,
368
+ NoKeyAfterOrigin => "no key after origin" ,
369
+ NoMasterFingerprintFound => "no master fingerprint found after '['" ,
370
+ UnclosedSquareBracket => "unclosed '['" ,
371
+ WildcardAsDerivedDescriptorKey => "cannot parse key with a wilcard as a DerivedDescriptorKey" ,
372
+ } ;
373
+
374
+ f. write_str ( err)
375
+ }
376
+ }
377
+
327
378
/// Descriptor Key parsing errors
328
- // FIXME: replace with error enums
329
- #[ derive( Debug , PartialEq , Clone , Copy ) ]
330
- pub struct DescriptorKeyParseError ( & ' static str ) ;
379
+ #[ derive( Debug , PartialEq , Clone ) ]
380
+ #[ non_exhaustive]
381
+ pub enum DescriptorKeyParseError {
382
+ /// Error while parsing a BIP32 extended private key
383
+ Bip32Xpriv ( bip32:: Error ) ,
384
+ /// Error while parsing a BIP32 extended public key
385
+ Bip32Xpub ( bip32:: Error ) ,
386
+ /// Error while parsing a derivation index
387
+ DerivationIndexError {
388
+ /// The invalid index
389
+ index : String ,
390
+ /// The underlying parse error
391
+ err : bitcoin:: bip32:: Error ,
392
+ } ,
393
+ /// Error deriving the hardened private key.
394
+ DeriveHardenedKey ( bip32:: Error ) ,
395
+ /// Error indicating the key data was malformed
396
+ MalformedKeyData ( MalformedKeyDataKind ) ,
397
+ /// Error while parsing the master derivation path.
398
+ MasterDerivationPath ( bip32:: Error ) ,
399
+ /// Error indicating a malformed master fingerprint (invalid hex).
400
+ MasterFingerprint {
401
+ /// The invalid fingerprint
402
+ fingerprint : String ,
403
+ /// The underlying parse error
404
+ err : bitcoin:: hex:: HexToArrayError ,
405
+ } ,
406
+ /// Error while parsing a simple public key.
407
+ FullPublicKey ( bitcoin:: key:: ParsePublicKeyError ) ,
408
+ /// Error while parsing a WIF private key.
409
+ WifPrivateKey ( bitcoin:: key:: FromWifError ) ,
410
+ /// Error while parsing an X-only public key (Secp256k1 error).
411
+ XonlyPublicKey ( bitcoin:: secp256k1:: Error ) ,
412
+ }
331
413
332
414
impl fmt:: Display for DescriptorKeyParseError {
333
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result { f. write_str ( self . 0 ) }
415
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
416
+ match self {
417
+ DescriptorKeyParseError :: Bip32Xpriv ( err) => {
418
+ write ! ( f, "error while parsing BIP32 Xpriv: {err}" )
419
+ }
420
+ DescriptorKeyParseError :: Bip32Xpub ( err) => {
421
+ write ! ( f, "error while parsing BIP32 Xpub: {err}" )
422
+ }
423
+ DescriptorKeyParseError :: DerivationIndexError { index, err } => {
424
+ write ! ( f, "error while parsing derivation index '{index}': {err}" )
425
+ }
426
+ DescriptorKeyParseError :: DeriveHardenedKey ( err) => {
427
+ write ! ( f, "unable to derive the hardened steps: {err}" )
428
+ }
429
+ DescriptorKeyParseError :: MalformedKeyData ( err) => {
430
+ write ! ( f, "{err}" )
431
+ }
432
+ DescriptorKeyParseError :: MasterDerivationPath ( err) => {
433
+ write ! ( f, "error while parsing master derivation path: {err}" )
434
+ }
435
+ DescriptorKeyParseError :: MasterFingerprint { fingerprint, err } => {
436
+ write ! ( f, "error while parsing master fingerprint '{fingerprint}': {err}" )
437
+ }
438
+ DescriptorKeyParseError :: FullPublicKey ( err) => {
439
+ write ! ( f, "error while parsing full public key: {err}" )
440
+ }
441
+ DescriptorKeyParseError :: WifPrivateKey ( err) => {
442
+ write ! ( f, "error while parsing WIF private key: {err}" )
443
+ }
444
+ DescriptorKeyParseError :: XonlyPublicKey ( err) => {
445
+ write ! ( f, "error while parsing xonly public key: {err}" )
446
+ }
447
+ }
448
+ }
334
449
}
335
450
336
451
#[ cfg( feature = "std" ) ]
337
452
impl error:: Error for DescriptorKeyParseError {
338
- fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > { None }
453
+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
454
+ use DescriptorKeyParseError :: * ;
455
+
456
+ match self {
457
+ Bip32Xpriv ( err)
458
+ | Bip32Xpub ( err)
459
+ | DerivationIndexError { err, .. }
460
+ | DeriveHardenedKey ( err)
461
+ | MasterDerivationPath ( err) => Some ( err) ,
462
+ MasterFingerprint { err, .. } => Some ( err) ,
463
+ FullPublicKey ( err) => Some ( err) ,
464
+ WifPrivateKey ( err) => Some ( err) ,
465
+ XonlyPublicKey ( err) => Some ( err) ,
466
+ MalformedKeyData ( _) => None ,
467
+ }
468
+ }
339
469
}
340
470
341
471
impl fmt:: Display for DescriptorPublicKey {
@@ -485,16 +615,15 @@ impl FromStr for DescriptorPublicKey {
485
615
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
486
616
// A "raw" public key without any origin is the least we accept.
487
617
if s. len ( ) < 64 {
488
- return Err ( DescriptorKeyParseError (
489
- "Key too short (<64 characters); use parse_descriptor for parsing \
490
- descriptors with private keys.",
618
+ return Err ( DescriptorKeyParseError :: MalformedKeyData (
619
+ MalformedKeyDataKind :: KeyTooShort ,
491
620
) ) ;
492
621
}
493
622
494
623
let ( key_part, origin) = parse_key_origin ( s) ?;
495
624
496
625
if key_part. contains ( "pub" ) {
497
- let ( xpub, derivation_paths, wildcard) = parse_xkey_deriv :: < bip32 :: Xpub > ( key_part) ?;
626
+ let ( xpub, derivation_paths, wildcard) = parse_xkey_deriv ( parse_bip32_xpub , key_part) ?;
498
627
if derivation_paths. len ( ) > 1 {
499
628
Ok ( DescriptorPublicKey :: MultiXPub ( DescriptorMultiXKey {
500
629
origin,
@@ -513,28 +642,26 @@ impl FromStr for DescriptorPublicKey {
513
642
} else {
514
643
let key = match key_part. len ( ) {
515
644
64 => {
516
- let x_only_key = XOnlyPublicKey :: from_str ( key_part) . map_err ( |_| {
517
- DescriptorKeyParseError ( "Error while parsing simple xonly key" )
518
- } ) ?;
645
+ let x_only_key = XOnlyPublicKey :: from_str ( key_part)
646
+ . map_err ( DescriptorKeyParseError :: XonlyPublicKey ) ?;
519
647
SinglePubKey :: XOnly ( x_only_key)
520
648
}
521
649
66 | 130 => {
522
650
if !( & key_part[ 0 ..2 ] == "02"
523
651
|| & key_part[ 0 ..2 ] == "03"
524
652
|| & key_part[ 0 ..2 ] == "04" )
525
653
{
526
- return Err ( DescriptorKeyParseError (
527
- "Only publickeys with prefixes 02/03/04 are allowed" ,
654
+ return Err ( DescriptorKeyParseError :: MalformedKeyData (
655
+ MalformedKeyDataKind :: InvalidFullPublicKeyPrefix ,
528
656
) ) ;
529
657
}
530
- let key = bitcoin:: PublicKey :: from_str ( key_part) . map_err ( |_| {
531
- DescriptorKeyParseError ( "Error while parsing simple public key" )
532
- } ) ?;
658
+ let key = bitcoin:: PublicKey :: from_str ( key_part)
659
+ . map_err ( DescriptorKeyParseError :: FullPublicKey ) ?;
533
660
SinglePubKey :: FullKey ( key)
534
661
}
535
662
_ => {
536
- return Err ( DescriptorKeyParseError (
537
- "Public keys must be 64/66/130 characters in size" ,
663
+ return Err ( DescriptorKeyParseError :: MalformedKeyData (
664
+ MalformedKeyDataKind :: InvalidPublicKeyLength ,
538
665
) )
539
666
}
540
667
} ;
@@ -772,10 +899,11 @@ impl FromStr for DescriptorSecretKey {
772
899
773
900
if key_part. len ( ) <= 52 {
774
901
let sk = bitcoin:: PrivateKey :: from_str ( key_part)
775
- . map_err ( |_| DescriptorKeyParseError ( "Error while parsing a WIF private key" ) ) ?;
902
+ . map_err ( DescriptorKeyParseError :: WifPrivateKey ) ?;
776
903
Ok ( DescriptorSecretKey :: Single ( SinglePriv { key : sk, origin } ) )
777
904
} else {
778
- let ( xpriv, derivation_paths, wildcard) = parse_xkey_deriv :: < bip32:: Xpriv > ( key_part) ?;
905
+ let ( xpriv, derivation_paths, wildcard) =
906
+ parse_xkey_deriv ( parse_bip32_xpriv, key_part) ?;
779
907
if derivation_paths. len ( ) > 1 {
780
908
Ok ( DescriptorSecretKey :: MultiXPrv ( DescriptorMultiXKey {
781
909
origin,
@@ -799,42 +927,57 @@ impl FromStr for DescriptorSecretKey {
799
927
fn parse_key_origin ( s : & str ) -> Result < ( & str , Option < bip32:: KeySource > ) , DescriptorKeyParseError > {
800
928
for ch in s. as_bytes ( ) {
801
929
if * ch < 20 || * ch > 127 {
802
- return Err ( DescriptorKeyParseError ( "Encountered an unprintable character" ) ) ;
930
+ return Err ( DescriptorKeyParseError :: MalformedKeyData (
931
+ MalformedKeyDataKind :: EncounteredUnprintableCharacter ,
932
+ ) ) ;
803
933
}
804
934
}
805
935
806
936
if s. is_empty ( ) {
807
- return Err ( DescriptorKeyParseError ( "Empty key" ) ) ;
937
+ return Err ( DescriptorKeyParseError :: MalformedKeyData ( MalformedKeyDataKind :: EmptyKey ) ) ;
808
938
}
809
939
let mut parts = s[ 1 ..] . split ( ']' ) ;
810
940
811
941
if let Some ( '[' ) = s. chars ( ) . next ( ) {
812
942
let mut raw_origin = parts
813
943
. next ( )
814
- . ok_or ( DescriptorKeyParseError ( "Unclosed '['" ) ) ?
944
+ . ok_or ( DescriptorKeyParseError :: MalformedKeyData (
945
+ MalformedKeyDataKind :: UnclosedSquareBracket ,
946
+ ) ) ?
815
947
. split ( '/' ) ;
816
948
817
949
let origin_id_hex = raw_origin
818
950
. next ( )
819
- . ok_or ( DescriptorKeyParseError ( "No master fingerprint found after '['" ) ) ?;
951
+ . ok_or ( DescriptorKeyParseError :: MalformedKeyData (
952
+ MalformedKeyDataKind :: NoMasterFingerprintFound ,
953
+ ) ) ?;
820
954
821
955
if origin_id_hex. len ( ) != 8 {
822
- return Err ( DescriptorKeyParseError ( "Master fingerprint should be 8 characters long" ) ) ;
956
+ return Err ( DescriptorKeyParseError :: MalformedKeyData (
957
+ MalformedKeyDataKind :: InvalidMasterFingerprintLength ,
958
+ ) ) ;
823
959
}
824
- let parent_fingerprint = bip32:: Fingerprint :: from_hex ( origin_id_hex) . map_err ( |_| {
825
- DescriptorKeyParseError ( "Malformed master fingerprint, expected 8 hex chars" )
960
+ let parent_fingerprint = bip32:: Fingerprint :: from_hex ( origin_id_hex) . map_err ( |err| {
961
+ DescriptorKeyParseError :: MasterFingerprint {
962
+ fingerprint : origin_id_hex. to_owned ( ) ,
963
+ err,
964
+ }
826
965
} ) ?;
827
966
let origin_path = raw_origin
828
967
. map ( bip32:: ChildNumber :: from_str)
829
968
. collect :: < Result < bip32:: DerivationPath , bip32:: Error > > ( )
830
- . map_err ( |_| DescriptorKeyParseError ( "Error while parsing master derivation path" ) ) ?;
969
+ . map_err ( DescriptorKeyParseError :: MasterDerivationPath ) ?;
831
970
832
971
let key = parts
833
972
. next ( )
834
- . ok_or ( DescriptorKeyParseError ( "No key after origin." ) ) ?;
973
+ . ok_or ( DescriptorKeyParseError :: MalformedKeyData (
974
+ MalformedKeyDataKind :: NoKeyAfterOrigin ,
975
+ ) ) ?;
835
976
836
977
if parts. next ( ) . is_some ( ) {
837
- Err ( DescriptorKeyParseError ( "Multiple ']' in Descriptor Public Key" ) )
978
+ Err ( DescriptorKeyParseError :: MalformedKeyData (
979
+ MalformedKeyDataKind :: MultipleFingerprintsInPublicKey ,
980
+ ) )
838
981
} else {
839
982
Ok ( ( key, Some ( ( parent_fingerprint, origin_path) ) ) )
840
983
}
@@ -843,16 +986,26 @@ fn parse_key_origin(s: &str) -> Result<(&str, Option<bip32::KeySource>), Descrip
843
986
}
844
987
}
845
988
846
- /// Parse an extended key concatenated to a derivation path.
847
- fn parse_xkey_deriv < K : InnerXKey > (
989
+ fn parse_bip32_xpub ( xkey_str : & str ) -> Result < bip32:: Xpub , DescriptorKeyParseError > {
990
+ bip32:: Xpub :: from_str ( xkey_str) . map_err ( DescriptorKeyParseError :: Bip32Xpub )
991
+ }
992
+
993
+ fn parse_bip32_xpriv ( xkey_str : & str ) -> Result < bip32:: Xpriv , DescriptorKeyParseError > {
994
+ bip32:: Xpriv :: from_str ( xkey_str) . map_err ( DescriptorKeyParseError :: Bip32Xpriv )
995
+ }
996
+
997
+ fn parse_xkey_deriv < Key > (
998
+ parse_xkey_fn : impl Fn ( & str ) -> Result < Key , DescriptorKeyParseError > ,
848
999
key_deriv : & str ,
849
- ) -> Result < ( K , Vec < bip32:: DerivationPath > , Wildcard ) , DescriptorKeyParseError > {
1000
+ ) -> Result < ( Key , Vec < bip32:: DerivationPath > , Wildcard ) , DescriptorKeyParseError > {
850
1001
let mut key_deriv = key_deriv. split ( '/' ) ;
851
1002
let xkey_str = key_deriv
852
1003
. next ( )
853
- . ok_or ( DescriptorKeyParseError ( "No key found after origin description" ) ) ?;
854
- let xkey =
855
- K :: from_str ( xkey_str) . map_err ( |_| DescriptorKeyParseError ( "Error while parsing xkey." ) ) ?;
1004
+ . ok_or ( DescriptorKeyParseError :: MalformedKeyData (
1005
+ MalformedKeyDataKind :: NoKeyAfterOrigin ,
1006
+ ) ) ?;
1007
+
1008
+ let xkey = parse_xkey_fn ( xkey_str) ?;
856
1009
857
1010
let mut wildcard = Wildcard :: None ;
858
1011
let mut multipath = false ;
@@ -865,26 +1018,26 @@ fn parse_xkey_deriv<K: InnerXKey>(
865
1018
wildcard = Wildcard :: Hardened ;
866
1019
None
867
1020
} else if wildcard != Wildcard :: None {
868
- Some ( Err ( DescriptorKeyParseError (
869
- "'*' may only appear as last element in a derivation path." ,
1021
+ Some ( Err ( DescriptorKeyParseError :: MalformedKeyData (
1022
+ MalformedKeyDataKind :: InvalidWildcardInDerivationPath ,
870
1023
) ) )
871
1024
} else {
872
1025
// BIP389 defines a new step in the derivation path. This step contains two or more
873
1026
// derivation indexes in the form '<1;2;3';4h;5H;6>'.
874
1027
if p. starts_with ( '<' ) && p. ends_with ( '>' ) {
875
1028
// There may only be one occurence of this step.
876
1029
if multipath {
877
- return Some ( Err ( DescriptorKeyParseError (
878
- "'<' may only appear once in a derivation path." ,
1030
+ return Some ( Err ( DescriptorKeyParseError :: MalformedKeyData (
1031
+ MalformedKeyDataKind :: MultipleDerivationPathIndexSteps ,
879
1032
) ) ) ;
880
1033
}
881
1034
multipath = true ;
882
1035
883
1036
// The step must contain at least two derivation indexes.
884
1037
// So it's at least '<' + a number + ';' + a number + '>'.
885
1038
if p. len ( ) < 5 || !p. contains ( ';' ) {
886
- return Some ( Err ( DescriptorKeyParseError (
887
- "Invalid multi index step in multipath descriptor." ,
1039
+ return Some ( Err ( DescriptorKeyParseError :: MalformedKeyData (
1040
+ MalformedKeyDataKind :: InvalidMultiIndexStep ,
888
1041
) ) ) ;
889
1042
}
890
1043
@@ -894,10 +1047,11 @@ fn parse_xkey_deriv<K: InnerXKey>(
894
1047
indexes
895
1048
. into_iter ( )
896
1049
. map ( |s| {
897
- bip32:: ChildNumber :: from_str ( s) . map_err ( |_| {
898
- DescriptorKeyParseError (
899
- "Error while parsing index in key derivation path." ,
900
- )
1050
+ bip32:: ChildNumber :: from_str ( s) . map_err ( |err| {
1051
+ DescriptorKeyParseError :: DerivationIndexError {
1052
+ index : s. to_owned ( ) ,
1053
+ err,
1054
+ }
901
1055
} )
902
1056
} )
903
1057
. collect :: < Result < Vec < bip32:: ChildNumber > , _ > > ( ) ,
@@ -907,8 +1061,9 @@ fn parse_xkey_deriv<K: InnerXKey>(
907
1061
Some (
908
1062
bip32:: ChildNumber :: from_str ( p)
909
1063
. map ( |i| vec ! [ i] )
910
- . map_err ( |_| {
911
- DescriptorKeyParseError ( "Error while parsing key derivation path" )
1064
+ . map_err ( |err| DescriptorKeyParseError :: DerivationIndexError {
1065
+ index : p. to_owned ( ) ,
1066
+ err,
912
1067
} ) ,
913
1068
)
914
1069
}
@@ -1139,8 +1294,8 @@ impl FromStr for DefiniteDescriptorKey {
1139
1294
1140
1295
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
1141
1296
let inner = DescriptorPublicKey :: from_str ( s) ?;
1142
- DefiniteDescriptorKey :: new ( inner) . ok_or ( DescriptorKeyParseError (
1143
- "cannot parse key with a wilcard as a DerivedDescriptorKey" ,
1297
+ DefiniteDescriptorKey :: new ( inner) . ok_or ( DescriptorKeyParseError :: MalformedKeyData (
1298
+ MalformedKeyDataKind :: WildcardAsDerivedDescriptorKey ,
1144
1299
) )
1145
1300
}
1146
1301
}
@@ -1215,8 +1370,7 @@ mod test {
1215
1370
use serde_test:: { assert_tokens, Token } ;
1216
1371
1217
1372
use super :: {
1218
- DescriptorKeyParseError , DescriptorMultiXKey , DescriptorPublicKey , DescriptorSecretKey ,
1219
- MiniscriptKey , Wildcard ,
1373
+ DescriptorMultiXKey , DescriptorPublicKey , DescriptorSecretKey , MiniscriptKey , Wildcard ,
1220
1374
} ;
1221
1375
use crate :: prelude:: * ;
1222
1376
@@ -1225,52 +1379,50 @@ mod test {
1225
1379
// And ones with misplaced wildcard
1226
1380
let desc = "[78412e3a/44'/0'/0']xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/1/*/44" ;
1227
1381
assert_eq ! (
1228
- DescriptorPublicKey :: from_str( desc) ,
1229
- Err ( DescriptorKeyParseError (
1230
- "\' *\' may only appear as last element in a derivation path."
1231
- ) )
1382
+ DescriptorPublicKey :: from_str( desc) . unwrap_err( ) . to_string( ) ,
1383
+ "\' *\' may only appear as last element in a derivation path"
1232
1384
) ;
1233
1385
1234
1386
// And ones with invalid fingerprints
1235
1387
let desc = "[NonHexor]xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/1/*" ;
1236
1388
assert_eq ! (
1237
- DescriptorPublicKey :: from_str( desc) ,
1238
- Err ( DescriptorKeyParseError ( "Malformed master fingerprint, expected 8 hex chars" ) )
1389
+ DescriptorPublicKey :: from_str( desc) . unwrap_err ( ) . to_string ( ) ,
1390
+ "error while parsing master fingerprint 'NonHexor': failed to parse hex digit"
1239
1391
) ;
1240
1392
1241
1393
// And ones with invalid xpubs..
1242
1394
let desc = "[78412e3a]xpub1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaLcgJvLJuZZvRcEL/1/*" ;
1243
1395
assert_eq ! (
1244
- DescriptorPublicKey :: from_str( desc) ,
1245
- Err ( DescriptorKeyParseError ( "Error while parsing xkey." ) )
1396
+ DescriptorPublicKey :: from_str( desc) . unwrap_err ( ) . to_string ( ) ,
1397
+ "error while parsing BIP32 Xpub: base58 encoding error"
1246
1398
) ;
1247
1399
1248
1400
// ..or invalid raw keys
1249
1401
let desc = "[78412e3a]0208a117f3897c3a13c9384b8695eed98dc31bc2500feb19a1af424cd47a5d83/1/*" ;
1250
1402
assert_eq ! (
1251
- DescriptorPublicKey :: from_str( desc) ,
1252
- Err ( DescriptorKeyParseError ( "Public keys must be 64/66/ 130 characters in size") )
1403
+ DescriptorPublicKey :: from_str( desc) . unwrap_err ( ) . to_string ( ) ,
1404
+ "public keys must be 64, 66 or 130 characters in size",
1253
1405
) ;
1254
1406
1255
1407
// ..or invalid separators
1256
1408
let desc = "[78412e3a]]03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8" ;
1257
1409
assert_eq ! (
1258
- DescriptorPublicKey :: from_str( desc) ,
1259
- Err ( DescriptorKeyParseError ( "Multiple \' ]\' in Descriptor Public Key") )
1410
+ DescriptorPublicKey :: from_str( desc) . unwrap_err ( ) . to_string ( ) ,
1411
+ "multiple \' ]\' in Descriptor Public Key"
1260
1412
) ;
1261
1413
1262
1414
// fuzzer errors
1263
1415
let desc = "[11111f11]033333333333333333333333333333323333333333333333333333333433333333]]333]]3]]101333333333333433333]]]10]333333mmmm" ;
1264
1416
assert_eq ! (
1265
- DescriptorPublicKey :: from_str( desc) ,
1266
- Err ( DescriptorKeyParseError ( "Multiple \' ]\' in Descriptor Public Key") )
1417
+ DescriptorPublicKey :: from_str( desc) . unwrap_err ( ) . to_string ( ) ,
1418
+ "multiple \' ]\' in Descriptor Public Key"
1267
1419
) ;
1268
1420
1269
1421
// fuzz failure, hybrid keys
1270
1422
let desc = "0777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777" ;
1271
1423
assert_eq ! (
1272
- DescriptorPublicKey :: from_str( desc) ,
1273
- Err ( DescriptorKeyParseError ( "Only publickeys with prefixes 02/03/04 are allowed") )
1424
+ DescriptorPublicKey :: from_str( desc) . unwrap_err ( ) . to_string ( ) ,
1425
+ "only full public keys with prefixes '02', '03' or '04' are allowed"
1274
1426
) ;
1275
1427
}
1276
1428
@@ -1279,22 +1431,24 @@ mod test {
1279
1431
// Xpubs are invalid
1280
1432
let secret_key = "xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL" ;
1281
1433
assert_eq ! (
1282
- DescriptorSecretKey :: from_str( secret_key) ,
1283
- Err ( DescriptorKeyParseError ( "Error while parsing xkey." ) )
1434
+ DescriptorSecretKey :: from_str( secret_key)
1435
+ . unwrap_err( )
1436
+ . to_string( ) ,
1437
+ "error while parsing BIP32 Xpriv: unknown version magic bytes: [4, 136, 178, 30]"
1284
1438
) ;
1285
1439
1286
1440
// And ones with invalid fingerprints
1287
1441
let desc = "[NonHexor]tprv8ZgxMBicQKsPcwcD4gSnMti126ZiETsuX7qwrtMypr6FBwAP65puFn4v6c3jrN9VwtMRMph6nyT63NrfUL4C3nBzPcduzVSuHD7zbX2JKVc/1/*" ;
1288
1442
assert_eq ! (
1289
- DescriptorSecretKey :: from_str( desc) ,
1290
- Err ( DescriptorKeyParseError ( "Malformed master fingerprint, expected 8 hex chars" ) )
1443
+ DescriptorSecretKey :: from_str( desc) . unwrap_err ( ) . to_string ( ) ,
1444
+ "error while parsing master fingerprint 'NonHexor': failed to parse hex digit"
1291
1445
) ;
1292
1446
1293
1447
// ..or invalid raw keys
1294
1448
let desc = "[78412e3a]L32jTfVLei6BYTPUpwpJSkrHx8iL9GZzeErVS8y4Y/1/*" ;
1295
1449
assert_eq ! (
1296
- DescriptorSecretKey :: from_str( desc) ,
1297
- Err ( DescriptorKeyParseError ( "Error while parsing a WIF private key" ) )
1450
+ DescriptorSecretKey :: from_str( desc) . unwrap_err ( ) . to_string ( ) ,
1451
+ "error while parsing WIF private key: invalid base58"
1298
1452
) ;
1299
1453
}
1300
1454
0 commit comments