ddk_manager/contract/
signed_contract.rs

1//! #SignedContract
2
3use crate::conversion_utils::PROTOCOL_VERSION;
4use crate::utils::get_new_serial_id;
5use crate::ChannelId;
6
7use super::accepted_contract::AcceptedContract;
8use ddk_dlc::dlc_input::DlcInputInfo;
9use ddk_messages::CetAdaptorSignature;
10use ddk_messages::CetAdaptorSignatures;
11use ddk_messages::FundingSignatures;
12use ddk_messages::SignDlc;
13use secp256k1_zkp::ecdsa::Signature;
14use secp256k1_zkp::EcdsaAdaptorSignature;
15
16/// Contain information about a contract that was fully signed.
17#[derive(Clone)]
18pub struct SignedContract {
19    /// The accepted contract that was signed.
20    pub accepted_contract: AcceptedContract,
21    /// The adaptor signatures of the offering party (None if offering party).
22    pub adaptor_signatures: Vec<EcdsaAdaptorSignature>,
23    /// The refund signature of the offering party.
24    pub offer_refund_signature: Signature,
25    /// The signatures for the funding inputs of the offering party.
26    pub funding_signatures: FundingSignatures,
27    /// The [`ChannelId`] to which the contract was associated if any.
28    pub channel_id: Option<ChannelId>,
29}
30
31impl SignedContract {
32    /// Construct the sign DLC message from the signed contract
33    pub fn get_sign_dlc(&self, cet_adaptor_signatures: Vec<EcdsaAdaptorSignature>) -> SignDlc {
34        let contract_id = self.accepted_contract.get_contract_id();
35
36        SignDlc {
37            protocol_version: PROTOCOL_VERSION,
38            contract_id,
39            cet_adaptor_signatures: CetAdaptorSignatures {
40                ecdsa_adaptor_signatures: cet_adaptor_signatures
41                    .into_iter()
42                    .map(|x| CetAdaptorSignature { signature: x })
43                    .collect(),
44            },
45            refund_signature: self.offer_refund_signature,
46            funding_signatures: self.funding_signatures.clone(),
47        }
48    }
49
50    /// Use an existing contract to create a funding input for a spliced DLC.
51    pub(crate) fn get_dlc_input(&self) -> DlcInputInfo {
52        let fund_tx = self.accepted_contract.dlc_transactions.fund.clone();
53
54        let fund_vout = self
55            .accepted_contract
56            .dlc_transactions
57            .get_fund_output_index() as u32;
58        let local_fund_pubkey = self
59            .accepted_contract
60            .offered_contract
61            .offer_params
62            .fund_pubkey;
63        let remote_fund_pubkey = self.accepted_contract.accept_params.fund_pubkey;
64        let fund_amount = self
65            .accepted_contract
66            .dlc_transactions
67            .get_fund_output()
68            .value;
69
70        DlcInputInfo {
71            fund_tx,
72            fund_vout,
73            local_fund_pubkey,
74            remote_fund_pubkey,
75            fund_amount,
76            max_witness_len: 220,
77            input_serial_id: get_new_serial_id(),
78            contract_id: self.accepted_contract.get_contract_id(),
79        }
80    }
81}