@@ -34,7 +34,7 @@ use crate::chain::BestBlock;
3434use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, LowerBoundedFeeEstimator};
3535use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, LATENCY_GRACE_PERIOD_BLOCKS};
3636use crate::chain::transaction::{OutPoint, TransactionData};
37- use crate::chain::keysinterface::{Sign, KeysInterface};
37+ use crate::chain::keysinterface::{Sign, KeysInterface, BaseSign };
3838use crate::util::events::ClosureReason;
3939use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, VecWriter};
4040use 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)
@@ -5936,7 +5942,7 @@ impl<Signer: Sign> Channel<Signer> {
59365942 }
59375943}
59385944
5939- const SERIALIZATION_VERSION: u8 = 2 ;
5945+ const SERIALIZATION_VERSION: u8 = 3 ;
59405946const MIN_SERIALIZATION_VERSION: u8 = 2;
59415947
59425948impl_writeable_tlv_based_enum!(InboundHTLCRemovalReason,;
@@ -5998,7 +6004,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
59986004 // Note that we write out as if remove_uncommitted_htlcs_and_mark_paused had just been
59996005 // called.
60006006
6001- write_ver_prefix!(writer, SERIALIZATION_VERSION , MIN_SERIALIZATION_VERSION);
6007+ write_ver_prefix!(writer, MIN_SERIALIZATION_VERSION , MIN_SERIALIZATION_VERSION);
60026008
60036009 // `user_id` used to be a single u64 value. In order to remain backwards compatible with
60046010 // versions prior to 0.0.113, the u128 is serialized as two separate u64 values. We write
@@ -6280,6 +6286,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
62806286 (21, self.outbound_scid_alias, required),
62816287 (23, channel_ready_event_emitted, option),
62826288 (25, user_id_high_opt, option),
6289+ (27, self.channel_keys_id, required),
62836290 });
62846291
62856292 Ok(())
@@ -6316,16 +6323,20 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
63166323
63176324 let latest_monitor_update_id = Readable::read(reader)?;
63186325
6319- let keys_len: u32 = Readable::read(reader)?;
6320- let mut keys_data = Vec::with_capacity(cmp::min(keys_len as usize, MAX_ALLOC_SIZE));
6321- while keys_data.len() != keys_len as usize {
6322- // Read 1KB at a time to avoid accidentally allocating 4GB on corrupted channel keys
6323- let mut data = [0; 1024];
6324- let read_slice = &mut data[0..cmp::min(1024, keys_len as usize - keys_data.len())];
6325- reader.read_exact(read_slice)?;
6326- keys_data.extend_from_slice(read_slice);
6326+ let mut keys_data = None;
6327+ if ver <= 2 {
6328+ // Read the serialize signer bytes. We'll choose to deserialize them or not based on whether
6329+ // the `channel_keys_id` TLV is present below.
6330+ let keys_len: u32 = Readable::read(reader)?;
6331+ keys_data = Some(Vec::with_capacity(cmp::min(keys_len as usize, MAX_ALLOC_SIZE)));
6332+ while keys_data.as_ref().unwrap().len() != keys_len as usize {
6333+ // Read 1KB at a time to avoid accidentally allocating 4GB on corrupted channel keys
6334+ let mut data = [0; 1024];
6335+ let read_slice = &mut data[0..cmp::min(1024, keys_len as usize - keys_data.as_ref().unwrap().len())];
6336+ reader.read_exact(read_slice)?;
6337+ keys_data.as_mut().unwrap().extend_from_slice(read_slice);
6338+ }
63276339 }
6328- let holder_signer = keys_source.read_chan_signer(&keys_data)?;
63296340
63306341 // Read the old serialization for shutdown_pubkey, preferring the TLV field later if set.
63316342 let mut shutdown_scriptpubkey = match <PublicKey as Readable>::read(reader) {
@@ -6543,6 +6554,7 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
65436554 let mut channel_ready_event_emitted = None;
65446555
65456556 let mut user_id_high_opt: Option<u64> = None;
6557+ let mut channel_keys_id: Option<[u8; 32]> = None;
65466558
65476559 read_tlv_fields!(reader, {
65486560 (0, announcement_sigs, option),
@@ -6562,8 +6574,25 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
65626574 (21, outbound_scid_alias, option),
65636575 (23, channel_ready_event_emitted, option),
65646576 (25, user_id_high_opt, option),
6577+ (27, channel_keys_id, option),
65656578 });
65666579
6580+ let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
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+ } else {
6590+ // `keys_data` can be `None` if we had corrupted data.
6591+ let keys_data = keys_data.ok_or(DecodeError::InvalidValue)?;
6592+ let holder_signer = keys_source.read_chan_signer(&keys_data)?;
6593+ (holder_signer.channel_keys_id(), holder_signer)
6594+ };
6595+
65676596 if let Some(preimages) = preimages_opt {
65686597 let mut iter = preimages.into_iter();
65696598 for htlc in pending_outbound_htlcs.iter_mut() {
@@ -6713,6 +6742,7 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
67136742 historical_inbound_htlc_fulfills,
67146743
67156744 channel_type: channel_type.unwrap(),
6745+ channel_keys_id,
67166746 })
67176747 }
67186748}
0 commit comments