Skip to content

Commit 625915f

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 ededeb5 commit 625915f

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
@@ -2125,6 +2125,9 @@ where
21252125

21262126
inbound_payment_key: inbound_payment::ExpandedKey,
21272127

2128+
/// The key used to encrypt our peer storage that would be sent to our peers.
2129+
our_peerstorage_encryption_key: [u8;32],
2130+
21282131
/// LDK puts the [fake scids] that it generates into namespaces, to identify the type of an
21292132
/// incoming payment. To make it harder for a third-party to identify the type of a payment,
21302133
/// we encrypt the namespace identifier using these bytes.
@@ -2974,6 +2977,7 @@ where
29742977
secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
29752978
let inbound_pmt_key_material = node_signer.get_inbound_payment_key_material();
29762979
let expanded_inbound_key = inbound_payment::ExpandedKey::new(&inbound_pmt_key_material);
2980+
let our_peerstorage_encryption_key = node_signer.get_peer_storage_key();
29772981
ChannelManager {
29782982
default_configuration: config.clone(),
29792983
chain_hash: ChainHash::using_genesis_block(params.network),
@@ -2998,6 +3002,8 @@ where
29983002
secp_ctx,
29993003

30003004
inbound_payment_key: expanded_inbound_key,
3005+
our_peerstorage_encryption_key,
3006+
30013007
fake_scid_rand_bytes: entropy_source.get_secure_random_bytes(),
30023008

30033009
probing_cookie_secret: entropy_source.get_secure_random_bytes(),
@@ -3034,6 +3040,11 @@ where
30343040
&self.default_configuration
30353041
}
30363042

3043+
pub fn get_encrypted_our_peer_storage(&self) -> Vec<u8> {
3044+
let our_peer_storage = self.our_peer_storage.read().unwrap();
3045+
our_peer_storage.encrypt_our_peer_storage(self.our_peerstorage_encryption_key)
3046+
}
3047+
30373048
fn create_and_insert_outbound_scid_alias(&self) -> u64 {
30383049
let height = self.best_block.read().unwrap().height;
30393050
let mut outbound_scid_alias = 0;
@@ -8138,6 +8149,30 @@ where
81388149
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))
81398150
}
81408151
};
8152+
8153+
{
8154+
let per_peer_state = self.per_peer_state.read().unwrap();
8155+
let mut peer_state_lock = per_peer_state.get(counterparty_node_id)
8156+
.ok_or_else(|| {
8157+
debug_assert!(false);
8158+
MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
8159+
}).map(|mtx| mtx.lock().unwrap())?;
8160+
let peer_state = &mut *peer_state_lock;
8161+
let our_peer_storage = self.get_encrypted_our_peer_storage();
8162+
8163+
for context in peer_state.channel_by_id.iter().map(|(_, phase)| phase.context()) {
8164+
// Update latest PeerStorage for the peer.
8165+
peer_state.pending_msg_events.push(
8166+
events::MessageSendEvent::SendPeerStorageMessage {
8167+
node_id: context.get_counterparty_node_id(),
8168+
msg: msgs::PeerStorageMessage {
8169+
data: our_peer_storage.clone()
8170+
},
8171+
}
8172+
);
8173+
}
8174+
}
8175+
81418176
self.fail_holding_cell_htlcs(htlcs_to_fail, msg.channel_id, counterparty_node_id);
81428177
Ok(())
81438178
}
@@ -10375,6 +10410,7 @@ where
1037510410
if let Some(peer_state_mutex) = per_peer_state.get(counterparty_node_id) {
1037610411
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
1037710412
let peer_state = &mut *peer_state_lock;
10413+
let num_channels = peer_state.total_channel_count();
1037810414
let pending_msg_events = &mut peer_state.pending_msg_events;
1037910415

1038010416
if let Some(peer_storage) = self.peer_storage.lock().unwrap().get(counterparty_node_id) {
@@ -10386,6 +10422,15 @@ where
1038610422
});
1038710423
}
1038810424

10425+
if peer_state.latest_features.supports_provide_peer_storage() && num_channels > 0 {
10426+
let our_peer_storage = self.get_encrypted_our_peer_storage();
10427+
pending_msg_events.push(events::MessageSendEvent::SendPeerStorageMessage {
10428+
node_id: counterparty_node_id.clone(),
10429+
msg: msgs::PeerStorageMessage {
10430+
data: our_peer_storage
10431+
},
10432+
});
10433+
}
1038910434

1039010435
for (_, phase) in peer_state.channel_by_id.iter_mut() {
1039110436
match phase {
@@ -12334,6 +12379,7 @@ where
1233412379

1233512380
let inbound_pmt_key_material = args.node_signer.get_inbound_payment_key_material();
1233612381
let expanded_inbound_key = inbound_payment::ExpandedKey::new(&inbound_pmt_key_material);
12382+
let our_peerstorage_encryption_key = args.node_signer.get_peer_storage_key();
1233712383

1233812384
let mut claimable_payments = hash_map_with_capacity(claimable_htlcs_list.len());
1233912385
if let Some(purposes) = claimable_htlc_purposes {
@@ -12556,6 +12602,7 @@ where
1255612602
best_block: RwLock::new(BestBlock::new(best_block_hash, best_block_height)),
1255712603

1255812604
inbound_payment_key: expanded_inbound_key,
12605+
our_peerstorage_encryption_key,
1255912606
pending_inbound_payments: Mutex::new(pending_inbound_payments),
1256012607
pending_outbound_payments: pending_outbounds,
1256112608
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
@@ -835,6 +835,8 @@ pub trait NodeSigner {
835835
/// [phantom node payments]: PhantomKeysManager
836836
fn get_inbound_payment_key_material(&self) -> KeyMaterial;
837837

838+
fn get_peer_storage_key(&self) -> [u8;32];
839+
838840
/// Get node id based on the provided [`Recipient`].
839841
///
840842
/// This method must return the same value each time it is called with a given [`Recipient`]
@@ -2173,6 +2175,11 @@ impl NodeSigner for KeysManager {
21732175
self.inbound_payment_key.clone()
21742176
}
21752177

2178+
fn get_peer_storage_key(&self) -> [u8;32] {
2179+
let (t1, _) = hkdf_extract_expand_twice(b"Peer Storage Encryption Key", &self.get_node_secret_key().secret_bytes());
2180+
t1
2181+
}
2182+
21762183
fn sign_invoice(
21772184
&self, hrp_bytes: &[u8], invoice_data: &[u5], recipient: Recipient,
21782185
) -> Result<RecoverableSignature, ()> {
@@ -2351,6 +2358,11 @@ impl NodeSigner for PhantomKeysManager {
23512358
self.inbound_payment_key.clone()
23522359
}
23532360

2361+
fn get_peer_storage_key(&self) -> [u8;32] {
2362+
let (t1, _) = hkdf_extract_expand_twice(b"Peer Storage Encryption Key", &self.get_node_secret_key().secret_bytes());
2363+
t1
2364+
}
2365+
23542366
fn sign_invoice(
23552367
&self, hrp_bytes: &[u8], invoice_data: &[u5], recipient: Recipient,
23562368
) -> Result<RecoverableSignature, ()> {

0 commit comments

Comments
 (0)