Skip to content

Commit 6c2bf86

Browse files
committed
fixup: Add method to derive Peer Storage encryption key
1 parent 00e7e65 commit 6c2bf86

File tree

9 files changed

+41
-22
lines changed

9 files changed

+41
-22
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use lightning::blinded_path::message::{BlindedMessagePath, MessageContext};
3737
use lightning::blinded_path::payment::{BlindedPaymentPath, ReceiveTlvs};
3838
use lightning::chain;
3939
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
40+
use lightning::chain::chainmonitor::PeerStorageKey;
4041
use lightning::chain::channelmonitor::{ChannelMonitor, MonitorEvent};
4142
use lightning::chain::transaction::OutPoint;
4243
use lightning::chain::{
@@ -338,8 +339,8 @@ impl NodeSigner for KeyProvider {
338339
unreachable!()
339340
}
340341

341-
fn get_peer_storage_key(&self) -> [u8; 32] {
342-
SecretKey::from_slice(&[42; 32]).unwrap().secret_bytes()
342+
fn get_peer_storage_key(&self) -> PeerStorageKey {
343+
PeerStorageKey::new(&[42; 32])
343344
}
344345

345346
fn sign_bolt12_invoice(

fuzz/src/full_stack.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use lightning::blinded_path::message::{BlindedMessagePath, MessageContext};
3434
use lightning::blinded_path::payment::{BlindedPaymentPath, ReceiveTlvs};
3535
use lightning::chain;
3636
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
37-
use lightning::chain::chainmonitor;
37+
use lightning::chain::chainmonitor::{self, PeerStorageKey};
3838
use lightning::chain::transaction::OutPoint;
3939
use lightning::chain::{BestBlock, ChannelMonitorUpdateStatus, Confirm, Listen};
4040
use lightning::events::Event;
@@ -422,8 +422,8 @@ impl NodeSigner for KeyProvider {
422422
Ok(secp_ctx.sign_ecdsa(&msg_hash, &self.node_secret))
423423
}
424424

425-
fn get_peer_storage_key(&self) -> [u8; 32] {
426-
SecretKey::from_slice(&[42; 32]).unwrap().secret_bytes()
425+
fn get_peer_storage_key(&self) -> PeerStorageKey {
426+
PeerStorageKey::new(&[42; 32])
427427
}
428428
}
429429

fuzz/src/onion_message.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use lightning::blinded_path::message::{
99
AsyncPaymentsContext, BlindedMessagePath, MessageContext, OffersContext,
1010
};
1111
use lightning::blinded_path::EmptyNodeIdLookUp;
12+
use lightning::chain::chainmonitor::PeerStorageKey;
1213
use lightning::ln::inbound_payment::ExpandedKey;
1314
use lightning::ln::msgs::{self, BaseMessageHandler, DecodeError, OnionMessageHandler};
1415
use lightning::ln::peer_handler::IgnoringMessageHandler;
@@ -250,7 +251,7 @@ impl NodeSigner for KeyProvider {
250251
unreachable!()
251252
}
252253

253-
fn get_peer_storage_key(&self) -> [u8; 32] {
254+
fn get_peer_storage_key(&self) -> PeerStorageKey {
254255
unreachable!()
255256
}
256257
}

lightning/src/chain/chainmonitor.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,21 @@ impl<ChannelSigner: EcdsaChannelSigner> Deref for LockedChannelMonitor<'_, Chann
218218
}
219219

220220
/// Represents Secret Key used for encrypting Peer Storage.
221-
type PeerStorageKey = [u8; 32];
221+
#[derive(Clone, PartialEq, Eq)]
222+
pub struct PeerStorageKey ([u8; 32]);
223+
224+
impl PeerStorageKey {
225+
/// Creates a new `PeerStorageKey` from a `[u8; 32]` array.
226+
pub fn new(key: [u8; 32]) -> Self {
227+
PeerStorageKey(key)
228+
}
229+
230+
/// Returns a reference to the inner `[u8; 32]` array.
231+
pub fn as_bytes(&self) -> &[u8; 32] {
232+
&self.0
233+
}
234+
}
235+
222236

223237
/// An implementation of [`chain::Watch`] for monitoring channels.
224238
///

lightning/src/ln/blinded_payment_tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::blinded_path::utils::is_padded;
1818
use crate::events::{Event, HTLCDestination, PaymentFailureReason};
1919
use crate::ln::types::ChannelId;
2020
use crate::types::payment::{PaymentHash, PaymentSecret};
21+
use crate::chain::chainmonitor::PeerStorageKey;
2122
use crate::ln::channelmanager;
2223
use crate::ln::channelmanager::{HTLCFailureMsg, PaymentId, RecipientOnionFields};
2324
use crate::types::features::{BlindedHopFeatures, ChannelFeatures, NodeFeatures};
@@ -1609,7 +1610,7 @@ fn route_blinding_spec_test_vector() {
16091610
fn sign_invoice(
16101611
&self, _invoice: &RawBolt11Invoice, _recipient: Recipient,
16111612
) -> Result<RecoverableSignature, ()> { unreachable!() }
1612-
fn get_peer_storage_key(&self) -> [u8; 32] { unreachable!() }
1613+
fn get_peer_storage_key(&self) -> PeerStorageKey { unreachable!() }
16131614
fn sign_bolt12_invoice(
16141615
&self, _invoice: &UnsignedBolt12Invoice,
16151616
) -> Result<schnorr::Signature, ()> { unreachable!() }
@@ -1919,7 +1920,7 @@ fn test_trampoline_inbound_payment_decoding() {
19191920
fn sign_invoice(
19201921
&self, _invoice: &RawBolt11Invoice, _recipient: Recipient,
19211922
) -> Result<RecoverableSignature, ()> { unreachable!() }
1922-
fn get_peer_storage_key(&self) -> [u8; 32] { unreachable!() }
1923+
fn get_peer_storage_key(&self) -> PeerStorageKey { unreachable!() }
19231924
fn sign_bolt12_invoice(
19241925
&self, _invoice: &UnsignedBolt12Invoice,
19251926
) -> Result<schnorr::Signature, ()> { unreachable!() }

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16362,7 +16362,7 @@ mod tests {
1636216362
#[cfg(ldk_bench)]
1636316363
pub mod bench {
1636416364
use crate::chain::Listen;
16365-
use crate::chain::chainmonitor::{ChainMonitor, Persist};
16365+
use crate::chain::chainmonitor::{ChainMonitor, PeerStorageKey, Persist};
1636616366
use crate::sign::{KeysManager, InMemorySigner, NodeSigner};
1636716367
use crate::events::Event;
1636816368
use crate::ln::channelmanager::{BestBlock, ChainParameters, ChannelManager, PaymentHash, PaymentPreimage, PaymentId, RecipientOnionFields, Retry};

lightning/src/sign/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use bitcoin::{secp256k1, Psbt, Sequence, Txid, WPubkeyHash, Witness};
3737

3838
use lightning_invoice::RawBolt11Invoice;
3939

40+
use crate::chain::chainmonitor::PeerStorageKey;
4041
use crate::chain::transaction::OutPoint;
4142
use crate::crypto::utils::{hkdf_extract_expand_twice, sign, sign_with_aux_rand};
4243
use crate::ln::chan_utils;
@@ -839,7 +840,7 @@ pub trait NodeSigner {
839840
///
840841
/// Thus, if you wish to rely on recovery using this method, you should use a key which
841842
/// can be re-derived from data which would be available after state loss (eg the wallet seed)
842-
fn get_peer_storage_key(&self) -> [u8; 32];
843+
fn get_peer_storage_key(&self) -> PeerStorageKey;
843844

844845
/// Get node id based on the provided [`Recipient`].
845846
///
@@ -1780,7 +1781,7 @@ pub struct KeysManager {
17801781
shutdown_pubkey: PublicKey,
17811782
channel_master_key: Xpriv,
17821783
channel_child_index: AtomicUsize,
1783-
peer_storage_key: SecretKey,
1784+
peer_storage_key: PeerStorageKey,
17841785

17851786
#[cfg(test)]
17861787
pub(crate) entropy_source: RandomBytes,
@@ -1890,7 +1891,7 @@ impl KeysManager {
18901891
node_id,
18911892
inbound_payment_key: ExpandedKey::new(inbound_pmt_key_bytes),
18921893

1893-
peer_storage_key,
1894+
peer_storage_key: PeerStorageKey::new(peer_storage_key.secret_bytes()),
18941895

18951896
destination_script,
18961897
shutdown_pubkey,
@@ -2117,8 +2118,8 @@ impl NodeSigner for KeysManager {
21172118
self.inbound_payment_key.clone()
21182119
}
21192120

2120-
fn get_peer_storage_key(&self) -> [u8; 32] {
2121-
self.peer_storage_key.secret_bytes()
2121+
fn get_peer_storage_key(&self) -> PeerStorageKey {
2122+
self.peer_storage_key.clone()
21222123
}
21232124

21242125
fn sign_invoice(
@@ -2282,8 +2283,8 @@ impl NodeSigner for PhantomKeysManager {
22822283
self.inbound_payment_key.clone()
22832284
}
22842285

2285-
fn get_peer_storage_key(&self) -> [u8; 32] {
2286-
self.inner.peer_storage_key.secret_bytes()
2286+
fn get_peer_storage_key(&self) -> PeerStorageKey {
2287+
self.inner.peer_storage_key.clone()
22872288
}
22882289

22892290
fn sign_invoice(

lightning/src/util/dyn_signer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::prelude::*;
44

55
use core::any::Any;
66

7+
use crate::chain::chainmonitor::PeerStorageKey;
78
use crate::ln::chan_utils::{
89
ChannelPublicKeys, ChannelTransactionParameters, ClosingTransaction, CommitmentTransaction,
910
HTLCOutputInCommitment, HolderCommitmentTransaction,
@@ -215,7 +216,7 @@ inner,
215216
invoice: &crate::offers::invoice::UnsignedBolt12Invoice
216217
) -> Result<secp256k1::schnorr::Signature, ()>,
217218
fn get_inbound_payment_key(,) -> ExpandedKey,
218-
fn get_peer_storage_key(,) -> [u8; 32]
219+
fn get_peer_storage_key(,) -> PeerStorageKey
219220
);
220221

221222
delegate!(DynKeysInterface, SignerProvider,
@@ -284,7 +285,7 @@ delegate!(DynPhantomKeysInterface, NodeSigner,
284285
fn sign_bolt12_invoice(, invoice: &crate::offers::invoice::UnsignedBolt12Invoice
285286
) -> Result<secp256k1::schnorr::Signature, ()>,
286287
fn get_inbound_payment_key(,) -> ExpandedKey,
287-
fn get_peer_storage_key(,) -> [u8; 32]
288+
fn get_peer_storage_key(,) -> PeerStorageKey
288289
);
289290

290291
impl SignerProvider for DynPhantomKeysInterface {

lightning/src/util/test_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::chain::chaininterface;
1515
use crate::chain::chaininterface::ConfirmationTarget;
1616
#[cfg(any(test, feature = "_externalize_tests"))]
1717
use crate::chain::chaininterface::FEERATE_FLOOR_SATS_PER_KW;
18-
use crate::chain::chainmonitor::{ChainMonitor, Persist};
18+
use crate::chain::chainmonitor::{ChainMonitor, PeerStorageKey, Persist};
1919
use crate::chain::channelmonitor::{
2020
ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, MonitorEvent,
2121
};
@@ -1452,7 +1452,7 @@ impl NodeSigner for TestNodeSigner {
14521452
unreachable!()
14531453
}
14541454

1455-
fn get_peer_storage_key(&self) -> [u8; 32] {
1455+
fn get_peer_storage_key(&self) -> PeerStorageKey {
14561456
unreachable!()
14571457
}
14581458

@@ -1534,7 +1534,7 @@ impl NodeSigner for TestKeysInterface {
15341534
self.backing.sign_invoice(invoice, recipient)
15351535
}
15361536

1537-
fn get_peer_storage_key(&self) -> [u8; 32] {
1537+
fn get_peer_storage_key(&self) -> PeerStorageKey {
15381538
self.backing.get_peer_storage_key()
15391539
}
15401540

0 commit comments

Comments
 (0)