@@ -411,11 +411,28 @@ pub trait KeysInterface {
411
411
///
412
412
/// This method must return the same value each time it is called with a given `Recipient`
413
413
/// parameter.
414
+ ///
415
+ /// Errors if the `Recipient` variant is not supported by the implementation.
414
416
fn get_node_secret ( & self , recipient : Recipient ) -> Result < SecretKey , ( ) > ;
417
+ /// Get node id based on the provided [`Recipient`]. This public key corresponds to the secret in
418
+ /// [`get_node_secret`].
419
+ ///
420
+ /// This method must return the same value each time it is called with a given `Recipient`
421
+ /// parameter.
422
+ ///
423
+ /// Errors if the `Recipient` variant is not supported by the implementation.
424
+ ///
425
+ /// [`get_node_secret`]: KeysInterface::get_node_secret
426
+ fn get_node_id ( & self , recipient : Recipient ) -> Result < PublicKey , ( ) > {
427
+ let secp_ctx = Secp256k1 :: signing_only ( ) ;
428
+ Ok ( PublicKey :: from_secret_key ( & secp_ctx, & self . get_node_secret ( recipient) ?) )
429
+ }
415
430
/// Gets the ECDH shared secret of our [`node secret`] and `other_key`, multiplying by `tweak` if
416
431
/// one is provided. Note that this tweak can be applied to `other_key` instead of our node
417
432
/// secret, though this is less efficient.
418
433
///
434
+ /// Errors if the `Recipient` variant is not supported by the implementation.
435
+ ///
419
436
/// [`node secret`]: Self::get_node_secret
420
437
fn ecdh ( & self , recipient : Recipient , other_key : & PublicKey , tweak : Option < & Scalar > ) -> Result < SharedSecret , ( ) > ;
421
438
/// Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
@@ -455,6 +472,8 @@ pub trait KeysInterface {
455
472
/// The hrp is ascii bytes, while the invoice data is base32.
456
473
///
457
474
/// The secret key used to sign the invoice is dependent on the [`Recipient`].
475
+ ///
476
+ /// Errors if the `Recipient` variant is not supported by the implementation.
458
477
fn sign_invoice ( & self , hrp_bytes : & [ u8 ] , invoice_data : & [ u5 ] , receipient : Recipient ) -> Result < RecoverableSignature , ( ) > ;
459
478
460
479
/// Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
@@ -871,6 +890,7 @@ impl ReadableArgs<SecretKey> for InMemorySigner {
871
890
pub struct KeysManager {
872
891
secp_ctx : Secp256k1 < secp256k1:: All > ,
873
892
node_secret : SecretKey ,
893
+ node_id : PublicKey ,
874
894
inbound_payment_key : KeyMaterial ,
875
895
destination_script : Script ,
876
896
shutdown_pubkey : PublicKey ,
@@ -912,6 +932,7 @@ impl KeysManager {
912
932
match ExtendedPrivKey :: new_master ( Network :: Testnet , seed) {
913
933
Ok ( master_key) => {
914
934
let node_secret = master_key. ckd_priv ( & secp_ctx, ChildNumber :: from_hardened_idx ( 0 ) . unwrap ( ) ) . expect ( "Your RNG is busted" ) . private_key ;
935
+ let node_id = PublicKey :: from_secret_key ( & secp_ctx, & node_secret) ;
915
936
let destination_script = match master_key. ckd_priv ( & secp_ctx, ChildNumber :: from_hardened_idx ( 1 ) . unwrap ( ) ) {
916
937
Ok ( destination_key) => {
917
938
let wpubkey_hash = WPubkeyHash :: hash ( & ExtendedPubKey :: from_priv ( & secp_ctx, & destination_key) . to_pub ( ) . to_bytes ( ) ) ;
@@ -939,6 +960,7 @@ impl KeysManager {
939
960
let mut res = KeysManager {
940
961
secp_ctx,
941
962
node_secret,
963
+ node_id,
942
964
inbound_payment_key : KeyMaterial ( inbound_pmt_key_bytes) ,
943
965
944
966
destination_script,
@@ -1158,6 +1180,13 @@ impl KeysInterface for KeysManager {
1158
1180
}
1159
1181
}
1160
1182
1183
+ fn get_node_id ( & self , recipient : Recipient ) -> Result < PublicKey , ( ) > {
1184
+ match recipient {
1185
+ Recipient :: Node => Ok ( self . node_id . clone ( ) ) ,
1186
+ Recipient :: PhantomNode => Err ( ( ) )
1187
+ }
1188
+ }
1189
+
1161
1190
fn ecdh ( & self , recipient : Recipient , other_key : & PublicKey , tweak : Option < & Scalar > ) -> Result < SharedSecret , ( ) > {
1162
1191
let mut node_secret = self . get_node_secret ( recipient) ?;
1163
1192
if let Some ( tweak) = tweak {
@@ -1238,6 +1267,7 @@ pub struct PhantomKeysManager {
1238
1267
inner : KeysManager ,
1239
1268
inbound_payment_key : KeyMaterial ,
1240
1269
phantom_secret : SecretKey ,
1270
+ phantom_node_id : PublicKey ,
1241
1271
}
1242
1272
1243
1273
impl KeysInterface for PhantomKeysManager {
@@ -1250,6 +1280,13 @@ impl KeysInterface for PhantomKeysManager {
1250
1280
}
1251
1281
}
1252
1282
1283
+ fn get_node_id ( & self , recipient : Recipient ) -> Result < PublicKey , ( ) > {
1284
+ match recipient {
1285
+ Recipient :: Node => self . inner . get_node_id ( Recipient :: Node ) ,
1286
+ Recipient :: PhantomNode => Ok ( self . phantom_node_id . clone ( ) ) ,
1287
+ }
1288
+ }
1289
+
1253
1290
fn ecdh ( & self , recipient : Recipient , other_key : & PublicKey , tweak : Option < & Scalar > ) -> Result < SharedSecret , ( ) > {
1254
1291
let mut node_secret = self . get_node_secret ( recipient) ?;
1255
1292
if let Some ( tweak) = tweak {
@@ -1303,10 +1340,13 @@ impl PhantomKeysManager {
1303
1340
pub fn new ( seed : & [ u8 ; 32 ] , starting_time_secs : u64 , starting_time_nanos : u32 , cross_node_seed : & [ u8 ; 32 ] ) -> Self {
1304
1341
let inner = KeysManager :: new ( seed, starting_time_secs, starting_time_nanos) ;
1305
1342
let ( inbound_key, phantom_key) = hkdf_extract_expand_twice ( b"LDK Inbound and Phantom Payment Key Expansion" , cross_node_seed) ;
1343
+ let phantom_secret = SecretKey :: from_slice ( & phantom_key) . unwrap ( ) ;
1344
+ let phantom_node_id = PublicKey :: from_secret_key ( & inner. secp_ctx , & phantom_secret) ;
1306
1345
Self {
1307
1346
inner,
1308
1347
inbound_payment_key : KeyMaterial ( inbound_key) ,
1309
- phantom_secret : SecretKey :: from_slice ( & phantom_key) . unwrap ( ) ,
1348
+ phantom_secret,
1349
+ phantom_node_id,
1310
1350
}
1311
1351
}
1312
1352
0 commit comments