From baba0fde571591eff59329b89a266b3f8afb4320 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 18 May 2023 15:28:31 +1000 Subject: [PATCH] Put NonStandardSighashTypeError inside ecdsa::Error variant As per convention; put the error type inside a variant and delegate to it instead of carrying an integer around. --- bitcoin/src/crypto/ecdsa.rs | 15 +++++++-------- bitcoin/src/psbt/serialize.rs | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/bitcoin/src/crypto/ecdsa.rs b/bitcoin/src/crypto/ecdsa.rs index 1b0c3c69..d6981f47 100644 --- a/bitcoin/src/crypto/ecdsa.rs +++ b/bitcoin/src/crypto/ecdsa.rs @@ -37,8 +37,7 @@ impl Signature { /// Deserializes from slice following the standardness rules for [`EcdsaSighashType`]. pub fn from_slice(sl: &[u8]) -> Result { let (hash_ty, sig) = sl.split_last().ok_or(Error::EmptySignature)?; - let hash_ty = EcdsaSighashType::from_standard(*hash_ty as u32) - .map_err(|_| Error::NonStandardSighashType(*hash_ty as u32))?; + let hash_ty = EcdsaSighashType::from_standard(*hash_ty as u32)?; let sig = secp256k1::ecdsa::Signature::from_der(sig).map_err(Error::Secp256k1)?; Ok(Signature { sig, hash_ty }) } @@ -189,8 +188,8 @@ impl<'a> IntoIterator for &'a SerializedSignature { pub enum Error { /// Hex decoding error Hex(hex::HexToBytesError), - /// Base58 encoding error - NonStandardSighashType(u32), + /// Non-standard sighash type + SighashType(NonStandardSighashTypeError), /// Empty Signature EmptySignature, /// secp256k1-related error @@ -203,8 +202,7 @@ impl fmt::Display for Error { match *self { Hex(ref e) => write_err!(f, "signature hex decoding error"; e), - NonStandardSighashType(hash_ty) => - write!(f, "non-standard signature hash type {}", hash_ty), + SighashType(ref e) => write_err!(f, "non-standard signature hash type"; e), EmptySignature => write!(f, "empty ECDSA signature"), Secp256k1(ref e) => write_err!(f, "invalid ECDSA signature"; e), } @@ -219,7 +217,8 @@ impl std::error::Error for Error { match self { Hex(e) => Some(e), Secp256k1(e) => Some(e), - NonStandardSighashType(_) | EmptySignature => None, + SighashType(e) => Some(e), + EmptySignature => None, } } } @@ -229,7 +228,7 @@ impl From for Error { } impl From for Error { - fn from(err: NonStandardSighashTypeError) -> Self { Error::NonStandardSighashType(err.0) } + fn from(err: NonStandardSighashTypeError) -> Self { Error::SighashType(err) } } impl From for Error { diff --git a/bitcoin/src/psbt/serialize.rs b/bitcoin/src/psbt/serialize.rs index dfca1b6c..0d5deb61 100644 --- a/bitcoin/src/psbt/serialize.rs +++ b/bitcoin/src/psbt/serialize.rs @@ -176,7 +176,7 @@ impl Deserialize for ecdsa::Signature { // would use check the signature assuming sighash_u32 as `0x01`. ecdsa::Signature::from_slice(bytes).map_err(|e| match e { ecdsa::Error::EmptySignature => Error::InvalidEcdsaSignature(e), - ecdsa::Error::NonStandardSighashType(flag) => Error::NonStandardSighashType(flag), + ecdsa::Error::SighashType(err) => Error::NonStandardSighashType(err.0), ecdsa::Error::Secp256k1(..) => Error::InvalidEcdsaSignature(e), ecdsa::Error::Hex(..) => unreachable!("Decoding from slice, not hex"), })