reusable method for computing funding output

This commit is contained in:
conduition
2024-02-29 05:23:55 +00:00
parent 5d2fdc809e
commit 74bb611851
2 changed files with 22 additions and 5 deletions

View File

@@ -2,7 +2,7 @@ pub(crate) mod fees;
pub(crate) mod outcome;
pub(crate) mod split;
use bitcoin::{transaction::InputWeightPrediction, Amount, FeeRate};
use bitcoin::{transaction::InputWeightPrediction, Amount, FeeRate, TxOut};
use secp::Point;
use crate::{
@@ -10,6 +10,7 @@ use crate::{
errors::Error,
oracles::EventAnnouncment,
parties::{MarketMaker, Player},
spend_info::FundingSpendInfo,
};
use std::collections::{BTreeMap, BTreeSet};
@@ -92,6 +93,17 @@ pub struct WinCondition {
}
impl ContractParameters {
/// Returns the transaction output which the funding transaction should pay to.
///
/// Avoid overusing this method, as it recomputes the aggregated key every time
/// it is invoked. Instead, prefer
/// [`TicketedDLC::funding_output`][crate::TicketedDLC::funding_output].
pub fn funding_output(&self) -> Result<TxOut, Error> {
let spend_info =
FundingSpendInfo::new(&self.market_maker, &self.players, self.funding_value)?;
Ok(spend_info.funding_output())
}
pub(crate) fn outcome_output_value(&self) -> Result<Amount, Error> {
let input_weights = [InputWeightPrediction::P2TR_KEY_DEFAULT_SIGHASH];
let fee = fees::fee_calc_safe(self.fee_rate, input_weights, [P2TR_SCRIPT_PUBKEY_SIZE])?;

View File

@@ -42,6 +42,14 @@ impl FundingSpendInfo {
&self.key_agg_ctx
}
/// Returns the transaction output which the funding transaction should pay to.
pub(crate) fn funding_output(&self) -> TxOut {
TxOut {
script_pubkey: self.script_pubkey(),
value: self.funding_value,
}
}
/// Returns the TX locking script for funding the ticketed DLC multisig.
pub(crate) fn script_pubkey(&self) -> ScriptBuf {
// This is safe because the musig key aggregation formula prevents
@@ -56,10 +64,7 @@ impl FundingSpendInfo {
&self,
outcome_tx: &Transaction,
) -> Result<TapSighash, bitcoin::sighash::Error> {
let funding_prevouts = [TxOut {
script_pubkey: self.script_pubkey(),
value: self.funding_value,
}];
let funding_prevouts = [self.funding_output()];
SighashCache::new(outcome_tx).taproot_key_spend_signature_hash(
0,