Skip to content

Commit

Permalink
Merge #169: Rename Sighash type downcasing the middle h
Browse files Browse the repository at this point in the history
0a97313 update CHANGELOG with Sighash renaming (Riccardo Casatta)
bb7bddc SigHash -> Sighash (Riccardo Casatta)
468b787 SigHashType -> SighashType (Riccardo Casatta)
229be29 EcdsaSigHashType -> EcdsaSighashType (Riccardo Casatta)
43e36ff SchnorrSigHashType -> SchnorrSighashType (Riccardo Casatta)
d78493e SigHashCache -> SighashCache (Riccardo Casatta)

Pull request description:

  The same has been made in rust-bitcoin

ACKs for top commit:
  apoelstra:
    ACK 0a97313

Tree-SHA512: df871a1ee4fbe23dd532ab3a9b3ca64092085508152916d505e894f9c315614afb4c546ac17665a2c7e9e128ea4068aca516b602159332ebbb9fc9e5d234dd49
  • Loading branch information
apoelstra committed Jul 17, 2023
2 parents ba4aad3 + 0a97313 commit 4a963db
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 203 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

# 0.23.0 -

* rename all Sighash types downcasing the middle "h", for example: SigHash -> Sighash

# 0.22.0 - 2023-06-08

* [https://github.com/ElementsProject/rust-elements/pull/159](Update `TapTweak`, and `schnorr` module generally, to match rust-bitcoin)
Expand Down
22 changes: 11 additions & 11 deletions elementsd-tests/src/taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ use elements::encode::{deserialize, serialize_hex};
use elements::hashes::Hash;
use elements::script::Builder;
use elements::secp256k1_zkp;
use elements::sighash::{self, SigHashCache};
use elements::sighash::{self, SighashCache};
use elements::taproot::{LeafVersion, TapTweakHash, TaprootBuilder, TaprootSpendInfo, TapLeafHash};
use elements::OutPoint;
use elements::{
confidential, opcodes, AssetIssuance, BlockHash, LockTime, SchnorrSig, SchnorrSigHashType, Script,
confidential, opcodes, AssetIssuance, BlockHash, LockTime, SchnorrSig, SchnorrSighashType, Script,
Sequence, TxInWitness, TxOut, Txid,
};
use elements::{AddressParams, Transaction, TxIn, TxOutSecrets};
Expand Down Expand Up @@ -144,7 +144,7 @@ fn taproot_spend_test(
elementsd: &ElementsD,
secp: &Secp256k1<secp256k1_zkp::All>,
genesis_hash: BlockHash,
sighash_ty: SchnorrSigHashType,
sighash_ty: SchnorrSighashType,
blind_prevout: bool,
blind_tx: bool,
key_spend: bool,
Expand Down Expand Up @@ -193,7 +193,7 @@ fn taproot_spend_test(
.unwrap();
}

let mut cache = SigHashCache::new(&tx);
let mut cache = SighashCache::new(&tx);

if key_spend {
// test key spend
Expand Down Expand Up @@ -274,13 +274,13 @@ fn taproot_tests() {
let genesis_hash = BlockHash::from_str(&genesis_hash_str).unwrap();

let sighash_tys = [
SchnorrSigHashType::Default,
SchnorrSigHashType::Single,
SchnorrSigHashType::SinglePlusAnyoneCanPay,
SchnorrSigHashType::None,
SchnorrSigHashType::NonePlusAnyoneCanPay,
SchnorrSigHashType::All,
SchnorrSigHashType::AllPlusAnyoneCanPay,
SchnorrSighashType::Default,
SchnorrSighashType::Single,
SchnorrSighashType::SinglePlusAnyoneCanPay,
SchnorrSighashType::None,
SchnorrSighashType::NonePlusAnyoneCanPay,
SchnorrSighashType::All,
SchnorrSighashType::AllPlusAnyoneCanPay,
];

for &conf_prevout in &[true, false] {
Expand Down
6 changes: 3 additions & 3 deletions src/hash_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ hash_newtype! {
pub struct BlockHash(sha256d::Hash);

/// "Hash of the transaction according to the signature algorithm"
pub struct SigHash(sha256d::Hash);
pub struct Sighash(sha256d::Hash);

/// A hash of a public key.
pub struct PubkeyHash(hash160::Hash);
Expand All @@ -62,11 +62,11 @@ hash_newtype! {

impl_hashencode!(Txid);
impl_hashencode!(Wtxid);
impl_hashencode!(SigHash);
impl_hashencode!(Sighash);
impl_hashencode!(BlockHash);
impl_hashencode!(TxMerkleNode);

impl ThirtyTwoByteHash for SigHash {
impl ThirtyTwoByteHash for Sighash {
fn into_32(self) -> [u8; 32] {
self.0.to_byte_array()
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub use bitcoin::bech32;
pub use bitcoin::hashes;
// export everything at the top level so it can be used as `elements::Transaction` etc.
pub use crate::address::{Address, AddressParams, AddressError};
pub use crate::transaction::{OutPoint, PeginData, PegoutData, EcdsaSigHashType, TxIn, TxOut, TxInWitness, TxOutWitness, Transaction, AssetIssuance};
pub use crate::transaction::{OutPoint, PeginData, PegoutData, EcdsaSighashType, TxIn, TxOut, TxInWitness, TxOutWitness, Transaction, AssetIssuance};
pub use crate::transaction::Sequence;
pub use crate::blind::{ConfidentialTxOutError, TxOutSecrets, SurjectionInput, TxOutError, VerificationError, BlindError, UnblindError, BlindValueProofs, BlindAssetProofs};
pub use crate::block::{BlockHeader, Block};
Expand All @@ -83,5 +83,5 @@ pub use crate::hash_types::*;
pub use crate::issuance::{AssetId, ContractHash};
pub use crate::locktime::LockTime;
pub use crate::script::Script;
pub use crate::sighash::SchnorrSigHashType;
pub use crate::sighash::SchnorrSighashType;
pub use crate::schnorr::{SchnorrSig, SchnorrSigError};
6 changes: 3 additions & 3 deletions src/pset/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ pub enum Error {
/// Actual
actual: Txid,
},
/// Unable to parse as a standard SigHash type.
NonStandardSigHashType(u32),
/// Unable to parse as a standard Sighash type.
NonStandardSighashType(u32),
/// Parsing errors from bitcoin_hashes
HashParseError(hashes::Error),
/// The pre-image must hash to the correponding pset hash
Expand Down Expand Up @@ -129,7 +129,7 @@ impl fmt::Display for Error {
expected: ref e,
actual: ref a,
} => write!(f, "different id: expected {}, actual {}", e, a),
Error::NonStandardSigHashType(ref sht) => {
Error::NonStandardSighashType(ref sht) => {
write!(f, "non-standard sighash type: {}", sht)
}
Error::InvalidMagic => f.write_str("invalid magic"),
Expand Down
48 changes: 24 additions & 24 deletions src/pset/map/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use crate::pset::map::Map;
use crate::pset::raw;
use crate::pset::serialize;
use crate::pset::{self, error, Error};
use crate::{transaction::SighashTypeParseError, SchnorrSigHashType};
use crate::{AssetIssuance, BlockHash, EcdsaSigHashType, Script, Transaction, TxIn, TxOut, Txid};
use crate::{transaction::SighashTypeParseError, SchnorrSighashType};
use crate::{AssetIssuance, BlockHash, EcdsaSighashType, Script, Transaction, TxIn, TxOut, Txid};
use bitcoin::bip32::KeySource;
use bitcoin::{PublicKey, key::XOnlyPublicKey};
use secp256k1_zkp::{self, RangeProof, Tweak, ZERO_TWEAK};
Expand Down Expand Up @@ -292,7 +292,7 @@ impl Default for Input {
}

/// 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
/// for converting to/from [`PsbtSighashType`] from/to the desired signature hash type they need.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand All @@ -305,7 +305,7 @@ serde_string_impl!(PsbtSighashType, "a PsbtSighashType data");
impl fmt::Display for PsbtSighashType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.schnorr_hash_ty() {
Some(SchnorrSigHashType::Reserved) | None => write!(f, "{:#x}", self.inner),
Some(SchnorrSighashType::Reserved) | None => write!(f, "{:#x}", self.inner),
Some(schnorr_hash_ty) => fmt::Display::fmt(&schnorr_hash_ty, f),
}
}
Expand All @@ -321,8 +321,8 @@ impl FromStr for PsbtSighashType {
// NB: some of Schnorr sighash types are non-standard for pre-taproot
// inputs. We also do not support SIGHASH_RESERVED in verbatim form
// ("0xFF" string should be used instead).
match SchnorrSigHashType::from_str(s) {
Ok(SchnorrSigHashType::Reserved) => {
match SchnorrSighashType::from_str(s) {
Ok(SchnorrSighashType::Reserved) => {
return Err(SighashTypeParseError {
unrecognized: s.to_owned(),
})
Expand All @@ -341,36 +341,36 @@ impl FromStr for PsbtSighashType {
})
}
}
impl From<EcdsaSigHashType> for PsbtSighashType {
fn from(ecdsa_hash_ty: EcdsaSigHashType) -> Self {
impl From<EcdsaSighashType> for PsbtSighashType {
fn from(ecdsa_hash_ty: EcdsaSighashType) -> Self {
PsbtSighashType {
inner: ecdsa_hash_ty as u32,
}
}
}

impl From<SchnorrSigHashType> for PsbtSighashType {
fn from(schnorr_hash_ty: SchnorrSigHashType) -> Self {
impl From<SchnorrSighashType> for PsbtSighashType {
fn from(schnorr_hash_ty: SchnorrSighashType) -> Self {
PsbtSighashType {
inner: schnorr_hash_ty as u32,
}
}
}

impl PsbtSighashType {
/// Returns the [`EcdsaSigHashType`] if the [`PsbtSighashType`] can be
/// Returns the [`EcdsaSighashType`] if the [`PsbtSighashType`] can be
/// converted to one.
pub fn ecdsa_hash_ty(self) -> Option<EcdsaSigHashType> {
EcdsaSigHashType::from_standard(self.inner).ok()
pub fn ecdsa_hash_ty(self) -> Option<EcdsaSighashType> {
EcdsaSighashType::from_standard(self.inner).ok()
}

/// Returns the [`SchnorrSigHashType`] if the [`PsbtSighashType`] can be
/// Returns the [`SchnorrSighashType`] if the [`PsbtSighashType`] can be
/// converted to one.
pub fn schnorr_hash_ty(self) -> Option<SchnorrSigHashType> {
pub fn schnorr_hash_ty(self) -> Option<SchnorrSighashType> {
if self.inner > 0xffu32 {
None
} else {
SchnorrSigHashType::from_u8(self.inner as u8)
SchnorrSighashType::from_u8(self.inner as u8)
}
}

Expand All @@ -390,28 +390,28 @@ impl PsbtSighashType {
}

impl Input {
/// Obtains the [`EcdsaSigHashType`] for this input if one is specified. If no sighash type is
/// specified, returns [`EcdsaSigHashType::All`].
/// Obtains the [`EcdsaSighashType`] for this input if one is specified. If no sighash type is
/// specified, returns [`EcdsaSighashType::All`].
///
/// # Errors
///
/// If the `sighash_type` field is set to a non-standard ECDSA sighash value.
pub fn ecdsa_hash_ty(&self) -> Option<EcdsaSigHashType> {
pub fn ecdsa_hash_ty(&self) -> Option<EcdsaSighashType> {
self.sighash_type
.map(|sighash_type| sighash_type.ecdsa_hash_ty())
.unwrap_or(Some(EcdsaSigHashType::All))
.unwrap_or(Some(EcdsaSighashType::All))
}

/// Obtains the [`SchnorrSigHashType`] for this input if one is specified. If no sighash type is
/// specified, returns [`SchnorrSigHashType::Default`].
/// Obtains the [`SchnorrSighashType`] for this input if one is specified. If no sighash type is
/// specified, returns [`SchnorrSighashType::Default`].
///
/// # Errors
///
/// If the `sighash_type` field is set to a invalid Schnorr sighash value.
pub fn schnorr_hash_ty(&self) -> Option<SchnorrSigHashType> {
pub fn schnorr_hash_ty(&self) -> Option<SchnorrSighashType> {
self.sighash_type
.map(|sighash_type| sighash_type.schnorr_hash_ty())
.unwrap_or(Some(SchnorrSigHashType::Default))
.unwrap_or(Some(SchnorrSighashType::Default))
}

/// Create a psbt input from prevout
Expand Down
2 changes: 1 addition & 1 deletion src/pset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//!
//! Implementation of BIP174 Partially Signed Bitcoin Transaction Format as
//! defined at <https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki>
//! except we define PSETs containing non-standard SigHash types as invalid.
//! except we define PSETs containing non-standard Sighash types as invalid.
//! Extension for PSET is based on PSET defined in BIP370.
//! <https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki>

Expand Down
6 changes: 3 additions & 3 deletions src/pset/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use super::map::{PsbtSighashType, TapTree};
use crate::schnorr;
use crate::taproot::{ControlBlock, LeafVersion, TapBranchHash, TapLeafHash};

use crate::sighash::SchnorrSigHashType;
use crate::sighash::SchnorrSighashType;
use crate::taproot::TaprootBuilder;

/// A trait for serializing a value as raw data for insertion into PSET
Expand Down Expand Up @@ -300,7 +300,7 @@ impl Deserialize for schnorr::SchnorrSig {
fn deserialize(bytes: &[u8]) -> Result<Self, encode::Error> {
match bytes.len() {
65 => {
let hash_ty = SchnorrSigHashType::from_u8(bytes[64])
let hash_ty = SchnorrSighashType::from_u8(bytes[64])
.ok_or(encode::Error::ParseFailed("Invalid Sighash type"))?;
let sig = secp256k1_zkp::schnorr::Signature::from_slice(&bytes[..64])
.map_err(|_| encode::Error::ParseFailed("Invalid Schnorr signature"))?;
Expand All @@ -311,7 +311,7 @@ impl Deserialize for schnorr::SchnorrSig {
.map_err(|_| encode::Error::ParseFailed("Invalid Schnorr signature"))?;
Ok(schnorr::SchnorrSig {
sig,
hash_ty: SchnorrSigHashType::Default,
hash_ty: SchnorrSighashType::Default,
})
}
_ => Err(encode::Error::ParseFailed("Invalid Schnorr signature len")),
Expand Down
10 changes: 5 additions & 5 deletions src/schnorr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use std::fmt;

use crate::taproot::{TapBranchHash, TapTweakHash};
use crate::SchnorrSigHashType;
use crate::SchnorrSighashType;
use secp256k1_zkp::{self, constants::SCHNORR_SIGNATURE_SIZE, Secp256k1, Verification};
pub use secp256k1_zkp::{KeyPair, XOnlyPublicKey};

Expand Down Expand Up @@ -209,7 +209,7 @@ pub struct SchnorrSig {
/// The underlying schnorr signature
pub sig: secp256k1_zkp::schnorr::Signature,
/// The corresponding hash type
pub hash_ty: SchnorrSigHashType,
pub hash_ty: SchnorrSighashType,
}

impl SchnorrSig {
Expand All @@ -220,11 +220,11 @@ impl SchnorrSig {
// default type
let sig = secp256k1_zkp::schnorr::Signature::from_slice(sl)
.map_err(|_| SchnorrSigError::InvalidSchnorrSig)?;
return Ok( SchnorrSig { sig, hash_ty : SchnorrSigHashType::Default });
return Ok( SchnorrSig { sig, hash_ty : SchnorrSighashType::Default });
}
let (hash_ty, sig) = sl.split_last()
.ok_or(SchnorrSigError::InvalidSchnorrSig)?;
let hash_ty = SchnorrSigHashType::from_u8(*hash_ty)
let hash_ty = SchnorrSighashType::from_u8(*hash_ty)
.ok_or(SchnorrSigError::InvalidSighashType(*hash_ty))?;
let sig = secp256k1_zkp::schnorr::Signature::from_slice(sig)
.map_err(|_| SchnorrSigError::InvalidSchnorrSig)?;
Expand All @@ -235,7 +235,7 @@ impl SchnorrSig {
pub fn to_vec(&self) -> Vec<u8> {
// TODO: add support to serialize to a writer to SerializedSig
let mut ser_sig = self.sig.as_ref().to_vec();
if let SchnorrSigHashType::Default = self.hash_ty {
if let SchnorrSighashType::Default = self.hash_ty {
// default sighash type, don't add extra sighash byte
} else {
ser_sig.push(self.hash_ty as u8);
Expand Down
Loading

0 comments on commit 4a963db

Please sign in to comment.