1010use bitcoin::amount::Amount;
1111use bitcoin::constants::ChainHash;
1212use bitcoin::script::{Script, ScriptBuf, Builder, WScriptHash};
13- use bitcoin::transaction::{Transaction, TxIn};
13+ use bitcoin::transaction::{Transaction, TxIn, TxOut };
1414use bitcoin::sighash::EcdsaSighashType;
1515use bitcoin::consensus::encode;
1616use bitcoin::absolute::LockTime;
@@ -31,11 +31,9 @@ use crate::types::payment::{PaymentPreimage, PaymentHash};
3131use crate::types::features::{ChannelTypeFeatures, InitFeatures};
3232use crate::ln::interactivetxs::{
3333 get_output_weight, HandleTxCompleteValue, HandleTxCompleteResult, InteractiveTxConstructor,
34- InteractiveTxConstructorArgs, InteractiveTxSigningSession, InteractiveTxMessageSendResult,
35- TX_COMMON_FIELDS_WEIGHT,
34+ InteractiveTxConstructorArgs, InteractiveTxMessageSend, InteractiveTxSigningSession, InteractiveTxMessageSendResult,
35+ OutputOwned, SharedOwnedOutput, TX_COMMON_FIELDS_WEIGHT,
3636};
37- #[cfg(splicing)]
38- use crate::ln::interactivetxs::InteractiveTxMessageSend;
3937use crate::ln::msgs;
4038use crate::ln::msgs::{ClosingSigned, ClosingSignedFeeRange, DecodeError};
4139use crate::ln::script::{self, ShutdownScript};
@@ -1713,6 +1711,58 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
17131711
17141712 fn dual_funding_context(&self) -> &DualFundingChannelContext;
17151713
1714+ fn dual_funding_context_mut(&mut self) -> &mut DualFundingChannelContext;
1715+
1716+ fn is_initiator(&self) -> bool;
1717+
1718+ fn begin_interactive_funding_tx_construction<ES: Deref>(
1719+ &mut self, entropy_source: &ES, holder_node_id: PublicKey,
1720+ ) -> Result<Option<InteractiveTxMessageSend>, APIError>
1721+ where ES::Target: EntropySource
1722+ {
1723+ let mut funding_outputs = Vec::new();
1724+ let funding_output_value_satoshis = self.context().get_value_satoshis();
1725+ let funding_output_script_pubkey = self.context().get_funding_redeemscript().to_p2wsh();
1726+ let expected_remote_shared_funding_output = if self.is_initiator() {
1727+ let tx_out = TxOut {
1728+ value: Amount::from_sat(funding_output_value_satoshis),
1729+ script_pubkey: funding_output_script_pubkey,
1730+ };
1731+ funding_outputs.push(
1732+ if self.dual_funding_context().their_funding_satoshis.unwrap_or(0) == 0 {
1733+ OutputOwned::SharedControlFullyOwned(tx_out)
1734+ } else {
1735+ OutputOwned::Shared(SharedOwnedOutput::new(
1736+ tx_out, self.dual_funding_context().our_funding_satoshis
1737+ ))
1738+ }
1739+ );
1740+ None
1741+ } else {
1742+ Some((funding_output_script_pubkey, funding_output_value_satoshis))
1743+ };
1744+
1745+ let constructor_args = InteractiveTxConstructorArgs {
1746+ entropy_source,
1747+ holder_node_id,
1748+ counterparty_node_id: self.context().counterparty_node_id,
1749+ channel_id: self.context().channel_id(),
1750+ feerate_sat_per_kw: self.dual_funding_context_mut().funding_feerate_sat_per_1000_weight,
1751+ is_initiator: self.is_initiator(),
1752+ funding_tx_locktime: self.dual_funding_context_mut().funding_tx_locktime,
1753+ inputs_to_contribute: self.dual_funding_context_mut().our_funding_inputs.take().unwrap_or_else(|| vec![]),
1754+ outputs_to_contribute: funding_outputs,
1755+ expected_remote_shared_funding_output,
1756+ };
1757+ let mut tx_constructor = InteractiveTxConstructor::new(constructor_args)
1758+ .map_err(|_| APIError::APIMisuseError { err: "Incorrect shared output provided".into() })?;
1759+ let msg = tx_constructor.take_initiator_first_message();
1760+
1761+ self.interactive_tx_constructor_mut().replace(tx_constructor);
1762+
1763+ Ok(msg)
1764+ }
1765+
17161766 fn tx_add_input(&mut self, msg: &msgs::TxAddInput) -> InteractiveTxMessageSendResult {
17171767 InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
17181768 Some(ref mut tx_constructor) => tx_constructor.handle_tx_add_input(msg).map_err(
@@ -1875,9 +1925,15 @@ impl<SP: Deref> InteractivelyFunded<SP> for OutboundV2Channel<SP> where SP::Targ
18751925 fn dual_funding_context(&self) -> &DualFundingChannelContext {
18761926 &self.dual_funding_context
18771927 }
1928+ fn dual_funding_context_mut(&mut self) -> &mut DualFundingChannelContext {
1929+ &mut self.dual_funding_context
1930+ }
18781931 fn interactive_tx_constructor_mut(&mut self) -> &mut Option<InteractiveTxConstructor> {
18791932 &mut self.interactive_tx_constructor
18801933 }
1934+ fn is_initiator(&self) -> bool {
1935+ true
1936+ }
18811937}
18821938
18831939impl<SP: Deref> InteractivelyFunded<SP> for InboundV2Channel<SP> where SP::Target: SignerProvider {
@@ -1890,9 +1946,15 @@ impl<SP: Deref> InteractivelyFunded<SP> for InboundV2Channel<SP> where SP::Targe
18901946 fn dual_funding_context(&self) -> &DualFundingChannelContext {
18911947 &self.dual_funding_context
18921948 }
1949+ fn dual_funding_context_mut(&mut self) -> &mut DualFundingChannelContext {
1950+ &mut self.dual_funding_context
1951+ }
18931952 fn interactive_tx_constructor_mut(&mut self) -> &mut Option<InteractiveTxConstructor> {
18941953 &mut self.interactive_tx_constructor
18951954 }
1955+ fn is_initiator(&self) -> bool {
1956+ false
1957+ }
18961958}
18971959
18981960impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
@@ -4295,6 +4357,8 @@ pub(super) fn calculate_our_funding_satoshis(
42954357pub(super) struct DualFundingChannelContext {
42964358 /// The amount in satoshis we will be contributing to the channel.
42974359 pub our_funding_satoshis: u64,
4360+ /// The amount in satoshis our counterparty will be contributing to the channel.
4361+ pub their_funding_satoshis: Option<u64>,
42984362 /// The funding transaction locktime suggested by the initiator. If set by us, it is always set
42994363 /// to the current block height to align incentives against fee-sniping.
43004364 pub funding_tx_locktime: LockTime,
@@ -4306,7 +4370,7 @@ pub(super) struct DualFundingChannelContext {
43064370 /// minus any fees paid for our contributed weight. This means that change will never be generated
43074371 /// and the maximum value possible will go towards funding the channel.
43084372 #[allow(dead_code)] // TODO(dual_funding): Remove once contribution to V2 channels is enabled.
4309- pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
4373+ pub our_funding_inputs: Option< Vec<(TxIn, TransactionU16LenLimited)> >,
43104374}
43114375
43124376// Holder designates channel data owned for the benefit of the user client.
@@ -9047,9 +9111,10 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
90479111 unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
90489112 dual_funding_context: DualFundingChannelContext {
90499113 our_funding_satoshis: funding_satoshis,
9114+ their_funding_satoshis: None,
90509115 funding_tx_locktime,
90519116 funding_feerate_sat_per_1000_weight,
9052- our_funding_inputs: funding_inputs,
9117+ our_funding_inputs: Some( funding_inputs) ,
90539118 },
90549119 interactive_tx_constructor: None,
90559120 };
@@ -9212,9 +9277,10 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
92129277
92139278 let dual_funding_context = DualFundingChannelContext {
92149279 our_funding_satoshis: funding_satoshis,
9280+ their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
92159281 funding_tx_locktime: LockTime::from_consensus(msg.locktime),
92169282 funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
9217- our_funding_inputs: funding_inputs.clone(),
9283+ our_funding_inputs: Some( funding_inputs.clone() ),
92189284 };
92199285
92209286 let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
0 commit comments