Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SigHash -> Sighash #48

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ base64 = ["bitcoin/base64"]

[dependencies]
bitcoin = "0.30.0"
elements = "0.22.0"
elements = "0.23.0"
bitcoin-miniscript = { package = "miniscript", version = "10.0" }
simplicity = { git = "https://github.com/BlockstreamResearch/rust-simplicity", rev = "8ed4d4e0caf550148f1e17784af7665bad74b384" }
simplicity = { git = "https://github.com/BlockstreamResearch/rust-simplicity", rev = "d5c0d65320816bfdf36411feed4bdff0708b5b12" }

# Do NOT use this as a feature! Use the `serde` feature instead.
actual-serde = { package = "serde", version = "1.0", optional = true }
Expand Down
6 changes: 3 additions & 3 deletions bitcoind-tests/tests/test_arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!

use elements::pset::PartiallySignedTransaction as Psbt;
use elements::sighash::SigHashCache;
use elements::sighash::SighashCache;
use elements::taproot::TapLeafHash;
use elements::{
confidential, pset as psbt, secp256k1_zkp as secp256k1, sighash, OutPoint, Script, Sequence,
Expand Down Expand Up @@ -97,10 +97,10 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec
// Get all the pubkeys and the corresponding secret keys

let unsigned_tx = &psbt.extract_tx().unwrap();
let mut sighash_cache = SigHashCache::new(unsigned_tx);
let mut sighash_cache = SighashCache::new(unsigned_tx);
match derived_desc {
Descriptor::TrExt(ref tr) => {
let hash_ty = sighash::SchnorrSigHashType::Default;
let hash_ty = sighash::SchnorrSighashType::Default;

let prevouts = [witness_utxo];
let prevouts = sighash::Prevouts::All(&prevouts);
Expand Down
4 changes: 2 additions & 2 deletions bitcoind-tests/tests/test_cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ pub fn test_from_cpp_ms(cl: &ElementsD, testdata: &TestData) {
// Get the required sighash message
let amt = confidential::Value::Explicit(100_000_000);
let unsigned_tx = psbts[i].extract_tx().unwrap();
let mut sighash_cache = elements::sighash::SigHashCache::new(&unsigned_tx);
let sighash_ty = elements::EcdsaSigHashType::All;
let mut sighash_cache = elements::sighash::SighashCache::new(&unsigned_tx);
let sighash_ty = elements::EcdsaSighashType::All;
let sighash = sighash_cache.segwitv0_sighash(0, &ms.encode(), amt, sighash_ty);

// requires both signing and verification because we check the tx
Expand Down
6 changes: 3 additions & 3 deletions bitcoind-tests/tests/test_csfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!

use elements::pset::PartiallySignedTransaction as Psbt;
use elements::sighash::SigHashCache;
use elements::sighash::SighashCache;
use elements::taproot::TapLeafHash;
use elements::{
confidential, pset as psbt, secp256k1_zkp as secp256k1, sighash, OutPoint, Script, Sequence,
Expand Down Expand Up @@ -98,10 +98,10 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec
// Get all the pubkeys and the corresponding secret keys

let unsigned_tx = &psbt.extract_tx().unwrap();
let mut sighash_cache = SigHashCache::new(unsigned_tx);
let mut sighash_cache = SighashCache::new(unsigned_tx);
match derived_desc {
Descriptor::TrExt(ref tr) => {
let hash_ty = sighash::SchnorrSigHashType::Default;
let hash_ty = sighash::SchnorrSighashType::Default;

let prevouts = [witness_utxo];
let prevouts = sighash::Prevouts::All(&prevouts);
Expand Down
8 changes: 4 additions & 4 deletions bitcoind-tests/tests/test_desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{error, fmt};

use elements::hashes::{sha256d, Hash};
use elements::pset::PartiallySignedTransaction as Psbt;
use elements::sighash::SigHashCache;
use elements::sighash::SighashCache;
use elements::taproot::TapLeafHash;
use elements::{
confidential, pset as psbt, secp256k1_zkp as secp256k1, sighash, OutPoint, SchnorrSig, Script,
Expand Down Expand Up @@ -133,11 +133,11 @@ pub fn test_desc_satisfy(
// Get all the pubkeys and the corresponding secret keys

let unsigned_tx = &psbt.extract_tx().unwrap();
let mut sighash_cache = SigHashCache::new(unsigned_tx);
let mut sighash_cache = SighashCache::new(unsigned_tx);
match derived_desc {
Descriptor::Tr(ref tr) => {
// Fixme: take a parameter
let hash_ty = sighash::SchnorrSigHashType::Default;
let hash_ty = sighash::SchnorrSighashType::Default;

let internal_key_present = x_only_pks
.iter()
Expand Down Expand Up @@ -251,7 +251,7 @@ pub fn test_desc_satisfy(
.to_secp_msg();

// Fixme: Take a parameter
let hash_ty = elements::EcdsaSigHashType::All;
let hash_ty = elements::EcdsaSighashType::All;

// Finally construct the signature and add to psbt
for sk in sks_reqd {
Expand Down
6 changes: 3 additions & 3 deletions bitcoind-tests/tests/test_introspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!

use elements::pset::PartiallySignedTransaction as Psbt;
use elements::sighash::SigHashCache;
use elements::sighash::SighashCache;
use elements::taproot::TapLeafHash;
use elements::{
confidential, pset as psbt, secp256k1_zkp as secp256k1, sighash, OutPoint, Script, Sequence,
Expand Down Expand Up @@ -97,10 +97,10 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec
// Get all the pubkeys and the corresponding secret keys

let unsigned_tx = &psbt.extract_tx().unwrap();
let mut sighash_cache = SigHashCache::new(unsigned_tx);
let mut sighash_cache = SighashCache::new(unsigned_tx);
match derived_desc {
Descriptor::TrExt(ref tr) => {
let hash_ty = sighash::SchnorrSigHashType::Default;
let hash_ty = sighash::SchnorrSighashType::Default;

let prevouts = [witness_utxo];
let prevouts = sighash::Prevouts::All(&prevouts);
Expand Down
6 changes: 3 additions & 3 deletions examples/psbt_sign_finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str::FromStr;
use elements::bitcoin::PrivateKey;
use elements::encode::{serialize, serialize_hex};
use elements::hashes::Hash;
use elements::sighash::SigHashCache;
use elements::sighash::SighashCache;
use elements::{confidential, AssetId, LockTime, TxOutWitness};
use miniscript::elements::pset::PartiallySignedTransaction as Psbt;
use miniscript::elements::{
Expand Down Expand Up @@ -135,7 +135,7 @@ fn main() {
psbt.inputs_mut()[0].witness_utxo = Some(witness_utxo);

let tx = &psbt.extract_tx().unwrap();
let mut sighash_cache = SigHashCache::new(tx);
let mut sighash_cache = SighashCache::new(tx);

// genesis hash is not used at all for sighash calculation
let genesis_hash = elements::BlockHash::all_zeros();
Expand All @@ -145,7 +145,7 @@ fn main() {
.to_secp_msg();

// Fixme: Take a parameter
let hash_ty = elements::EcdsaSigHashType::All;
let hash_ty = elements::EcdsaSighashType::All;

let sk1 = backup1_private.inner;
let sk2 = backup2_private.inner;
Expand Down
2 changes: 1 addition & 1 deletion examples/sign_multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn main() {
531d75c136272f127a5dc14acc0722301cbddc222262934151f140da345af177",
)
.unwrap(),
elements::EcdsaSigHashType::All,
elements::EcdsaSighashType::All,
);

let descriptor_str = format!(
Expand Down
2 changes: 1 addition & 1 deletion examples/verify_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn main() {
let iter = interpreter.iter_custom(
Box::new(|key_sig: &KeySigPair| {
let (pk, ecdsa_sig) = key_sig.as_ecdsa().expect("Ecdsa Sig");
ecdsa_sig.1 == elements::EcdsaSigHashType::All
ecdsa_sig.1 == elements::EcdsaSighashType::All
&& secp.verify_ecdsa(&message, &ecdsa_sig.0, &pk.inner).is_ok()
}),
None, // txenv
Expand Down
12 changes: 6 additions & 6 deletions src/descriptor/csfs_cov/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ mod tests {
use elements::secp256k1_zkp::ZERO_TWEAK;
use elements::{
self, confidential, opcodes, script, secp256k1_zkp, AssetId, AssetIssuance,
EcdsaSigHashType, LockTime, OutPoint, Script, Sequence, Transaction, TxIn, TxInWitness,
EcdsaSighashType, LockTime, OutPoint, Script, Sequence, Transaction, TxIn, TxInWitness,
TxOut, Txid,
};

Expand Down Expand Up @@ -225,7 +225,7 @@ mod tests {
0,
confidential::Value::Explicit(200_000),
&script_code,
EcdsaSigHashType::All,
EcdsaSighashType::All,
);

// Create a signature to sign the input
Expand All @@ -236,7 +236,7 @@ mod tests {
&secp256k1_zkp::Message::from_slice(&sighash_u256[..]).unwrap(),
&cov_sk,
);
let el_sig = (sig, EcdsaSigHashType::All);
let el_sig = (sig, EcdsaSighashType::All);

// For satisfying the Pk part of the covenant
struct SimpleSat {
Expand Down Expand Up @@ -284,7 +284,7 @@ mod tests {
assert_eq!(
constraints.last().unwrap(),
&SatisfiedConstraint::PublicKey {
key_sig: interpreter::KeySigPair::Ecdsa(desc.pk, (sig, EcdsaSigHashType::All))
key_sig: interpreter::KeySigPair::Ecdsa(desc.pk, (sig, EcdsaSighashType::All))
}
);
Ok(())
Expand Down Expand Up @@ -429,7 +429,7 @@ mod tests {
0,
confidential::Value::Explicit(200_000),
&script_code,
EcdsaSigHashType::All,
EcdsaSighashType::All,
);

// Create a signature to sign the input
Expand All @@ -440,7 +440,7 @@ mod tests {
&secp256k1_zkp::Message::from_slice(&sighash_u256[..]).unwrap(),
&sks[0],
);
let sig = (sig, EcdsaSigHashType::All);
let sig = (sig, EcdsaSighashType::All);

// For satisfying the Pk part of the covenant
struct SimpleSat {
Expand Down
12 changes: 6 additions & 6 deletions src/descriptor/csfs_cov/satisfy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

use elements::encode::Encodable;
use elements::hashes::{sha256d, Hash};
use elements::sighash::SigHashCache;
use elements::{self, confidential, EcdsaSigHashType, OutPoint, Script, SigHash, Transaction};
use elements::sighash::SighashCache;
use elements::{self, confidential, EcdsaSighashType, OutPoint, Script, Sighash, Transaction};

use super::CovError;
use crate::{MiniscriptKey, Satisfier, ToPublicKey};
Expand All @@ -36,7 +36,7 @@ pub struct LegacyCovSatisfier<'tx, 'ptx> {
/// The input index being spent
idx: u32,
/// The sighash type
hash_type: EcdsaSigHashType,
hash_type: EcdsaSighashType,

// Segwitv0
/// The script code required for segwit sighash
Expand All @@ -53,7 +53,7 @@ impl<'tx, 'ptx> LegacyCovSatisfier<'tx, 'ptx> {
idx: u32,
value: confidential::Value,
script_code: &'ptx Script,
hash_type: EcdsaSigHashType,
hash_type: EcdsaSighashType,
) -> Self {
assert!((idx as usize) < tx.input.len());
Self {
Expand All @@ -70,8 +70,8 @@ impl<'tx, 'ptx> LegacyCovSatisfier<'tx, 'ptx> {
/// Note that this does not do any caching, so it
/// will be slightly inefficient as compared to
/// using sighash
pub fn segwit_sighash(&self) -> Result<SigHash, CovError> {
let mut cache = SigHashCache::new(self.tx);
pub fn segwit_sighash(&self) -> Result<Sighash, CovError> {
let mut cache = SighashCache::new(self.tx);
// TODO: error types
let script_code = self.script_code.ok_or(CovError::MissingScriptCode)?;
let value = self.value.ok_or(CovError::MissingValue)?;
Expand Down
6 changes: 3 additions & 3 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1579,7 +1579,7 @@ mod tests {
impl Satisfier<bitcoin::PublicKey> for SimpleSat {
fn lookup_ecdsa_sig(&self, pk: &bitcoin::PublicKey) -> Option<ElementsSig> {
if *pk == self.pk {
Some((self.sig, elements::EcdsaSigHashType::All))
Some((self.sig, elements::EcdsaSighashType::All))
} else {
None
}
Expand Down Expand Up @@ -1808,8 +1808,8 @@ mod tests {
let satisfier = {
let mut satisfier = HashMap::with_capacity(2);

satisfier.insert(a, (sig_a, ::elements::EcdsaSigHashType::All));
satisfier.insert(b, (sig_b, ::elements::EcdsaSigHashType::All));
satisfier.insert(a, (sig_a, ::elements::EcdsaSighashType::All));
satisfier.insert(b, (sig_b, ::elements::EcdsaSighashType::All));

satisfier
};
Expand Down
12 changes: 6 additions & 6 deletions src/interpreter/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ pub enum Error {
/// MultiSig missing at least `1` witness elements out of `k + 1` required
InsufficientSignaturesMultiSig,
/// Invalid Sighash type
InvalidSchnorrSigHashType(Vec<u8>),
InvalidSchnorrSighashType(Vec<u8>),
/// ecdsa Signature failed to verify
InvalidEcdsaSignature(bitcoin::PublicKey),
/// Signature failed to verify
InvalidSchnorrSignature(bitcoin::key::XOnlyPublicKey),
/// Last byte of this signature isn't a standard sighash type
NonStandardSigHash(Vec<u8>),
NonStandardSighash(Vec<u8>),
/// Miniscript error
Miniscript(crate::Error),
/// MultiSig requires 1 extra zero element apart from the `k` signatures
Expand Down Expand Up @@ -154,14 +154,14 @@ impl fmt::Display for Error {
}
Error::IncorrectWScriptHash => f.write_str("witness script did not match scriptpubkey"),
Error::InsufficientSignaturesMultiSig => f.write_str("Insufficient signatures for CMS"),
Error::InvalidSchnorrSigHashType(ref sig) => write!(
Error::InvalidSchnorrSighashType(ref sig) => write!(
f,
"Invalid sighash type for schnorr signature '{}'",
sig.to_hex()
),
Error::InvalidEcdsaSignature(pk) => write!(f, "bad ecdsa signature with pk {}", pk),
Error::InvalidSchnorrSignature(pk) => write!(f, "bad schnorr signature with pk {}", pk),
Error::NonStandardSigHash(ref sig) => write!(
Error::NonStandardSighash(ref sig) => write!(
f,
"Non standard sighash type for signature '{}'",
sig.to_hex()
Expand Down Expand Up @@ -233,8 +233,8 @@ impl error::Error for Error {
| InsufficientSignaturesMultiSig
| InvalidEcdsaSignature(_)
| InvalidSchnorrSignature(_)
| InvalidSchnorrSigHashType(_)
| NonStandardSigHash(_)
| InvalidSchnorrSighashType(_)
| NonStandardSighash(_)
| MissingExtraZeroMultiSig
| MultiSigEvaluationError
| NonEmptyWitness
Expand Down
14 changes: 7 additions & 7 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::str::FromStr;

use bitcoin;
use elements::hashes::{hash160, ripemd160, sha256, Hash, HashEngine};
use elements::{self, secp256k1_zkp, sighash, EcdsaSigHashType, LockTime, Sequence, SigHash};
use elements::{self, secp256k1_zkp, sighash, EcdsaSighashType, LockTime, Sequence, Sighash};

use crate::extensions::{CovExtArgs, ParseableExt, TxEnv};
use crate::miniscript::context::{NoChecks, SigType};
Expand Down Expand Up @@ -259,7 +259,7 @@ where
sighash::Prevouts::All(prevouts) => prevouts.get(input_index),
}
}
let mut cache = elements::sighash::SigHashCache::new(tx);
let mut cache = elements::sighash::SighashCache::new(tx);
match sig {
KeySigPair::Ecdsa(key, ecdsa_sig) => {
let script_pubkey = self.script_code.as_ref().expect("Legacy have script code");
Expand Down Expand Up @@ -1101,7 +1101,7 @@ where
{
let sighash_bytes = self.stack[1].as_push().expect("Push checked above");
let sighash_u32 = util::slice_to_u32_le(sighash_bytes);
let sighash_ty = EcdsaSigHashType::from_u32(sighash_u32);
let sighash_ty = EcdsaSighashType::from_u32(sighash_u32);
let sig_vec = self.stack[0].as_push().expect("Size checked above");
ser_sig.extend(sig_vec);
ser_sig.push(sighash_ty as u8);
Expand All @@ -1117,9 +1117,9 @@ where
.rev()
.flat_map(|x| Vec::from(x.as_push().expect("Push checked above")))
.collect();
let mut eng = SigHash::engine();
let mut eng = Sighash::engine();
eng.input(&sighash_msg);
let sighash_u256 = SigHash::from_engine(eng);
let sighash_u256 = Sighash::from_engine(eng);
let msg = elements::secp256k1_zkp::Message::from_slice(&sighash_u256[..]).unwrap();

// Legacy Cov scripts only operate on Ecdsa key sig pairs
Expand Down Expand Up @@ -1242,7 +1242,7 @@ mod tests {
compressed: true,
};
let sig = secp.sign_ecdsa(&msg, &sk);
ecdsa_sigs.push((sig, elements::EcdsaSigHashType::All));
ecdsa_sigs.push((sig, elements::EcdsaSighashType::All));
let mut sigser = sig.serialize_der().to_vec();
sigser.push(0x01); // sighash_all
pks.push(pk);
Expand All @@ -1254,7 +1254,7 @@ mod tests {
let schnorr_sig = secp.sign_schnorr_with_aux_rand(&msg, &keypair, &[0u8; 32]);
let schnorr_sig = elements::SchnorrSig {
sig: schnorr_sig,
hash_ty: elements::SchnorrSigHashType::Default,
hash_ty: elements::SchnorrSighashType::Default,
};
ser_schnorr_sigs.push(schnorr_sig.to_vec());
schnorr_sigs.push(schnorr_sig);
Expand Down
2 changes: 1 addition & 1 deletion src/miniscript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@ mod tests {
) -> Option<elements::SchnorrSig> {
Some(elements::SchnorrSig {
sig: self.0,
hash_ty: elements::SchnorrSigHashType::Default,
hash_ty: elements::SchnorrSighashType::Default,
})
}
}
Expand Down
Loading
Loading