@@ -34,9 +34,9 @@ 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
- use crate :: util:: ser:: { Readable , ReadableArgs , Writeable , Writer , VecWriter } ;
39
+ use crate :: util:: ser:: { Readable , ReadableArgs , Writeable , Writer } ;
40
40
use crate :: util:: logger:: Logger ;
41
41
use crate :: util:: errors:: APIError ;
42
42
use crate :: util:: config:: { UserConfig , ChannelConfig , LegacyChannelConfig , ChannelHandshakeConfig , ChannelHandshakeLimits } ;
@@ -739,6 +739,10 @@ pub(super) struct Channel<Signer: Sign> {
739
739
740
740
// We track whether we already emitted a `ChannelReady` event.
741
741
channel_ready_event_emitted : bool ,
742
+
743
+ /// The unique identifier used to re-derive the private key material for the channel through
744
+ /// [`KeysInterface::derive_channel_signer`].
745
+ _channel_keys_id : [ u8 ; 32 ] ,
742
746
}
743
747
744
748
#[ cfg( any( test, fuzzing) ) ]
@@ -912,6 +916,7 @@ impl<Signer: Sign> Channel<Signer> {
912
916
913
917
let holder_selected_contest_delay = config. channel_handshake_config . our_to_self_delay ;
914
918
let holder_signer = keys_provider. get_channel_signer ( false , channel_value_satoshis) ;
919
+ let channel_keys_id = holder_signer. channel_keys_id ( ) ;
915
920
let pubkeys = holder_signer. pubkeys ( ) . clone ( ) ;
916
921
917
922
if !their_features. supports_wumbo ( ) && channel_value_satoshis > MAX_FUNDING_SATOSHIS_NO_WUMBO {
@@ -1072,6 +1077,7 @@ impl<Signer: Sign> Channel<Signer> {
1072
1077
historical_inbound_htlc_fulfills : HashSet :: new ( ) ,
1073
1078
1074
1079
channel_type : Self :: get_initial_channel_type ( & config) ,
1080
+ _channel_keys_id : channel_keys_id,
1075
1081
} )
1076
1082
}
1077
1083
@@ -1155,6 +1161,7 @@ impl<Signer: Sign> Channel<Signer> {
1155
1161
}
1156
1162
1157
1163
let holder_signer = keys_provider. get_channel_signer ( true , msg. funding_satoshis ) ;
1164
+ let channel_keys_id = holder_signer. channel_keys_id ( ) ;
1158
1165
let pubkeys = holder_signer. pubkeys ( ) . clone ( ) ;
1159
1166
let counterparty_pubkeys = ChannelPublicKeys {
1160
1167
funding_pubkey : msg. funding_pubkey ,
@@ -1417,6 +1424,7 @@ impl<Signer: Sign> Channel<Signer> {
1417
1424
historical_inbound_htlc_fulfills : HashSet :: new ( ) ,
1418
1425
1419
1426
channel_type,
1427
+ _channel_keys_id : channel_keys_id,
1420
1428
} ;
1421
1429
1422
1430
Ok ( chan)
@@ -5963,7 +5971,7 @@ impl<Signer: Sign> Channel<Signer> {
5963
5971
}
5964
5972
}
5965
5973
5966
- const SERIALIZATION_VERSION : u8 = 2 ;
5974
+ const SERIALIZATION_VERSION : u8 = 3 ;
5967
5975
const MIN_SERIALIZATION_VERSION : u8 = 2 ;
5968
5976
5969
5977
impl_writeable_tlv_based_enum ! ( InboundHTLCRemovalReason , ;
@@ -6044,13 +6052,6 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
6044
6052
6045
6053
self . latest_monitor_update_id . write ( writer) ?;
6046
6054
6047
- let mut key_data = VecWriter ( Vec :: new ( ) ) ;
6048
- self . holder_signer . write ( & mut key_data) ?;
6049
- assert ! ( key_data. 0 . len( ) < core:: usize :: MAX ) ;
6050
- assert ! ( key_data. 0 . len( ) < core:: u32 :: MAX as usize ) ;
6051
- ( key_data. 0 . len ( ) as u32 ) . write ( writer) ?;
6052
- writer. write_all ( & key_data. 0 [ ..] ) ?;
6053
-
6054
6055
// Write out the old serialization for shutdown_pubkey for backwards compatibility, if
6055
6056
// deserialized from that format.
6056
6057
match self . shutdown_scriptpubkey . as_ref ( ) . and_then ( |script| script. as_legacy_pubkey ( ) ) {
@@ -6283,6 +6284,10 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
6283
6284
// we write the high bytes as an option here.
6284
6285
let user_id_high_opt = Some ( ( self . user_id >> 64 ) as u64 ) ;
6285
6286
6287
+ // `channel_keys_id` is serialized as an option to remain backwards compatible until we bump
6288
+ // `MIN_SERIALIZATION_VERSION` to 3.
6289
+ let channel_keys_id = Some ( self . _channel_keys_id ) ;
6290
+
6286
6291
write_tlv_fields ! ( writer, {
6287
6292
( 0 , self . announcement_sigs, option) ,
6288
6293
// minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -6298,6 +6303,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
6298
6303
( 5 , self . config, required) ,
6299
6304
( 6 , serialized_holder_htlc_max_in_flight, option) ,
6300
6305
( 7 , self . shutdown_scriptpubkey, option) ,
6306
+ ( 8 , channel_keys_id, option) ,
6301
6307
( 9 , self . target_closing_feerate_sats_per_kw, option) ,
6302
6308
( 11 , self . monitor_pending_finalized_fulfills, vec_type) ,
6303
6309
( 13 , self . channel_creation_height, required) ,
@@ -6343,16 +6349,19 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
6343
6349
6344
6350
let latest_monitor_update_id = Readable :: read ( reader) ?;
6345
6351
6346
- let keys_len: u32 = Readable :: read ( reader) ?;
6347
- let mut keys_data = Vec :: with_capacity ( cmp:: min ( keys_len as usize , MAX_ALLOC_SIZE ) ) ;
6348
- while keys_data. len ( ) != keys_len as usize {
6349
- // Read 1KB at a time to avoid accidentally allocating 4GB on corrupted channel keys
6350
- let mut data = [ 0 ; 1024 ] ;
6351
- let read_slice = & mut data[ 0 ..cmp:: min ( 1024 , keys_len as usize - keys_data. len ( ) ) ] ;
6352
- reader. read_exact ( read_slice) ?;
6353
- keys_data. extend_from_slice ( read_slice) ;
6352
+ let mut holder_signer = None ;
6353
+ if ver <= 2 {
6354
+ let keys_len: u32 = Readable :: read ( reader) ?;
6355
+ let mut keys_data = Vec :: with_capacity ( cmp:: min ( keys_len as usize , MAX_ALLOC_SIZE ) ) ;
6356
+ while keys_data. len ( ) != keys_len as usize {
6357
+ // Read 1KB at a time to avoid accidentally allocating 4GB on corrupted channel keys
6358
+ let mut data = [ 0 ; 1024 ] ;
6359
+ let read_slice = & mut data[ 0 ..cmp:: min ( 1024 , keys_len as usize - keys_data. len ( ) ) ] ;
6360
+ reader. read_exact ( read_slice) ?;
6361
+ keys_data. extend_from_slice ( read_slice) ;
6362
+ }
6363
+ holder_signer = Some ( keys_source. read_chan_signer ( & keys_data) ?) ;
6354
6364
}
6355
- let holder_signer = keys_source. read_chan_signer ( & keys_data) ?;
6356
6365
6357
6366
// Read the old serialization for shutdown_pubkey, preferring the TLV field later if set.
6358
6367
let mut shutdown_scriptpubkey = match <PublicKey as Readable >:: read ( reader) {
@@ -6570,6 +6579,7 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
6570
6579
let mut channel_ready_event_emitted = None ;
6571
6580
6572
6581
let mut user_id_high_opt: Option < u64 > = None ;
6582
+ let mut channel_keys_id: Option < [ u8 ; 32 ] > = None ;
6573
6583
6574
6584
read_tlv_fields ! ( reader, {
6575
6585
( 0 , announcement_sigs, option) ,
@@ -6580,6 +6590,7 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
6580
6590
( 5 , config, option) , // Note that if none is provided we will *not* overwrite the existing one.
6581
6591
( 6 , holder_max_htlc_value_in_flight_msat, option) ,
6582
6592
( 7 , shutdown_scriptpubkey, option) ,
6593
+ ( 8 , channel_keys_id, option) ,
6583
6594
( 9 , target_closing_feerate_sats_per_kw, option) ,
6584
6595
( 11 , monitor_pending_finalized_fulfills, vec_type) ,
6585
6596
( 13 , channel_creation_height, option) ,
@@ -6591,6 +6602,22 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
6591
6602
( 25 , user_id_high_opt, option) ,
6592
6603
} ) ;
6593
6604
6605
+ let ( channel_keys_id, mut holder_signer) = if ver <= 2 {
6606
+ assert ! ( holder_signer. is_some( ) && channel_keys_id. is_none( ) ) ;
6607
+ let holder_signer = holder_signer. unwrap ( ) ;
6608
+ ( holder_signer. channel_keys_id ( ) , holder_signer)
6609
+ } else {
6610
+ assert ! ( holder_signer. is_none( ) && channel_keys_id. is_some( ) ) ;
6611
+ let channel_keys_id = channel_keys_id. unwrap ( ) ;
6612
+ ( channel_keys_id, keys_source. derive_channel_signer ( channel_value_satoshis, channel_keys_id) )
6613
+ } ;
6614
+ // If we've gotten to the funding stage of the channel, populate the signer with its
6615
+ // required channel parameters.
6616
+ let non_shutdown_state = channel_state & ( !MULTI_STATE_FLAGS ) ;
6617
+ if non_shutdown_state >= ( ChannelState :: FundingCreated as u32 ) {
6618
+ holder_signer. ready_channel ( & channel_parameters) ;
6619
+ }
6620
+
6594
6621
if let Some ( preimages) = preimages_opt {
6595
6622
let mut iter = preimages. into_iter ( ) ;
6596
6623
for htlc in pending_outbound_htlcs. iter_mut ( ) {
@@ -6740,6 +6767,7 @@ impl<'a, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<<K::Target as KeysInte
6740
6767
historical_inbound_htlc_fulfills,
6741
6768
6742
6769
channel_type : channel_type. unwrap ( ) ,
6770
+ _channel_keys_id : channel_keys_id,
6743
6771
} )
6744
6772
}
6745
6773
}
0 commit comments