@@ -405,6 +405,17 @@ pub trait KeysInterface {
405
405
/// This method must return the same value each time it is called with a given `Recipient`
406
406
/// parameter.
407
407
fn get_node_secret ( & self , recipient : Recipient ) -> Result < SecretKey , ( ) > ;
408
+ /// Get node id based on the provided [`Recipient`]. This public key corresponds to the secret in
409
+ /// [`get_node_secret`].
410
+ ///
411
+ /// This method must return the same value each time it is called with a given `Recipient`
412
+ /// parameter.
413
+ ///
414
+ /// [`get_node_secret`]: KeysInterface::get_node_secret
415
+ fn get_node_id ( & self , recipient : Recipient ) -> Result < PublicKey , ( ) > {
416
+ let secp_ctx = Secp256k1 :: signing_only ( ) ;
417
+ Ok ( PublicKey :: from_secret_key ( & secp_ctx, & self . get_node_secret ( recipient) ?) )
418
+ }
408
419
/// Gets the ECDH shared secret of our [`node secret`] and `other_key`, multiplying by `tweak` if
409
420
/// one is provided. Note that this tweak can be applied to `other_key` instead of our node
410
421
/// secret, though this is less efficient.
@@ -853,6 +864,7 @@ impl ReadableArgs<SecretKey> for InMemorySigner {
853
864
pub struct KeysManager {
854
865
secp_ctx : Secp256k1 < secp256k1:: All > ,
855
866
node_secret : SecretKey ,
867
+ node_id : PublicKey ,
856
868
inbound_payment_key : KeyMaterial ,
857
869
destination_script : Script ,
858
870
shutdown_pubkey : PublicKey ,
@@ -894,6 +906,7 @@ impl KeysManager {
894
906
match ExtendedPrivKey :: new_master ( Network :: Testnet , seed) {
895
907
Ok ( master_key) => {
896
908
let node_secret = master_key. ckd_priv ( & secp_ctx, ChildNumber :: from_hardened_idx ( 0 ) . unwrap ( ) ) . expect ( "Your RNG is busted" ) . private_key ;
909
+ let node_id = PublicKey :: from_secret_key ( & secp_ctx, & node_secret) ;
897
910
let destination_script = match master_key. ckd_priv ( & secp_ctx, ChildNumber :: from_hardened_idx ( 1 ) . unwrap ( ) ) {
898
911
Ok ( destination_key) => {
899
912
let wpubkey_hash = WPubkeyHash :: hash ( & ExtendedPubKey :: from_priv ( & secp_ctx, & destination_key) . to_pub ( ) . to_bytes ( ) ) ;
@@ -921,6 +934,7 @@ impl KeysManager {
921
934
let mut res = KeysManager {
922
935
secp_ctx,
923
936
node_secret,
937
+ node_id,
924
938
inbound_payment_key : KeyMaterial ( inbound_pmt_key_bytes) ,
925
939
926
940
destination_script,
@@ -1140,6 +1154,13 @@ impl KeysInterface for KeysManager {
1140
1154
}
1141
1155
}
1142
1156
1157
+ fn get_node_id ( & self , recipient : Recipient ) -> Result < PublicKey , ( ) > {
1158
+ match recipient {
1159
+ Recipient :: Node => Ok ( self . node_id . clone ( ) ) ,
1160
+ Recipient :: PhantomNode => Err ( ( ) )
1161
+ }
1162
+ }
1163
+
1143
1164
fn ecdh ( & self , recipient : Recipient , other_key : & PublicKey , tweak : Option < & Scalar > ) -> Result < SharedSecret , ( ) > {
1144
1165
let mut node_secret = self . get_node_secret ( recipient) ?;
1145
1166
if let Some ( tweak) = tweak {
@@ -1220,6 +1241,7 @@ pub struct PhantomKeysManager {
1220
1241
inner : KeysManager ,
1221
1242
inbound_payment_key : KeyMaterial ,
1222
1243
phantom_secret : SecretKey ,
1244
+ phantom_node_id : PublicKey ,
1223
1245
}
1224
1246
1225
1247
impl KeysInterface for PhantomKeysManager {
@@ -1232,6 +1254,13 @@ impl KeysInterface for PhantomKeysManager {
1232
1254
}
1233
1255
}
1234
1256
1257
+ fn get_node_id ( & self , recipient : Recipient ) -> Result < PublicKey , ( ) > {
1258
+ match recipient {
1259
+ Recipient :: Node => self . inner . get_node_id ( Recipient :: Node ) ,
1260
+ Recipient :: PhantomNode => Ok ( self . phantom_node_id . clone ( ) ) ,
1261
+ }
1262
+ }
1263
+
1235
1264
fn ecdh ( & self , recipient : Recipient , other_key : & PublicKey , tweak : Option < & Scalar > ) -> Result < SharedSecret , ( ) > {
1236
1265
let mut node_secret = self . get_node_secret ( recipient) ?;
1237
1266
if let Some ( tweak) = tweak {
@@ -1285,10 +1314,13 @@ impl PhantomKeysManager {
1285
1314
pub fn new ( seed : & [ u8 ; 32 ] , starting_time_secs : u64 , starting_time_nanos : u32 , cross_node_seed : & [ u8 ; 32 ] ) -> Self {
1286
1315
let inner = KeysManager :: new ( seed, starting_time_secs, starting_time_nanos) ;
1287
1316
let ( inbound_key, phantom_key) = hkdf_extract_expand_twice ( b"LDK Inbound and Phantom Payment Key Expansion" , cross_node_seed) ;
1317
+ let phantom_secret = SecretKey :: from_slice ( & phantom_key) . unwrap ( ) ;
1318
+ let phantom_node_id = PublicKey :: from_secret_key ( & inner. secp_ctx , & phantom_secret) ;
1288
1319
Self {
1289
1320
inner,
1290
1321
inbound_payment_key : KeyMaterial ( inbound_key) ,
1291
- phantom_secret : SecretKey :: from_slice ( & phantom_key) . unwrap ( ) ,
1322
+ phantom_secret,
1323
+ phantom_node_id,
1292
1324
}
1293
1325
}
1294
1326
0 commit comments