Skip to content

Commit 2114c80

Browse files
committed
Introduce ProtocolEncrypt
1 parent ca163c3 commit 2114c80

File tree

3 files changed

+71
-17
lines changed

3 files changed

+71
-17
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -392,16 +392,33 @@ pub enum Recipient {
392392
PhantomNode,
393393
}
394394

395+
/// A transport and onion encryptor / decryptor that uses the node key
396+
pub trait ProtocolEncrypt {
397+
/// Get node secret key (aka node_id or network_key) based on the provided [`Recipient`].
398+
///
399+
/// This method must return the same value each time it is called with a given `Recipient`
400+
/// parameter.
401+
fn get_node_secret(&self, recipient: Recipient) -> Result<SecretKey, ()>;
402+
}
403+
395404
/// A trait to describe an object which can get user secrets and key material.
396405
pub trait KeysInterface {
397406
/// A type which implements Sign which will be returned by get_channel_signer.
398407
type Signer : Sign;
408+
/// A type which implements ProtoconCnrypt which will be returned by get_protocol_keys.
409+
type ProtocolEncryptor: ProtocolEncrypt;
410+
411+
/// A protocol encryptor / decryptor that uses the node secret key
412+
fn get_protocol_keys(&self) -> Self::ProtocolEncryptor;
399413

400414
/// Get node secret key (aka node_id or network_key) based on the provided [`Recipient`].
401415
///
402416
/// This method must return the same value each time it is called with a given `Recipient`
403417
/// parameter.
404-
fn get_node_secret(&self, recipient: Recipient) -> Result<SecretKey, ()>;
418+
fn get_node_secret(&self, recipient: Recipient) -> Result<SecretKey, ()> {
419+
self.get_protocol_keys().get_node_secret(recipient)
420+
}
421+
405422
/// Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
406423
///
407424
/// This method should return a different value each time it is called, to avoid linking
@@ -828,6 +845,21 @@ impl ReadableArgs<SecretKey> for InMemorySigner {
828845
}
829846
}
830847

