Skip to content

Commit 71a8051

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 e61f3a2 commit 71a8051

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,17 @@ pub trait KeysInterface {
405405
/// This method must return the same value each time it is called with a given `Recipient`
406406
/// parameter.
407407
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+
}
408419
/// Gets the ECDH shared secret of our [`node secret`] and `other_key`, multiplying by `tweak` if
409420
/// one is provided. Note that this tweak can be applied to `other_key` instead of our node
410421
/// secret, though this is less efficient.
@@ -853,6 +864,7 @@ impl ReadableArgs<SecretKey> for InMemorySigner {
853864
pub struct KeysManager {
854865
secp_ctx: Secp256k1<secp256k1::All>,
855866
node_secret: SecretKey,
867+
node_id: PublicKey,
856868
inbound_payment_key: KeyMaterial,
857869
destination_script: Script,
858870
shutdown_pubkey: PublicKey,
@@ -894,6 +906,7 @@ impl KeysManager {
894906
match ExtendedPrivKey::new_master(Network::Testnet, seed) {
895907
Ok(master_key) => {
896908
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);
897910
let destination_script = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(1).unwrap()) {
898911
Ok(destination_key) => {
899912
let wpubkey_hash = WPubkeyHash::hash(&ExtendedPubKey::from_priv(&secp_ctx, &destination_key).to_pub().to_bytes());
@@ -921,6 +934,7 @@ impl KeysManager {
921934
let mut res = KeysManager {
922935
secp_ctx,
923936
node_secret,
937+
node_id,
924938
inbound_payment_key: KeyMaterial(inbound_pmt_key_bytes),
925939

926940
destination_script,
@@ -1140,6 +1154,13 @@ impl KeysInterface for KeysManager {
11401154
}
11411155
}
11421156

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+
11431164
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
11441165
let mut node_secret = self.get_node_secret(recipient)?;
11451166
if let Some(tweak) = tweak {
@@ -1220,6 +1241,7 @@ pub struct PhantomKeysManager {
12201241
inner: KeysManager,
12211242
inbound_payment_key: KeyMaterial,
12221243
phantom_secret: SecretKey,
1244+
phantom_node_id: PublicKey,
12231245
}
12241246

12251247
impl KeysInterface for PhantomKeysManager {
@@ -1232,6 +1254,13 @@ impl KeysInterface for PhantomKeysManager {
12321254
}
12331255
}
12341256

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+
12351264
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
12361265
let mut node_secret = self.get_node_secret(recipient)?;
12371266
if let Some(tweak) = tweak {
@@ -1285,10 +1314,13 @@ impl PhantomKeysManager {
12851314
pub fn new(seed: &[u8; 32], starting_time_secs: u64, starting_time_nanos: u32, cross_node_seed: &[u8; 32]) -> Self {
12861315
let inner = KeysManager::new(seed, starting_time_secs, starting_time_nanos);
12871316
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);
12881319
Self {
12891320
inner,
12901321
inbound_payment_key: KeyMaterial(inbound_key),
1291-
phantom_secret: SecretKey::from_slice(&phantom_key).unwrap(),
1322+
phantom_secret,
1323+
phantom_node_id,
12921324
}
12931325
}
12941326

lightning/src/util/test_utils.rs

Lines changed: 3 additions & 0 deletions
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)