Skip to content

Commit 436dc4b

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 6d9c676 commit 436dc4b

File tree

6 files changed

+57
-5
lines changed

6 files changed

+57
-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 can be used to spend the funding output for this channel.
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
@@ -1121,12 +1121,17 @@ impl ChannelTransactionParameters {
11211121
}
11221122
}
11231123

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

11321137
/// Returns the counterparty's pubkeys.

lightning/src/ln/channel_state.rs

Lines changed: 24 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 can be used to spend the funding output for this channel.
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,17 @@ 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+
pub fn get_funding_output(&self) -> Option<bitcoin::TxOut> {
484+
match self.funding_redeem_script.as_ref() {
485+
None => None,
486+
Some(redeem_script) => Some(bitcoin::TxOut {
487+
value: bitcoin::Amount::from_sat(self.channel_value_satoshis),
488+
script_pubkey: redeem_script.to_p2wsh(),
489+
}),
490+
}
491+
}
492+
478493
pub(super) fn from_channel<SP: Deref, F: Deref>(
479494
channel: &Channel<SP>, best_block_height: u32, latest_features: InitFeatures,
480495
fee_estimator: &LowerBoundedFeeEstimator<F>,
@@ -509,6 +524,9 @@ impl ChannelDetails {
509524
outbound_htlc_maximum_msat: context.get_counterparty_htlc_maximum_msat(funding),
510525
},
511526
funding_txo: funding.get_funding_txo(),
527+
funding_redeem_script: funding
528+
.channel_transaction_parameters
529+
.make_funding_redeemscript_opt(),
512530
// Note that accept_channel (or open_channel) is always the first message, so
513531
// `have_received_message` indicates that type negotiation has completed.
514532
channel_type: if context.have_received_message() {
@@ -583,6 +601,7 @@ impl_writeable_tlv_based!(ChannelDetails, {
583601
(41, channel_shutdown_state, option),
584602
(43, pending_inbound_htlcs, optional_vec),
585603
(45, pending_outbound_htlcs, optional_vec),
604+
(47, funding_redeem_script, option),
586605
(_unused, user_channel_id, (static_value,
587606
_user_channel_id_low.unwrap_or(0) as u128 | ((_user_channel_id_high.unwrap_or(0) as u128) << 64)
588607
)),
@@ -627,6 +646,7 @@ mod tests {
627646
use crate::{
628647
chain::transaction::OutPoint,
629648
ln::{
649+
chan_utils::make_funding_redeemscript,
630650
channel_state::{
631651
InboundHTLCDetails, InboundHTLCStateDetails, OutboundHTLCDetails,
632652
OutboundHTLCStateDetails,
@@ -658,6 +678,10 @@ mod tests {
658678
txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(),
659679
index: 1,
660680
}),
681+
funding_redeem_script: Some(make_funding_redeemscript(
682+
&PublicKey::from_slice(&[2; 33]).unwrap(),
683+
&PublicKey::from_slice(&[2; 33]).unwrap(),
684+
)),
661685
channel_type: None,
662686
short_channel_id: None,
663687
outbound_scid_alias: None,

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3303,6 +3303,8 @@ macro_rules! emit_channel_pending_event {
33033303
($locked_events: expr, $channel: expr) => {
33043304
if $channel.context.should_emit_channel_pending_event() {
33053305
let funding_txo = $channel.funding.get_funding_txo().unwrap();
3306+
let funding_redeem_script =
3307+
Some($channel.funding.channel_transaction_parameters.make_funding_redeemscript());
33063308
$locked_events.push_back((
33073309
events::Event::ChannelPending {
33083310
channel_id: $channel.context.channel_id(),
@@ -3311,6 +3313,7 @@ macro_rules! emit_channel_pending_event {
33113313
user_channel_id: $channel.context.get_user_id(),
33123314
funding_txo: funding_txo.into_bitcoin_outpoint(),
33133315
channel_type: Some($channel.funding.get_channel_type().clone()),
3316+
funding_redeem_script,
33143317
},
33153318
None,
33163319
));

lightning/src/routing/router.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3926,6 +3926,7 @@ mod tests {
39263926
use crate::blinded_path::BlindedHop;
39273927
use crate::chain::transaction::OutPoint;
39283928
use crate::crypto::chacha20::ChaCha20;
3929+
use crate::ln::chan_utils::make_funding_redeemscript;
39293930
use crate::ln::channel_state::{ChannelCounterparty, ChannelDetails, ChannelShutdownState};
39303931
use crate::ln::channelmanager;
39313932
use crate::ln::msgs::{UnsignedChannelUpdate, MAX_VALUE_MSAT};
@@ -3984,6 +3985,10 @@ mod tests {
39843985
outbound_htlc_maximum_msat: None,
39853986
},
39863987
funding_txo: Some(OutPoint { txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(), index: 0 }),
3988+
funding_redeem_script: Some(make_funding_redeemscript(
3989+
&PublicKey::from_slice(&[2; 33]).unwrap(),
3990+
&PublicKey::from_slice(&[2; 33]).unwrap(),
3991+
)),
39873992
channel_type: None,
39883993
short_channel_id,
39893994
outbound_scid_alias: None,
@@ -9384,6 +9389,7 @@ pub(crate) mod bench_utils {
93849389
use std::io::Read;
93859390

93869391
use crate::chain::transaction::OutPoint;
9392+
use crate::ln::chan_utils::make_funding_redeemscript;
93879393
use crate::ln::channel_state::{ChannelCounterparty, ChannelShutdownState};
93889394
use crate::ln::channelmanager;
93899395
use crate::ln::types::ChannelId;
@@ -9479,6 +9485,10 @@ pub(crate) mod bench_utils {
94799485
funding_txo: Some(OutPoint {
94809486
txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(), index: 0
94819487
}),
9488+
funding_redeem_script: Some(make_funding_redeemscript(
9489+
&PublicKey::from_slice(&[2; 33]).unwrap(),
9490+
&PublicKey::from_slice(&[2; 33]).unwrap(),
9491+
)),
94829492
channel_type: None,
94839493
short_channel_id: Some(1),
94849494
inbound_scid_alias: None,

0 commit comments

Comments
 (0)