@@ -41,8 +41,8 @@ use crate::chain::transaction::OutPoint;
41
41
use crate :: crypto:: utils:: { hkdf_extract_expand_twice, sign, sign_with_aux_rand} ;
42
42
use crate :: ln:: chan_utils;
43
43
use crate :: ln:: chan_utils:: {
44
- get_revokeable_redeemscript , make_funding_redeemscript , ChannelPublicKeys ,
45
- ChannelTransactionParameters , ClosingTransaction , CommitmentTransaction ,
44
+ get_counterparty_payment_script , get_revokeable_redeemscript , make_funding_redeemscript ,
45
+ ChannelPublicKeys , ChannelTransactionParameters , ClosingTransaction , CommitmentTransaction ,
46
46
HTLCOutputInCommitment , HolderCommitmentTransaction ,
47
47
} ;
48
48
use crate :: ln:: channel:: ANCHOR_OUTPUT_VALUE_SATOSHI ;
@@ -56,6 +56,7 @@ use crate::ln::msgs::PartialSignatureWithNonce;
56
56
use crate :: ln:: msgs:: { UnsignedChannelAnnouncement , UnsignedGossipMessage } ;
57
57
use crate :: ln:: script:: ShutdownScript ;
58
58
use crate :: offers:: invoice:: UnsignedBolt12Invoice ;
59
+ use crate :: types:: features:: ChannelTypeFeatures ;
59
60
use crate :: types:: payment:: PaymentPreimage ;
60
61
use crate :: util:: async_poll:: AsyncResult ;
61
62
use crate :: util:: ser:: { ReadableArgs , Writeable } ;
@@ -140,6 +141,16 @@ pub(crate) const P2WPKH_WITNESS_WEIGHT: u64 = 1 /* num stack items */ +
140
141
pub ( crate ) const P2TR_KEY_PATH_WITNESS_WEIGHT : u64 = 1 /* witness items */
141
142
+ 1 /* schnorr sig len */ + 64 /* schnorr sig */ ;
142
143
144
+ /// If a [`KeysManager`] is built with [`KeysManager::new`] with `v2_remote_key_derivation` set,
145
+ /// the script which we receive funds to on-chain when our counterparty force-closes a channel is
146
+ /// one of this many possible derivation paths.
147
+ ///
148
+ /// Keping this limited allows for scanning the chain to find lost funds if our state is destroyed,
149
+ /// while this being more than a handful provides some privacy by not constantly reusing the same
150
+ /// scripts on-chain across channels.
151
+ // Note that this MUST remain below the maximum BIP 32 derivation paths (2^31)
152
+ pub const STATIC_PAYMENT_KEY_COUNT : u16 = 1000 ;
153
+
143
154
/// Information about a spendable output to our "payment key".
144
155
///
145
156
/// See [`SpendableOutputDescriptor::StaticPaymentOutput`] for more details on how to spend this.
@@ -371,7 +382,7 @@ impl SpendableOutputDescriptor {
371
382
if let Some ( basepoint) = delayed_payment_basepoint. as_ref ( ) {
372
383
// Required to derive signing key: privkey = basepoint_secret + SHA256(per_commitment_point || basepoint)
373
384
let add_tweak = basepoint. derive_add_tweak ( & per_commitment_point) ;
374
- let payment_key = DelayedPaymentKey ( add_public_key_tweak (
385
+ let delayed_payment_key = DelayedPaymentKey ( add_public_key_tweak (
375
386
secp_ctx,
376
387
& basepoint. to_public_key ( ) ,
377
388
& add_tweak,
@@ -381,7 +392,7 @@ impl SpendableOutputDescriptor {
381
392
Some ( get_revokeable_redeemscript (
382
393
& revocation_pubkey,
383
394
* to_self_delay,
384
- & payment_key ,
395
+ & delayed_payment_key ,
385
396
) ) ,
386
397
Some ( add_tweak) ,
387
398
)
@@ -1140,8 +1151,12 @@ pub struct InMemorySigner {
1140
1151
funding_key : sealed:: MaybeTweakedSecretKey ,
1141
1152
/// Holder secret key for blinded revocation pubkey.
1142
1153
pub revocation_base_key : SecretKey ,
1143
- /// Holder secret key used for our balance in counterparty-broadcasted commitment transactions.
1144
- pub payment_key : SecretKey ,
1154
+ /// Holder secret key used for our balance in counterparty-broadcasted commitment transactions,
1155
+ /// old-style derivation.
1156
+ payment_key_v1 : SecretKey ,
1157
+ /// Holder secret key used for our balance in counterparty-broadcasted commitment transactions,
1158
+ /// new-style derivation.
1159
+ payment_key_v2 : SecretKey ,
1145
1160
/// Holder secret key used in an HTLC transaction.
1146
1161
pub delayed_payment_base_key : SecretKey ,
1147
1162
/// Holder HTLC secret key used in commitment transaction HTLC outputs.
@@ -1160,7 +1175,8 @@ impl PartialEq for InMemorySigner {
1160
1175
fn eq ( & self , other : & Self ) -> bool {
1161
1176
self . funding_key == other. funding_key
1162
1177
&& self . revocation_base_key == other. revocation_base_key
1163
- && self . payment_key == other. payment_key
1178
+ && self . payment_key_v1 == other. payment_key_v1
1179
+ && self . payment_key_v2 == other. payment_key_v2
1164
1180
&& self . delayed_payment_base_key == other. delayed_payment_base_key
1165
1181
&& self . htlc_base_key == other. htlc_base_key
1166
1182
&& self . commitment_seed == other. commitment_seed
@@ -1174,7 +1190,8 @@ impl Clone for InMemorySigner {
1174
1190
Self {
1175
1191
funding_key : self . funding_key . clone ( ) ,
1176
1192
revocation_base_key : self . revocation_base_key . clone ( ) ,
1177
- payment_key : self . payment_key . clone ( ) ,
1193
+ payment_key_v1 : self . payment_key_v1 . clone ( ) ,
1194
+ payment_key_v2 : self . payment_key_v2 . clone ( ) ,
1178
1195
delayed_payment_base_key : self . delayed_payment_base_key . clone ( ) ,
1179
1196
htlc_base_key : self . htlc_base_key . clone ( ) ,
1180
1197
commitment_seed : self . commitment_seed . clone ( ) ,
@@ -1186,24 +1203,57 @@ impl Clone for InMemorySigner {
1186
1203
}
1187
1204
1188
1205
impl InMemorySigner {
1189
- /// Creates a new [`InMemorySigner`].
1206
+ # [ cfg ( any ( feature = "_test_utils" , test ) ) ]
1190
1207
pub fn new < C : Signing > (
1191
1208
secp_ctx : & Secp256k1 < C > , funding_key : SecretKey , revocation_base_key : SecretKey ,
1192
- payment_key : SecretKey , delayed_payment_base_key : SecretKey , htlc_base_key : SecretKey ,
1193
- commitment_seed : [ u8 ; 32 ] , channel_keys_id : [ u8 ; 32 ] , rand_bytes_unique_start : [ u8 ; 32 ] ,
1209
+ payment_key_v1 : SecretKey , payment_key_v2 : SecretKey , delayed_payment_base_key : SecretKey ,
1210
+ htlc_base_key : SecretKey , commitment_seed : [ u8 ; 32 ] , channel_keys_id : [ u8 ; 32 ] ,
1211
+ rand_bytes_unique_start : [ u8 ; 32 ] ,
1212
+ ) -> InMemorySigner {
1213
+ // TODO: Make the key used dynamic
1214
+ let holder_channel_pubkeys = InMemorySigner :: make_holder_keys (
1215
+ secp_ctx,
1216
+ & funding_key,
1217
+ & revocation_base_key,
1218
+ & payment_key_v1,
1219
+ & delayed_payment_base_key,
1220
+ & htlc_base_key,
1221
+ ) ;
1222
+ InMemorySigner {
1223
+ funding_key : sealed:: MaybeTweakedSecretKey :: from ( funding_key) ,
1224
+ revocation_base_key,
1225
+ payment_key_v1,
1226
+ payment_key_v2,
1227
+ delayed_payment_base_key,
1228
+ htlc_base_key,
1229
+ commitment_seed,
1230
+ holder_channel_pubkeys,
1231
+ channel_keys_id,
1232
+ entropy_source : RandomBytes :: new ( rand_bytes_unique_start) ,
1233
+ }
1234
+ }
1235
+
1236
+ #[ cfg( not( any( feature = "_test_utils" , test) ) ) ]
1237
+ fn new < C : Signing > (
1238
+ secp_ctx : & Secp256k1 < C > , funding_key : SecretKey , revocation_base_key : SecretKey ,
1239
+ payment_key_v1 : SecretKey , payment_key_v2 : SecretKey , delayed_payment_base_key : SecretKey ,
1240
+ htlc_base_key : SecretKey , commitment_seed : [ u8 ; 32 ] , channel_keys_id : [ u8 ; 32 ] ,
1241
+ rand_bytes_unique_start : [ u8 ; 32 ] ,
1194
1242
) -> InMemorySigner {
1243
+ // TODO: Make the key used dynamic
1195
1244
let holder_channel_pubkeys = InMemorySigner :: make_holder_keys (
1196
1245
secp_ctx,
1197
1246
& funding_key,
1198
1247
& revocation_base_key,
1199
- & payment_key ,
1248
+ & payment_key_v1 ,
1200
1249
& delayed_payment_base_key,
1201
1250
& htlc_base_key,
1202
1251
) ;
1203
1252
InMemorySigner {
1204
1253
funding_key : sealed:: MaybeTweakedSecretKey :: from ( funding_key) ,
1205
1254
revocation_base_key,
1206
- payment_key,
1255
+ payment_key_v1,
1256
+ payment_key_v2,
1207
1257
delayed_payment_base_key,
1208
1258
htlc_base_key,
1209
1259
commitment_seed,
@@ -1264,14 +1314,28 @@ impl InMemorySigner {
1264
1314
return Err ( ( ) ) ;
1265
1315
}
1266
1316
1267
- let remotepubkey = bitcoin :: PublicKey :: new ( self . holder_channel_pubkeys . payment_point ) ;
1268
- let supports_anchors_zero_fee_htlc_tx = descriptor
1317
+ let legacy_default_channel_type = ChannelTypeFeatures :: only_static_remote_key ( ) ;
1318
+ let channel_type_features = descriptor
1269
1319
. channel_transaction_parameters
1270
1320
. as_ref ( )
1271
- . map ( |params| params. channel_type_features . supports_anchors_zero_fee_htlc_tx ( ) )
1272
- . unwrap_or ( false ) ;
1321
+ . map ( |params| & params. channel_type_features )
1322
+ . unwrap_or ( & legacy_default_channel_type) ;
1323
+
1324
+ let payment_point_v1 = PublicKey :: from_secret_key ( secp_ctx, & self . payment_key_v1 ) ;
1325
+ let payment_point_v2 = PublicKey :: from_secret_key ( secp_ctx, & self . payment_key_v2 ) ;
1326
+ let spk_v1 = get_counterparty_payment_script ( channel_type_features, & payment_point_v1) ;
1327
+ let spk_v2 = get_counterparty_payment_script ( channel_type_features, & payment_point_v2) ;
1328
+
1329
+ let ( remotepubkey, payment_key) = if spk_v1 == descriptor. output . script_pubkey {
1330
+ ( bitcoin:: PublicKey :: new ( payment_point_v1) , & self . payment_key_v1 )
1331
+ } else {
1332
+ if spk_v2 != descriptor. output . script_pubkey {
1333
+ return Err ( ( ) ) ;
1334
+ }
1335
+ ( bitcoin:: PublicKey :: new ( payment_point_v2) , & self . payment_key_v2 )
1336
+ } ;
1273
1337
1274
- let witness_script = if supports_anchors_zero_fee_htlc_tx {
1338
+ let witness_script = if channel_type_features . supports_anchors_zero_fee_htlc_tx ( ) {
1275
1339
chan_utils:: get_to_countersigner_keyed_anchor_redeemscript ( & remotepubkey. inner )
1276
1340
} else {
1277
1341
ScriptBuf :: new_p2pkh ( & remotepubkey. pubkey_hash ( ) )
@@ -1286,8 +1350,8 @@ impl InMemorySigner {
1286
1350
)
1287
1351
. unwrap( ) [ ..]
1288
1352
) ;
1289
- let remotesig = sign_with_aux_rand ( secp_ctx, & sighash, & self . payment_key , & self ) ;
1290
- let payment_script = if supports_anchors_zero_fee_htlc_tx {
1353
+ let remotesig = sign_with_aux_rand ( secp_ctx, & sighash, payment_key, & self ) ;
1354
+ let payment_script = if channel_type_features . supports_anchors_zero_fee_htlc_tx ( ) {
1291
1355
witness_script. to_p2wsh ( )
1292
1356
} else {
1293
1357
ScriptBuf :: new_p2wpkh ( & remotepubkey. wpubkey_hash ( ) . unwrap ( ) )
@@ -1300,7 +1364,7 @@ impl InMemorySigner {
1300
1364
let mut witness = Vec :: with_capacity ( 2 ) ;
1301
1365
witness. push ( remotesig. serialize_der ( ) . to_vec ( ) ) ;
1302
1366
witness[ 0 ] . push ( EcdsaSighashType :: All as u8 ) ;
1303
- if supports_anchors_zero_fee_htlc_tx {
1367
+ if channel_type_features . supports_anchors_zero_fee_htlc_tx ( ) {
1304
1368
witness. push ( witness_script. to_bytes ( ) ) ;
1305
1369
} else {
1306
1370
witness. push ( remotepubkey. to_bytes ( ) ) ;
@@ -1874,6 +1938,7 @@ pub struct KeysManager {
1874
1938
destination_script : ScriptBuf ,
1875
1939
shutdown_pubkey : PublicKey ,
1876
1940
channel_master_key : Xpriv ,
1941
+ static_payment_key : Xpriv ,
1877
1942
channel_child_index : AtomicUsize ,
1878
1943
peer_storage_key : PeerStorageKey ,
1879
1944
receive_auth_key : ReceiveAuthKey ,
@@ -1915,6 +1980,7 @@ impl KeysManager {
1915
1980
const INBOUND_PAYMENT_KEY_INDEX : ChildNumber = ChildNumber :: Hardened { index : 5 } ;
1916
1981
const PEER_STORAGE_KEY_INDEX : ChildNumber = ChildNumber :: Hardened { index : 6 } ;
1917
1982
const RECEIVE_AUTH_KEY_INDEX : ChildNumber = ChildNumber :: Hardened { index : 7 } ;
1983
+ const STATIC_PAYMENT_KEY_INDEX : ChildNumber = ChildNumber :: Hardened { index : 8 } ;
1918
1984
1919
1985
let secp_ctx = Secp256k1 :: new ( ) ;
1920
1986
// Note that when we aren't serializing the key, network doesn't matter
@@ -1962,6 +2028,10 @@ impl KeysManager {
1962
2028
. expect ( "Your RNG is busted" )
1963
2029
. private_key ;
1964
2030
2031
+ let static_payment_key = master_key
2032
+ . derive_priv ( & secp_ctx, & STATIC_PAYMENT_KEY_INDEX )
2033
+ . expect ( "Your RNG is busted" ) ;
2034
+
1965
2035
let mut rand_bytes_engine = Sha256 :: engine ( ) ;
1966
2036
rand_bytes_engine. input ( & starting_time_secs. to_be_bytes ( ) ) ;
1967
2037
rand_bytes_engine. input ( & starting_time_nanos. to_be_bytes ( ) ) ;
@@ -1984,6 +2054,7 @@ impl KeysManager {
1984
2054
1985
2055
channel_master_key,
1986
2056
channel_child_index : AtomicUsize :: new ( 0 ) ,
2057
+ static_payment_key,
1987
2058
1988
2059
entropy_source : RandomBytes :: new ( rand_bytes_unique_start) ,
1989
2060
@@ -2004,6 +2075,19 @@ impl KeysManager {
2004
2075
self . node_secret
2005
2076
}
2006
2077
2078
+ fn derive_payment_key_v2 ( & self , params : & [ u8 ; 32 ] ) -> SecretKey {
2079
+ let mut eight_bytes = [ 0 ; 8 ] ;
2080
+ eight_bytes. copy_from_slice ( & params[ 0 ..8 ] ) ;
2081
+ let idx = u64:: from_le_bytes ( eight_bytes) % u64:: from ( STATIC_PAYMENT_KEY_COUNT ) ;
2082
+ self . static_payment_key
2083
+ . derive_priv (
2084
+ & self . secp_ctx ,
2085
+ & ChildNumber :: from_hardened_idx ( idx as u32 ) . expect ( "key space exhausted" ) ,
2086
+ )
2087
+ . expect ( "Your RNG is busted" )
2088
+ . private_key
2089
+ }
2090
+
2007
2091
/// Derive an old [`EcdsaChannelSigner`] containing per-channel secrets based on a key derivation parameters.
2008
2092
pub fn derive_channel_keys ( & self , params : & [ u8 ; 32 ] ) -> InMemorySigner {
2009
2093
let chan_id = u64:: from_be_bytes ( params[ 0 ..8 ] . try_into ( ) . unwrap ( ) ) ;
@@ -2044,16 +2128,17 @@ impl KeysManager {
2044
2128
}
2045
2129
let funding_key = key_step ! ( b"funding key" , commitment_seed) ;
2046
2130
let revocation_base_key = key_step ! ( b"revocation base key" , funding_key) ;
2047
- let payment_key = key_step ! ( b"payment key" , revocation_base_key) ;
2048
- let delayed_payment_base_key = key_step ! ( b"delayed payment base key" , payment_key ) ;
2131
+ let payment_key_v1 = key_step ! ( b"payment key" , revocation_base_key) ;
2132
+ let delayed_payment_base_key = key_step ! ( b"delayed payment base key" , payment_key_v1 ) ;
2049
2133
let htlc_base_key = key_step ! ( b"HTLC base key" , delayed_payment_base_key) ;
2050
2134
let prng_seed = self . get_secure_random_bytes ( ) ;
2051
2135
2052
2136
InMemorySigner :: new (
2053
2137
& self . secp_ctx ,
2054
2138
funding_key,
2055
2139
revocation_base_key,
2056
- payment_key,
2140
+ payment_key_v1,
2141
+ self . derive_payment_key_v2 ( & commitment_seed) ,
2057
2142
delayed_payment_base_key,
2058
2143
htlc_base_key,
2059
2144
commitment_seed,
0 commit comments