@@ -1983,29 +1983,61 @@ impl KeysManager {
1983
1983
. expect ( "Your RNG is busted" ) ;
1984
1984
unique_start. input ( & child_privkey. private_key [ ..] ) ;
1985
1985
1986
- let seed = Sha256 :: from_engine ( unique_start) . to_byte_array ( ) ;
1986
+ let unique_seed = Sha256 :: from_engine ( unique_start) . to_byte_array ( ) ;
1987
1987
1988
1988
let commitment_seed = {
1989
1989
let mut sha = Sha256 :: engine ( ) ;
1990
- sha. input ( & seed ) ;
1990
+ sha. input ( & unique_seed ) ;
1991
1991
sha. input ( & b"commitment seed" [ ..] ) ;
1992
1992
Sha256 :: from_engine ( sha) . to_byte_array ( )
1993
1993
} ;
1994
1994
macro_rules! key_step {
1995
1995
( $info: expr, $prev_key: expr) => { {
1996
1996
let mut sha = Sha256 :: engine( ) ;
1997
- sha. input( & seed ) ;
1997
+ sha. input( & unique_seed ) ;
1998
1998
sha. input( & $prev_key[ ..] ) ;
1999
1999
sha. input( & $info[ ..] ) ;
2000
2000
SecretKey :: from_slice( & Sha256 :: from_engine( sha) . to_byte_array( ) )
2001
2001
. expect( "SHA-256 is busted" )
2002
2002
} } ;
2003
2003
}
2004
- let funding_key = key_step ! ( b"funding key" , commitment_seed) ;
2005
- let revocation_base_key = key_step ! ( b"revocation base key" , funding_key) ;
2006
- let payment_key = key_step ! ( b"payment key" , revocation_base_key) ;
2007
- let delayed_payment_base_key = key_step ! ( b"delayed payment base key" , payment_key) ;
2008
- let htlc_base_key = key_step ! ( b"HTLC base key" , delayed_payment_base_key) ;
2004
+
2005
+ let funding_key;
2006
+ let revocation_base_key;
2007
+ let payment_key;
2008
+ let delayed_payment_base_key;
2009
+ let htlc_base_key;
2010
+
2011
+ let channel_keys_derivation_version =
2012
+ channel_keys_derivation_version_from_id ( channel_keys_id) ;
2013
+ if channel_keys_derivation_version < 1 {
2014
+ // In LDK versions prior to 0.1 we used to derive the `payment_key` uniquely on a
2015
+ // per-channel basis, which disallowed users to re-derive them if they lost their
2016
+ // `channel_keys_id`.
2017
+ funding_key = key_step ! ( b"funding key" , commitment_seed) ;
2018
+ revocation_base_key = key_step ! ( b"revocation base key" , funding_key) ;
2019
+ payment_key = key_step ! ( b"payment key" , revocation_base_key) ;
2020
+ delayed_payment_base_key = key_step ! ( b"delayed payment base key" , payment_key) ;
2021
+ htlc_base_key = key_step ! ( b"HTLC base key" , delayed_payment_base_key) ;
2022
+ } else {
2023
+ funding_key = key_step ! ( b"funding key" , commitment_seed) ;
2024
+ revocation_base_key = key_step ! ( b"revocation base key" , funding_key) ;
2025
+ delayed_payment_base_key = key_step ! ( b"delayed payment base key" , revocation_base_key) ;
2026
+ htlc_base_key = key_step ! ( b"HTLC base key" , delayed_payment_base_key) ;
2027
+
2028
+ // Starting with LDK v0.1, we derive `payment_key` directly from our seed, allowing
2029
+ // users to re-derive it even when losing all channel state. This allows them to
2030
+ // recover any non-HTLC-encumbered funds in case of data loss if the counterparty is so
2031
+ // nice to force-closure for them.
2032
+ payment_key = {
2033
+ let mut sha = Sha256 :: engine ( ) ;
2034
+ sha. input ( & self . seed ) ;
2035
+ sha. input ( & b"static payment key" [ ..] ) ;
2036
+ SecretKey :: from_slice ( & Sha256 :: from_engine ( sha) . to_byte_array ( ) )
2037
+ . expect ( "SHA-256 is busted" )
2038
+ } ;
2039
+ } ;
2040
+
2009
2041
let prng_seed = self . get_secure_random_bytes ( ) ;
2010
2042
2011
2043
InMemorySigner :: new (
@@ -2249,6 +2281,14 @@ impl OutputSpender for KeysManager {
2249
2281
}
2250
2282
}
2251
2283
2284
+ /// The version of the channel key derivation scheme.
2285
+ pub ( crate ) const CHANNEL_KEYS_DERIVATION_VERSION : u8 = 1 ;
2286
+
2287
+ /// Returns the keys derviation version set in `channel_keys_id`.
2288
+ pub ( crate ) fn channel_keys_derivation_version_from_id ( channel_keys_id : [ u8 ; 32 ] ) -> u8 {
2289
+ channel_keys_id[ 0 ]
2290
+ }
2291
+
2252
2292
impl SignerProvider for KeysManager {
2253
2293
type EcdsaSigner = InMemorySigner ;
2254
2294
#[ cfg( taproot) ]
@@ -2261,11 +2301,16 @@ impl SignerProvider for KeysManager {
2261
2301
// `child_idx` is the only thing guaranteed to make each channel unique without a restart
2262
2302
// (though `user_channel_id` should help, depending on user behavior). If it manages to
2263
2303
// roll over, we may generate duplicate keys for two different channels, which could result
2264
- // in loss of funds. Because we only support 32-bit+ systems, assert that our `AtomicUsize`
2265
- // doesn't reach `u32::MAX`.
2266
- assert ! ( child_idx < core:: u32 :: MAX as usize , "2^32 channels opened without restart" ) ;
2304
+ // in loss of funds. Because we only support 32-bit+ systems, and we use the first byte as
2305
+ // a versioning field, assert that our `AtomicUsize`
2306
+ // doesn't reach the maximal 24-bit value, i.e., `U24_MAX`.
2307
+ const U24_MAX : usize = 0xFFFFFFF ;
2308
+ assert ! ( child_idx < U24_MAX , "2^31 channels opened without restart" ) ;
2309
+ let child_idx_be_bytes = ( child_idx as u32 ) . to_be_bytes ( ) ;
2310
+
2267
2311
let mut id = [ 0 ; 32 ] ;
2268
- id[ 0 ..4 ] . copy_from_slice ( & ( child_idx as u32 ) . to_be_bytes ( ) ) ;
2312
+ id[ 0 ] = CHANNEL_KEYS_DERIVATION_VERSION ;
2313
+ id[ 1 ..4 ] . copy_from_slice ( & child_idx_be_bytes[ 1 ..4 ] ) ;
2269
2314
id[ 4 ..8 ] . copy_from_slice ( & self . starting_time_nanos . to_be_bytes ( ) ) ;
2270
2315
id[ 8 ..16 ] . copy_from_slice ( & self . starting_time_secs . to_be_bytes ( ) ) ;
2271
2316
id[ 16 ..32 ] . copy_from_slice ( & user_channel_id. to_be_bytes ( ) ) ;
0 commit comments