@@ -720,6 +720,60 @@ impl<'de> Deserialize<'de> for Nonce {
720720 }
721721}
722722
723+ /// Error decoding hexadecimal string into tweak-like value.
724+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
725+ pub enum TweakHexDecodeError {
726+ /// Invalid hexadecimal string.
727+ InvalidHex ( hex_conservative:: DecodeFixedLengthBytesError ) ,
728+ /// Invalid tweak after decoding hexadecimal string.
729+ InvalidTweak ( secp256k1_zkp:: Error ) ,
730+ }
731+
732+ impl fmt:: Display for TweakHexDecodeError {
733+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
734+ match self {
735+ TweakHexDecodeError :: InvalidHex ( err) => {
736+ write ! ( f, "Invalid hex: {}" , err)
737+ }
738+ TweakHexDecodeError :: InvalidTweak ( err) => {
739+ write ! ( f, "Invalid tweak: {}" , err)
740+ }
741+ }
742+ }
743+ }
744+
745+ #[ doc( hidden) ]
746+ impl From < hex_conservative:: DecodeFixedLengthBytesError > for TweakHexDecodeError {
747+ fn from ( err : hex_conservative:: DecodeFixedLengthBytesError ) -> Self {
748+ TweakHexDecodeError :: InvalidHex ( err)
749+ }
750+ }
751+
752+ #[ doc( hidden) ]
753+ impl From < secp256k1_zkp:: Error > for TweakHexDecodeError {
754+ fn from ( err : secp256k1_zkp:: Error ) -> Self {
755+ TweakHexDecodeError :: InvalidTweak ( err)
756+ }
757+ }
758+
759+ impl From < TweakHexDecodeError > for encode:: Error {
760+ fn from ( value : TweakHexDecodeError ) -> Self {
761+ match value {
762+ TweakHexDecodeError :: InvalidHex ( err) => encode:: Error :: HexFixedError ( err) ,
763+ TweakHexDecodeError :: InvalidTweak ( err) => encode:: Error :: Secp256k1zkp ( err) ,
764+ }
765+ }
766+ }
767+
768+ impl std:: error:: Error for TweakHexDecodeError {
769+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
770+ match self {
771+ TweakHexDecodeError :: InvalidHex ( err) => Some ( err) ,
772+ TweakHexDecodeError :: InvalidTweak ( err) => Some ( err) ,
773+ }
774+ }
775+ }
776+
723777/// Blinding factor used for asset commitments.
724778#[ derive( Copy , Clone , Debug , Eq , PartialEq , PartialOrd , Ord , Hash ) ]
725779pub struct AssetBlindingFactor ( pub ( crate ) Tweak ) ;
@@ -747,16 +801,13 @@ impl AssetBlindingFactor {
747801}
748802
749803impl hex:: FromHex for AssetBlindingFactor {
750- fn from_byte_iter < I > ( iter : I ) -> Result < Self , hex:: Error >
751- where I : Iterator < Item =Result < u8 , hex:: Error > > +
752- ExactSizeIterator +
753- DoubleEndedIterator
754- {
755- let slice = <[ u8 ; 32 ] >:: from_byte_iter ( iter. rev ( ) ) ?;
756- // Incorrect Return Error
757- // See: https://github.com/rust-bitcoin/bitcoin_hashes/issues/124
758- let inner = Tweak :: from_inner ( slice)
759- . map_err ( |_e| hex:: Error :: InvalidChar ( 0 ) ) ?;
804+ type Err = TweakHexDecodeError ;
805+
806+ fn from_hex ( s : & str ) -> Result < Self , Self :: Err > {
807+ let mut slice: [ u8 ; 32 ] = hex_conservative:: decode_to_array ( s) ?;
808+ slice. reverse ( ) ;
809+
810+ let inner = Tweak :: from_inner ( slice) ?;
760811 Ok ( AssetBlindingFactor ( inner) )
761812 }
762813}
@@ -951,16 +1002,13 @@ impl Neg for ValueBlindingFactor {
9511002}
9521003
9531004impl hex:: FromHex for ValueBlindingFactor {
954- fn from_byte_iter < I > ( iter : I ) -> Result < Self , hex:: Error >
955- where I : Iterator < Item =Result < u8 , hex:: Error > > +
956- ExactSizeIterator +
957- DoubleEndedIterator
958- {
959- let slice = <[ u8 ; 32 ] >:: from_byte_iter ( iter. rev ( ) ) ?;
960- // Incorrect Return Error
961- // See: https://github.com/rust-bitcoin/bitcoin_hashes/issues/124
962- let inner = Tweak :: from_inner ( slice)
963- . map_err ( |_e| hex:: Error :: InvalidChar ( 0 ) ) ?;
1005+ type Err = TweakHexDecodeError ;
1006+
1007+ fn from_hex ( s : & str ) -> Result < Self , Self :: Err > {
1008+ let mut slice: [ u8 ; 32 ] = hex_conservative:: decode_to_array ( s) ?;
1009+ slice. reverse ( ) ;
1010+
1011+ let inner = Tweak :: from_inner ( slice) ?;
9641012 Ok ( ValueBlindingFactor ( inner) )
9651013 }
9661014}
0 commit comments