Skip to content

Commit ac96689

Browse files
Aditya SharmaAditya Sharma
authored andcommitted
lightning: Add a key inside NodeSigner which would be used to encrypt or decrpt the peerstorage and send PeerStorage on every RAA and upon reconnection.
1 parent 8d6ae08 commit ac96689

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,9 @@ where
20152015

20162016
inbound_payment_key: inbound_payment::ExpandedKey,
20172017

2018+
/// The key used to encrypt our peer storage that would be sent to our peers.
2019+
our_peerstorage_encryption_key: [u8;32],
2020+
20182021
/// LDK puts the [fake scids] that it generates into namespaces, to identify the type of an
20192022
/// incoming payment. To make it harder for a third-party to identify the type of a payment,
20202023
/// we encrypt the namespace identifier using these bytes.
@@ -2853,6 +2856,7 @@ where
28532856
secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
28542857
let inbound_pmt_key_material = node_signer.get_inbound_payment_key_material();
28552858
let expanded_inbound_key = inbound_payment::ExpandedKey::new(&inbound_pmt_key_material);
2859+
let our_peerstorage_encryption_key = node_signer.get_peer_storage_key();
28562860
ChannelManager {
28572861
default_configuration: config.clone(),
28582862
chain_hash: ChainHash::using_genesis_block(params.network),
@@ -2877,6 +2881,8 @@ where
28772881
secp_ctx,
28782882

28792883
inbound_payment_key: expanded_inbound_key,
2884+
our_peerstorage_encryption_key,
2885+
28802886
fake_scid_rand_bytes: entropy_source.get_secure_random_bytes(),
28812887

28822888
probing_cookie_secret: entropy_source.get_secure_random_bytes(),
@@ -2913,6 +2919,11 @@ where
29132919
&self.default_configuration
29142920
}
29152921

2922+
pub fn get_encrypted_our_peer_storage(&self) -> Vec<u8> {
2923+
let our_peer_storage = self.our_peer_storage.read().unwrap();
2924+
our_peer_storage.encrypt_our_peer_storage(self.our_peerstorage_encryption_key)
2925+
}
2926+
29162927
fn create_and_insert_outbound_scid_alias(&self) -> u64 {
29172928
let height = self.best_block.read().unwrap().height;
29182929
let mut outbound_scid_alias = 0;
@@ -7870,6 +7881,30 @@ where
78707881
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.channel_id))
78717882
}
78727883
};
7884+
7885+
{
7886+
let per_peer_state = self.per_peer_state.read().unwrap();
7887+
let mut peer_state_lock = per_peer_state.get(counterparty_node_id)
7888+
.ok_or_else(|| {
7889+
debug_assert!(false);
7890+
MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
7891+
}).map(|mtx| mtx.lock().unwrap())?;
7892+
let peer_state = &mut *peer_state_lock;
7893+
let our_peer_storage = self.get_encrypted_our_peer_storage();
7894+
7895+
for context in peer_state.channel_by_id.iter().map(|(_, phase)| phase.context()) {
7896+
// Update latest PeerStorage for the peer.
7897+
peer_state.pending_msg_events.push(
7898+
events::MessageSendEvent::SendPeerStorageMessage {
7899+
node_id: context.get_counterparty_node_id(),
7900+
msg: msgs::PeerStorageMessage {
7901+
data: our_peer_storage.clone()
7902+
},
7903+
}
7904+
);
7905+
}
7906+
}
7907+
78737908
self.fail_holding_cell_htlcs(htlcs_to_fail, msg.channel_id, counterparty_node_id);
78747909
Ok(())
78757910
}
@@ -10067,6 +10102,7 @@ where
1006710102
if let Some(peer_state_mutex) = per_peer_state.get(counterparty_node_id) {
1006810103
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
1006910104
let peer_state = &mut *peer_state_lock;
10105+
let num_channels = peer_state.total_channel_count();
1007010106
let pending_msg_events = &mut peer_state.pending_msg_events;
1007110107
let peer_storage = self.peer_storage.lock().unwrap().get(counterparty_node_id).unwrap_or(&Vec::<u8>::new()).clone();
1007210108

@@ -10079,6 +10115,15 @@ where
1007910115
});
1008010116
}
1008110117

10118+
if peer_state.latest_features.supports_provide_peer_storage() && num_channels > 0 {
10119+
let our_peer_storage = self.get_encrypted_our_peer_storage();
10120+
pending_msg_events.push(events::MessageSendEvent::SendPeerStorageMessage {
10121+
node_id: counterparty_node_id.clone(),
10122+
msg: msgs::PeerStorageMessage {
10123+
data: our_peer_storage
10124+
},
10125+
});
10126+
}
1008210127

1008310128
for (_, phase) in peer_state.channel_by_id.iter_mut() {
1008410129
match phase {
@@ -12012,6 +12057,7 @@ where
1201212057

1201312058
let inbound_pmt_key_material = args.node_signer.get_inbound_payment_key_material();
1201412059
let expanded_inbound_key = inbound_payment::ExpandedKey::new(&inbound_pmt_key_material);
12060+
let our_peerstorage_encryption_key = args.node_signer.get_peer_storage_key();
1201512061

1201612062
let mut claimable_payments = hash_map_with_capacity(claimable_htlcs_list.len());
1201712063
if let Some(purposes) = claimable_htlc_purposes {
@@ -12229,6 +12275,7 @@ where
1222912275
best_block: RwLock::new(BestBlock::new(best_block_hash, best_block_height)),
1223012276

1223112277
inbound_payment_key: expanded_inbound_key,
12278+
our_peerstorage_encryption_key,
1223212279
pending_inbound_payments: Mutex::new(pending_inbound_payments),
1223312280
pending_outbound_payments: pending_outbounds,
1223412281
pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs.unwrap()),

lightning/src/sign/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,8 @@ pub trait NodeSigner {
824824
/// [phantom node payments]: PhantomKeysManager
825825
fn get_inbound_payment_key_material(&self) -> KeyMaterial;
826826

827+
fn get_peer_storage_key(&self) -> [u8;32];
828+
827829
/// Get node id based on the provided [`Recipient`].
828830
///
829831
/// This method must return the same value each time it is called with a given [`Recipient`]
@@ -2162,6 +2164,11 @@ impl NodeSigner for KeysManager {
21622164
self.inbound_payment_key.clone()
21632165
}
21642166

2167+
fn get_peer_storage_key(&self) -> [u8;32] {
2168+
let (t1, _) = hkdf_extract_expand_twice(b"Peer Storage Encryption Key", &self.get_node_secret_key().secret_bytes());
2169+
t1
2170+
}
2171+
21652172
fn sign_invoice(
21662173
&self, hrp_bytes: &[u8], invoice_data: &[u5], recipient: Recipient,
21672174
) -> Result<RecoverableSignature, ()> {
@@ -2340,6 +2347,11 @@ impl NodeSigner for PhantomKeysManager {
23402347
self.inbound_payment_key.clone()
23412348
}
23422349

2350+
fn get_peer_storage_key(&self) -> [u8;32] {
2351+
let (t1, _) = hkdf_extract_expand_twice(b"Peer Storage Encryption Key", &self.get_node_secret_key().secret_bytes());
2352+
t1
2353+
}
2354+
23432355
fn sign_invoice(
23442356
&self, hrp_bytes: &[u8], invoice_data: &[u5], recipient: Recipient,
23452357
) -> Result<RecoverableSignature, ()> {

0 commit comments

Comments
 (0)