@@ -100,7 +100,7 @@ pub enum SpendableOutputDescriptor {
100
100
output : TxOut ,
101
101
/// The channel keys state used to proceed to derivation of signing key. Must
102
102
/// be pass to KeysInterface::derive_channel_keys.
103
- key_derivation_params : ( u64 , u64 ) ,
103
+ channel_keys_id : [ u8 ; 32 ] ,
104
104
/// The revocation_pubkey used to derive witnessScript
105
105
revocation_pubkey : PublicKey
106
106
} ,
@@ -118,7 +118,7 @@ pub enum SpendableOutputDescriptor {
118
118
output : TxOut ,
119
119
/// The channel keys state used to proceed to derivation of signing key. Must
120
120
/// be pass to KeysInterface::derive_channel_keys.
121
- key_derivation_params : ( u64 , u64 ) ,
121
+ channel_keys_id : [ u8 ; 32 ] ,
122
122
}
123
123
}
124
124
@@ -130,22 +130,20 @@ impl Writeable for SpendableOutputDescriptor {
130
130
outpoint. write ( writer) ?;
131
131
output. write ( writer) ?;
132
132
} ,
133
- & SpendableOutputDescriptor :: DynamicOutputP2WSH { ref outpoint, ref per_commitment_point, ref to_self_delay, ref output, ref key_derivation_params , ref revocation_pubkey } => {
133
+ & SpendableOutputDescriptor :: DynamicOutputP2WSH { ref outpoint, ref per_commitment_point, ref to_self_delay, ref output, ref channel_keys_id , ref revocation_pubkey } => {
134
134
1u8 . write ( writer) ?;
135
135
outpoint. write ( writer) ?;
136
136
per_commitment_point. write ( writer) ?;
137
137
to_self_delay. write ( writer) ?;
138
138
output. write ( writer) ?;
139
- key_derivation_params. 0 . write ( writer) ?;
140
- key_derivation_params. 1 . write ( writer) ?;
139
+ channel_keys_id. write ( writer) ?;
141
140
revocation_pubkey. write ( writer) ?;
142
141
} ,
143
- & SpendableOutputDescriptor :: StaticOutputCounterpartyPayment { ref outpoint, ref output, ref key_derivation_params } => {
142
+ & SpendableOutputDescriptor :: StaticOutputCounterpartyPayment { ref outpoint, ref output, ref channel_keys_id } => {
144
143
2u8 . write ( writer) ?;
145
144
outpoint. write ( writer) ?;
146
145
output. write ( writer) ?;
147
- key_derivation_params. 0 . write ( writer) ?;
148
- key_derivation_params. 1 . write ( writer) ?;
146
+ channel_keys_id. write ( writer) ?;
149
147
} ,
150
148
}
151
149
Ok ( ( ) )
@@ -164,13 +162,13 @@ impl Readable for SpendableOutputDescriptor {
164
162
per_commitment_point : Readable :: read ( reader) ?,
165
163
to_self_delay : Readable :: read ( reader) ?,
166
164
output : Readable :: read ( reader) ?,
167
- key_derivation_params : ( Readable :: read ( reader) ?, Readable :: read ( reader ) ? ) ,
165
+ channel_keys_id : Readable :: read ( reader) ?,
168
166
revocation_pubkey : Readable :: read ( reader) ?,
169
167
} ) ,
170
168
2u8 => Ok ( SpendableOutputDescriptor :: StaticOutputCounterpartyPayment {
171
169
outpoint : Readable :: read ( reader) ?,
172
170
output : Readable :: read ( reader) ?,
173
- key_derivation_params : ( Readable :: read ( reader) ?, Readable :: read ( reader ) ? ) ,
171
+ channel_keys_id : Readable :: read ( reader) ?,
174
172
} ) ,
175
173
_ => Err ( DecodeError :: InvalidValue ) ,
176
174
}
@@ -221,10 +219,10 @@ pub trait ChannelKeys : Send+Clone + Writeable {
221
219
fn release_commitment_secret ( & self , idx : u64 ) -> [ u8 ; 32 ] ;
222
220
/// Gets the holder's channel public keys and basepoints
223
221
fn pubkeys ( & self ) -> & ChannelPublicKeys ;
224
- /// Gets arbitrary identifiers describing the set of keys which are provided back to you in
225
- /// some SpendableOutputDescriptor types. These should be sufficient to identify this
222
+ /// Gets an arbitrary identifier describing the set of keys which are provided back to you in
223
+ /// some SpendableOutputDescriptor types. This should be sufficient to identify this
226
224
/// ChannelKeys object uniquely and lookup or re-derive its keys.
227
- fn key_derivation_params ( & self ) -> ( u64 , u64 ) ;
225
+ fn channel_keys_id ( & self ) -> [ u8 ; 32 ] ;
228
226
229
227
/// Create a signature for a counterparty's commitment transaction and associated HTLC transactions.
230
228
///
@@ -375,7 +373,7 @@ pub struct InMemoryChannelKeys {
375
373
/// The total value of this channel
376
374
channel_value_satoshis : u64 ,
377
375
/// Key derivation parameters
378
- key_derivation_params : ( u64 , u64 ) ,
376
+ channel_keys_id : [ u8 ; 32 ] ,
379
377
}
380
378
381
379
impl InMemoryChannelKeys {
@@ -389,7 +387,7 @@ impl InMemoryChannelKeys {
389
387
htlc_base_key : SecretKey ,
390
388
commitment_seed : [ u8 ; 32 ] ,
391
389
channel_value_satoshis : u64 ,
392
- key_derivation_params : ( u64 , u64 ) ) -> InMemoryChannelKeys {
390
+ channel_keys_id : [ u8 ; 32 ] ) -> InMemoryChannelKeys {
393
391
let holder_channel_pubkeys =
394
392
InMemoryChannelKeys :: make_holder_keys ( secp_ctx, & funding_key, & revocation_base_key,
395
393
& payment_key, & delayed_payment_base_key,
@@ -404,7 +402,7 @@ impl InMemoryChannelKeys {
404
402
channel_value_satoshis,
405
403
holder_channel_pubkeys,
406
404
channel_parameters : None ,
407
- key_derivation_params ,
405
+ channel_keys_id ,
408
406
}
409
407
}
410
408
@@ -468,7 +466,7 @@ impl ChannelKeys for InMemoryChannelKeys {
468
466
}
469
467
470
468
fn pubkeys ( & self ) -> & ChannelPublicKeys { & self . holder_channel_pubkeys }
471
- fn key_derivation_params ( & self ) -> ( u64 , u64 ) { self . key_derivation_params }
469
+ fn channel_keys_id ( & self ) -> [ u8 ; 32 ] { self . channel_keys_id }
472
470
473
471
fn sign_counterparty_commitment < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , commitment_tx : & CommitmentTransaction , secp_ctx : & Secp256k1 < T > ) -> Result < ( Signature , Vec < Signature > ) , ( ) > {
474
472
let trusted_tx = commitment_tx. trust ( ) ;
@@ -600,8 +598,7 @@ impl Writeable for InMemoryChannelKeys {
600
598
self . commitment_seed . write ( writer) ?;
601
599
self . channel_parameters . write ( writer) ?;
602
600
self . channel_value_satoshis . write ( writer) ?;
603
- self . key_derivation_params . 0 . write ( writer) ?;
604
- self . key_derivation_params . 1 . write ( writer) ?;
601
+ self . channel_keys_id . write ( writer) ?;
605
602
606
603
Ok ( ( ) )
607
604
}
@@ -622,8 +619,7 @@ impl Readable for InMemoryChannelKeys {
622
619
InMemoryChannelKeys :: make_holder_keys ( & secp_ctx, & funding_key, & revocation_base_key,
623
620
& payment_key, & delayed_payment_base_key,
624
621
& htlc_base_key) ;
625
- let params_1 = Readable :: read ( reader) ?;
626
- let params_2 = Readable :: read ( reader) ?;
622
+ let keys_id = Readable :: read ( reader) ?;
627
623
628
624
Ok ( InMemoryChannelKeys {
629
625
funding_key,
@@ -635,7 +631,7 @@ impl Readable for InMemoryChannelKeys {
635
631
channel_value_satoshis,
636
632
holder_channel_pubkeys,
637
633
channel_parameters : counterparty_channel_data,
638
- key_derivation_params : ( params_1 , params_2 ) ,
634
+ channel_keys_id : keys_id ,
639
635
} )
640
636
}
641
637
}
@@ -731,19 +727,19 @@ impl KeysManager {
731
727
/// Derive an old set of ChannelKeys for per-channel secrets based on a key derivation
732
728
/// parameters.
733
729
/// Key derivation parameters are accessible through a per-channel secrets
734
- /// ChannelKeys::key_derivation_params and is provided inside DynamicOuputP2WSH in case of
730
+ /// ChannelKeys::channel_keys_id and is provided inside DynamicOuputP2WSH in case of
735
731
/// onchain output detection for which a corresponding delayed_payment_key must be derived.
736
- pub fn derive_channel_keys ( & self , channel_value_satoshis : u64 , params_1 : u64 , params_2 : u64 ) -> InMemoryChannelKeys {
737
- let chan_id = ( ( params_1 & 0xFFFF_FFFF_0000_0000 ) >> 32 ) as u32 ;
732
+ pub fn derive_channel_keys ( & self , channel_value_satoshis : u64 , params : & [ u8 ; 32 ] ) -> InMemoryChannelKeys {
733
+ let chan_id = byte_utils:: slice_to_be64 ( & params[ 0 ..8 ] ) ;
734
+ assert ! ( chan_id <= std:: u32 :: MAX as u64 ) ; // Otherwise the params field wasn't created by us
738
735
let mut unique_start = Sha256 :: engine ( ) ;
739
- unique_start. input ( & byte_utils:: be64_to_array ( params_2) ) ;
740
- unique_start. input ( & byte_utils:: be32_to_array ( params_1 as u32 ) ) ;
736
+ unique_start. input ( params) ;
741
737
unique_start. input ( & self . seed ) ;
742
738
743
739
// We only seriously intend to rely on the channel_master_key for true secure
744
740
// entropy, everything else just ensures uniqueness. We rely on the unique_start (ie
745
741
// starting_time provided in the constructor) to be unique.
746
- let child_privkey = self . channel_master_key . ckd_priv ( & self . secp_ctx , ChildNumber :: from_hardened_idx ( chan_id) . expect ( "key space exhausted" ) ) . expect ( "Your RNG is busted" ) ;
742
+ let child_privkey = self . channel_master_key . ckd_priv ( & self . secp_ctx , ChildNumber :: from_hardened_idx ( chan_id as u32 ) . expect ( "key space exhausted" ) ) . expect ( "Your RNG is busted" ) ;
747
743
unique_start. input ( & child_privkey. private_key . key [ ..] ) ;
748
744
749
745
let seed = Sha256 :: from_engine ( unique_start) . into_inner ( ) ;
@@ -778,7 +774,7 @@ impl KeysManager {
778
774
htlc_base_key,
779
775
commitment_seed,
780
776
channel_value_satoshis,
781
- ( params_1 , params_2 ) ,
777
+ params . clone ( )
782
778
)
783
779
}
784
780
}
@@ -800,8 +796,12 @@ impl KeysInterface for KeysManager {
800
796
801
797
fn get_channel_keys ( & self , _inbound : bool , channel_value_satoshis : u64 ) -> Self :: ChanKeySigner {
802
798
let child_ix = self . channel_child_index . fetch_add ( 1 , Ordering :: AcqRel ) ;
803
- let ix_and_nanos: u64 = ( child_ix as u64 ) << 32 | ( self . starting_time_nanos as u64 ) ;
804
- self . derive_channel_keys ( channel_value_satoshis, ix_and_nanos, self . starting_time_secs )
799
+ assert ! ( child_ix <= std:: u32 :: MAX as usize ) ;
800
+ let mut id = [ 0 ; 32 ] ;
801
+ id[ 0 ..8 ] . copy_from_slice ( & byte_utils:: be64_to_array ( child_ix as u64 ) ) ;
802
+ id[ 8 ..16 ] . copy_from_slice ( & byte_utils:: be64_to_array ( self . starting_time_nanos as u64 ) ) ;
803
+ id[ 16 ..24 ] . copy_from_slice ( & byte_utils:: be64_to_array ( self . starting_time_secs ) ) ;
804
+ self . derive_channel_keys ( channel_value_satoshis, & id)
805
805
}
806
806
807
807
fn get_secure_random_bytes ( & self ) -> [ u8 ; 32 ] {
0 commit comments