From 6bfd1e60b37c4873bea0760347eb6f89d711c318 Mon Sep 17 00:00:00 2001 From: conduition Date: Sun, 18 Feb 2024 17:11:20 +0000 Subject: [PATCH] add sighash methods for sweep transactions --- src/contract/outcome.rs | 6 ---- src/spend_info/split.rs | 64 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/src/contract/outcome.rs b/src/contract/outcome.rs index c7b1034..25a1961 100644 --- a/src/contract/outcome.rs +++ b/src/contract/outcome.rs @@ -24,12 +24,6 @@ impl OutcomeTransactionBuildOutput { pub fn outcome_txs(&self) -> &[Transaction] { &self.outcome_txs } - - // /// Return a slice of the spending info objects needed to spend - // /// from an outcome transaction. - // pub(crate) fn outcome_spend_infos(&self) -> &[OutcomeSpendInfo] { - // &self.outcome_spend_infos - // } } /// Construct a set of unsigned outcome transactions which spend from the funding TX. diff --git a/src/spend_info/split.rs b/src/spend_info/split.rs index 5e24f9d..4ca937c 100644 --- a/src/spend_info/split.rs +++ b/src/spend_info/split.rs @@ -1,13 +1,16 @@ use bitcoin::{ key::constants::SCHNORR_SIGNATURE_SIZE, opcodes::all::*, - taproot::{LeafVersion, TaprootSpendInfo}, + sighash::{Prevouts, SighashCache}, + taproot::{LeafVersion, TapLeafHash, TaprootSpendInfo}, transaction::InputWeightPrediction, - Amount, ScriptBuf, + Amount, ScriptBuf, TapSighash, TapSighashType, Transaction, TxOut, }; use musig2::KeyAggContext; use secp::Point; +use std::borrow::Borrow; + use crate::{ errors::Error, hashlock::PREIMAGE_SIZE, @@ -208,5 +211,60 @@ impl SplitSpendInfo { ) } - // pub(crate) fn sighash_tx_win(&self) + /// Derive the signature hash for a win transaction, which spends from + /// a split transaction. + pub(crate) fn sighash_tx_win>( + &self, + win_tx: &Transaction, + input_index: usize, + prevouts: &Prevouts, + ) -> Result { + let leaf_hash = TapLeafHash::from_script(&self.win_script, LeafVersion::TapScript); + + let sighash = SighashCache::new(win_tx).taproot_script_spend_signature_hash( + input_index, + prevouts, + leaf_hash, + TapSighashType::Default, + )?; + Ok(sighash) + } + + /// Derive the signature hash for a reclaim transaction, which spends from + /// a split transaction. + pub(crate) fn sighash_tx_reclaim>( + &self, + reclaim_tx: &Transaction, + input_index: usize, + prevouts: &Prevouts, + ) -> Result { + let leaf_hash = TapLeafHash::from_script(&self.reclaim_script, LeafVersion::TapScript); + + let sighash = SighashCache::new(reclaim_tx).taproot_script_spend_signature_hash( + input_index, + prevouts, + leaf_hash, + TapSighashType::Default, + )?; + Ok(sighash) + } + + /// Derive the signature hash for a sellback transaction, which spends from + /// a split transaction. + pub(crate) fn sighash_tx_sellback>( + &self, + sellback_tx: &Transaction, + input_index: usize, + prevouts: &Prevouts, + ) -> Result { + let leaf_hash = TapLeafHash::from_script(&self.sellback_script, LeafVersion::TapScript); + + let sighash = SighashCache::new(sellback_tx).taproot_script_spend_signature_hash( + input_index, + prevouts, + leaf_hash, + TapSighashType::Default, + )?; + Ok(sighash) + } }