@@ -412,6 +412,17 @@ pub trait KeysInterface {
412
412
/// This method must return the same value each time it is called with a given `Recipient`
413
413
/// parameter.
414
414
fn get_node_secret ( & self , recipient : Recipient ) -> Result < SecretKey , ( ) > ;
415
+ /// Get node id based on the provided [`Recipient`]. This public key corresponds to the secret in
416
+ /// [`get_node_secret`].
417
+ ///
418
+ /// This method must return the same value each time it is called with a given `Recipient`
419
+ /// parameter.
420
+ ///
421
+ /// [`get_node_secret`]: KeysInterface::get_node_secret
422
+ fn get_node_id ( & self , recipient : Recipient ) -> Result < PublicKey , ( ) > {
423
+ let secp_ctx = Secp256k1 :: signing_only ( ) ;
424
+ Ok ( PublicKey :: from_secret_key ( & secp_ctx, & self . get_node_secret ( recipient) ?) )
425
+ }
415
426
/// Gets the ECDH shared secret of our [`node secret`] and `other_key`, multiplying by `tweak` if
416
427
/// one is provided. Note that this tweak can be applied to `other_key` instead of our node
417
428
/// secret, though this is less efficient.
@@ -871,6 +882,7 @@ impl ReadableArgs<SecretKey> for InMemorySigner {
871
882
pub struct KeysManager {
872
883
secp_ctx : Secp256k1 < secp256k1:: All > ,
873
884
node_secret : SecretKey ,
885
+ node_id : PublicKey ,
874
886
inbound_payment_key : KeyMaterial ,
875
887
destination_script : Script ,
876
888
shutdown_pubkey : PublicKey ,
@@ -912,6 +924,7 @@ impl KeysManager {
912
924
match ExtendedPrivKey :: new_master ( Network :: Testnet , seed) {
913
925
Ok ( master_key) => {
914
926
let node_secret = master_key. ckd_priv ( & secp_ctx, ChildNumber :: from_hardened_idx ( 0 ) . unwrap ( ) ) . expect ( "Your RNG is busted" ) . private_key ;
927
+ let node_id = PublicKey :: from_secret_key ( & secp_ctx, & node_secret) ;
915
928
let destination_script = match master_key. ckd_priv ( & secp_ctx, ChildNumber :: from_hardened_idx ( 1 ) . unwrap ( ) ) {
916
929
Ok ( destination_key) => {
917
930
let wpubkey_hash = WPubkeyHash :: hash ( & ExtendedPubKey :: from_priv ( & secp_ctx, & destination_key) . to_pub ( ) . to_bytes ( ) ) ;
@@ -939,6 +952,7 @@ impl KeysManager {
939
952
let mut res = KeysManager {
940
953
secp_ctx,
941
954
node_secret,
955
+ node_id,
942
956
inbound_payment_key : KeyMaterial ( inbound_pmt_key_bytes) ,
943
957
944
958
destination_script,
@@ -1158,6 +1172,13 @@ impl KeysInterface for KeysManager {
1158
1172
}
1159
1173
}
1160
1174
1175
+ fn get_node_id ( & self , recipient : Recipient ) -> Result < PublicKey , ( ) > {
1176
+ match recipient {
1177
+ Recipient :: Node => Ok ( self . node_id . clone ( ) ) ,
1178
+ Recipient :: PhantomNode => Err ( ( ) )
1179
+ }
1180
+ }
1181
+
1161
1182
fn ecdh ( & self , recipient : Recipient , other_key : & PublicKey , tweak : Option < & Scalar > ) -> Result < SharedSecret , ( ) > {
1162
1183
let mut node_secret = self . get_node_secret ( recipient) ?;
1163
1184
if let Some ( tweak) = tweak {
@@ -1238,6 +1259,7 @@ pub struct PhantomKeysManager {
1238
1259
inner : KeysManager ,
1239
1260
inbound_payment_key : KeyMaterial ,
1240
1261
phantom_secret : SecretKey ,
1262
+ phantom_node_id : PublicKey ,
1241
1263
}
1242
1264
1243
1265
impl KeysInterface for PhantomKeysManager {
@@ -1250,6 +1272,13 @@ impl KeysInterface for PhantomKeysManager {
1250
1272
}
1251
1273
}
1252
1274
1275
+ fn get_node_id ( & self , recipient : Recipient ) -> Result < PublicKey , ( ) > {
1276
+ match recipient {
1277
+ Recipient :: Node => self . inner . get_node_id ( Recipient :: Node ) ,
1278
+ Recipient :: PhantomNode => Ok ( self . phantom_node_id . clone ( ) ) ,
1279
+ }
1280
+ }
1281
+
1253
1282
fn ecdh ( & self , recipient : Recipient , other_key : & PublicKey , tweak : Option < & Scalar > ) -> Result < SharedSecret , ( ) > {
1254
1283
let mut node_secret = self . get_node_secret ( recipient) ?;
1255
1284
if let Some ( tweak) = tweak {
@@ -1303,10 +1332,13 @@ impl PhantomKeysManager {
1303
1332
pub fn new ( seed : & [ u8 ; 32 ] , starting_time_secs : u64 , starting_time_nanos : u32 , cross_node_seed : & [ u8 ; 32 ] ) -> Self {
1304
1333
let inner = KeysManager :: new ( seed, starting_time_secs, starting_time_nanos) ;
1305
1334
let ( inbound_key, phantom_key) = hkdf_extract_expand_twice ( b"LDK Inbound and Phantom Payment Key Expansion" , cross_node_seed) ;
1335
+ let phantom_secret = SecretKey :: from_slice ( & phantom_key) . unwrap ( ) ;
1336
+ let phantom_node_id = PublicKey :: from_secret_key ( & inner. secp_ctx , & phantom_secret) ;
1306
1337
Self {
1307
1338
inner,
1308
1339
inbound_payment_key : KeyMaterial ( inbound_key) ,
1309
- phantom_secret : SecretKey :: from_slice ( & phantom_key) . unwrap ( ) ,
1340
+ phantom_secret,
1341
+ phantom_node_id,
1310
1342
}
1311
1343
}
1312
1344
0 commit comments