Rename SchnorrSigHashType -> SchnorrSighashType

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 `SchnorrSigHashType` to `SchnorrSighashType`.
This commit is contained in:
Tobin Harding 2022-03-29 08:52:33 +11:00
parent 5522454583
commit 6caba2ed24
5 changed files with 116 additions and 116 deletions

View File

@ -43,7 +43,7 @@ use hash_types::{SigHash, Txid, Wtxid};
use VarInt; use VarInt;
#[cfg(doc)] #[cfg(doc)]
use util::sighash::SchnorrSigHashType; use util::sighash::SchnorrSighashType;
/// Used for signature hash for invalid use of SIGHASH_SINGLE. /// Used for signature hash for invalid use of SIGHASH_SINGLE.
const UINT256_ONE: [u8; 32] = [ const UINT256_ONE: [u8; 32] = [
@ -742,7 +742,7 @@ pub type SigHashType = EcdsaSighashType;
/// Hashtype of an input's signature, encoded in the last byte of the signature. /// Hashtype of an input's signature, encoded in the last byte of the signature.
/// ///
/// Fixed values so they can be cast as integer types for encoding (see also /// Fixed values so they can be cast as integer types for encoding (see also
/// [`SchnorrSigHashType`]). /// [`SchnorrSighashType`]).
#[derive(PartialEq, Eq, Debug, Copy, Clone)] #[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub enum EcdsaSighashType { pub enum EcdsaSighashType {
/// 0x1: Sign all outputs. /// 0x1: Sign all outputs.

View File

@ -134,7 +134,7 @@ pub use util::amount::Amount;
pub use util::amount::Denomination; pub use util::amount::Denomination;
pub use util::amount::SignedAmount; pub use util::amount::SignedAmount;
pub use util::merkleblock::MerkleBlock; pub use util::merkleblock::MerkleBlock;
pub use util::sighash::SchnorrSigHashType; pub use util::sighash::SchnorrSighashType;
pub use util::ecdsa::{self, EcdsaSig, EcdsaSigError}; pub use util::ecdsa::{self, EcdsaSig, EcdsaSigError};
pub use util::schnorr::{self, SchnorrSig, SchnorrSigError}; pub use util::schnorr::{self, SchnorrSig, SchnorrSigError};

View File

@ -34,7 +34,7 @@ use util::key::PublicKey;
use util::taproot::{ControlBlock, LeafVersion, TapLeafHash, TapBranchHash}; use util::taproot::{ControlBlock, LeafVersion, TapLeafHash, TapBranchHash};
use util::sighash; use util::sighash;
use {EcdsaSighashType, SchnorrSigHashType, EcdsaSig, SchnorrSig}; use {EcdsaSighashType, SchnorrSighashType, EcdsaSig, SchnorrSig};
/// Type: Non-Witness UTXO PSBT_IN_NON_WITNESS_UTXO = 0x00 /// Type: Non-Witness UTXO PSBT_IN_NON_WITNESS_UTXO = 0x00
const PSBT_IN_NON_WITNESS_UTXO: u8 = 0x00; const PSBT_IN_NON_WITNESS_UTXO: u8 = 0x00;
@ -148,7 +148,7 @@ 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)]
@ -160,7 +160,7 @@ pub struct PsbtSigHashType {
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),
Ok(schnorr_hash_ty) => fmt::Display::fmt(&schnorr_hash_ty, f), Ok(schnorr_hash_ty) => fmt::Display::fmt(&schnorr_hash_ty, f),
} }
} }
@ -176,8 +176,8 @@ impl FromStr for PsbtSigHashType {
// NB: some of Schnorr sighash types are non-standard for pre-taproot // NB: some of Schnorr sighash types are non-standard for pre-taproot
// inputs. We also do not support SIGHASH_RESERVED in verbatim form // inputs. We also do not support SIGHASH_RESERVED in verbatim form
// ("0xFF" string should be used instead). // ("0xFF" string should be used instead).
match SchnorrSigHashType::from_str(s) { match SchnorrSighashType::from_str(s) {
Ok(SchnorrSigHashType::Reserved) => return Err(SigHashTypeParseError{ unrecognized: s.to_owned() }), Ok(SchnorrSighashType::Reserved) => return Err(SigHashTypeParseError{ unrecognized: s.to_owned() }),
Ok(ty) => return Ok(ty.into()), Ok(ty) => return Ok(ty.into()),
Err(_) => {} Err(_) => {}
} }
@ -197,8 +197,8 @@ impl From<EcdsaSighashType> for PsbtSigHashType {
} }
} }
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 }
} }
} }
@ -210,20 +210,20 @@ impl PsbtSigHashType {
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 {
Err(sighash::Error::InvalidSigHashType(self.inner)) Err(sighash::Error::InvalidSigHashType(self.inner))
} else { } else {
SchnorrSigHashType::from_u8(self.inner as u8) SchnorrSighashType::from_u8(self.inner as u8)
} }
} }
/// 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 }
} }
@ -250,16 +250,16 @@ impl Input {
.unwrap_or(Ok(EcdsaSighashType::All)) .unwrap_or(Ok(EcdsaSighashType::All))
} }
/// Obtains the [`SchnorrSigHashType`] for this input if one is specified. If no sighash type is /// Obtains the [`SchnorrSighashType`] for this input if one is specified. If no sighash type is
/// specified, returns [`SchnorrSigHashType::Default`]. /// specified, returns [`SchnorrSighashType::Default`].
/// ///
/// # Errors /// # Errors
/// ///
/// If the `sighash_type` field is set to a invalid Schnorr sighash value. /// If the `sighash_type` field is set to a invalid Schnorr sighash value.
pub fn schnorr_hash_ty(&self) -> Result<SchnorrSigHashType, sighash::Error> { pub fn schnorr_hash_ty(&self) -> Result<SchnorrSighashType, sighash::Error> {
self.sighash_type self.sighash_type
.map(|sighash_type| sighash_type.schnorr_hash_ty()) .map(|sighash_type| sighash_type.schnorr_hash_ty())
.unwrap_or(Ok(SchnorrSigHashType::Default)) .unwrap_or(Ok(SchnorrSighashType::Default))
} }
pub(super) fn insert_pair(&mut self, pair: raw::Pair) -> Result<(), encode::Error> { pub(super) fn insert_pair(&mut self, pair: raw::Pair) -> Result<(), encode::Error> {
@ -562,13 +562,13 @@ mod test {
#[test] #[test]
fn psbt_sighash_type_schnorr() { fn psbt_sighash_type_schnorr() {
for schnorr in &[ for schnorr in &[
SchnorrSigHashType::Default, SchnorrSighashType::Default,
SchnorrSigHashType::All, SchnorrSighashType::All,
SchnorrSigHashType::None, SchnorrSighashType::None,
SchnorrSigHashType::Single, SchnorrSighashType::Single,
SchnorrSigHashType::AllPlusAnyoneCanPay, SchnorrSighashType::AllPlusAnyoneCanPay,
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);
@ -581,7 +581,7 @@ mod test {
#[test] #[test]
fn psbt_sighash_type_schnorr_notstd() { fn psbt_sighash_type_schnorr_notstd() {
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);

View File

@ -25,7 +25,7 @@ use secp256k1::{XOnlyPublicKey as _XOnlyPublicKey, KeyPair as _KeyPair};
use secp256k1::{self, Secp256k1, Verification, constants}; use secp256k1::{self, Secp256k1, Verification, constants};
use hashes::Hash; use hashes::Hash;
use util::taproot::{TapBranchHash, TapTweakHash}; use util::taproot::{TapBranchHash, TapTweakHash};
use SchnorrSigHashType; use SchnorrSighashType;
/// Deprecated re-export of [`secp256k1::XOnlyPublicKey`] /// Deprecated re-export of [`secp256k1::XOnlyPublicKey`]
#[deprecated(since = "0.28.0", note = "Please use `util::key::XOnlyPublicKey` instead")] #[deprecated(since = "0.28.0", note = "Please use `util::key::XOnlyPublicKey` instead")]
@ -220,7 +220,7 @@ pub struct SchnorrSig {
/// The underlying schnorr signature /// The underlying schnorr signature
pub sig: secp256k1::schnorr::Signature, pub sig: secp256k1::schnorr::Signature,
/// The corresponding hash type /// The corresponding hash type
pub hash_ty: SchnorrSigHashType, pub hash_ty: SchnorrSighashType,
} }
impl SchnorrSig { impl SchnorrSig {
@ -231,11 +231,11 @@ impl SchnorrSig {
// default type // default type
let sig = secp256k1::schnorr::Signature::from_slice(sl) let sig = secp256k1::schnorr::Signature::from_slice(sl)
.map_err(SchnorrSigError::Secp256k1)?; .map_err(SchnorrSigError::Secp256k1)?;
return Ok( SchnorrSig { sig, hash_ty : SchnorrSigHashType::Default }); return Ok( SchnorrSig { sig, hash_ty : SchnorrSighashType::Default });
}, },
65 => { 65 => {
let (hash_ty, sig) = sl.split_last().expect("Slice len checked == 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_u8(*hash_ty)
.map_err(|_| SchnorrSigError::InvalidSighashType(*hash_ty))?; .map_err(|_| SchnorrSigError::InvalidSighashType(*hash_ty))?;
let sig = secp256k1::schnorr::Signature::from_slice(sig) let sig = secp256k1::schnorr::Signature::from_slice(sig)
.map_err(SchnorrSigError::Secp256k1)?; .map_err(SchnorrSigError::Secp256k1)?;
@ -251,7 +251,7 @@ impl SchnorrSig {
pub fn to_vec(&self) -> Vec<u8> { pub fn to_vec(&self) -> Vec<u8> {
// TODO: add support to serialize to a writer to SerializedSig // TODO: add support to serialize to a writer to SerializedSig
let mut ser_sig = self.sig.as_ref().to_vec(); let mut ser_sig = self.sig.as_ref().to_vec();
if self.hash_ty == SchnorrSigHashType::Default { if self.hash_ty == SchnorrSighashType::Default {
// default sighash type, don't add extra sighash byte // default sighash type, don't add extra sighash byte
} else { } else {
ser_sig.push(self.hash_ty as u8); ser_sig.push(self.hash_ty as u8);

View File

@ -81,7 +81,7 @@ struct TaprootCache {
} }
/// Contains outputs of previous transactions. /// Contains outputs of previous transactions.
/// In the case [`SchnorrSigHashType`] variant is `ANYONECANPAY`, [`Prevouts::One`] may be provided /// In the case [`SchnorrSighashType`] variant is `ANYONECANPAY`, [`Prevouts::One`] may be provided
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Prevouts<'u, T> where T: 'u + Borrow<TxOut> { pub enum Prevouts<'u, T> where T: 'u + Borrow<TxOut> {
/// `One` variant allows to provide the single Prevout needed. It's useful for example /// `One` variant allows to provide the single Prevout needed. It's useful for example
@ -107,8 +107,8 @@ pub struct ScriptPath<'s> {
/// Hashtype of an input's signature, encoded in the last byte of the signature /// Hashtype of an input's signature, encoded in the last byte of the signature
/// Fixed values so they can be casted as integer types for encoding /// Fixed values so they can be casted as integer types for encoding
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum SchnorrSigHashType { pub enum SchnorrSighashType {
/// 0x0: Used when not explicitly specified, defaulting to [`SchnorrSigHashType::All`] /// 0x0: Used when not explicitly specified, defaulting to [`SchnorrSighashType::All`]
Default = 0x00, Default = 0x00,
/// 0x1: Sign all outputs /// 0x1: Sign all outputs
All = 0x01, All = 0x01,
@ -129,37 +129,37 @@ pub enum SchnorrSigHashType {
/// Reserved for future use, `#[non_exhaustive]` is not available with current MSRV /// Reserved for future use, `#[non_exhaustive]` is not available with current MSRV
Reserved = 0xFF, Reserved = 0xFF,
} }
serde_string_impl!(SchnorrSigHashType, "a SchnorrSigHashType data"); serde_string_impl!(SchnorrSighashType, "a SchnorrSighashType data");
impl fmt::Display for SchnorrSigHashType { impl fmt::Display for SchnorrSighashType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self { let s = match self {
SchnorrSigHashType::Default => "SIGHASH_DEFAULT", SchnorrSighashType::Default => "SIGHASH_DEFAULT",
SchnorrSigHashType::All => "SIGHASH_ALL", SchnorrSighashType::All => "SIGHASH_ALL",
SchnorrSigHashType::None => "SIGHASH_NONE", SchnorrSighashType::None => "SIGHASH_NONE",
SchnorrSigHashType::Single => "SIGHASH_SINGLE", SchnorrSighashType::Single => "SIGHASH_SINGLE",
SchnorrSigHashType::AllPlusAnyoneCanPay => "SIGHASH_ALL|SIGHASH_ANYONECANPAY", SchnorrSighashType::AllPlusAnyoneCanPay => "SIGHASH_ALL|SIGHASH_ANYONECANPAY",
SchnorrSigHashType::NonePlusAnyoneCanPay => "SIGHASH_NONE|SIGHASH_ANYONECANPAY", SchnorrSighashType::NonePlusAnyoneCanPay => "SIGHASH_NONE|SIGHASH_ANYONECANPAY",
SchnorrSigHashType::SinglePlusAnyoneCanPay => "SIGHASH_SINGLE|SIGHASH_ANYONECANPAY", SchnorrSighashType::SinglePlusAnyoneCanPay => "SIGHASH_SINGLE|SIGHASH_ANYONECANPAY",
SchnorrSigHashType::Reserved => "SIGHASH_RESERVED", SchnorrSighashType::Reserved => "SIGHASH_RESERVED",
}; };
f.write_str(s) f.write_str(s)
} }
} }
impl str::FromStr for SchnorrSigHashType { impl str::FromStr for SchnorrSighashType {
type Err = SigHashTypeParseError; type Err = SigHashTypeParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
match s { match s {
"SIGHASH_DEFAULT" => Ok(SchnorrSigHashType::Default), "SIGHASH_DEFAULT" => Ok(SchnorrSighashType::Default),
"SIGHASH_ALL" => Ok(SchnorrSigHashType::All), "SIGHASH_ALL" => Ok(SchnorrSighashType::All),
"SIGHASH_NONE" => Ok(SchnorrSigHashType::None), "SIGHASH_NONE" => Ok(SchnorrSighashType::None),
"SIGHASH_SINGLE" => Ok(SchnorrSigHashType::Single), "SIGHASH_SINGLE" => Ok(SchnorrSighashType::Single),
"SIGHASH_ALL|SIGHASH_ANYONECANPAY" => Ok(SchnorrSigHashType::AllPlusAnyoneCanPay), "SIGHASH_ALL|SIGHASH_ANYONECANPAY" => Ok(SchnorrSighashType::AllPlusAnyoneCanPay),
"SIGHASH_NONE|SIGHASH_ANYONECANPAY" => Ok(SchnorrSigHashType::NonePlusAnyoneCanPay), "SIGHASH_NONE|SIGHASH_ANYONECANPAY" => Ok(SchnorrSighashType::NonePlusAnyoneCanPay),
"SIGHASH_SINGLE|SIGHASH_ANYONECANPAY" => Ok(SchnorrSigHashType::SinglePlusAnyoneCanPay), "SIGHASH_SINGLE|SIGHASH_ANYONECANPAY" => Ok(SchnorrSighashType::SinglePlusAnyoneCanPay),
"SIGHASH_RESERVED" => Ok(SchnorrSigHashType::Reserved), "SIGHASH_RESERVED" => Ok(SchnorrSighashType::Reserved),
_ => Err(SigHashTypeParseError{ unrecognized: s.to_owned() }), _ => Err(SigHashTypeParseError{ unrecognized: s.to_owned() }),
} }
} }
@ -288,45 +288,45 @@ impl<'s> From<ScriptPath<'s>> for TapLeafHash {
} }
} }
impl From<EcdsaSighashType> for SchnorrSigHashType { impl From<EcdsaSighashType> for SchnorrSighashType {
fn from(s: EcdsaSighashType) -> Self { fn from(s: EcdsaSighashType) -> Self {
match s { match s {
EcdsaSighashType::All => SchnorrSigHashType::All, EcdsaSighashType::All => SchnorrSighashType::All,
EcdsaSighashType::None => SchnorrSigHashType::None, EcdsaSighashType::None => SchnorrSighashType::None,
EcdsaSighashType::Single => SchnorrSigHashType::Single, EcdsaSighashType::Single => SchnorrSighashType::Single,
EcdsaSighashType::AllPlusAnyoneCanPay => SchnorrSigHashType::AllPlusAnyoneCanPay, EcdsaSighashType::AllPlusAnyoneCanPay => SchnorrSighashType::AllPlusAnyoneCanPay,
EcdsaSighashType::NonePlusAnyoneCanPay => SchnorrSigHashType::NonePlusAnyoneCanPay, EcdsaSighashType::NonePlusAnyoneCanPay => SchnorrSighashType::NonePlusAnyoneCanPay,
EcdsaSighashType::SinglePlusAnyoneCanPay => SchnorrSigHashType::SinglePlusAnyoneCanPay, EcdsaSighashType::SinglePlusAnyoneCanPay => SchnorrSighashType::SinglePlusAnyoneCanPay,
} }
} }
} }
impl SchnorrSigHashType { impl SchnorrSighashType {
/// Break the sighash flag into the "real" sighash flag and the ANYONECANPAY boolean /// Break the sighash flag into the "real" sighash flag and the ANYONECANPAY boolean
pub(crate) fn split_anyonecanpay_flag(self) -> (SchnorrSigHashType, bool) { pub(crate) fn split_anyonecanpay_flag(self) -> (SchnorrSighashType, bool) {
match self { match self {
SchnorrSigHashType::Default => (SchnorrSigHashType::Default, false), SchnorrSighashType::Default => (SchnorrSighashType::Default, false),
SchnorrSigHashType::All => (SchnorrSigHashType::All, false), SchnorrSighashType::All => (SchnorrSighashType::All, false),
SchnorrSigHashType::None => (SchnorrSigHashType::None, false), SchnorrSighashType::None => (SchnorrSighashType::None, false),
SchnorrSigHashType::Single => (SchnorrSigHashType::Single, false), SchnorrSighashType::Single => (SchnorrSighashType::Single, false),
SchnorrSigHashType::AllPlusAnyoneCanPay => (SchnorrSigHashType::All, true), SchnorrSighashType::AllPlusAnyoneCanPay => (SchnorrSighashType::All, true),
SchnorrSigHashType::NonePlusAnyoneCanPay => (SchnorrSigHashType::None, true), SchnorrSighashType::NonePlusAnyoneCanPay => (SchnorrSighashType::None, true),
SchnorrSigHashType::SinglePlusAnyoneCanPay => (SchnorrSigHashType::Single, true), SchnorrSighashType::SinglePlusAnyoneCanPay => (SchnorrSighashType::Single, true),
SchnorrSigHashType::Reserved => (SchnorrSigHashType::Reserved, false), SchnorrSighashType::Reserved => (SchnorrSighashType::Reserved, false),
} }
} }
/// Create a [`SchnorrSigHashType`] from raw `u8` /// Create a [`SchnorrSighashType`] from raw `u8`
pub fn from_u8(hash_ty: u8) -> Result<Self, Error> { pub fn from_u8(hash_ty: u8) -> Result<Self, Error> {
match hash_ty { match hash_ty {
0x00 => Ok(SchnorrSigHashType::Default), 0x00 => Ok(SchnorrSighashType::Default),
0x01 => Ok(SchnorrSigHashType::All), 0x01 => Ok(SchnorrSighashType::All),
0x02 => Ok(SchnorrSigHashType::None), 0x02 => Ok(SchnorrSighashType::None),
0x03 => Ok(SchnorrSigHashType::Single), 0x03 => Ok(SchnorrSighashType::Single),
0x81 => Ok(SchnorrSigHashType::AllPlusAnyoneCanPay), 0x81 => Ok(SchnorrSighashType::AllPlusAnyoneCanPay),
0x82 => Ok(SchnorrSigHashType::NonePlusAnyoneCanPay), 0x82 => Ok(SchnorrSighashType::NonePlusAnyoneCanPay),
0x83 => Ok(SchnorrSigHashType::SinglePlusAnyoneCanPay), 0x83 => Ok(SchnorrSighashType::SinglePlusAnyoneCanPay),
0xFF => Ok(SchnorrSigHashType::Reserved), 0xFF => Ok(SchnorrSighashType::Reserved),
x => Err(Error::InvalidSigHashType(x as u32)), x => Err(Error::InvalidSigHashType(x as u32)),
} }
} }
@ -355,7 +355,7 @@ impl<R: Deref<Target=Transaction>> SigHashCache<R> {
prevouts: &Prevouts<T>, prevouts: &Prevouts<T>,
annex: Option<Annex>, annex: Option<Annex>,
leaf_hash_code_separator: Option<(TapLeafHash, u32)>, leaf_hash_code_separator: Option<(TapLeafHash, u32)>,
sighash_type: SchnorrSigHashType, sighash_type: SchnorrSighashType,
) -> Result<(), Error> { ) -> Result<(), Error> {
prevouts.check_all(&self.tx)?; prevouts.check_all(&self.tx)?;
@ -395,7 +395,7 @@ impl<R: Deref<Target=Transaction>> SigHashCache<R> {
// If hash_type & 3 does not equal SIGHASH_NONE or SIGHASH_SINGLE: // If hash_type & 3 does not equal SIGHASH_NONE or SIGHASH_SINGLE:
// sha_outputs (32): the SHA256 of the serialization of all outputs in CTxOut format. // sha_outputs (32): the SHA256 of the serialization of all outputs in CTxOut format.
if sighash != SchnorrSigHashType::None && sighash != SchnorrSigHashType::Single { if sighash != SchnorrSighashType::None && sighash != SchnorrSighashType::Single {
self.common_cache().outputs.consensus_encode(&mut writer)?; self.common_cache().outputs.consensus_encode(&mut writer)?;
} }
@ -450,7 +450,7 @@ impl<R: Deref<Target=Transaction>> SigHashCache<R> {
// * Data about this output: // * Data about this output:
// If hash_type & 3 equals SIGHASH_SINGLE: // If hash_type & 3 equals SIGHASH_SINGLE:
// sha_single_output (32): the SHA256 of the corresponding output in CTxOut format. // sha_single_output (32): the SHA256 of the corresponding output in CTxOut format.
if sighash == SchnorrSigHashType::Single { if sighash == SchnorrSighashType::Single {
let mut enc = sha256::Hash::engine(); let mut enc = sha256::Hash::engine();
self.tx self.tx
.output .output
@ -484,7 +484,7 @@ impl<R: Deref<Target=Transaction>> SigHashCache<R> {
prevouts: &Prevouts<T>, prevouts: &Prevouts<T>,
annex: Option<Annex>, annex: Option<Annex>,
leaf_hash_code_separator: Option<(TapLeafHash, u32)>, leaf_hash_code_separator: Option<(TapLeafHash, u32)>,
sighash_type: SchnorrSigHashType, sighash_type: SchnorrSighashType,
) -> Result<TapSighashHash, Error> { ) -> Result<TapSighashHash, Error> {
let mut enc = TapSighashHash::engine(); let mut enc = TapSighashHash::engine();
self.taproot_encode_signing_data_to( self.taproot_encode_signing_data_to(
@ -503,7 +503,7 @@ impl<R: Deref<Target=Transaction>> SigHashCache<R> {
&mut self, &mut self,
input_index: usize, input_index: usize,
prevouts: &Prevouts<T>, prevouts: &Prevouts<T>,
sighash_type: SchnorrSigHashType, sighash_type: SchnorrSighashType,
) -> Result<TapSighashHash, Error> { ) -> Result<TapSighashHash, Error> {
let mut enc = TapSighashHash::engine(); let mut enc = TapSighashHash::engine();
self.taproot_encode_signing_data_to( self.taproot_encode_signing_data_to(
@ -526,7 +526,7 @@ impl<R: Deref<Target=Transaction>> SigHashCache<R> {
input_index: usize, input_index: usize,
prevouts: &Prevouts<T>, prevouts: &Prevouts<T>,
leaf_hash: S, leaf_hash: S,
sighash_type: SchnorrSigHashType, sighash_type: SchnorrSighashType,
) -> Result<TapSighashHash, Error> { ) -> Result<TapSighashHash, Error> {
let mut enc = TapSighashHash::engine(); let mut enc = TapSighashHash::engine();
self.taproot_encode_signing_data_to( self.taproot_encode_signing_data_to(
@ -823,7 +823,7 @@ mod tests {
"01365724000000000023542156b39dab4f8f3508e0432cfb41fab110170acaa2d4c42539cb90a4dc7c093bc500", "01365724000000000023542156b39dab4f8f3508e0432cfb41fab110170acaa2d4c42539cb90a4dc7c093bc500",
0, 0,
"33ca0ebfb4a945eeee9569fc0f5040221275f88690b7f8592ada88ce3bdf6703", "33ca0ebfb4a945eeee9569fc0f5040221275f88690b7f8592ada88ce3bdf6703",
SchnorrSigHashType::Default, None, None, None SchnorrSighashType::Default, None, None, None
); );
test_taproot_sighash( test_taproot_sighash(
@ -831,7 +831,7 @@ mod tests {
"02591f220000000000225120f25ad35583ea31998d968871d7de1abd2a52f6fe4178b54ea158274806ff4ece48fb310000000000225120f25ad35583ea31998d968871d7de1abd2a52f6fe4178b54ea158274806ff4ece", "02591f220000000000225120f25ad35583ea31998d968871d7de1abd2a52f6fe4178b54ea158274806ff4ece48fb310000000000225120f25ad35583ea31998d968871d7de1abd2a52f6fe4178b54ea158274806ff4ece",
1, 1,
"626ab955d58c9a8a600a0c580549d06dc7da4e802eb2a531f62a588e430967a8", "626ab955d58c9a8a600a0c580549d06dc7da4e802eb2a531f62a588e430967a8",
SchnorrSigHashType::All, None, None, None SchnorrSighashType::All, None, None, None
); );
test_taproot_sighash( test_taproot_sighash(
@ -839,7 +839,7 @@ mod tests {
"01c4811000000000002251201bf9297d0a2968ae6693aadd0fa514717afefd218087a239afb7418e2d22e65c", "01c4811000000000002251201bf9297d0a2968ae6693aadd0fa514717afefd218087a239afb7418e2d22e65c",
0, 0,
"dfa9437f9c9a1d1f9af271f79f2f5482f287cdb0d2e03fa92c8a9b216cc6061c", "dfa9437f9c9a1d1f9af271f79f2f5482f287cdb0d2e03fa92c8a9b216cc6061c",
SchnorrSigHashType::AllPlusAnyoneCanPay, None, None, None SchnorrSighashType::AllPlusAnyoneCanPay, None, None, None
); );
test_taproot_sighash( test_taproot_sighash(
@ -847,7 +847,7 @@ mod tests {
"0144c84d0000000000225120e3f2107989c88e67296ab2faca930efa2e3a5bd3ff0904835a11c9e807458621", "0144c84d0000000000225120e3f2107989c88e67296ab2faca930efa2e3a5bd3ff0904835a11c9e807458621",
0, 0,
"3129de36a5d05fff97ffca31eb75fcccbbbc27b3147a7a36a9e4b45d8b625067", "3129de36a5d05fff97ffca31eb75fcccbbbc27b3147a7a36a9e4b45d8b625067",
SchnorrSigHashType::None, None, None, None SchnorrSighashType::None, None, None, None
); );
test_taproot_sighash( test_taproot_sighash(
@ -855,7 +855,7 @@ mod tests {
"013fed110000000000225120eb536ae8c33580290630fc495046e998086a64f8f33b93b07967d9029b265c55", "013fed110000000000225120eb536ae8c33580290630fc495046e998086a64f8f33b93b07967d9029b265c55",
0, 0,
"2441e8b0e063a2083ee790f14f2045022f07258ddde5ee01de543c9e789d80ae", "2441e8b0e063a2083ee790f14f2045022f07258ddde5ee01de543c9e789d80ae",
SchnorrSigHashType::NonePlusAnyoneCanPay, None, None, None SchnorrSighashType::NonePlusAnyoneCanPay, None, None, None
); );
test_taproot_sighash( test_taproot_sighash(
@ -863,7 +863,7 @@ mod tests {
"01efa558000000000022512007071ea3dc7e331b0687d0193d1e6d6ed10e645ef36f10ef8831d5e522ac9e80", "01efa558000000000022512007071ea3dc7e331b0687d0193d1e6d6ed10e645ef36f10ef8831d5e522ac9e80",
0, 0,
"30239345177cadd0e3ea413d49803580abb6cb27971b481b7788a78d35117a88", "30239345177cadd0e3ea413d49803580abb6cb27971b481b7788a78d35117a88",
SchnorrSigHashType::Single, None, None, None SchnorrSighashType::Single, None, None, None
); );
test_taproot_sighash( test_taproot_sighash(
@ -871,7 +871,7 @@ mod tests {
"0107af4e00000000002251202c36d243dfc06cb56a248e62df27ecba7417307511a81ae61aa41c597a929c69", "0107af4e00000000002251202c36d243dfc06cb56a248e62df27ecba7417307511a81ae61aa41c597a929c69",
0, 0,
"bf9c83f26c6dd16449e4921f813f551c4218e86f2ec906ca8611175b41b566df", "bf9c83f26c6dd16449e4921f813f551c4218e86f2ec906ca8611175b41b566df",
SchnorrSigHashType::SinglePlusAnyoneCanPay, None, None, None SchnorrSighashType::SinglePlusAnyoneCanPay, None, None, None
); );
} }
@ -882,7 +882,7 @@ mod tests {
"01ea49260000000000225120ab5e9800806bf18cb246edcf5fe63441208fe955a4b5a35bbff65f5db622a010", "01ea49260000000000225120ab5e9800806bf18cb246edcf5fe63441208fe955a4b5a35bbff65f5db622a010",
0, 0,
"3b003000add359a364a156e73e02846782a59d0d95ca8c4638aaad99f2ef915c", "3b003000add359a364a156e73e02846782a59d0d95ca8c4638aaad99f2ef915c",
SchnorrSigHashType::SinglePlusAnyoneCanPay, SchnorrSighashType::SinglePlusAnyoneCanPay,
Some("507b979802e62d397acb29f56743a791894b99372872fc5af06a4f6e8d242d0615cda53062bb20e6ec79756fe39183f0c128adfe85559a8fa042b042c018aa8010143799e44f0893c40e1e"), Some("507b979802e62d397acb29f56743a791894b99372872fc5af06a4f6e8d242d0615cda53062bb20e6ec79756fe39183f0c128adfe85559a8fa042b042c018aa8010143799e44f0893c40e1e"),
None, None,
None, None,
@ -896,7 +896,7 @@ mod tests {
"011bec34000000000022512028055142ea437db73382e991861446040b61dd2185c4891d7daf6893d79f7182", "011bec34000000000022512028055142ea437db73382e991861446040b61dd2185c4891d7daf6893d79f7182",
0, 0,
"d66de5274a60400c7b08c86ba6b7f198f40660079edf53aca89d2a9501317f2e", "d66de5274a60400c7b08c86ba6b7f198f40660079edf53aca89d2a9501317f2e",
SchnorrSigHashType::All, SchnorrSighashType::All,
None, None,
Some("20cc4e1107aea1d170c5ff5b6817e1303010049724fb3caa7941792ea9d29b3e2bacab"), Some("20cc4e1107aea1d170c5ff5b6817e1303010049724fb3caa7941792ea9d29b3e2bacab"),
None, None,
@ -910,7 +910,7 @@ mod tests {
"011bec34000000000022512028055142ea437db73382e991861446040b61dd2185c4891d7daf6893d79f7182", "011bec34000000000022512028055142ea437db73382e991861446040b61dd2185c4891d7daf6893d79f7182",
0, 0,
"d66de5274a60400c7b08c86ba6b7f198f40660079edf53aca89d2a9501317f2e", "d66de5274a60400c7b08c86ba6b7f198f40660079edf53aca89d2a9501317f2e",
SchnorrSigHashType::All, SchnorrSighashType::All,
None, None,
None, None,
Some("15a2530514e399f8b5cf0b3d3112cf5b289eaa3e308ba2071b58392fdc6da68a"), Some("15a2530514e399f8b5cf0b3d3112cf5b289eaa3e308ba2071b58392fdc6da68a"),
@ -924,7 +924,7 @@ mod tests {
"011458360000000000225120a7baec3fb9f84614e3899fcc010c638f80f13539344120e1f4d8b68a9a011a13", "011458360000000000225120a7baec3fb9f84614e3899fcc010c638f80f13539344120e1f4d8b68a9a011a13",
0, 0,
"a0042aa434f9a75904b64043f2a283f8b4c143c7f4f7f49a6cbe5b9f745f4c15", "a0042aa434f9a75904b64043f2a283f8b4c143c7f4f7f49a6cbe5b9f745f4c15",
SchnorrSigHashType::All, SchnorrSighashType::All,
Some("50a6272b470e1460e3332ade7bb14b81671c564fb6245761bd5bd531394b28860e0b3808ab229fb51791fb6ae6fa82d915b2efb8f6df83ae1f5ab3db13e30928875e2a22b749d89358de481f19286cd4caa792ce27f9559082d227a731c5486882cc707f83da361c51b7aadd9a0cf68fe7480c410fa137b454482d9a1ebf0f96d760b4d61426fc109c6e8e99a508372c45caa7b000a41f8251305da3f206c1849985ba03f3d9592832b4053afbd23ab25d0465df0bc25a36c223aacf8e04ec736a418c72dc319e4da3e972e349713ca600965e7c665f2090d5a70e241ac164115a1f5639f28b1773327715ca307ace64a2de7f0e3df70a2ffee3857689f909c0dad46d8a20fa373a4cc6eed6d4c9806bf146f0d76baae1"), Some("50a6272b470e1460e3332ade7bb14b81671c564fb6245761bd5bd531394b28860e0b3808ab229fb51791fb6ae6fa82d915b2efb8f6df83ae1f5ab3db13e30928875e2a22b749d89358de481f19286cd4caa792ce27f9559082d227a731c5486882cc707f83da361c51b7aadd9a0cf68fe7480c410fa137b454482d9a1ebf0f96d760b4d61426fc109c6e8e99a508372c45caa7b000a41f8251305da3f206c1849985ba03f3d9592832b4053afbd23ab25d0465df0bc25a36c223aacf8e04ec736a418c72dc319e4da3e972e349713ca600965e7c665f2090d5a70e241ac164115a1f5639f28b1773327715ca307ace64a2de7f0e3df70a2ffee3857689f909c0dad46d8a20fa373a4cc6eed6d4c9806bf146f0d76baae1"),
Some("7520ab9160dd8299dc1367659be3e8f66781fe440d52940c7f8d314a89b9f2698d406ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6eadac"), Some("7520ab9160dd8299dc1367659be3e8f66781fe440d52940c7f8d314a89b9f2698d406ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6eadac"),
None, None,
@ -945,27 +945,27 @@ mod tests {
let empty_vec = vec![]; let empty_vec = vec![];
let empty_prevouts : Prevouts<TxOut> = Prevouts::All(&empty_vec); let empty_prevouts : Prevouts<TxOut> = Prevouts::All(&empty_vec);
assert_eq!( assert_eq!(
c.taproot_signature_hash(0, &empty_prevouts, None, None, SchnorrSigHashType::All), c.taproot_signature_hash(0, &empty_prevouts, None, None, SchnorrSighashType::All),
Err(Error::PrevoutsSize) Err(Error::PrevoutsSize)
); );
let two = vec![TxOut::default(), TxOut::default()]; let two = vec![TxOut::default(), TxOut::default()];
let too_many_prevouts = Prevouts::All(&two); let too_many_prevouts = Prevouts::All(&two);
assert_eq!( assert_eq!(
c.taproot_signature_hash(0, &too_many_prevouts, None, None, SchnorrSigHashType::All), c.taproot_signature_hash(0, &too_many_prevouts, None, None, SchnorrSighashType::All),
Err(Error::PrevoutsSize) Err(Error::PrevoutsSize)
); );
let tx_out = TxOut::default(); let tx_out = TxOut::default();
let prevout = Prevouts::One(1, &tx_out); let prevout = Prevouts::One(1, &tx_out);
assert_eq!( assert_eq!(
c.taproot_signature_hash(0, &prevout, None, None, SchnorrSigHashType::All), c.taproot_signature_hash(0, &prevout, None, None, SchnorrSighashType::All),
Err(Error::PrevoutKind) Err(Error::PrevoutKind)
); );
assert_eq!( assert_eq!(
c.taproot_signature_hash(0, &prevout, None, None, SchnorrSigHashType::AllPlusAnyoneCanPay), c.taproot_signature_hash(0, &prevout, None, None, SchnorrSighashType::AllPlusAnyoneCanPay),
Err(Error::PrevoutIndex) Err(Error::PrevoutIndex)
); );
assert_eq!( assert_eq!(
c.taproot_signature_hash(10, &prevout, None, None, SchnorrSigHashType::AllPlusAnyoneCanPay), c.taproot_signature_hash(10, &prevout, None, None, SchnorrSighashType::AllPlusAnyoneCanPay),
Err(Error::IndexOutOfInputsBounds { Err(Error::IndexOutOfInputsBounds {
index: 10, index: 10,
inputs_size: 1 inputs_size: 1
@ -973,7 +973,7 @@ mod tests {
); );
let prevout = Prevouts::One(0, &tx_out); let prevout = Prevouts::One(0, &tx_out);
assert_eq!( assert_eq!(
c.taproot_signature_hash(0, &prevout, None, None, SchnorrSigHashType::SinglePlusAnyoneCanPay), c.taproot_signature_hash(0, &prevout, None, None, SchnorrSighashType::SinglePlusAnyoneCanPay),
Err(Error::SingleWithoutCorrespondingOutput { Err(Error::SingleWithoutCorrespondingOutput {
index: 0, index: 0,
outputs_size: 0 outputs_size: 0
@ -1000,7 +1000,7 @@ mod tests {
prevout_hex: &str, prevout_hex: &str,
input_index: usize, input_index: usize,
expected_hash: &str, expected_hash: &str,
sighash_type: SchnorrSigHashType, sighash_type: SchnorrSighashType,
annex_hex: Option<&str>, annex_hex: Option<&str>,
script_hex: Option<&str>, script_hex: Option<&str>,
script_leaf_hash: Option<&str>, script_leaf_hash: Option<&str>,
@ -1087,7 +1087,7 @@ mod tests {
} else { } else {
Some(hex_hash!(TapBranchHash, inp["given"]["merkleRoot"].as_str().unwrap())) Some(hex_hash!(TapBranchHash, inp["given"]["merkleRoot"].as_str().unwrap()))
}; };
let hash_ty = SchnorrSigHashType::from_u8(inp["given"]["hashType"].as_u64().unwrap() as u8).unwrap(); let hash_ty = SchnorrSighashType::from_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_internal_pk = hex_hash!(XOnlyPublicKey, inp["intermediary"]["internalPubkey"].as_str().unwrap());
let expected_tweak = hex_hash!(TapTweakHash, inp["intermediary"]["tweak"].as_str().unwrap()); let expected_tweak = hex_hash!(TapTweakHash, inp["intermediary"]["tweak"].as_str().unwrap());
@ -1096,9 +1096,9 @@ mod tests {
let expected_sig_hash = hex_hash!(TapSighashHash, inp["intermediary"]["sigHash"].as_str().unwrap()); let expected_sig_hash = hex_hash!(TapSighashHash, inp["intermediary"]["sigHash"].as_str().unwrap());
let sig_str = inp["expected"]["witness"][0].as_str().unwrap(); let sig_str = inp["expected"]["witness"][0].as_str().unwrap();
let (expected_key_spend_sig, expected_hash_ty) = if sig_str.len() == 128 { let (expected_key_spend_sig, expected_hash_ty) = if sig_str.len() == 128 {
(secp256k1::schnorr::Signature::from_str(sig_str).unwrap(), SchnorrSigHashType::Default) (secp256k1::schnorr::Signature::from_str(sig_str).unwrap(), SchnorrSighashType::Default)
} else { } else {
let hash_ty = SchnorrSigHashType::from_u8(Vec::<u8>::from_hex(&sig_str[128..]).unwrap()[0]).unwrap(); let hash_ty = SchnorrSighashType::from_u8(Vec::<u8>::from_hex(&sig_str[128..]).unwrap()[0]).unwrap();
(secp256k1::schnorr::Signature::from_str(&sig_str[..128]).unwrap(), hash_ty) (secp256k1::schnorr::Signature::from_str(&sig_str[..128]).unwrap(), hash_ty)
}; };
@ -1148,18 +1148,18 @@ mod tests {
#[test] #[test]
fn sighashtype_fromstr_display() { fn sighashtype_fromstr_display() {
let sighashtypes = vec![ let sighashtypes = vec![
("SIGHASH_DEFAULT", SchnorrSigHashType::Default), ("SIGHASH_DEFAULT", SchnorrSighashType::Default),
("SIGHASH_ALL", SchnorrSigHashType::All), ("SIGHASH_ALL", SchnorrSighashType::All),
("SIGHASH_NONE", SchnorrSigHashType::None), ("SIGHASH_NONE", SchnorrSighashType::None),
("SIGHASH_SINGLE", SchnorrSigHashType::Single), ("SIGHASH_SINGLE", SchnorrSighashType::Single),
("SIGHASH_ALL|SIGHASH_ANYONECANPAY", SchnorrSigHashType::AllPlusAnyoneCanPay), ("SIGHASH_ALL|SIGHASH_ANYONECANPAY", SchnorrSighashType::AllPlusAnyoneCanPay),
("SIGHASH_NONE|SIGHASH_ANYONECANPAY", SchnorrSigHashType::NonePlusAnyoneCanPay), ("SIGHASH_NONE|SIGHASH_ANYONECANPAY", SchnorrSighashType::NonePlusAnyoneCanPay),
("SIGHASH_SINGLE|SIGHASH_ANYONECANPAY", SchnorrSigHashType::SinglePlusAnyoneCanPay), ("SIGHASH_SINGLE|SIGHASH_ANYONECANPAY", SchnorrSighashType::SinglePlusAnyoneCanPay),
("SIGHASH_RESERVED", SchnorrSigHashType::Reserved), ("SIGHASH_RESERVED", SchnorrSighashType::Reserved),
]; ];
for (s, sht) in sighashtypes { for (s, sht) in sighashtypes {
assert_eq!(sht.to_string(), s); assert_eq!(sht.to_string(), s);
assert_eq!(SchnorrSigHashType::from_str(s).unwrap(), sht); assert_eq!(SchnorrSighashType::from_str(s).unwrap(), sht);
} }
let sht_mistakes = vec![ let sht_mistakes = vec![
"SIGHASH_ALL | SIGHASH_ANYONECANPAY", "SIGHASH_ALL | SIGHASH_ANYONECANPAY",
@ -1176,7 +1176,7 @@ mod tests {
"SigHash_NONE", "SigHash_NONE",
]; ];
for s in sht_mistakes { for s in sht_mistakes {
assert_eq!(SchnorrSigHashType::from_str(s).unwrap_err().to_string(), format!("Unrecognized SIGHASH string '{}'", s)); assert_eq!(SchnorrSighashType::from_str(s).unwrap_err().to_string(), format!("Unrecognized SIGHASH string '{}'", s));
} }
} }
} }