Skip to content

Commit 3e4d307

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 976e8a2 commit 3e4d307

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
@@ -2099,6 +2099,9 @@ where
20992099

21002100
inbound_payment_key: inbound_payment::ExpandedKey,
21012101

2102+
/// The key used to encrypt our peer storage that would be sent to our peers.
2103+
our_peerstorage_encryption_key: [u8;32],
2104+
21022105
/// LDK puts the [fake scids] that it generates into namespaces, to identify the type of an
21032106
/// incoming payment. To make it harder for a third-party to identify the type of a payment,
21042107
/// we encrypt the namespace identifier using these bytes.
@@ -2937,6 +2940,7 @@ where
29372940
secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
29382941
let inbound_pmt_key_material = node_signer.get_inbound_payment_key_material();
29392942
let expanded_inbound_key = inbound_payment::ExpandedKey::new(&inbound_pmt_key_material);
2943+
let our_peerstorage_encryption_key = node_signer.get_peer_storage_key();
29402944
ChannelManager {
29412945
default_configuration: config.clone(),
29422946
chain_hash: ChainHash::using_genesis_block(params.network),
@@ -2961,6 +2965,8 @@ where
29612965
secp_ctx,
29622966

29632967
inbound_payment_key: expanded_inbound_key,
2968+
our_peerstorage_encryption_key,
2969+
29642970
fake_scid_rand_bytes: entropy_source.get_secure_random_bytes(),
29652971

29662972
probing_cookie_secret: entropy_source.get_secure_random_bytes(),
@@ -2997,6 +3003,11 @@ where
29973003
&self.default_configuration
29983004
}
29993005

3006+
pub fn get_encrypted_our_peer_storage(&self) -> Vec<u8> {
3007+
let our_peer_storage = self.our_peer_storage.read().unwrap();
3008+
our_peer_storage.encrypt_our_peer_storage(self.our_peerstorage_encryption_key)
3009+
}
3010+
30003011
fn create_and_insert_outbound_scid_alias(&self) -> u64 {
30013012
let height = self.best_block.read().unwrap().height;
30023013
let mut outbound_scid_alias = 0;
@@ -8100,6 +8111,30 @@ where
81008111
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))
81018112
}
81028113
};
8114+
8115+
{
8116+
let per_peer_state = self.per_peer_state.read().unwrap();
8117+
let mut peer_state_lock = per_peer_state.get(counterparty_node_id)
8118+
.ok_or_else(|| {
8119+
debug_assert!(false);
8120+
MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
8121+
}).map(|mtx| mtx.lock().unwrap())?;
8122+
let peer_state = &mut *peer_state_lock;
8123+
let our_peer_storage = self.get_encrypted_our_peer_storage();
8124+
8125+
for context in peer_state.channel_by_id.iter().map(|(_, phase)| phase.context()) {
8126+
// Update latest PeerStorage for the peer.
8127+
peer_state.pending_msg_events.push(
8128+
events::MessageSendEvent::SendPeerStorageMessage {
8129+
node_id: context.get_counterparty_node_id(),
8130+
msg: msgs::PeerStorageMessage {
8131+
data: our_peer_storage.clone()
8132+
},
8133+
}
8134+
);
8135+
}
8136+
}
8137+
81038138
self.fail_holding_cell_htlcs(htlcs_to_fail, msg.channel_id, counterparty_node_id);
81048139
Ok(())
81058140
}
@@ -10337,6 +10372,7 @@ where
1033710372
if let Some(peer_state_mutex) = per_peer_state.get(counterparty_node_id) {
1033810373
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
1033910374
let peer_state = &mut *peer_state_lock;
10375+
let num_channels = peer_state.total_channel_count();
1034010376
let pending_msg_events = &mut peer_state.pending_msg_events;
1034110377
let peer_storage = self.peer_storage.lock().unwrap().get(counterparty_node_id).unwrap_or(&Vec::<u8>::new()).clone();
1034210378

@@ -10349,6 +10385,15 @@ where
1034910385
});
1035010386
}
1035110387

10388+
if peer_state.latest_features.supports_provide_peer_storage() && num_channels > 0 {
10389+
let our_peer_storage = self.get_encrypted_our_peer_storage();
10390+
pending_msg_events.push(events::MessageSendEvent::SendPeerStorageMessage {
10391+
node_id: counterparty_node_id.clone(),
10392+
msg: msgs::PeerStorageMessage {
10393+
data: our_peer_storage
10394+
},
10395+
});
10396+
}
1035210397

1035310398
for (_, phase) in peer_state.channel_by_id.iter_mut() {
1035410399
match phase {
@@ -12301,6 +12346,7 @@ where
1230112346

1230212347
let inbound_pmt_key_material = args.node_signer.get_inbound_payment_key_material();
1230312348
let expanded_inbound_key = inbound_payment::ExpandedKey::new(&inbound_pmt_key_material);
12349+
let our_peerstorage_encryption_key = args.node_signer.get_peer_storage_key();
1230412350

1230512351
let mut claimable_payments = hash_map_with_capacity(claimable_htlcs_list.len());
1230612352
if let Some(purposes) = claimable_htlc_purposes {
@@ -12523,6 +12569,7 @@ where
1252312569
best_block: RwLock::new(BestBlock::new(best_block_hash, best_block_height)),
1252412570

1252512571
inbound_payment_key: expanded_inbound_key,
12572+
our_peerstorage_encryption_key,
1252612573
pending_inbound_payments: Mutex::new(pending_inbound_payments),
1252712574
pending_outbound_payments: pending_outbounds,
1252812575
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)