Skip to content

Commit ac58e6e

Browse files
committed
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.
1 parent 6f9164f commit ac58e6e

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

src/util/psbt/map/input.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl PsbtSighashType {
216216
if self.inner > 0xffu32 {
217217
Err(sighash::Error::InvalidSighashType(self.inner))
218218
} else {
219-
SchnorrSighashType::from_u8(self.inner as u8)
219+
SchnorrSighashType::from_consensus_u8(self.inner as u8)
220220
}
221221
}
222222

src/util/schnorr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
//!
1919
2020
use core::fmt;
21+
2122
use crate::prelude::*;
2223

2324
use secp256k1::{self, Secp256k1, Verification, constants};
@@ -238,7 +239,7 @@ impl SchnorrSig {
238239
},
239240
65 => {
240241
let (hash_ty, sig) = sl.split_last().expect("Slice len checked == 65");
241-
let hash_ty = SchnorrSighashType::from_u8(*hash_ty)
242+
let hash_ty = SchnorrSighashType::from_consensus_u8(*hash_ty)
242243
.map_err(|_| SchnorrSigError::InvalidSighashType(*hash_ty))?;
243244
let sig = secp256k1::schnorr::Signature::from_slice(sig)
244245
.map_err(SchnorrSigError::Secp256k1)?;

src/util/sighash.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -338,17 +338,25 @@ impl SchnorrSighashType {
338338
}
339339

340340
/// Creates a [`SchnorrSighashType`] from raw `u8`.
341+
#[deprecated(since = "0.29.0", note = "use from_consensus_u8 instead")]
341342
pub fn from_u8(hash_ty: u8) -> Result<Self, Error> {
342-
match hash_ty {
343-
0x00 => Ok(SchnorrSighashType::Default),
344-
0x01 => Ok(SchnorrSighashType::All),
345-
0x02 => Ok(SchnorrSighashType::None),
346-
0x03 => Ok(SchnorrSighashType::Single),
347-
0x81 => Ok(SchnorrSighashType::AllPlusAnyoneCanPay),
348-
0x82 => Ok(SchnorrSighashType::NonePlusAnyoneCanPay),
349-
0x83 => Ok(SchnorrSighashType::SinglePlusAnyoneCanPay),
350-
x => Err(Error::InvalidSighashType(x as u32)),
351-
}
343+
Self::from_consensus_u8(hash_ty)
344+
}
345+
346+
/// Constructs a [`SchnorrSighashType`] from a raw `u8`.
347+
pub fn from_consensus_u8(hash_ty: u8) -> Result<Self, Error> {
348+
use SchnorrSighashType::*;
349+
350+
Ok(match hash_ty {
351+
0x00 => Default,
352+
0x01 => All,
353+
0x02 => None,
354+
0x03 => Single,
355+
0x81 => AllPlusAnyoneCanPay,
356+
0x82 => NonePlusAnyoneCanPay,
357+
0x83 => SinglePlusAnyoneCanPay,
358+
x => return Err(Error::InvalidSighashType(x as u32)),
359+
})
352360
}
353361
}
354362

@@ -1113,7 +1121,7 @@ mod tests {
11131121
} else {
11141122
Some(hex_hash!(TapBranchHash, inp["given"]["merkleRoot"].as_str().unwrap()))
11151123
};
1116-
let hash_ty = SchnorrSighashType::try_from(inp["given"]["hashType"].as_u64().unwrap() as u8).unwrap();
1124+
let hash_ty = SchnorrSighashType::from_consensus_u8(inp["given"]["hashType"].as_u64().unwrap() as u8).unwrap();
11171125

11181126
let expected_internal_pk = hex_hash!(XOnlyPublicKey, inp["intermediary"]["internalPubkey"].as_str().unwrap());
11191127
let expected_tweak = hex_hash!(TapTweakHash, inp["intermediary"]["tweak"].as_str().unwrap());
@@ -1124,7 +1132,7 @@ mod tests {
11241132
let (expected_key_spend_sig, expected_hash_ty) = if sig_str.len() == 128 {
11251133
(secp256k1::schnorr::Signature::from_str(sig_str).unwrap(), SchnorrSighashType::Default)
11261134
} else {
1127-
let hash_ty = SchnorrSighashType::try_from(Vec::<u8>::from_hex(&sig_str[128..]).unwrap()[0]).unwrap();
1135+
let hash_ty = SchnorrSighashType::from_consensus_u8(Vec::<u8>::from_hex(&sig_str[128..]).unwrap()[0]).unwrap();
11281136
(secp256k1::schnorr::Signature::from_str(&sig_str[..128]).unwrap(), hash_ty)
11291137
};
11301138

0 commit comments

Comments
 (0)