@@ -34,7 +34,7 @@ use crate::chain::BestBlock;
34
34
use crate :: chain:: chaininterface:: { FeeEstimator , ConfirmationTarget , LowerBoundedFeeEstimator } ;
35
35
use crate :: chain:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdate , ChannelMonitorUpdateStep , LATENCY_GRACE_PERIOD_BLOCKS } ;
36
36
use crate :: chain:: transaction:: { OutPoint , TransactionData } ;
37
- use crate :: chain:: keysinterface:: { Sign , KeysInterface } ;
37
+ use crate :: chain:: keysinterface:: { Sign , KeysInterface , BaseSign } ;
38
38
use crate :: util:: events:: ClosureReason ;
39
39
use crate :: util:: ser:: { Readable , ReadableArgs , Writeable , Writer , VecWriter } ;
40
40
use crate :: util:: logger:: Logger ;
@@ -737,6 +737,10 @@ pub(super) struct Channel<Signer: Sign> {
737
737
738
738
// We track whether we already emitted a `ChannelReady` event.
739
739
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 ] ,
740
744
}
741
745
742
746
#[ cfg( any( test, fuzzing) ) ]
@@ -1072,6 +1076,7 @@ impl<Signer: Sign> Channel<Signer> {
1072
1076
historical_inbound_htlc_fulfills : HashSet :: new ( ) ,
1073
1077
1074
1078
channel_type : Self :: get_initial_channel_type ( & config) ,
1079
+ channel_keys_id,
1075
1080
} )
1076
1081
}
1077
1082
@@ -1419,6 +1424,7 @@ impl<Signer: Sign> Channel<Signer> {
1419
1424
historical_inbound_htlc_fulfills : HashSet :: new ( ) ,
1420
1425
1421
1426
channel_type,
1427
+ channel_keys_id,
1422
1428
} ;
1423
1429
1424
1430
Ok ( chan)
@@ -5926,7 +5932,7 @@ impl<Signer: Sign> Channel<Signer> {
5926
5932
}
5927
5933
}
5928
5934
5929
- const SERIALIZATION_VERSION : u8 = 2 ;
5935
+ const SERIALIZATION_VERSION : u8 = 3 ;
5930
5936
const MIN_SERIALIZATION_VERSION : u8 = 2 ;
5931
5937
5932
5938
impl_writeable_tlv_based_enum ! ( InboundHTLCRemovalReason , ;
@@ -5988,7 +5994,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
5988
5994
// Note that we write out as if remove_uncommitted_htlcs_and_mark_paused had just been
5989
5995
// called.
5990
5996
5991
- write_ver_prefix ! ( writer, SERIALIZATION_VERSION , MIN_SERIALIZATION_VERSION ) ;
5997
+ write_ver_prefix ! ( writer, MIN_SERIALIZATION_VERSION , MIN_SERIALIZATION_VERSION ) ;
5992
5998
5993
5999
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
5994
6000
// 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> {
6246
6252
// we write the high bytes as an option here.
6247
6253
let user_id_high_opt = Some ( ( self . user_id >> 64 ) as u64 ) ;
6248
6254
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
+
6249
6259
write_tlv_fields ! ( writer, {
6250
6260
( 0 , self . announcement_sigs, option) ,
6251
6261
// minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -6270,6 +6280,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
6270
6280
( 21 , self . outbound_scid_alias, required) ,
6271
6281
( 23 , channel_ready_event_emitted, option) ,
6272
6282
( 25 , user_id_high_opt, option) ,
6283
+ ( 27 , channel_keys_id, option) ,
6273
6284
} ) ;
6274
6285
6275
6286
Ok ( ( ) )
@@ -6306,16 +6317,21 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
6306
6317
6307
6318
let latest_monitor_update_id = Readable :: read ( reader) ?;
6308
6319
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) ?) ;
6317
6334
}
6318
- let holder_signer = keys_source. read_chan_signer ( & keys_data) ?;
6319
6335
6320
6336
// Read the old serialization for shutdown_pubkey, preferring the TLV field later if set.
6321
6337
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
6533
6549
let mut channel_ready_event_emitted = None ;
6534
6550
6535
6551
let mut user_id_high_opt: Option < u64 > = None ;
6552
+ let mut channel_keys_id: Option < [ u8 ; 32 ] > = None ;
6536
6553
6537
6554
read_tlv_fields ! ( reader, {
6538
6555
( 0 , announcement_sigs, option) ,
@@ -6552,8 +6569,25 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
6552
6569
( 21 , outbound_scid_alias, option) ,
6553
6570
( 23 , channel_ready_event_emitted, option) ,
6554
6571
( 25 , user_id_high_opt, option) ,
6572
+ ( 27 , channel_keys_id, option) ,
6555
6573
} ) ;
6556
6574
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
+
6557
6591
if let Some ( preimages) = preimages_opt {
6558
6592
let mut iter = preimages. into_iter ( ) ;
6559
6593
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
6703
6737
historical_inbound_htlc_fulfills,
6704
6738
6705
6739
channel_type : channel_type. unwrap ( ) ,
6740
+ channel_keys_id,
6706
6741
} )
6707
6742
}
6708
6743
}
0 commit comments