Rename SchnorrSighashType::from_u8 -> from_consensus_u8

The `u8` parameter in the `SchnorrSighashType` constructor is a
consensus valid `u8`. Re-name the constructor to make this explicit.

Deprecate `from_u8` as is typical.
This commit is contained in:
Tobin C. Harding 2022-06-24 12:01:53 +10:00
parent af16286679
commit b29ff9b715
3 changed files with 23 additions and 14 deletions

View File

@ -216,7 +216,7 @@ impl PsbtSighashType {
if self.inner > 0xffu32 {
Err(sighash::Error::InvalidSighashType(self.inner))
} else {
SchnorrSighashType::from_u8(self.inner as u8)
SchnorrSighashType::from_consensus_u8(self.inner as u8)
}
}

View File

@ -18,6 +18,7 @@
//!
use core::fmt;
use crate::prelude::*;
use secp256k1::{self, Secp256k1, Verification, constants};
@ -238,7 +239,7 @@ impl SchnorrSig {
},
65 => {
let (hash_ty, sig) = sl.split_last().expect("Slice len checked == 65");
let hash_ty = SchnorrSighashType::from_u8(*hash_ty)
let hash_ty = SchnorrSighashType::from_consensus_u8(*hash_ty)
.map_err(|_| SchnorrSigError::InvalidSighashType(*hash_ty))?;
let sig = secp256k1::schnorr::Signature::from_slice(sig)
.map_err(SchnorrSigError::Secp256k1)?;

View File

@ -338,17 +338,25 @@ impl SchnorrSighashType {
}
/// Creates a [`SchnorrSighashType`] from raw `u8`.
#[deprecated(since = "0.29.0", note = "use from_consensus_u8 instead")]
pub fn from_u8(hash_ty: u8) -> Result<Self, Error> {
match hash_ty {
0x00 => Ok(SchnorrSighashType::Default),
0x01 => Ok(SchnorrSighashType::All),
0x02 => Ok(SchnorrSighashType::None),
0x03 => Ok(SchnorrSighashType::Single),
0x81 => Ok(SchnorrSighashType::AllPlusAnyoneCanPay),
0x82 => Ok(SchnorrSighashType::NonePlusAnyoneCanPay),
0x83 => Ok(SchnorrSighashType::SinglePlusAnyoneCanPay),
x => Err(Error::InvalidSighashType(x as u32)),
}
Self::from_consensus_u8(hash_ty)
}
/// Constructs a [`SchnorrSighashType`] from a raw `u8`.
pub fn from_consensus_u8(hash_ty: u8) -> Result<Self, Error> {
use SchnorrSighashType::*;
Ok(match hash_ty {
0x00 => Default,
0x01 => All,
0x02 => None,
0x03 => Single,
0x81 => AllPlusAnyoneCanPay,
0x82 => NonePlusAnyoneCanPay,
0x83 => SinglePlusAnyoneCanPay,
x => return Err(Error::InvalidSighashType(x as u32)),
})
}
}
@ -1113,7 +1121,7 @@ mod tests {
} else {
Some(hex_hash!(TapBranchHash, inp["given"]["merkleRoot"].as_str().unwrap()))
};
let hash_ty = SchnorrSighashType::try_from(inp["given"]["hashType"].as_u64().unwrap() as u8).unwrap();
let hash_ty = SchnorrSighashType::from_consensus_u8(inp["given"]["hashType"].as_u64().unwrap() as u8).unwrap();
let expected_internal_pk = hex_hash!(XOnlyPublicKey, inp["intermediary"]["internalPubkey"].as_str().unwrap());
let expected_tweak = hex_hash!(TapTweakHash, inp["intermediary"]["tweak"].as_str().unwrap());
@ -1124,7 +1132,7 @@ mod tests {
let (expected_key_spend_sig, expected_hash_ty) = if sig_str.len() == 128 {
(secp256k1::schnorr::Signature::from_str(sig_str).unwrap(), SchnorrSighashType::Default)
} else {
let hash_ty = SchnorrSighashType::try_from(Vec::<u8>::from_hex(&sig_str[128..]).unwrap()[0]).unwrap();
let hash_ty = SchnorrSighashType::from_consensus_u8(Vec::<u8>::from_hex(&sig_str[128..]).unwrap()[0]).unwrap();
(secp256k1::schnorr::Signature::from_str(&sig_str[..128]).unwrap(), hash_ty)
};