From 74bb611851d0913a6f1164404b6be742d2bf0ec4 Mon Sep 17 00:00:00 2001 From: conduition Date: Thu, 29 Feb 2024 05:23:55 +0000 Subject: [PATCH] reusable method for computing funding output --- src/contract/mod.rs | 14 +++++++++++++- src/spend_info/funding.rs | 13 +++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/contract/mod.rs b/src/contract/mod.rs index ee14271..7380f12 100644 --- a/src/contract/mod.rs +++ b/src/contract/mod.rs @@ -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 { + 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 { let input_weights = [InputWeightPrediction::P2TR_KEY_DEFAULT_SIGHASH]; let fee = fees::fee_calc_safe(self.fee_rate, input_weights, [P2TR_SCRIPT_PUBKEY_SIZE])?; diff --git a/src/spend_info/funding.rs b/src/spend_info/funding.rs index 0cc67f6..4ec2ca4 100644 --- a/src/spend_info/funding.rs +++ b/src/spend_info/funding.rs @@ -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 { - 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,