Add SchnorrSigHashType::from_u8

This commit is contained in:
sanket1729 2021-10-27 06:04:23 -07:00
parent 410e8bf46c
commit 648b3975a5
1 changed files with 18 additions and 0 deletions

View File

@ -164,6 +164,9 @@ pub enum Error {
/// Annex must be at least one byte long and the first bytes must be `0x50` /// Annex must be at least one byte long and the first bytes must be `0x50`
WrongAnnex, WrongAnnex,
/// Invalid Sighash type
InvalidSigHashType(u8),
} }
impl fmt::Display for Error { impl fmt::Display for Error {
@ -176,6 +179,7 @@ impl fmt::Display for Error {
Error::PrevoutIndex => write!(f, "The index requested is greater than available prevouts or different from the provided [Provided::Anyone] index"), Error::PrevoutIndex => write!(f, "The index requested is greater than available prevouts or different from the provided [Provided::Anyone] index"),
Error::PrevoutKind => write!(f, "A single prevout has been provided but all prevouts are needed without `ANYONECANPAY`"), Error::PrevoutKind => write!(f, "A single prevout has been provided but all prevouts are needed without `ANYONECANPAY`"),
Error::WrongAnnex => write!(f, "Annex must be at least one byte long and the first bytes must be `0x50`"), Error::WrongAnnex => write!(f, "Annex must be at least one byte long and the first bytes must be `0x50`"),
Error::InvalidSigHashType(hash_ty) => write!(f, "Invalid schnorr Signature hash type : {} ", hash_ty),
} }
} }
} }
@ -256,6 +260,20 @@ impl SchnorrSigHashType {
SchnorrSigHashType::Reserved => (SchnorrSigHashType::Reserved, false), SchnorrSigHashType::Reserved => (SchnorrSigHashType::Reserved, false),
} }
} }
/// Create a [`SchnorrSigHashType`] from raw u8
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)),
}
}
} }
impl<R: Deref<Target = Transaction>> SigHashCache<R> { impl<R: Deref<Target = Transaction>> SigHashCache<R> {