Skip to content

Commit c8bdf5b

Browse files
authored
backport: fix: change all taproot sighashes to Default and better parsing (#934) (#939)
fix: change all taproot sighashes to `Default` and better parsing (#934) * fix: change all taproot sighashes to Default - we save 1 byte * fix(l1tx): robust parsing of taproot signatures
1 parent 881983f commit c8bdf5b

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

crates/key-derivation/src/operator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ mod tests {
344344

345345
// Create the PSBT
346346
let mut psbt = Psbt::from_unsigned_tx(transaction).expect("could not create PSBT");
347-
let ty = TapSighashType::All.into();
347+
let ty = TapSighashType::Default.into();
348348
let origins = BTreeMap::from([(
349349
x_only_pubkey,
350350
(vec![], (wallet_fingerprint, derivation_path)),

crates/l1tx/src/deposit/deposit_tx.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use bitcoin::{
55
key::TapTweak,
66
opcodes::all::OP_RETURN,
77
sighash::{Prevouts, SighashCache},
8-
taproot::TAPROOT_CONTROL_NODE_SIZE,
9-
Amount, OutPoint, ScriptBuf, TapNodeHash, TapSighashType, Transaction, TxOut, XOnlyPublicKey,
8+
taproot::{self, TAPROOT_CONTROL_NODE_SIZE},
9+
Amount, OutPoint, ScriptBuf, TapNodeHash, Transaction, TxOut, XOnlyPublicKey,
1010
};
11-
use secp256k1::{constants::SCHNORR_SIGNATURE_SIZE, schnorr::Signature, Message};
11+
use secp256k1::Message;
1212
use strata_primitives::{
1313
buf::Buf32,
1414
l1::{DepositInfo, OutputRef},
@@ -71,7 +71,7 @@ pub fn extract_deposit_info(tx: &Transaction, config: &DepositTxParams) -> Optio
7171
})
7272
}
7373

74-
/// Validate that the transaction has been signed off by the N of N operators pubkey.
74+
/// Validate that the transaction has been signed off by the N-of-N operators pubkey.
7575
fn validate_deposit_signature(
7676
tx: &Transaction,
7777
tag_data: &DepositTag<'_>,
@@ -92,11 +92,12 @@ fn validate_deposit_signature(
9292
return None;
9393
}
9494
let sig_witness = &input.witness[0];
95-
if sig_witness.len() < SCHNORR_SIGNATURE_SIZE {
96-
return None;
97-
}
98-
let sig_bytes = &sig_witness[..SCHNORR_SIGNATURE_SIZE];
99-
let schnorr_sig = Signature::from_slice(sig_bytes).ok()?;
95+
96+
// rust-bitcoin taproot::Signature handles both both 64-byte (SIGHASH_DEFAULT)
97+
// and 65-byte (explicit sighash) signatures.
98+
let taproot_sig = taproot::Signature::from_slice(sig_witness).ok()?;
99+
let schnorr_sig = taproot_sig.signature;
100+
let sighash_type = taproot_sig.sighash_type;
100101

101102
// Parse the internal pubkey and merkle root
102103
let internal_pubkey = dep_config.operators_pubkey;
@@ -116,7 +117,8 @@ fn validate_deposit_signature(
116117
// Compute the sighash
117118
let prevout = Prevouts::All(&utxos);
118119
let sighash = SighashCache::new(tx)
119-
.taproot_key_spend_signature_hash(0, &prevout, TapSighashType::All)
120+
// NOTE: preserving the original sighash_type.
121+
.taproot_key_spend_signature_hash(0, &prevout, sighash_type)
120122
.unwrap();
121123

122124
// Prepare the message for signature verification

crates/test-utils/src/bitcoin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub fn create_test_deposit_tx(
135135
let prevtxout = [prev_txout];
136136
let prevouts = Prevouts::All(&prevtxout);
137137
let sighash = SighashCache::new(&mut tx)
138-
.taproot_key_spend_signature_hash(0, &prevouts, TapSighashType::All)
138+
.taproot_key_spend_signature_hash(0, &prevouts, TapSighashType::Default)
139139
.unwrap();
140140

141141
let msg = Message::from_digest(*sighash.as_ref());

0 commit comments

Comments
 (0)