@@ -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 : 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 : channel_keys_id,
14221428 } ;
14231429
14241430 Ok ( chan)
@@ -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 bump
6256+ // `SERIALIZATION_VERSION` to 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,6 +6317,8 @@ 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
6320+ // Read the serialize signer bytes. We'll choose to deserialize them or not based on whether
6321+ // the `channel_keys_id` TLV is present below.
63096322 let keys_len: u32 = Readable :: read ( reader) ?;
63106323 let mut keys_data = Vec :: with_capacity ( cmp:: min ( keys_len as usize , MAX_ALLOC_SIZE ) ) ;
63116324 while keys_data. len ( ) != keys_len as usize {
@@ -6315,7 +6328,6 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
63156328 reader. read_exact ( read_slice) ?;
63166329 keys_data. extend_from_slice ( read_slice) ;
63176330 }
6318- let holder_signer = keys_source. read_chan_signer ( & keys_data) ?;
63196331
63206332 // Read the old serialization for shutdown_pubkey, preferring the TLV field later if set.
63216333 let mut shutdown_scriptpubkey = match <PublicKey as Readable >:: read ( reader) {
@@ -6533,6 +6545,7 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
65336545 let mut channel_ready_event_emitted = None ;
65346546
65356547 let mut user_id_high_opt: Option < u64 > = None ;
6548+ let mut channel_keys_id: Option < [ u8 ; 32 ] > = None ;
65366549
65376550 read_tlv_fields ! ( reader, {
65386551 ( 0 , announcement_sigs, option) ,
@@ -6552,8 +6565,23 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
65526565 ( 21 , outbound_scid_alias, option) ,
65536566 ( 23 , channel_ready_event_emitted, option) ,
65546567 ( 25 , user_id_high_opt, option) ,
6568+ ( 27 , channel_keys_id, option) ,
65556569 } ) ;
65566570
6571+ let ( channel_keys_id, holder_signer) = if let Some ( channel_keys_id) = channel_keys_id {
6572+ let mut holder_signer = keys_source. derive_channel_signer ( channel_value_satoshis, channel_keys_id) ;
6573+ // If we've gotten to the funding stage of the channel, populate the signer with its
6574+ // required channel parameters.
6575+ let non_shutdown_state = channel_state & ( !MULTI_STATE_FLAGS ) ;
6576+ if non_shutdown_state >= ( ChannelState :: FundingCreated as u32 ) {
6577+ holder_signer. ready_channel ( & channel_parameters) ;
6578+ }
6579+ ( channel_keys_id, holder_signer)
6580+ } else {
6581+ let holder_signer = keys_source. read_chan_signer ( & keys_data) ?;
6582+ ( holder_signer. channel_keys_id ( ) , holder_signer)
6583+ } ;
6584+
65576585 if let Some ( preimages) = preimages_opt {
65586586 let mut iter = preimages. into_iter ( ) ;
65596587 for htlc in pending_outbound_htlcs. iter_mut ( ) {
@@ -6703,6 +6731,7 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
67036731 historical_inbound_htlc_fulfills,
67046732
67056733 channel_type : channel_type. unwrap ( ) ,
6734+ _channel_keys_id : channel_keys_id,
67066735 } )
67076736 }
67086737}
0 commit comments