10
10
use bitcoin::amount::Amount;
11
11
use bitcoin::constants::ChainHash;
12
12
use bitcoin::script::{Script, ScriptBuf, Builder, WScriptHash};
13
- use bitcoin::transaction::{Transaction, TxIn};
13
+ use bitcoin::transaction::{Transaction, TxIn, TxOut };
14
14
use bitcoin::sighash::EcdsaSighashType;
15
15
use bitcoin::consensus::encode;
16
16
use bitcoin::absolute::LockTime;
@@ -31,11 +31,9 @@ use crate::types::payment::{PaymentPreimage, PaymentHash};
31
31
use crate::types::features::{ChannelTypeFeatures, InitFeatures};
32
32
use crate::ln::interactivetxs::{
33
33
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,
36
36
};
37
- #[cfg(splicing)]
38
- use crate::ln::interactivetxs::InteractiveTxMessageSend;
39
37
use crate::ln::msgs;
40
38
use crate::ln::msgs::{ClosingSigned, ClosingSignedFeeRange, DecodeError};
41
39
use crate::ln::script::{self, ShutdownScript};
@@ -1713,6 +1711,58 @@ pub(super) trait InteractivelyFunded<SP: Deref> where SP::Target: SignerProvider
1713
1711
1714
1712
fn dual_funding_context(&self) -> &DualFundingChannelContext;
1715
1713
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
+
1716
1766
fn tx_add_input(&mut self, msg: &msgs::TxAddInput) -> InteractiveTxMessageSendResult {
1717
1767
InteractiveTxMessageSendResult(match self.interactive_tx_constructor_mut() {
1718
1768
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
1875
1925
fn dual_funding_context(&self) -> &DualFundingChannelContext {
1876
1926
&self.dual_funding_context
1877
1927
}
1928
+ fn dual_funding_context_mut(&mut self) -> &mut DualFundingChannelContext {
1929
+ &mut self.dual_funding_context
1930
+ }
1878
1931
fn interactive_tx_constructor_mut(&mut self) -> &mut Option<InteractiveTxConstructor> {
1879
1932
&mut self.interactive_tx_constructor
1880
1933
}
1934
+ fn is_initiator(&self) -> bool {
1935
+ true
1936
+ }
1881
1937
}
1882
1938
1883
1939
impl<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
1890
1946
fn dual_funding_context(&self) -> &DualFundingChannelContext {
1891
1947
&self.dual_funding_context
1892
1948
}
1949
+ fn dual_funding_context_mut(&mut self) -> &mut DualFundingChannelContext {
1950
+ &mut self.dual_funding_context
1951
+ }
1893
1952
fn interactive_tx_constructor_mut(&mut self) -> &mut Option<InteractiveTxConstructor> {
1894
1953
&mut self.interactive_tx_constructor
1895
1954
}
1955
+ fn is_initiator(&self) -> bool {
1956
+ false
1957
+ }
1896
1958
}
1897
1959
1898
1960
impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
@@ -4295,6 +4357,8 @@ pub(super) fn calculate_our_funding_satoshis(
4295
4357
pub(super) struct DualFundingChannelContext {
4296
4358
/// The amount in satoshis we will be contributing to the channel.
4297
4359
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>,
4298
4362
/// The funding transaction locktime suggested by the initiator. If set by us, it is always set
4299
4363
/// to the current block height to align incentives against fee-sniping.
4300
4364
pub funding_tx_locktime: LockTime,
@@ -4306,7 +4370,7 @@ pub(super) struct DualFundingChannelContext {
4306
4370
/// minus any fees paid for our contributed weight. This means that change will never be generated
4307
4371
/// and the maximum value possible will go towards funding the channel.
4308
4372
#[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)> >,
4310
4374
}
4311
4375
4312
4376
// 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 {
9047
9111
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
9048
9112
dual_funding_context: DualFundingChannelContext {
9049
9113
our_funding_satoshis: funding_satoshis,
9114
+ their_funding_satoshis: None,
9050
9115
funding_tx_locktime,
9051
9116
funding_feerate_sat_per_1000_weight,
9052
- our_funding_inputs: funding_inputs,
9117
+ our_funding_inputs: Some( funding_inputs) ,
9053
9118
},
9054
9119
interactive_tx_constructor: None,
9055
9120
};
@@ -9212,9 +9277,10 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
9212
9277
9213
9278
let dual_funding_context = DualFundingChannelContext {
9214
9279
our_funding_satoshis: funding_satoshis,
9280
+ their_funding_satoshis: Some(msg.common_fields.funding_satoshis),
9215
9281
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
9216
9282
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() ),
9218
9284
};
9219
9285
9220
9286
let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
0 commit comments