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.
This commit is contained in:
Tobin Harding 2022-02-25 08:59:54 +00:00
parent 564682627c
commit ac462897b1
3 changed files with 28 additions and 12 deletions

View File

@ -444,7 +444,7 @@ impl Transaction {
} }
fn is_invalid_use_of_sighash_single(&self, sighash: u32, input_index: usize) -> bool { 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() 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 /// **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, /// 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 /// `EcdsaSigHashType::from_consensus(n) as u32 != n` for non-standard values of `n`. While
/// `n`. While verifying signatures, the user should retain the `n` and use it compute the /// verifying signatures, the user should retain the `n` and use it compute the signature hash
/// signature hash message. /// message.
pub fn from_u32_consensus(n: u32) -> EcdsaSigHashType { pub fn from_consensus(n: u32) -> EcdsaSigHashType {
// In Bitcoin Core, the SignatureHash function will mask the (int32) value with // In Bitcoin Core, the SignatureHash function will mask the (int32) value with
// 0x1f to (apparently) deactivate ACP when checking for SINGLE and NONE bits. // 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. // 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 /// Creates a [`EcdsaSigHashType`] from a raw `u32`.
/// is non standard. #[deprecated(since="0.28.0", note="please use `from_standard`")]
pub fn from_u32_standard(n: u32) -> Result<EcdsaSigHashType, NonStandardSigHashType> { pub fn from_u32_standard(n: u32) -> Result<EcdsaSigHashType, NonStandardSigHashType> {
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<EcdsaSigHashType, NonStandardSigHashType> {
match n { match n {
// Standard sighashes, see https://github.com/bitcoin/bitcoin/blob/b805dbb0b9c90dadef0424e5b3bf86ac308e103e/src/script/interpreter.cpp#L189-L198 // Standard sighashes, see https://github.com/bitcoin/bitcoin/blob/b805dbb0b9c90dadef0424e5b3bf86ac308e103e/src/script/interpreter.cpp#L189-L198
0x01 => Ok(EcdsaSigHashType::All), 0x01 => Ok(EcdsaSigHashType::All),

View File

@ -47,7 +47,7 @@ impl EcdsaSig {
pub fn from_slice(sl: &[u8]) -> Result<Self, EcdsaSigError> { pub fn from_slice(sl: &[u8]) -> Result<Self, EcdsaSigError> {
let (hash_ty, sig) = sl.split_last() let (hash_ty, sig) = sl.split_last()
.ok_or(EcdsaSigError::EmptySignature)?; .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))?; .map_err(|_| EcdsaSigError::NonStandardSigHashType(*hash_ty as u32))?;
let sig = secp256k1::ecdsa::Signature::from_der(sig) let sig = secp256k1::ecdsa::Signature::from_der(sig)
.map_err(EcdsaSigError::Secp256k1)?; .map_err(EcdsaSigError::Secp256k1)?;
@ -80,7 +80,7 @@ impl FromStr for EcdsaSig {
.ok_or(EcdsaSigError::EmptySignature)?; .ok_or(EcdsaSigError::EmptySignature)?;
Ok(EcdsaSig { Ok(EcdsaSig {
sig: secp256k1::ecdsa::Signature::from_der(signature)?, 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)?
}) })
} }
} }

View File

@ -171,7 +171,7 @@ impl PsbtSigHashType {
/// Returns the [`EcdsaSigHashType`] if the [`PsbtSigHashType`] can be /// Returns the [`EcdsaSigHashType`] if the [`PsbtSigHashType`] can be
/// converted to one. /// converted to one.
pub fn ecdsa_hash_ty(self) -> Result<EcdsaSigHashType, NonStandardSigHashType> { pub fn ecdsa_hash_ty(self) -> Result<EcdsaSigHashType, NonStandardSigHashType> {
EcdsaSigHashType::from_u32_standard(self.inner) EcdsaSigHashType::from_standard(self.inner)
} }
/// Returns the [`SchnorrSigHashType`] if the [`PsbtSigHashType`] can be /// Returns the [`SchnorrSigHashType`] if the [`PsbtSigHashType`] can be