From ac462897b1bc7ac6863a2b85ca7732e030838189 Mon Sep 17 00:00:00 2001 From: Tobin Harding Date: Fri, 25 Feb 2022 08:59:54 +0000 Subject: [PATCH] Remove hungarian-ish notation The functions `from_u32_standard` and `from_u32_consensus` smell a bit like hungarian notation. We can look at the method definition to see that the methods accept `u32` arguments without mentioning that in the method names. Remove `_u32_` from the method names. This brings the `from_*` methods in line with the `to_standard` method also. --- src/blockdata/transaction.rs | 34 +++++++++++++++++++++++++--------- src/util/ecdsa.rs | 4 ++-- src/util/psbt/map/input.rs | 2 +- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index 70260420..e5fb7c05 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -444,7 +444,7 @@ impl Transaction { } fn is_invalid_use_of_sighash_single(&self, sighash: u32, input_index: usize) -> bool { - let ty = EcdsaSigHashType::from_u32_consensus(sighash); + let ty = EcdsaSigHashType::from_consensus(sighash); ty == EcdsaSigHashType::Single && input_index >= self.output.len() } @@ -806,15 +806,22 @@ impl EcdsaSigHashType { } } - /// Reads a 4-byte uint32 as a sighash type. + /// Creates a [`EcdsaSigHashType`] from a raw `u32`. + #[deprecated(since="0.28.0", note="please use `from_consensus`")] + pub fn from_u32_consensus(n: u32) -> EcdsaSigHashType { + EcdsaSigHashType::from_consensus(n) + } + + /// Creates a [`EcdsaSigHashType`] from a raw `u32`. /// /// **Note**: this replicates consensus behaviour, for current standardness rules correctness - /// you probably want [Self::from_u32_standard]. + /// you probably want [`Self::from_standard`]. + /// /// This might cause unexpected behavior because it does not roundtrip. That is, - /// `EcdsaSigHashType::from_u32_consensus(n) as u32 != n` for non-standard values of - /// `n`. While verifying signatures, the user should retain the `n` and use it compute the - /// signature hash message. - pub fn from_u32_consensus(n: u32) -> EcdsaSigHashType { + /// `EcdsaSigHashType::from_consensus(n) as u32 != n` for non-standard values of `n`. While + /// verifying signatures, the user should retain the `n` and use it compute the signature hash + /// message. + pub fn from_consensus(n: u32) -> EcdsaSigHashType { // In Bitcoin Core, the SignatureHash function will mask the (int32) value with // 0x1f to (apparently) deactivate ACP when checking for SINGLE and NONE bits. // We however want to be matching also against on ACP-masked ALL, SINGLE, and NONE. @@ -834,9 +841,18 @@ impl EcdsaSigHashType { } } - /// Read a 4-byte uint32 as a standard sighash type, returning an error if the type - /// is non standard. + /// Creates a [`EcdsaSigHashType`] from a raw `u32`. + #[deprecated(since="0.28.0", note="please use `from_standard`")] pub fn from_u32_standard(n: u32) -> Result { + EcdsaSigHashType::from_standard(n) + } + + /// Creates a [`EcdsaSigHashType`] from a raw `u32`. + /// + /// # Errors + /// + /// If `n` is a non-standard sighash value. + pub fn from_standard(n: u32) -> Result { match n { // Standard sighashes, see https://github.com/bitcoin/bitcoin/blob/b805dbb0b9c90dadef0424e5b3bf86ac308e103e/src/script/interpreter.cpp#L189-L198 0x01 => Ok(EcdsaSigHashType::All), diff --git a/src/util/ecdsa.rs b/src/util/ecdsa.rs index 5a04b1cc..e4861769 100644 --- a/src/util/ecdsa.rs +++ b/src/util/ecdsa.rs @@ -47,7 +47,7 @@ impl EcdsaSig { pub fn from_slice(sl: &[u8]) -> Result { let (hash_ty, sig) = sl.split_last() .ok_or(EcdsaSigError::EmptySignature)?; - let hash_ty = EcdsaSigHashType::from_u32_standard(*hash_ty as u32) + let hash_ty = EcdsaSigHashType::from_standard(*hash_ty as u32) .map_err(|_| EcdsaSigError::NonStandardSigHashType(*hash_ty as u32))?; let sig = secp256k1::ecdsa::Signature::from_der(sig) .map_err(EcdsaSigError::Secp256k1)?; @@ -80,7 +80,7 @@ impl FromStr for EcdsaSig { .ok_or(EcdsaSigError::EmptySignature)?; Ok(EcdsaSig { sig: secp256k1::ecdsa::Signature::from_der(signature)?, - hash_ty: EcdsaSigHashType::from_u32_standard(*sighash_byte as u32)? + hash_ty: EcdsaSigHashType::from_standard(*sighash_byte as u32)? }) } } diff --git a/src/util/psbt/map/input.rs b/src/util/psbt/map/input.rs index e89fa9c9..9d275cbe 100644 --- a/src/util/psbt/map/input.rs +++ b/src/util/psbt/map/input.rs @@ -171,7 +171,7 @@ impl PsbtSigHashType { /// Returns the [`EcdsaSigHashType`] if the [`PsbtSigHashType`] can be /// converted to one. pub fn ecdsa_hash_ty(self) -> Result { - EcdsaSigHashType::from_u32_standard(self.inner) + EcdsaSigHashType::from_standard(self.inner) } /// Returns the [`SchnorrSigHashType`] if the [`PsbtSigHashType`] can be