848+
/// A protocol encryptor that keeps the key in memory
849+
pub struct InMemoryProtocolEncryptor {
850+
/// Private key of our node secret, used for signing channel announcements
851+
pub node_secret: SecretKey,
852+
}
853+
854+
impl ProtocolEncrypt for InMemoryProtocolEncryptor {
855+
fn get_node_secret(&self, recipient: Recipient) -> Result<SecretKey, ()> {
856+
match recipient {
857+
Recipient::Node => Ok(self.node_secret.clone()),
858+
Recipient::PhantomNode => Err(())
859+
}
860+
}
861+
}
862+
831863
/// Simple KeysInterface implementor that takes a 32-byte seed for use as a BIP 32 extended key
832864
/// and derives keys from that.
833865
///
@@ -1122,12 +1154,10 @@ impl KeysManager {
11221154

11231155
impl KeysInterface for KeysManager {
11241156
type Signer = InMemorySigner;
1157+
type ProtocolEncryptor = InMemoryProtocolEncryptor;
11251158

1126-
fn get_node_secret(&self, recipient: Recipient) -> Result<SecretKey, ()> {
1127-
match recipient {
1128-
Recipient::Node => Ok(self.node_secret.clone()),
1129-
Recipient::PhantomNode => Err(())
1130-
}
1159+
fn get_protocol_keys(&self) -> Self::ProtocolEncryptor {
1160+
InMemoryProtocolEncryptor { node_secret: self.node_secret }
11311161
}
11321162

11331163
fn get_inbound_payment_key_material(&self) -> KeyMaterial {
@@ -1170,7 +1200,7 @@ impl KeysInterface for KeysManager {
11701200
fn sign_invoice(&self, hrp_bytes: &[u8], invoice_data: &[u5], recipient: Recipient) -> Result<RecoverableSignature, ()> {
11711201
let preimage = construct_invoice_preimage(&hrp_bytes, &invoice_data);
11721202
let secret = match recipient {
1173-
Recipient::Node => self.get_node_secret(Recipient::Node)?,
1203+
Recipient::Node => self.get_protocol_keys().get_node_secret(Recipient::Node)?,
11741204
Recipient::PhantomNode => return Err(()),
11751205
};
11761206
Ok(self.secp_ctx.sign_recoverable(&hash_to_message!(&Sha256::hash(&preimage)), &secret))
@@ -1204,13 +1234,32 @@ pub struct PhantomKeysManager {
12041234
phantom_secret: SecretKey,
12051235
}
12061236

1207-
impl KeysInterface for PhantomKeysManager {
1208-
type Signer = InMemorySigner;
1237+
/// A protocol encryptor that keeps the key in memory, with phantom node support
1238+
pub struct PhantomProtocolEncryptor {
1239+
/// Private key of our node secret
1240+
pub node_secret: SecretKey,
1241+
/// Private key of our phantom node secret
1242+
pub phantom_secret: SecretKey,
1243+
}
12091244

1245+
impl ProtocolEncrypt for PhantomProtocolEncryptor {
12101246
fn get_node_secret(&self, recipient: Recipient) -> Result<SecretKey, ()> {
12111247
match recipient {
1212-
Recipient::Node => self.inner.get_node_secret(Recipient::Node),
1213-
Recipient::PhantomNode => Ok(self.phantom_secret.clone()),
1248+
Recipient::Node => Ok(self.node_secret.clone()),
1249+
Recipient::PhantomNode => Ok(self.phantom_secret.clone())
1250+
}
1251+
}
1252+
}
1253+
1254+
1255+
impl KeysInterface for PhantomKeysManager {
1256+
type Signer = InMemorySigner;
1257+
type ProtocolEncryptor = PhantomProtocolEncryptor;
1258+
1259+
fn get_protocol_keys(&self) -> Self::ProtocolEncryptor {
1260+
PhantomProtocolEncryptor {
1261+
node_secret: self.inner.node_secret,
1262+
phantom_secret: self.phantom_secret,
12141263
}
12151264
}
12161265

lightning/src/ln/channel.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6271,7 +6271,7 @@ mod tests {
62716271
use ln::chan_utils::{ChannelPublicKeys, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters, htlc_success_tx_weight, htlc_timeout_tx_weight};
62726272
use chain::BestBlock;
62736273
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
6274-
use chain::keysinterface::{InMemorySigner, Recipient, KeyMaterial, KeysInterface, BaseSign};
6274+
use chain::keysinterface::{InMemorySigner, Recipient, KeyMaterial, KeysInterface, BaseSign, InMemoryProtocolEncryptor};
62756275
use chain::transaction::OutPoint;
62766276
use util::config::UserConfig;
62776277
use util::enforcing_trait_impls::EnforcingSigner;
@@ -6318,8 +6318,9 @@ mod tests {
63186318
}
63196319
impl KeysInterface for Keys {
63206320
type Signer = InMemorySigner;
6321+
type ProtocolEncryptor = InMemoryProtocolEncryptor;
63216322

6322-
fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> { panic!(); }
6323+
fn get_protocol_keys(&self) -> Self::ProtocolEncryptor { panic!(); }
63236324
fn get_inbound_payment_key_material(&self) -> KeyMaterial { panic!(); }
63246325
fn get_destination_script(&self) -> Script {
63256326
let secp_ctx = Secp256k1::signing_only();

lightning/src/util/test_utils.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use sync::{Mutex, Arc};
4747
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
4848
use core::{cmp, mem};
4949
use bitcoin::bech32::u5;
50-
use chain::keysinterface::{InMemorySigner, Recipient, KeyMaterial};
50+
use chain::keysinterface::{InMemorySigner, Recipient, KeyMaterial, InMemoryProtocolEncryptor, PhantomProtocolEncryptor};
5151

5252
pub struct TestVecWriter(pub Vec<u8>);
5353
impl Writer for TestVecWriter {
@@ -70,7 +70,9 @@ pub struct OnlyReadsKeysInterface {}
7070
impl keysinterface::KeysInterface for OnlyReadsKeysInterface {
7171
type Signer = EnforcingSigner;
7272

73-
fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> { unreachable!(); }
73+
type ProtocolEncryptor = InMemoryProtocolEncryptor;
74+
75+
fn get_protocol_keys(&self) -> Self::ProtocolEncryptor { unreachable!(); }
7476
fn get_inbound_payment_key_material(&self) -> KeyMaterial { unreachable!(); }
7577
fn get_destination_script(&self) -> Script { unreachable!(); }
7678
fn get_shutdown_scriptpubkey(&self) -> ShutdownScript { unreachable!(); }
@@ -485,10 +487,12 @@ pub struct TestKeysInterface {
485487

486488
impl keysinterface::KeysInterface for TestKeysInterface {
487489
type Signer = EnforcingSigner;
490+
type ProtocolEncryptor = PhantomProtocolEncryptor;
488491

489-
fn get_node_secret(&self, recipient: Recipient) -> Result<SecretKey, ()> {
490-
self.backing.get_node_secret(recipient)
492+
fn get_protocol_keys(&self) -> Self::ProtocolEncryptor {
493+
self.backing.get_protocol_keys()
491494
}
495+
492496
fn get_inbound_payment_key_material(&self) -> keysinterface::KeyMaterial {
493497
self.backing.get_inbound_payment_key_material()
494498
}

0 commit comments

Comments
 (0)