Skip to content

Commit 734bc6a

Browse files
committed
Add funding redeem script to ChannelDetails and ChannelPending event
Original context and motivation comes from here: lightningdevkit/ldk-node#677 (comment) When splicing-in, the default case is our channel utxo + our wallet utxos being combined. This works great however, it can give our wallet issues calculating fees after the fact because our wallet needs to know about our channel's utxo. We currently have it's outpoint and satoshi value available, but not its output script so we are unable to construct the TxOut for the channel. This adds the redeem script to the `ChannelDetails` and `ChannelPending` event which gives us enough information to be able to construct it.
1 parent e2ca5ba commit 734bc6a

File tree

6 files changed

+61
-5
lines changed

6 files changed

+61
-5
lines changed

fuzz/src/router.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
229229
txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(),
230230
index: 0,
231231
}),
232+
funding_redeem_script: None,
232233
channel_type: None,
233234
short_channel_id: Some(scid),
234235
inbound_scid_alias: None,

lightning/src/events/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,10 @@ pub enum Event {
14131413
///
14141414
/// Will be `None` for channels created prior to LDK version 0.0.122.
14151415
channel_type: Option<ChannelTypeFeatures>,
1416+
/// The witness script that is used to lock the channel's funding output to commitment transactions.
1417+
///
1418+
/// This field will be `None` for objects serialized with LDK versions prior to 0.2.0.
1419+
funding_redeem_script: Option<ScriptBuf>,
14161420
},
14171421
/// Used to indicate that a channel with the given `channel_id` is ready to be used. This event
14181422
/// is emitted when
@@ -2234,6 +2238,7 @@ impl Writeable for Event {
22342238
ref counterparty_node_id,
22352239
ref funding_txo,
22362240
ref channel_type,
2241+
ref funding_redeem_script,
22372242
} => {
22382243
31u8.write(writer)?;
22392244
write_tlv_fields!(writer, {
@@ -2243,6 +2248,7 @@ impl Writeable for Event {
22432248
(4, former_temporary_channel_id, required),
22442249
(6, counterparty_node_id, required),
22452250
(8, funding_txo, required),
2251+
(9, funding_redeem_script, option),
22462252
});
22472253
},
22482254
&Event::ConnectionNeeded { .. } => {
@@ -2815,13 +2821,15 @@ impl MaybeReadable for Event {
28152821
let mut counterparty_node_id = RequiredWrapper(None);
28162822
let mut funding_txo = RequiredWrapper(None);
28172823
let mut channel_type = None;
2824+
let mut funding_redeem_script = None;
28182825
read_tlv_fields!(reader, {
28192826
(0, channel_id, required),
28202827
(1, channel_type, option),
28212828
(2, user_channel_id, required),
28222829
(4, former_temporary_channel_id, required),
28232830
(6, counterparty_node_id, required),
28242831
(8, funding_txo, required),
2832+
(9, funding_redeem_script, option),
28252833
});
28262834

28272835
Ok(Some(Event::ChannelPending {
@@ -2831,6 +2839,7 @@ impl MaybeReadable for Event {
28312839
counterparty_node_id: counterparty_node_id.0.unwrap(),
28322840
funding_txo: funding_txo.0.unwrap(),
28332841
channel_type,
2842+
funding_redeem_script,
28342843
}))
28352844
};
28362845
f()

lightning/src/ln/chan_utils.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,12 +1120,17 @@ impl ChannelTransactionParameters {
11201120
}
11211121
}
11221122

1123-
#[rustfmt::skip]
11241123
pub(crate) fn make_funding_redeemscript(&self) -> ScriptBuf {
1125-
make_funding_redeemscript(
1126-
&self.holder_pubkeys.funding_pubkey,
1127-
&self.counterparty_parameters.as_ref().unwrap().pubkeys.funding_pubkey
1128-
)
1124+
self.make_funding_redeemscript_opt().unwrap()
1125+
}
1126+
1127+
pub(crate) fn make_funding_redeemscript_opt(&self) -> Option<ScriptBuf> {
1128+
self.counterparty_parameters.as_ref().map(|p| {
1129+
make_funding_redeemscript(
1130+
&self.holder_pubkeys.funding_pubkey,
1131+
&p.pubkeys.funding_pubkey,
1132+
)
1133+
})
11291134
}
11301135

11311136
/// Returns the counterparty's pubkeys.

lightning/src/ln/channel_state.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,10 @@ pub struct ChannelDetails {
450450
///
451451
/// This field is empty for objects serialized with LDK versions prior to 0.0.122.
452452
pub pending_outbound_htlcs: Vec<OutboundHTLCDetails>,
453+
/// The witness script that is used to lock the channel's funding output to commitment transactions.
454+
///
455+
/// This field will be `None` for objects serialized with LDK versions prior to 0.2.0.
456+
pub funding_redeem_script: Option<bitcoin::ScriptBuf>,
453457
}
454458

455459
impl ChannelDetails {
@@ -475,6 +479,21 @@ impl ChannelDetails {
475479
self.short_channel_id.or(self.outbound_scid_alias)
476480
}
477481

482+
/// Gets the funding output for this channel, if available.
483+
///
484+
/// During a splice, the funding output will change and this value will be updated
485+
/// after the splice transaction has reached sufficient confirmations and we've
486+
/// exchanged `splice_locked` messages.
487+
pub fn get_funding_output(&self) -> Option<bitcoin::TxOut> {
488+
match self.funding_redeem_script.as_ref() {
489+
None => None,
490+
Some(redeem_script) => Some(bitcoin::TxOut {
491+
value: bitcoin::Amount::from_sat(self.channel_value_satoshis),
492+
script_pubkey: redeem_script.to_p2wsh(),
493+
}),
494+
}
495+
}
496+
478497
pub(super) fn from_channel<SP: Deref, F: Deref>(
479498
channel: &Channel<SP>, best_block_height: u32, latest_features: InitFeatures,
480499
fee_estimator: &LowerBoundedFeeEstimator<F>,
@@ -509,6 +528,9 @@ impl ChannelDetails {
509528
outbound_htlc_maximum_msat: context.get_counterparty_htlc_maximum_msat(funding),
510529
},
511530
funding_txo: funding.get_funding_txo(),
531+
funding_redeem_script: funding
532+
.channel_transaction_parameters
533+
.make_funding_redeemscript_opt(),
512534
// Note that accept_channel (or open_channel) is always the first message, so
513535
// `have_received_message` indicates that type negotiation has completed.
514536
channel_type: if context.have_received_message() {
@@ -583,6 +605,7 @@ impl_writeable_tlv_based!(ChannelDetails, {
583605
(41, channel_shutdown_state, option),
584606
(43, pending_inbound_htlcs, optional_vec),
585607
(45, pending_outbound_htlcs, optional_vec),
608+
(47, funding_redeem_script, option),
586609
(_unused, user_channel_id, (static_value,
587610
_user_channel_id_low.unwrap_or(0) as u128 | ((_user_channel_id_high.unwrap_or(0) as u128) << 64)
588611
)),
@@ -627,6 +650,7 @@ mod tests {
627650
use crate::{
628651
chain::transaction::OutPoint,
629652
ln::{
653+
chan_utils::make_funding_redeemscript,
630654
channel_state::{
631655
InboundHTLCDetails, InboundHTLCStateDetails, OutboundHTLCDetails,
632656
OutboundHTLCStateDetails,
@@ -658,6 +682,10 @@ mod tests {
658682
txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(),
659683
index: 1,
660684
}),
685+
funding_redeem_script: Some(make_funding_redeemscript(
686+
&PublicKey::from_slice(&[2; 33]).unwrap(),
687+
&PublicKey::from_slice(&[2; 33]).unwrap(),
688+
)),
661689
channel_type: None,
662690
short_channel_id: None,
663691
outbound_scid_alias: None,

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3491,6 +3491,8 @@ macro_rules! emit_channel_pending_event {
34913491
($locked_events: expr, $channel: expr) => {
34923492
if $channel.context.should_emit_channel_pending_event() {
34933493
let funding_txo = $channel.funding.get_funding_txo().unwrap();
3494+
let funding_redeem_script =
3495+
Some($channel.funding.channel_transaction_parameters.make_funding_redeemscript());
34943496
$locked_events.push_back((
34953497
events::Event::ChannelPending {
34963498
channel_id: $channel.context.channel_id(),
@@ -3499,6 +3501,7 @@ macro_rules! emit_channel_pending_event {
34993501
user_channel_id: $channel.context.get_user_id(),
35003502
funding_txo: funding_txo.into_bitcoin_outpoint(),
35013503
channel_type: Some($channel.funding.get_channel_type().clone()),
3504+
funding_redeem_script,
35023505
},
35033506
None,
35043507
));

lightning/src/routing/router.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3893,6 +3893,7 @@ mod tests {
38933893
use crate::blinded_path::BlindedHop;
38943894
use crate::chain::transaction::OutPoint;
38953895
use crate::crypto::chacha20::ChaCha20;
3896+
use crate::ln::chan_utils::make_funding_redeemscript;
38963897
use crate::ln::channel_state::{ChannelCounterparty, ChannelDetails, ChannelShutdownState};
38973898
use crate::ln::channelmanager;
38983899
use crate::ln::msgs::{UnsignedChannelUpdate, MAX_VALUE_MSAT};
@@ -3950,6 +3951,10 @@ mod tests {
39503951
outbound_htlc_maximum_msat: None,
39513952
},
39523953
funding_txo: Some(OutPoint { txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(), index: 0 }),
3954+
funding_redeem_script: Some(make_funding_redeemscript(
3955+
&PublicKey::from_slice(&[2; 33]).unwrap(),
3956+
&PublicKey::from_slice(&[2; 33]).unwrap(),
3957+
)),
39533958
channel_type: None,
39543959
short_channel_id,
39553960
outbound_scid_alias: None,
@@ -9250,6 +9255,7 @@ pub(crate) mod bench_utils {
92509255
use std::io::Read;
92519256

92529257
use crate::chain::transaction::OutPoint;
9258+
use crate::ln::chan_utils::make_funding_redeemscript;
92539259
use crate::ln::channel_state::{ChannelCounterparty, ChannelShutdownState};
92549260
use crate::ln::channelmanager;
92559261
use crate::ln::types::ChannelId;
@@ -9345,6 +9351,10 @@ pub(crate) mod bench_utils {
93459351
funding_txo: Some(OutPoint {
93469352
txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(), index: 0
93479353
}),
9354+
funding_redeem_script: Some(make_funding_redeemscript(
9355+
&PublicKey::from_slice(&[2; 33]).unwrap(),
9356+
&PublicKey::from_slice(&[2; 33]).unwrap(),
9357+
)),
93489358
channel_type: None,
93499359
short_channel_id: Some(1),
93509360
inbound_scid_alias: None,

0 commit comments

Comments
 (0)