@@ -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 : 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 : channel_keys_id,
1422
1428
} ;
1423
1429
1424
1430
Ok ( chan)
@@ -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 bump
6256
+ // `SERIALIZATION_VERSION` to 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,6 +6317,8 @@ 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
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.
6309
6322
let keys_len: u32 = Readable :: read ( reader) ?;
6310
6323
let mut keys_data = Vec :: with_capacity ( cmp:: min ( keys_len as usize , MAX_ALLOC_SIZE ) ) ;
6311
6324
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
6315
6328
reader. read_exact ( read_slice) ?;
6316
6329
keys_data. extend_from_slice ( read_slice) ;
6317
6330
}
6318
- let holder_signer = keys_source. read_chan_signer ( & keys_data) ?;
6319
6331
6320
6332
// Read the old serialization for shutdown_pubkey, preferring the TLV field later if set.
6321
6333
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
6533
6545
let mut channel_ready_event_emitted = None ;
6534
6546
6535
6547
let mut user_id_high_opt: Option < u64 > = None ;
6548
+ let mut channel_keys_id: Option < [ u8 ; 32 ] > = None ;
6536
6549
6537
6550
read_tlv_fields ! ( reader, {
6538
6551
( 0 , announcement_sigs, option) ,
@@ -6552,8 +6565,23 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
6552
6565
( 21 , outbound_scid_alias, option) ,
6553
6566
( 23 , channel_ready_event_emitted, option) ,
6554
6567
( 25 , user_id_high_opt, option) ,
6568
+ ( 27 , channel_keys_id, option) ,
6555
6569
} ) ;
6556
6570
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
+
6557
6585
if let Some ( preimages) = preimages_opt {
6558
6586
let mut iter = preimages. into_iter ( ) ;
6559
6587
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
6703
6731
historical_inbound_htlc_fulfills,
6704
6732
6705
6733
channel_type : channel_type. unwrap ( ) ,
6734
+ _channel_keys_id : channel_keys_id,
6706
6735
} )
6707
6736
}
6708
6737
}
0 commit comments