Skip to content

Commit 17c0fc8

Browse files
Add KeysInterface::get_node_id method
Useful since we're working on getting rid of KeysInterface::get_node_secret to complete support for remote signing. Will be used in upcoming work to check whether an outbound onion message blinded path has our node id as the introduction node id
1 parent ad82c9e commit 17c0fc8

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lightning/src/chain/keysinterface.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,17 @@ pub trait KeysInterface {
412412
/// This method must return the same value each time it is called with a given `Recipient`
413413
/// parameter.
414414
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+
}
415426
/// Gets the ECDH shared secret of our [`node secret`] and `other_key`, multiplying by `tweak` if
416427
/// one is provided. Note that this tweak can be applied to `other_key` instead of our node
417428
/// secret, though this is less efficient.
@@ -871,6 +882,7 @@ impl ReadableArgs<SecretKey> for InMemorySigner {
871882
pub struct KeysManager {
872883
secp_ctx: Secp256k1<secp256k1::All>,
873884
node_secret: SecretKey,
885+
node_id: PublicKey,
874886
inbound_payment_key: KeyMaterial,
875887
destination_script: Script,
876888
shutdown_pubkey: PublicKey,
@@ -912,6 +924,7 @@ impl KeysManager {
912924
match ExtendedPrivKey::new_master(Network::Testnet, seed) {
913925
Ok(master_key) => {
914926
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);
915928
let destination_script = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(1).unwrap()) {
916929
Ok(destination_key) => {
917930
let wpubkey_hash = WPubkeyHash::hash(&ExtendedPubKey::from_priv(&secp_ctx, &destination_key).to_pub().to_bytes());
@@ -939,6 +952,7 @@ impl KeysManager {
939952
let mut res = KeysManager {
940953
secp_ctx,
941954
node_secret,
955+
node_id,
942956
inbound_payment_key: KeyMaterial(inbound_pmt_key_bytes),
943957

944958
destination_script,
@@ -1158,6 +1172,13 @@ impl KeysInterface for KeysManager {
11581172
}
11591173
}
11601174

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+
11611182
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
11621183
let mut node_secret = self.get_node_secret(recipient)?;
11631184
if let Some(tweak) = tweak {
@@ -1238,6 +1259,7 @@ pub struct PhantomKeysManager {
12381259
inner: KeysManager,
12391260
inbound_payment_key: KeyMaterial,
12401261
phantom_secret: SecretKey,
1262+
phantom_node_id: PublicKey,
12411263
}
12421264

12431265
impl KeysInterface for PhantomKeysManager {
@@ -1250,6 +1272,13 @@ impl KeysInterface for PhantomKeysManager {
12501272
}
12511273
}
12521274

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+
12531282
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
12541283
let mut node_secret = self.get_node_secret(recipient)?;
12551284
if let Some(tweak) = tweak {
@@ -1303,10 +1332,13 @@ impl PhantomKeysManager {
13031332
pub fn new(seed: &[u8; 32], starting_time_secs: u64, starting_time_nanos: u32, cross_node_seed: &[u8; 32]) -> Self {
13041333
let inner = KeysManager::new(seed, starting_time_secs, starting_time_nanos);
13051334
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);
13061337
Self {
13071338
inner,
13081339
inbound_payment_key: KeyMaterial(inbound_key),
1309-
phantom_secret: SecretKey::from_slice(&phantom_key).unwrap(),
1340+
phantom_secret,
1341+
phantom_node_id,
13101342
}
13111343
}
13121344

lightning/src/util/test_utils.rs

+3
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,9 @@ impl keysinterface::KeysInterface for TestKeysInterface {
608608
fn get_node_secret(&self, recipient: Recipient) -> Result<SecretKey, ()> {
609609
self.backing.get_node_secret(recipient)
610610
}
611+
fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
612+
self.backing.get_node_id(recipient)
613+
}
611614
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
612615
self.backing.ecdh(recipient, other_key, tweak)
613616
}

0 commit comments

Comments
 (0)