From 648b3975a534bd55567efab7db0ac0419af4eecc Mon Sep 17 00:00:00 2001 From: sanket1729 Date: Wed, 27 Oct 2021 06:04:23 -0700 Subject: [PATCH] Add SchnorrSigHashType::from_u8 --- src/util/sighash.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/util/sighash.rs b/src/util/sighash.rs index ae874660..4d4047c8 100644 --- a/src/util/sighash.rs +++ b/src/util/sighash.rs @@ -164,6 +164,9 @@ pub enum Error { /// Annex must be at least one byte long and the first bytes must be `0x50` WrongAnnex, + + /// Invalid Sighash type + InvalidSigHashType(u8), } 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::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::InvalidSigHashType(hash_ty) => write!(f, "Invalid schnorr Signature hash type : {} ", hash_ty), } } } @@ -256,6 +260,20 @@ impl SchnorrSigHashType { SchnorrSigHashType::Reserved => (SchnorrSigHashType::Reserved, false), } } + + /// Create a [`SchnorrSigHashType`] from raw u8 + pub fn from_u8(hash_ty: u8) -> Result { + 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> SigHashCache {