Skip to content

Commit 5e14cae

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 f49f2e5 commit 5e14cae

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;
@@ -7862,6 +7873,30 @@ where
78627873
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))
78637874
}
78647875
};
7876+
7877+
{
7878+
let per_peer_state = self.per_peer_state.read().unwrap();
7879+
let mut peer_state_lock = per_peer_state.get(counterparty_node_id)
7880+
.ok_or_else(|| {
7881+
debug_assert!(false);
7882+
MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
7883+
}).map(|mtx| mtx.lock().unwrap())?;
7884+
let peer_state = &mut *peer_state_lock;
7885+
let our_peer_storage = self.get_encrypted_our_peer_storage();
7886+
7887+
for context in peer_state.channel_by_id.iter().map(|(_, phase)| phase.context()) {
7888+
// Update latest PeerStorage for the peer.
7889+
peer_state.pending_msg_events.push(
7890+
events::MessageSendEvent::SendPeerStorageMessage {
7891+
node_id: context.get_counterparty_node_id(),
7892+
msg: msgs::PeerStorageMessage {
7893+
data: our_peer_storage.clone()
7894+
},
7895+
}
7896+
);
7897+
}
7898+
}
7899+
78657900
self.fail_holding_cell_htlcs(htlcs_to_fail, msg.channel_id, counterparty_node_id);
78667901
Ok(())
78677902
}
@@ -10059,6 +10094,7 @@ where
1005910094
if let Some(peer_state_mutex) = per_peer_state.get(counterparty_node_id) {
1006010095
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
1006110096
let peer_state = &mut *peer_state_lock;
10097+
let num_channels = peer_state.total_channel_count();
1006210098
let pending_msg_events = &mut peer_state.pending_msg_events;
1006310099
let peer_storage = self.peer_storage.lock().unwrap().get(counterparty_node_id).unwrap_or(&Vec::<u8>::new()).clone();
1006410100

@@ -10071,6 +10107,15 @@ where
1007110107
});
1007210108
}
1007310109

10110+
if peer_state.latest_features.supports_provide_peer_storage() && num_channels > 0 {
10111+
let our_peer_storage = self.get_encrypted_our_peer_storage();
10112+
pending_msg_events.push(events::MessageSendEvent::SendPeerStorageMessage {
10113+
node_id: counterparty_node_id.clone(),
10114+
msg: msgs::PeerStorageMessage {
10115+
data: our_peer_storage
10116+
},
10117+
});
10118+
}
1007410119

1007510120
for (_, phase) in peer_state.channel_by_id.iter_mut() {
1007610121
match phase {
@@ -11997,6 +12042,7 @@ where
1199712042

1199812043
let inbound_pmt_key_material = args.node_signer.get_inbound_payment_key_material();
1199912044
let expanded_inbound_key = inbound_payment::ExpandedKey::new(&inbound_pmt_key_material);
12045+
let our_peerstorage_encryption_key = args.node_signer.get_peer_storage_key();
1200012046

1200112047
let mut claimable_payments = hash_map_with_capacity(claimable_htlcs_list.len());
1200212048
if let Some(purposes) = claimable_htlc_purposes {
@@ -12214,6 +12260,7 @@ where
1221412260
best_block: RwLock::new(BestBlock::new(best_block_hash, best_block_height)),
1221512261

1221612262
inbound_payment_key: expanded_inbound_key,
12263+
our_peerstorage_encryption_key,
1221712264
pending_inbound_payments: Mutex::new(pending_inbound_payments),
1221812265
pending_outbound_payments: pending_outbounds,
1221912266
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)