Skip to content

Commit

Permalink
Rename SchnorrSighashType::from_u8 -> from_consensus_u8
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tcharding committed Jun 24, 2022
1 parent ecf3bf2 commit b162ea1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/util/psbt/map/input.rs
Expand Up @@ -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)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/util/schnorr.rs
Expand Up @@ -18,6 +18,7 @@
//!

use core::fmt;

use crate::prelude::*;

use secp256k1::{self, Secp256k1, Verification, constants};
Expand Down Expand Up @@ -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)?;
Expand Down
32 changes: 20 additions & 12 deletions src/util/sighash.rs
Expand Up @@ -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)),
})
}
}

Expand Down Expand Up @@ -1114,7 +1122,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());
Expand All @@ -1125,7 +1133,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)
};

Expand Down

0 comments on commit b162ea1

Please sign in to comment.