Skip to content

Commit a257906

Browse files
authored
Merge pull request #1779 from valentinewallace/2022-10-node-pk-keysinterface
Add `KeysInterface::get_node_id` method
2 parents fc9a4c2 + 7082d6c commit a257906

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

lightning/src/chain/keysinterface.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,28 @@ pub trait KeysInterface {
411411
///
412412
/// This method must return the same value each time it is called with a given `Recipient`
413413
/// parameter.
414+
///
415+
/// Errors if the `Recipient` variant is not supported by the implementation.
414416
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+
}
415430
/// Gets the ECDH shared secret of our [`node secret`] and `other_key`, multiplying by `tweak` if
416431
/// one is provided. Note that this tweak can be applied to `other_key` instead of our node
417432
/// secret, though this is less efficient.
418433
///
434+
/// Errors if the `Recipient` variant is not supported by the implementation.
435+
///
419436
/// [`node secret`]: Self::get_node_secret
420437
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()>;
421438
/// Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
@@ -455,6 +472,8 @@ pub trait KeysInterface {
455472
/// The hrp is ascii bytes, while the invoice data is base32.
456473
///
457474
/// 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.
458477
fn sign_invoice(&self, hrp_bytes: &[u8], invoice_data: &[u5], receipient: Recipient) -> Result<RecoverableSignature, ()>;
459478

460479
/// Get secret key material as bytes for use in encrypting and decrypting inbound payment data.
@@ -871,6 +890,7 @@ impl ReadableArgs<SecretKey> for InMemorySigner {
871890
pub struct KeysManager {
872891
secp_ctx: Secp256k1<secp256k1::All>,
873892
node_secret: SecretKey,
893+
node_id: PublicKey,
874894
inbound_payment_key: KeyMaterial,
875895
destination_script: Script,
876896
shutdown_pubkey: PublicKey,
@@ -912,6 +932,7 @@ impl KeysManager {
912932
match ExtendedPrivKey::new_master(Network::Testnet, seed) {
913933
Ok(master_key) => {
914934
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);
915936
let destination_script = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(1).unwrap()) {
916937
Ok(destination_key) => {
917938
let wpubkey_hash = WPubkeyHash::hash(&ExtendedPubKey::from_priv(&secp_ctx, &destination_key).to_pub().to_bytes());
@@ -939,6 +960,7 @@ impl KeysManager {
939960
let mut res = KeysManager {
940961
secp_ctx,
941962
node_secret,
963+
node_id,
942964
inbound_payment_key: KeyMaterial(inbound_pmt_key_bytes),
943965

944966
destination_script,
@@ -1158,6 +1180,13 @@ impl KeysInterface for KeysManager {
11581180
}
11591181
}
11601182

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+
11611190
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
11621191
let mut node_secret = self.get_node_secret(recipient)?;
11631192
if let Some(tweak) = tweak {
@@ -1238,6 +1267,7 @@ pub struct PhantomKeysManager {
12381267
inner: KeysManager,
12391268
inbound_payment_key: KeyMaterial,
12401269
phantom_secret: SecretKey,
1270+
phantom_node_id: PublicKey,
12411271
}
12421272

12431273
impl KeysInterface for PhantomKeysManager {
@@ -1250,6 +1280,13 @@ impl KeysInterface for PhantomKeysManager {
12501280
}
12511281
}
12521282

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+
12531290
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
12541291
let mut node_secret = self.get_node_secret(recipient)?;
12551292
if let Some(tweak) = tweak {
@@ -1303,10 +1340,13 @@ impl PhantomKeysManager {
13031340
pub fn new(seed: &[u8; 32], starting_time_secs: u64, starting_time_nanos: u32, cross_node_seed: &[u8; 32]) -> Self {
13041341
let inner = KeysManager::new(seed, starting_time_secs, starting_time_nanos);
13051342
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);
13061345
Self {
13071346
inner,
13081347
inbound_payment_key: KeyMaterial(inbound_key),
1309-
phantom_secret: SecretKey::from_slice(&phantom_key).unwrap(),
1348+
phantom_secret,
1349+
phantom_node_id,
13101350
}
13111351
}
13121352

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)