diff --git a/src/util/psbt/map/input.rs b/src/util/psbt/map/input.rs index 28ee2114..f798ee16 100644 --- a/src/util/psbt/map/input.rs +++ b/src/util/psbt/map/input.rs @@ -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) } } diff --git a/src/util/schnorr.rs b/src/util/schnorr.rs index b2a711c8..8a63c7e5 100644 --- a/src/util/schnorr.rs +++ b/src/util/schnorr.rs @@ -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)?; diff --git a/src/util/sighash.rs b/src/util/sighash.rs index 81734711..54b52df9 100644 --- a/src/util/sighash.rs +++ b/src/util/sighash.rs @@ -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 { - 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 { + 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::::from_hex(&sig_str[128..]).unwrap()[0]).unwrap(); + let hash_ty = SchnorrSighashType::from_consensus_u8(Vec::::from_hex(&sig_str[128..]).unwrap()[0]).unwrap(); (secp256k1::schnorr::Signature::from_str(&sig_str[..128]).unwrap(), hash_ty) };