Rename PsbtSigHashType -> PsbtSighashType

Our usage of `SigHash` implies that 'sighash' is _two_ words; 'sighash'
is a well known word in the Bitcoin ecosystem it should appear in
identifiers as `Sighash`.

Rename `PsbtSigHashType` to `PsbtSighashType`.
This commit is contained in:
Tobin Harding 2022-03-29 08:58:07 +11:00
parent c19ec339ef
commit e37652578b
4 changed files with 33 additions and 33 deletions

View File

@ -95,7 +95,7 @@ pub struct Input {
pub partial_sigs: BTreeMap<PublicKey, EcdsaSig>, pub partial_sigs: BTreeMap<PublicKey, EcdsaSig>,
/// The sighash type to be used for this input. Signatures for this input /// The sighash type to be used for this input. Signatures for this input
/// must use the sighash type. /// must use the sighash type.
pub sighash_type: Option<PsbtSigHashType>, pub sighash_type: Option<PsbtSighashType>,
/// The redeem script for this input. /// The redeem script for this input.
pub redeem_script: Option<Script>, pub redeem_script: Option<Script>,
/// The witness script for this input. /// The witness script for this input.
@ -150,14 +150,14 @@ pub struct Input {
/// A Signature hash type for the corresponding input. As of taproot upgrade, the signature hash /// A Signature hash type for the corresponding input. As of taproot upgrade, the signature hash
/// type can be either [`EcdsaSighashType`] or [`SchnorrSighashType`] but it is not possible to know /// type can be either [`EcdsaSighashType`] or [`SchnorrSighashType`] but it is not possible to know
/// directly which signature hash type the user is dealing with. Therefore, the user is responsible /// directly which signature hash type the user is dealing with. Therefore, the user is responsible
/// for converting to/from [`PsbtSigHashType`] from/to the desired signature hash type they need. /// for converting to/from [`PsbtSighashType`] from/to the desired signature hash type they need.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PsbtSigHashType { pub struct PsbtSighashType {
pub (in ::util::psbt) inner: u32, pub (in ::util::psbt) inner: u32,
} }
impl fmt::Display for PsbtSigHashType { impl fmt::Display for PsbtSighashType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.schnorr_hash_ty() { match self.schnorr_hash_ty() {
Ok(SchnorrSighashType::Reserved) | Err(_) => write!(f, "{:#x}", self.inner), Ok(SchnorrSighashType::Reserved) | Err(_) => write!(f, "{:#x}", self.inner),
@ -166,7 +166,7 @@ impl fmt::Display for PsbtSigHashType {
} }
} }
impl FromStr for PsbtSigHashType { impl FromStr for PsbtSighashType {
type Err = SighashTypeParseError; type Err = SighashTypeParseError;
#[inline] #[inline]
@ -185,32 +185,32 @@ impl FromStr for PsbtSigHashType {
// We accept non-standard sighash values. // We accept non-standard sighash values.
// TODO: Swap `trim_left_matches` for `trim_start_matches` once MSRV >= 1.30. // TODO: Swap `trim_left_matches` for `trim_start_matches` once MSRV >= 1.30.
if let Ok(inner) = u32::from_str_radix(s.trim_left_matches("0x"), 16) { if let Ok(inner) = u32::from_str_radix(s.trim_left_matches("0x"), 16) {
return Ok(PsbtSigHashType { inner }); return Ok(PsbtSighashType { inner });
} }
Err(SighashTypeParseError{ unrecognized: s.to_owned() }) Err(SighashTypeParseError{ unrecognized: s.to_owned() })
} }
} }
impl From<EcdsaSighashType> for PsbtSigHashType { impl From<EcdsaSighashType> for PsbtSighashType {
fn from(ecdsa_hash_ty: EcdsaSighashType) -> Self { fn from(ecdsa_hash_ty: EcdsaSighashType) -> Self {
PsbtSigHashType { inner: ecdsa_hash_ty as u32 } PsbtSighashType { inner: ecdsa_hash_ty as u32 }
} }
} }
impl From<SchnorrSighashType> for PsbtSigHashType { impl From<SchnorrSighashType> for PsbtSighashType {
fn from(schnorr_hash_ty: SchnorrSighashType) -> Self { fn from(schnorr_hash_ty: SchnorrSighashType) -> Self {
PsbtSigHashType { inner: schnorr_hash_ty as u32 } PsbtSighashType { inner: schnorr_hash_ty as u32 }
} }
} }
impl PsbtSigHashType { impl PsbtSighashType {
/// Returns the [`EcdsaSighashType`] if the [`PsbtSigHashType`] can be /// Returns the [`EcdsaSighashType`] if the [`PsbtSighashType`] can be
/// converted to one. /// converted to one.
pub fn ecdsa_hash_ty(self) -> Result<EcdsaSighashType, NonStandardSighashType> { pub fn ecdsa_hash_ty(self) -> Result<EcdsaSighashType, NonStandardSighashType> {
EcdsaSighashType::from_standard(self.inner) EcdsaSighashType::from_standard(self.inner)
} }
/// Returns the [`SchnorrSighashType`] if the [`PsbtSigHashType`] can be /// Returns the [`SchnorrSighashType`] if the [`PsbtSighashType`] can be
/// converted to one. /// converted to one.
pub fn schnorr_hash_ty(self) -> Result<SchnorrSighashType, sighash::Error> { pub fn schnorr_hash_ty(self) -> Result<SchnorrSighashType, sighash::Error> {
if self.inner > 0xffu32 { if self.inner > 0xffu32 {
@ -220,16 +220,16 @@ impl PsbtSigHashType {
} }
} }
/// Creates a [`PsbtSigHashType`] from a raw `u32`. /// Creates a [`PsbtSighashType`] from a raw `u32`.
/// ///
/// Allows construction of a non-standard or non-valid sighash flag /// Allows construction of a non-standard or non-valid sighash flag
/// ([`EcdsaSighashType`], [`SchnorrSighashType`] respectively). /// ([`EcdsaSighashType`], [`SchnorrSighashType`] respectively).
pub fn from_u32(n: u32) -> PsbtSigHashType { pub fn from_u32(n: u32) -> PsbtSighashType {
PsbtSigHashType { inner: n } PsbtSighashType { inner: n }
} }
/// Converts [`PsbtSigHashType`] to a raw `u32` sighash flag. /// Converts [`PsbtSighashType`] to a raw `u32` sighash flag.
/// ///
/// No guarantees are made as to the standardness or validity of the returned value. /// No guarantees are made as to the standardness or validity of the returned value.
pub fn to_u32(self) -> u32 { pub fn to_u32(self) -> u32 {
@ -286,7 +286,7 @@ impl Input {
} }
PSBT_IN_SIGHASH_TYPE => { PSBT_IN_SIGHASH_TYPE => {
impl_psbt_insert_pair! { impl_psbt_insert_pair! {
self.sighash_type <= <raw_key: _>|<raw_value: PsbtSigHashType> self.sighash_type <= <raw_key: _>|<raw_value: PsbtSighashType>
} }
} }
PSBT_IN_REDEEM_SCRIPT => { PSBT_IN_REDEEM_SCRIPT => {
@ -551,9 +551,9 @@ mod test {
EcdsaSighashType::NonePlusAnyoneCanPay, EcdsaSighashType::NonePlusAnyoneCanPay,
EcdsaSighashType::SinglePlusAnyoneCanPay, EcdsaSighashType::SinglePlusAnyoneCanPay,
] { ] {
let sighash = PsbtSigHashType::from(*ecdsa); let sighash = PsbtSighashType::from(*ecdsa);
let s = format!("{}", sighash); let s = format!("{}", sighash);
let back = PsbtSigHashType::from_str(&s).unwrap(); let back = PsbtSighashType::from_str(&s).unwrap();
assert_eq!(back, sighash); assert_eq!(back, sighash);
assert_eq!(back.ecdsa_hash_ty().unwrap(), *ecdsa); assert_eq!(back.ecdsa_hash_ty().unwrap(), *ecdsa);
} }
@ -570,9 +570,9 @@ mod test {
SchnorrSighashType::NonePlusAnyoneCanPay, SchnorrSighashType::NonePlusAnyoneCanPay,
SchnorrSighashType::SinglePlusAnyoneCanPay, SchnorrSighashType::SinglePlusAnyoneCanPay,
] { ] {
let sighash = PsbtSigHashType::from(*schnorr); let sighash = PsbtSighashType::from(*schnorr);
let s = format!("{}", sighash); let s = format!("{}", sighash);
let back = PsbtSigHashType::from_str(&s).unwrap(); let back = PsbtSighashType::from_str(&s).unwrap();
assert_eq!(back, sighash); assert_eq!(back, sighash);
assert_eq!(back.schnorr_hash_ty().unwrap(), *schnorr); assert_eq!(back.schnorr_hash_ty().unwrap(), *schnorr);
} }
@ -583,10 +583,10 @@ mod test {
for (schnorr, schnorr_str) in &[ for (schnorr, schnorr_str) in &[
(SchnorrSighashType::Reserved, "0xff"), (SchnorrSighashType::Reserved, "0xff"),
] { ] {
let sighash = PsbtSigHashType::from(*schnorr); let sighash = PsbtSighashType::from(*schnorr);
let s = format!("{}", sighash); let s = format!("{}", sighash);
assert_eq!(&s, schnorr_str); assert_eq!(&s, schnorr_str);
let back = PsbtSigHashType::from_str(&s).unwrap(); let back = PsbtSighashType::from_str(&s).unwrap();
assert_eq!(back, sighash); assert_eq!(back, sighash);
assert_eq!(back.schnorr_hash_ty().unwrap(), *schnorr); assert_eq!(back.schnorr_hash_ty().unwrap(), *schnorr);
} }
@ -595,9 +595,9 @@ mod test {
#[test] #[test]
fn psbt_sighash_type_notstd() { fn psbt_sighash_type_notstd() {
let nonstd = 0xdddddddd; let nonstd = 0xdddddddd;
let sighash = PsbtSigHashType { inner: nonstd }; let sighash = PsbtSighashType { inner: nonstd };
let s = format!("{}", sighash); let s = format!("{}", sighash);
let back = PsbtSigHashType::from_str(&s).unwrap(); let back = PsbtSighashType::from_str(&s).unwrap();
assert_eq!(back, sighash); assert_eq!(back, sighash);
assert_eq!(back.ecdsa_hash_ty(), Err(NonStandardSighashType(nonstd))); assert_eq!(back.ecdsa_hash_ty(), Err(NonStandardSighashType(nonstd)));

View File

@ -23,7 +23,7 @@ mod global;
mod input; mod input;
mod output; mod output;
pub use self::input::{Input, PsbtSigHashType}; pub use self::input::{Input, PsbtSighashType};
pub use self::output::{Output, TapTree}; pub use self::output::{Output, TapTree};
/// A trait that describes a PSBT key-value map. /// A trait that describes a PSBT key-value map.

View File

@ -41,7 +41,7 @@ mod macros;
pub mod serialize; pub mod serialize;
mod map; mod map;
pub use self::map::{Input, Output, TapTree, PsbtSigHashType}; pub use self::map::{Input, Output, TapTree, PsbtSighashType};
use self::map::Map; use self::map::Map;
use util::bip32::{ExtendedPubKey, KeySource}; use util::bip32::{ExtendedPubKey, KeySource};

View File

@ -35,7 +35,7 @@ use util::taproot::{TapBranchHash, TapLeafHash, ControlBlock, LeafVersion};
use schnorr; use schnorr;
use util::key::PublicKey; use util::key::PublicKey;
use super::map::{TapTree, PsbtSigHashType}; use super::map::{TapTree, PsbtSighashType};
use util::taproot::TaprootBuilder; use util::taproot::TaprootBuilder;
/// A trait for serializing a value as raw data for insertion into PSBT /// A trait for serializing a value as raw data for insertion into PSBT
@ -191,16 +191,16 @@ impl Deserialize for Vec<u8> {
} }
} }
impl Serialize for PsbtSigHashType { impl Serialize for PsbtSighashType {
fn serialize(&self) -> Vec<u8> { fn serialize(&self) -> Vec<u8> {
serialize(&self.to_u32()) serialize(&self.to_u32())
} }
} }
impl Deserialize for PsbtSigHashType { impl Deserialize for PsbtSighashType {
fn deserialize(bytes: &[u8]) -> Result<Self, encode::Error> { fn deserialize(bytes: &[u8]) -> Result<Self, encode::Error> {
let raw: u32 = encode::deserialize(bytes)?; let raw: u32 = encode::deserialize(bytes)?;
Ok(PsbtSigHashType { inner: raw }) Ok(PsbtSighashType { inner: raw })
} }
} }
@ -375,7 +375,7 @@ mod tests {
#[test] #[test]
fn can_deserialize_non_standard_psbt_sig_hash_type() { fn can_deserialize_non_standard_psbt_sig_hash_type() {
let non_standard_sighash = [222u8, 0u8, 0u8, 0u8]; // 32 byte value. let non_standard_sighash = [222u8, 0u8, 0u8, 0u8]; // 32 byte value.
let sighash = PsbtSigHashType::deserialize(&non_standard_sighash); let sighash = PsbtSighashType::deserialize(&non_standard_sighash);
assert!(sighash.is_ok()) assert!(sighash.is_ok())
} }
} }