Skip to content

Commit

Permalink
fix sighash errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mmtftr committed Feb 3, 2025
1 parent ba1d352 commit abdabe6
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 117 deletions.
44 changes: 9 additions & 35 deletions core/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,13 @@ impl Actor {
txin_index: usize,
script_index: usize,
) -> Result<schnorr::Signature, BridgeError> {
let mut sighash_cache: SighashCache<&mut bitcoin::Transaction> =
SighashCache::new(&mut tx.tx);

let sig_hash = sighash_cache.taproot_script_spend_signature_hash(
let sighash = tx.calculate_script_spend_sighash_indexed(
txin_index,
&bitcoin::sighash::Prevouts::All(&tx.prevouts),
TapLeafHash::from_script(
&tx.prev_scripts[txin_index][script_index],
LeafVersion::TapScript,
),
bitcoin::sighash::TapSighashType::Default,
script_index,
TapSighashType::Default,
)?;

Ok(self.sign(sig_hash))
Ok(self.sign(sighash))
}

#[tracing::instrument(skip(self), err(level = tracing::Level::ERROR), ret(level = tracing::Level::TRACE))]
Expand All @@ -182,19 +175,7 @@ impl Actor {
input_index: usize,
sighash_type: Option<TapSighashType>,
) -> Result<schnorr::Signature, BridgeError> {
let mut sighash_cache = SighashCache::new(&mut tx_handler.tx);

let sig_hash = sighash_cache.taproot_key_spend_signature_hash(
input_index,
&match sighash_type {
Some(TapSighashType::SinglePlusAnyoneCanPay) => bitcoin::sighash::Prevouts::One(
input_index,
tx_handler.prevouts[input_index].clone(),
),
_ => bitcoin::sighash::Prevouts::All(&tx_handler.prevouts),
},
sighash_type.unwrap_or(TapSighashType::Default),
)?;
let sig_hash = tx_handler.calculate_pubkey_spend_sighash(input_index, sighash_type)?;

self.sign_with_tweak(sig_hash, None)
}
Expand Down Expand Up @@ -248,20 +229,13 @@ impl Actor {
txin_index: usize,
script_index: usize,
) -> Result<schnorr::Signature, BridgeError> {
let mut sighash_cache: SighashCache<&mut bitcoin::Transaction> =
SighashCache::new(&mut tx_handler.tx);

let sig_hash = sighash_cache.taproot_script_spend_signature_hash(
let sighash = tx_handler.calculate_script_spend_sighash_indexed(
txin_index,
&bitcoin::sighash::Prevouts::All(&tx_handler.prevouts),
TapLeafHash::from_script(
&tx_handler.prev_scripts[txin_index][script_index],
LeafVersion::TapScript,
),
bitcoin::sighash::TapSighashType::Default,
script_index,
TapSighashType::Default,
)?;

self.sign_with_tweak(sig_hash, None)
self.sign_with_tweak(sighash, None)
}

/// Returns derivied Winternitz secret key from given path.
Expand Down
18 changes: 9 additions & 9 deletions core/src/builder/sighash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub fn create_nofn_sighash_stream(
let mut challenge_tx = builder::transaction::create_challenge_txhandler(
&kickoff_txhandler,
operator_reimburse_address,
);
)?;

// Yields the sighash for the challenge_tx.input[0], which spends kickoff_tx.input[1] using SinglePlusAnyoneCanPay.
yield challenge_tx.calculate_pubkey_spend_sighash(
Expand Down Expand Up @@ -172,12 +172,12 @@ pub fn create_nofn_sighash_stream(
.map(|i| watchtower_all_challenge_addresses[i][sequential_collateral_tx_idx * config.num_kickoffs_per_sequential_collateral_tx + kickoff_idx].clone())
.collect::<Vec<_>>();

let mut watchtower_challenge_kickoff_txhandler =
let watchtower_challenge_kickoff_txhandler =
builder::transaction::create_watchtower_challenge_kickoff_txhandler_simplified(
&kickoff_txhandler,
config.num_watchtowers as u32,
&watchtower_challenge_addresses,
);
)?;

// Yields the sighash for the watchtower_challenge_kickoff_tx.input[0], which spends kickoff_tx.input[0].
yield watchtower_challenge_kickoff_txhandler.calculate_pubkey_spend_sighash(
Expand Down Expand Up @@ -213,14 +213,14 @@ pub fn create_nofn_sighash_stream(
nofn_xonly_pk,
*operator_xonly_pk,
network,
);
)?;

// Creates the operator_challenge_NACK_tx handler.
let mut operator_challenge_nack_txhandler =
builder::transaction::create_operator_challenge_nack_txhandler(
&watchtower_challenge_txhandler,
&kickoff_txhandler
);
)?;

// Yields the sighash for the operator_challenge_NACK_tx.input[0], which spends watchtower_challenge_tx.output[0].
yield operator_challenge_nack_txhandler.calculate_script_spend_sighash_indexed(
Expand All @@ -246,7 +246,7 @@ pub fn create_nofn_sighash_stream(
)?;

// Creates the assert_end_tx handler.
let mut assert_end_txhandler = builder::transaction::create_assert_end_txhandler(
let assert_end_txhandler = builder::transaction::create_assert_end_txhandler(
&kickoff_txhandler,
&assert_begin_txhandler,
&assert_tx_addrs,
Expand Down Expand Up @@ -286,7 +286,7 @@ pub fn create_nofn_sighash_stream(
let mut already_disproved_txhandler = builder::transaction::create_already_disproved_txhandler(
&assert_end_txhandler,
&sequential_collateral_txhandler,
);
)?;

// Yields the sighash for the already_disproved_tx.input[0], which spends assert_end_tx.output[1].
yield already_disproved_txhandler.calculate_script_spend_sighash_indexed(
Expand Down Expand Up @@ -431,7 +431,7 @@ pub fn create_operator_sighash_stream(
let mut already_disproved_txhandler = builder::transaction::create_already_disproved_txhandler(
&assert_end_txhandler,
&sequential_collateral_txhandler,
);
)?;

// Yields the sighash for the already_disproved_tx.input[0], which spends assert_end_tx.output[1].
yield already_disproved_txhandler.calculate_pubkey_spend_sighash(
Expand All @@ -442,7 +442,7 @@ pub fn create_operator_sighash_stream(
let mut disprove_txhandler = builder::transaction::create_disprove_txhandler(
&assert_end_txhandler,
&sequential_collateral_txhandler,
);
)?;

// Yields the sighash for the disprove_tx.input[1], which spends sequential_collateral_tx.output[0].
yield disprove_txhandler.calculate_pubkey_spend_sighash(
Expand Down
Loading

0 comments on commit abdabe6

Please sign in to comment.