@@ -447,66 +447,104 @@ fn fmt_derivation_paths(f: &mut fmt::Formatter, paths: &[bip32::DerivationPath])
447447 Ok ( ( ) )
448448}
449449
450- impl FromStr for DescriptorPublicKey {
450+ impl FromStr for DescriptorSinglePublicKey {
451+ type Err = DescriptorKeyParseError ;
452+
453+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
454+ let ( key_part, origin) = parse_key_origin ( s) ?;
455+
456+ let key = match key_part. len ( ) {
457+ 64 => {
458+ let x_only_key = XOnlyPublicKey :: from_str ( key_part) . map_err ( |_| {
459+ DescriptorKeyParseError ( "Error while parsing simple xonly key" )
460+ } ) ?;
461+ SinglePubKey :: XOnly ( x_only_key)
462+ }
463+ 66 | 130 => {
464+ if !( & key_part[ 0 ..2 ] == "02"
465+ || & key_part[ 0 ..2 ] == "03"
466+ || & key_part[ 0 ..2 ] == "04" )
467+ {
468+ return Err ( DescriptorKeyParseError (
469+ "Only publickeys with prefixes 02/03/04 are allowed" ,
470+ ) ) ;
471+ }
472+ let key = bitcoin:: PublicKey :: from_str ( key_part) . map_err ( |_| {
473+ DescriptorKeyParseError ( "Error while parsing simple public key" )
474+ } ) ?;
475+ SinglePubKey :: FullKey ( key)
476+ }
477+ _ => {
478+ return Err ( DescriptorKeyParseError (
479+ "Public keys must be 64/66/130 characters in size" ,
480+ ) )
481+ }
482+ } ;
483+
484+ Ok ( Self { key, origin } )
485+ }
486+ }
487+
488+ impl FromStr for DescriptorExtendedPublicKey {
451489 type Err = DescriptorKeyParseError ;
452490
453491 fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
454- // A "raw" public key without any origin is the least we accept.
455- if s. len ( ) < 64 {
492+ let ( key_part, origin) = parse_key_origin ( s) ?;
493+
494+ let ( xpub, derivation_paths, wildcard) =
495+ parse_xkey_deriv :: < bip32:: ExtendedPubKey > ( key_part) ?;
496+
497+ if derivation_paths. len ( ) > 1 {
456498 return Err ( DescriptorKeyParseError (
457- "Key too short (<66 char), doesn't match any format " ,
499+ "Multiple derivation paths are not allowed for single extended keys " ,
458500 ) ) ;
459501 }
460502
503+ Ok ( Self {
504+ origin,
505+ xkey : xpub,
506+ derivation_path : derivation_paths. into_iter ( ) . next ( ) . unwrap_or_default ( ) ,
507+ wildcard,
508+ } )
509+ }
510+ }
511+
512+ impl FromStr for DescriptorMultiExtendedPublicKey {
513+ type Err = DescriptorKeyParseError ;
514+
515+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
461516 let ( key_part, origin) = parse_key_origin ( s) ?;
462517
463- if key_part. contains ( "pub" ) {
464- let ( xpub, derivation_paths, wildcard) =
465- parse_xkey_deriv :: < bip32:: ExtendedPubKey > ( key_part) ?;
466- if derivation_paths. len ( ) > 1 {
467- Ok ( DescriptorPublicKey :: MultiXPub ( DescriptorMultiXKey {
468- origin,
469- xkey : xpub,
470- derivation_paths : DerivPaths :: new ( derivation_paths) . expect ( "Not empty" ) ,
471- wildcard,
472- } ) )
518+ let ( xpub, derivation_paths, wildcard) =
519+ parse_xkey_deriv :: < bip32:: ExtendedPubKey > ( key_part) ?;
520+
521+ if derivation_paths. len ( ) < 2 {
522+ return Err ( DescriptorKeyParseError (
523+ "Multiple derivation paths are required for multi extended keys" ,
524+ ) ) ;
525+ }
526+
527+ Ok ( Self {
528+ origin,
529+ xkey : xpub,
530+ derivation_paths : DerivPaths :: new ( derivation_paths) . expect ( "Not empty" ) ,
531+ wildcard,
532+ } )
533+ }
534+ }
535+
536+ impl FromStr for DescriptorPublicKey {
537+ type Err = DescriptorKeyParseError ;
538+
539+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
540+ if s. contains ( "pub" ) {
541+ if s. contains ( "<" ) {
542+ DescriptorMultiExtendedPublicKey :: from_str ( s) . map ( Self :: MultiXPub )
473543 } else {
474- Ok ( DescriptorPublicKey :: XPub ( DescriptorXKey {
475- origin,
476- xkey : xpub,
477- derivation_path : derivation_paths. into_iter ( ) . next ( ) . unwrap_or_default ( ) ,
478- wildcard,
479- } ) )
544+ DescriptorExtendedPublicKey :: from_str ( s) . map ( Self :: XPub )
480545 }
481546 } else {
482- let key = match key_part. len ( ) {
483- 64 => {
484- let x_only_key = XOnlyPublicKey :: from_str ( key_part) . map_err ( |_| {
485- DescriptorKeyParseError ( "Error while parsing simple xonly key" )
486- } ) ?;
487- SinglePubKey :: XOnly ( x_only_key)
488- }
489- 66 | 130 => {
490- if !( & key_part[ 0 ..2 ] == "02"
491- || & key_part[ 0 ..2 ] == "03"
492- || & key_part[ 0 ..2 ] == "04" )
493- {
494- return Err ( DescriptorKeyParseError (
495- "Only publickeys with prefixes 02/03/04 are allowed" ,
496- ) ) ;
497- }
498- let key = bitcoin:: PublicKey :: from_str ( key_part) . map_err ( |_| {
499- DescriptorKeyParseError ( "Error while parsing simple public key" )
500- } ) ?;
501- SinglePubKey :: FullKey ( key)
502- }
503- _ => {
504- return Err ( DescriptorKeyParseError (
505- "Public keys must be 64/66/130 characters in size" ,
506- ) )
507- }
508- } ;
509- Ok ( DescriptorPublicKey :: Single ( SinglePub { key, origin } ) )
547+ DescriptorSinglePublicKey :: from_str ( s) . map ( Self :: Single )
510548 }
511549 }
512550}
0 commit comments