Skip to content

Commit 2386a9f

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 2386a9f

File tree

6 files changed

+48
-2
lines changed

6 files changed

+48
-2
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ impl KeysInterface for KeyProvider {
167167
Ok(SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, self.node_id]).unwrap())
168168
}
169169

170+
fn get_node_id(&self, _recipient: Recipient) -> Result<PublicKey, ()> {
171+
let secp_ctx = Secp256k1::signing_only();
172+
Ok(PublicKey::from_secret_key(&secp_ctx, &self.get_node_secret(Recipient::Node).unwrap()))
173+
}
174+
170175
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
171176
let mut node_secret = self.get_node_secret(recipient)?;
172177
if let Some(tweak) = tweak {

fuzz/src/full_stack.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ impl KeysInterface for KeyProvider {
272272
Ok(self.node_secret.clone())
273273
}
274274

275+
fn get_node_id(&self, _recipient: Recipient) -> Result<PublicKey, ()> {
276+
let secp_ctx = Secp256k1::signing_only();
277+
Ok(PublicKey::from_secret_key(&secp_ctx, &self.node_secret))
278+
}
279+
275280
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
276281
let mut node_secret = self.get_node_secret(recipient)?;
277282
if let Some(tweak) = tweak {

fuzz/src/onion_message.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Imports that need to be added manually
22
use bitcoin::bech32::u5;
33
use bitcoin::blockdata::script::Script;
4-
use bitcoin::secp256k1::{PublicKey, Scalar, SecretKey};
4+
use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, SecretKey};
55
use bitcoin::secp256k1::ecdh::SharedSecret;
66
use bitcoin::secp256k1::ecdsa::RecoverableSignature;
77

@@ -67,6 +67,11 @@ impl KeysInterface for KeyProvider {
6767
Ok(self.node_secret.clone())
6868
}
6969

70+
fn get_node_id(&self, _recipient: Recipient) -> Result<PublicKey, ()> {
71+
let secp_ctx = Secp256k1::signing_only();
72+
Ok(PublicKey::from_secret_key(&secp_ctx, &self.get_node_secret(Recipient::Node).unwrap()))
73+
}
74+
7075
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
7176
let mut node_secret = self.get_node_secret(recipient)?;
7277
if let Some(tweak) = tweak {

lightning/src/chain/keysinterface.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,11 @@ 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`].
409+
///
410+
/// This method must return the same value each time it is called with a given `Recipient`
411+
/// parameter.
412+
fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()>;
408413
/// Gets the ECDH shared secret of our [`node secret`] and `other_key`, multiplying by `tweak` if
409414
/// one is provided. Note that this tweak can be applied to `other_key` instead of our node
410415
/// secret, though this is less efficient.
@@ -853,6 +858,7 @@ impl ReadableArgs<SecretKey> for InMemorySigner {
853858
pub struct KeysManager {
854859
secp_ctx: Secp256k1<secp256k1::All>,
855860
node_secret: SecretKey,
861+
node_id: PublicKey,
856862
inbound_payment_key: KeyMaterial,
857863
destination_script: Script,
858864
shutdown_pubkey: PublicKey,
@@ -894,6 +900,7 @@ impl KeysManager {
894900
match ExtendedPrivKey::new_master(Network::Testnet, seed) {
895901
Ok(master_key) => {
896902
let node_secret = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(0).unwrap()).expect("Your RNG is busted").private_key;
903+
let node_id = PublicKey::from_secret_key(&secp_ctx, &node_secret);
897904
let destination_script = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(1).unwrap()) {
898905
Ok(destination_key) => {
899906
let wpubkey_hash = WPubkeyHash::hash(&ExtendedPubKey::from_priv(&secp_ctx, &destination_key).to_pub().to_bytes());
@@ -921,6 +928,7 @@ impl KeysManager {
921928
let mut res = KeysManager {
922929
secp_ctx,
923930
node_secret,
931+
node_id,
924932
inbound_payment_key: KeyMaterial(inbound_pmt_key_bytes),
925933

926934
destination_script,
@@ -1140,6 +1148,13 @@ impl KeysInterface for KeysManager {
11401148
}
11411149
}
11421150

1151+
fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
1152+
match recipient {
1153+
Recipient::Node => Ok(self.node_id.clone()),
1154+
Recipient::PhantomNode => Err(())
1155+
}
1156+
}
1157+
11431158
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
11441159
let mut node_secret = self.get_node_secret(recipient)?;
11451160
if let Some(tweak) = tweak {
@@ -1220,6 +1235,7 @@ pub struct PhantomKeysManager {
12201235
inner: KeysManager,
12211236
inbound_payment_key: KeyMaterial,
12221237
phantom_secret: SecretKey,
1238+
phantom_node_id: PublicKey,
12231239
}
12241240

12251241
impl KeysInterface for PhantomKeysManager {
@@ -1232,6 +1248,13 @@ impl KeysInterface for PhantomKeysManager {
12321248
}
12331249
}
12341250

1251+
fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
1252+
match recipient {
1253+
Recipient::Node => self.inner.get_node_id(Recipient::Node),
1254+
Recipient::PhantomNode => Ok(self.phantom_node_id.clone()),
1255+
}
1256+
}
1257+
12351258
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
12361259
let mut node_secret = self.get_node_secret(recipient)?;
12371260
if let Some(tweak) = tweak {
@@ -1285,10 +1308,13 @@ impl PhantomKeysManager {
12851308
pub fn new(seed: &[u8; 32], starting_time_secs: u64, starting_time_nanos: u32, cross_node_seed: &[u8; 32]) -> Self {
12861309
let inner = KeysManager::new(seed, starting_time_secs, starting_time_nanos);
12871310
let (inbound_key, phantom_key) = hkdf_extract_expand_twice(b"LDK Inbound and Phantom Payment Key Expansion", cross_node_seed);
1311+
let phantom_secret = SecretKey::from_slice(&phantom_key).unwrap();
1312+
let phantom_node_id = PublicKey::from_secret_key(&inner.secp_ctx, &phantom_secret);
12881313
Self {
12891314
inner,
12901315
inbound_payment_key: KeyMaterial(inbound_key),
1291-
phantom_secret: SecretKey::from_slice(&phantom_key).unwrap(),
1316+
phantom_secret,
1317+
phantom_node_id,
12921318
}
12931319
}
12941320

lightning/src/ln/channel.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6710,6 +6710,7 @@ mod tests {
67106710
type Signer = InMemorySigner;
67116711

67126712
fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> { panic!(); }
6713+
fn get_node_id(&self, _recipient: Recipient) -> Result<PublicKey, ()> { panic!(); }
67136714
fn ecdh(&self, _recipient: Recipient, _other_key: &PublicKey, _tweak: Option<&Scalar>) -> Result<SharedSecret, ()> { panic!(); }
67146715
fn get_inbound_payment_key_material(&self) -> KeyMaterial { panic!(); }
67156716
fn get_destination_script(&self) -> Script {

lightning/src/util/test_utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ impl keysinterface::KeysInterface for OnlyReadsKeysInterface {
7676
type Signer = EnforcingSigner;
7777

7878
fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> { unreachable!(); }
79+
fn get_node_id(&self, _recipient: Recipient) -> Result<PublicKey, ()> { unreachable!(); }
7980
fn ecdh(&self, _recipient: Recipient, _other_key: &PublicKey, _tweak: Option<&Scalar>) -> Result<SharedSecret, ()> { unreachable!(); }
8081
fn get_inbound_payment_key_material(&self) -> KeyMaterial { unreachable!(); }
8182
fn get_destination_script(&self) -> Script { unreachable!(); }
@@ -608,6 +609,9 @@ impl keysinterface::KeysInterface for TestKeysInterface {
608609
fn get_node_secret(&self, recipient: Recipient) -> Result<SecretKey, ()> {
609610
self.backing.get_node_secret(recipient)
610611
}
612+
fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
613+
self.backing.get_node_id(recipient)
614+
}
611615
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
612616
self.backing.ecdh(recipient, other_key, tweak)
613617
}

0 commit comments

Comments
 (0)