Skip to content

Commit 5713656

Browse files
committed
Re-derive signers upon deserializing Channel
To do so, we introduce a new serialization version that doesn't store a channel's signer, and instead stores its signer's `channel_keys_id`. This is a unique identifier that can be provided to our `KeysInterface` to re-derive all private key material for said channel. We choose to not upgrade the minimum compatible serialization version until a later time, which will also remove any signer serialization logic on implementations of `KeysInterface` and `Sign`.
1 parent 43f7122 commit 5713656

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

lightning/src/ln/channel.rs

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::chain::BestBlock;
3434
use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, LowerBoundedFeeEstimator};
3535
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, LATENCY_GRACE_PERIOD_BLOCKS};
3636
use crate::chain::transaction::{OutPoint, TransactionData};
37-
use crate::chain::keysinterface::{Sign, KeysInterface};
37+
use crate::chain::keysinterface::{Sign, KeysInterface, BaseSign};
3838
use crate::util::events::ClosureReason;
3939
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, VecWriter};
4040
use crate::util::logger::Logger;
@@ -737,6 +737,10 @@ pub(super) struct Channel<Signer: Sign> {
737737

738738
// We track whether we already emitted a `ChannelReady` event.
739739
channel_ready_event_emitted: bool,
740+
741+
/// The unique identifier used to re-derive the private key material for the channel through
742+
/// [`KeysInterface::derive_channel_signer`].
743+
channel_keys_id: [u8; 32],
740744
}
741745

742746
#[cfg(any(test, fuzzing))]
@@ -1072,6 +1076,7 @@ impl<Signer: Sign> Channel<Signer> {
10721076
historical_inbound_htlc_fulfills: HashSet::new(),
10731077

10741078
channel_type: Self::get_initial_channel_type(&config),
1079+
channel_keys_id,
10751080
})
10761081
}
10771082

@@ -1419,6 +1424,7 @@ impl<Signer: Sign> Channel<Signer> {
14191424
historical_inbound_htlc_fulfills: HashSet::new(),
14201425

14211426
channel_type,
1427+
channel_keys_id,
14221428
};
14231429

14241430
Ok(chan)
@@ -5926,7 +5932,7 @@ impl<Signer: Sign> Channel<Signer> {
59265932
}
59275933
}
59285934

5929-
const SERIALIZATION_VERSION: u8 = 2;
5935+
const SERIALIZATION_VERSION: u8 = 3;
59305936
const MIN_SERIALIZATION_VERSION: u8 = 2;
59315937

59325938
impl_writeable_tlv_based_enum!(InboundHTLCRemovalReason,;
@@ -5988,7 +5994,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
59885994
// Note that we write out as if remove_uncommitted_htlcs_and_mark_paused had just been
59895995
// called.
59905996

5991-
write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION);
5997+
write_ver_prefix!(writer, MIN_SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION);
59925998

59935999
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
59946000
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values. We write
@@ -6246,6 +6252,10 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
62466252
// we write the high bytes as an option here.
62476253
let user_id_high_opt = Some((self.user_id >> 64) as u64);
62486254

6255+
// `channel_keys_id` is serialized as an option to remain backwards compatible until we
6256+
// start writing with `SERIALIZATION_VERSION` 3.
6257+
let channel_keys_id = Some(self.channel_keys_id);
6258+
62496259
write_tlv_fields!(writer, {
62506260
(0, self.announcement_sigs, option),
62516261
// minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -6270,6 +6280,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
62706280
(21, self.outbound_scid_alias, required),
62716281
(23, channel_ready_event_emitted, option),
62726282
(25, user_id_high_opt, option),
6283+
(27, channel_keys_id, option),
62736284
});
62746285

62756286
Ok(())
@@ -6306,16 +6317,21 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
63066317

63076318
let latest_monitor_update_id = Readable::read(reader)?;
63086319

6309-
let keys_len: u32 = Readable::read(reader)?;
6310-
let mut keys_data = Vec::with_capacity(cmp::min(keys_len as usize, MAX_ALLOC_SIZE));
6311-
while keys_data.len() != keys_len as usize {
6312-
// Read 1KB at a time to avoid accidentally allocating 4GB on corrupted channel keys
6313-
let mut data = [0; 1024];
6314-
let read_slice = &mut data[0..cmp::min(1024, keys_len as usize - keys_data.len())];
6315-
reader.read_exact(read_slice)?;
6316-
keys_data.extend_from_slice(read_slice);
6320+
let mut holder_signer = None;
6321+
if ver <= 2 {
6322+
// Read the serialize signer bytes. We'll choose to deserialize them or not based on whether
6323+
// the `channel_keys_id` TLV is present below.
6324+
let keys_len: u32 = Readable::read(reader)?;
6325+
let mut keys_data = Vec::with_capacity(cmp::min(keys_len as usize, MAX_ALLOC_SIZE));
6326+
while keys_data.len() != keys_len as usize {
6327+
// Read 1KB at a time to avoid accidentally allocating 4GB on corrupted channel keys
6328+
let mut data = [0; 1024];
6329+
let read_slice = &mut data[0..cmp::min(1024, keys_len as usize - keys_data.len())];
6330+
reader.read_exact(read_slice)?;
6331+
keys_data.extend_from_slice(read_slice);
6332+
}
6333+
holder_signer = Some(keys_source.read_chan_signer(&keys_data)?);
63176334
}
6318-
let holder_signer = keys_source.read_chan_signer(&keys_data)?;
63196335

63206336
// Read the old serialization for shutdown_pubkey, preferring the TLV field later if set.
63216337
let mut shutdown_scriptpubkey = match <PublicKey as Readable>::read(reader) {
@@ -6533,6 +6549,7 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
65336549
let mut channel_ready_event_emitted = None;
65346550

65356551
let mut user_id_high_opt: Option<u64> = None;
6552+
let mut channel_keys_id: Option<[u8; 32]> = None;
65366553

65376554
read_tlv_fields!(reader, {
65386555
(0, announcement_sigs, option),
@@ -6552,8 +6569,25 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
65526569
(21, outbound_scid_alias, option),
65536570
(23, channel_ready_event_emitted, option),
65546571
(25, user_id_high_opt, option),
6572+
(27, channel_keys_id, option),
65556573
});
65566574

6575+
let (channel_keys_id, holder_signer) = if ver <= 2 {
6576+
let holder_signer = holder_signer.unwrap();
6577+
(holder_signer.channel_keys_id(), holder_signer)
6578+
} else {
6579+
assert!(holder_signer.is_none());
6580+
let channel_keys_id = channel_keys_id.unwrap();
6581+
let mut holder_signer = keys_source.derive_channel_signer(channel_value_satoshis, channel_keys_id);
6582+
// If we've gotten to the funding stage of the channel, populate the signer with its
6583+
// required channel parameters.
6584+
let non_shutdown_state = channel_state & (!MULTI_STATE_FLAGS);
6585+
if non_shutdown_state >= (ChannelState::FundingCreated as u32) {
6586+
holder_signer.provide_channel_parameters(&channel_parameters);
6587+
}
6588+
(channel_keys_id, holder_signer)
6589+
};
6590+
65576591
if let Some(preimages) = preimages_opt {
65586592
let mut iter = preimages.into_iter();
65596593
for htlc in pending_outbound_htlcs.iter_mut() {
@@ -6703,6 +6737,7 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
67036737
historical_inbound_htlc_fulfills,
67046738

67056739
channel_type: channel_type.unwrap(),
6740+
channel_keys_id,
67066741
})
67076742
}
67086743
}

0 commit comments

Comments
 (0)