From 22c7aa880850d966d336193e9a4b91e1c3e05a9f Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 18 May 2023 15:11:26 +1000 Subject: [PATCH] Rename non standard sighash error type Error types conventionally include `Error` as a suffix. Rename `NonStandardSighashType` to `NonStandardSighashTypeError`. While we are at it make the inner type private to the crate, there is no need to leak the inner values type. --- bitcoin/src/crypto/ecdsa.rs | 6 +++--- bitcoin/src/crypto/sighash.rs | 10 +++++----- bitcoin/src/psbt/map/input.rs | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/bitcoin/src/crypto/ecdsa.rs b/bitcoin/src/crypto/ecdsa.rs index 8f295518..1bdb347d 100644 --- a/bitcoin/src/crypto/ecdsa.rs +++ b/bitcoin/src/crypto/ecdsa.rs @@ -13,7 +13,7 @@ use secp256k1; use crate::prelude::*; use crate::script::PushBytes; -use crate::sighash::{EcdsaSighashType, NonStandardSighashType}; +use crate::sighash::{EcdsaSighashType, NonStandardSighashTypeError}; const MAX_SIG_LEN: usize = 73; @@ -226,8 +226,8 @@ impl From for Error { fn from(e: secp256k1::Error) -> Error { Error::Secp256k1(e) } } -impl From for Error { - fn from(err: NonStandardSighashType) -> Self { Error::NonStandardSighashType(err.0) } +impl From for Error { + fn from(err: NonStandardSighashTypeError) -> Self { Error::NonStandardSighashType(err.0) } } impl From for Error { diff --git a/bitcoin/src/crypto/sighash.rs b/bitcoin/src/crypto/sighash.rs index ec4a2ad7..7e5b1e11 100644 --- a/bitcoin/src/crypto/sighash.rs +++ b/bitcoin/src/crypto/sighash.rs @@ -446,7 +446,7 @@ impl EcdsaSighashType { /// # Errors /// /// If `n` is a non-standard sighash value. - pub fn from_standard(n: u32) -> Result { + pub fn from_standard(n: u32) -> Result { use EcdsaSighashType::*; match n { @@ -457,7 +457,7 @@ impl EcdsaSighashType { 0x81 => Ok(AllPlusAnyoneCanPay), 0x82 => Ok(NonePlusAnyoneCanPay), 0x83 => Ok(SinglePlusAnyoneCanPay), - non_standard => Err(NonStandardSighashType(non_standard)), + non_standard => Err(NonStandardSighashTypeError(non_standard)), } } @@ -518,15 +518,15 @@ impl TapSighashType { /// This type is consensus valid but an input including it would prevent the transaction from /// being relayed on today's Bitcoin network. #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct NonStandardSighashType(pub u32); +pub struct NonStandardSighashTypeError(pub u32); -impl fmt::Display for NonStandardSighashType { +impl fmt::Display for NonStandardSighashTypeError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Non standard sighash type {}", self.0) } } -impl_std_error!(NonStandardSighashType); +impl_std_error!(NonStandardSighashTypeError); /// Error returned for failure during parsing one of the sighash types. /// diff --git a/bitcoin/src/psbt/map/input.rs b/bitcoin/src/psbt/map/input.rs index 5e610ceb..91993e06 100644 --- a/bitcoin/src/psbt/map/input.rs +++ b/bitcoin/src/psbt/map/input.rs @@ -18,7 +18,7 @@ use crate::psbt::map::Map; use crate::psbt::serialize::Deserialize; use crate::psbt::{self, error, raw, Error}; use crate::sighash::{ - self, EcdsaSighashType, NonStandardSighashType, SighashTypeParseError, TapSighashType, + self, EcdsaSighashType, NonStandardSighashTypeError, SighashTypeParseError, TapSighashType, }; use crate::taproot::{ControlBlock, LeafVersion, TapLeafHash, TapNodeHash}; @@ -190,7 +190,7 @@ impl From for PsbtSighashType { impl PsbtSighashType { /// Returns the [`EcdsaSighashType`] if the [`PsbtSighashType`] can be /// converted to one. - pub fn ecdsa_hash_ty(self) -> Result { + pub fn ecdsa_hash_ty(self) -> Result { EcdsaSighashType::from_standard(self.inner) } @@ -223,7 +223,7 @@ impl Input { /// # Errors /// /// If the `sighash_type` field is set to a non-standard ECDSA sighash value. - pub fn ecdsa_hash_ty(&self) -> Result { + pub fn ecdsa_hash_ty(&self) -> Result { self.sighash_type .map(|sighash_type| sighash_type.ecdsa_hash_ty()) .unwrap_or(Ok(EcdsaSighashType::All)) @@ -575,7 +575,7 @@ mod test { let back = PsbtSighashType::from_str(&s).unwrap(); assert_eq!(back, sighash); - assert_eq!(back.ecdsa_hash_ty(), Err(NonStandardSighashType(nonstd))); + assert_eq!(back.ecdsa_hash_ty(), Err(sighash::NonStandardSighashTypeError(nonstd))); assert_eq!(back.taproot_hash_ty(), Err(sighash::Error::InvalidSighashType(nonstd))); } }