Skip to content

Commit e7ae3de

Browse files
committed
ChannelKeys provides individual commitment secrets
1 parent cd13364 commit e7ae3de

File tree

5 files changed

+28
-23
lines changed

5 files changed

+28
-23
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ impl Readable for SpendableOutputDescriptor {
195195
// TODO: We should remove Clone by instead requesting a new ChannelKeys copy when we create
196196
// ChannelMonitors instead of expecting to clone the one out of the Channel into the monitors.
197197
pub trait ChannelKeys : Send+Clone {
198-
/// Gets the commitment seed
199-
fn commitment_seed(&self) -> &[u8; 32];
198+
/// Gets the commitment seed for a specific commitment number
199+
fn commitment_secret(&self, idx: u64) -> [u8; 32];
200200
/// Gets the local channel public keys and basepoints
201201
fn pubkeys(&self) -> &ChannelPublicKeys;
202202
/// Gets arbitrary identifiers describing the set of keys which are provided back to you in
@@ -404,7 +404,10 @@ impl InMemoryChannelKeys {
404404
}
405405

406406
impl ChannelKeys for InMemoryChannelKeys {
407-
fn commitment_seed(&self) -> &[u8; 32] { &self.commitment_seed }
407+
fn commitment_secret(&self, idx: u64) -> [u8; 32] {
408+
chan_utils::build_commitment_secret(&self.commitment_seed, idx)
409+
}
410+
408411
fn pubkeys(&self) -> &ChannelPublicKeys { &self.local_channel_pubkeys }
409412
fn key_derivation_params(&self) -> (u64, u64) { self.key_derivation_params }
410413

lightning/src/ln/chan_utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ impl HTLCType {
5252
// Various functions for key derivation and transaction creation for use within channels. Primarily
5353
// used in Channel and ChannelMonitor.
5454

55-
pub(super) fn build_commitment_secret(commitment_seed: &[u8; 32], idx: u64) -> [u8; 32] {
55+
/// Build the commitment secret from the seed and the commitment number
56+
pub fn build_commitment_secret(commitment_seed: &[u8; 32], idx: u64) -> [u8; 32] {
5657
let mut res: [u8; 32] = commitment_seed.clone();
5758
for i in 0..48 {
5859
let bitpos = 47 - i;

lightning/src/ln/channel.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
783783
// Utilities to derive keys:
784784

785785
fn build_local_commitment_secret(&self, idx: u64) -> SecretKey {
786-
let res = chan_utils::build_commitment_secret(self.local_keys.commitment_seed(), idx);
786+
let res = self.local_keys.commitment_secret(idx);
787787
SecretKey::from_slice(&res).unwrap()
788788
}
789789

@@ -2021,7 +2021,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
20212021
}
20222022

20232023
let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number - 1));
2024-
let per_commitment_secret = chan_utils::build_commitment_secret(self.local_keys.commitment_seed(), self.cur_local_commitment_transaction_number + 1);
2024+
let per_commitment_secret = self.local_keys.commitment_secret(self.cur_local_commitment_transaction_number + 1);
20252025

20262026
// Update state now that we've passed all the can-fail calls...
20272027
let mut need_our_commitment = false;
@@ -2660,7 +2660,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
26602660

26612661
fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
26622662
let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number));
2663-
let per_commitment_secret = chan_utils::build_commitment_secret(self.local_keys.commitment_seed(), self.cur_local_commitment_transaction_number + 2);
2663+
let per_commitment_secret = self.local_keys.commitment_secret(self.cur_local_commitment_transaction_number + 2);
26642664
msgs::RevokeAndACK {
26652665
channel_id: self.channel_id,
26662666
per_commitment_secret,
@@ -2743,7 +2743,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
27432743
if msg.next_remote_commitment_number > 0 {
27442744
match msg.data_loss_protect {
27452745
OptionalField::Present(ref data_loss) => {
2746-
if chan_utils::build_commitment_secret(self.local_keys.commitment_seed(), INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1) != data_loss.your_last_per_commitment_secret {
2746+
if self.local_keys.commitment_secret(INITIAL_COMMITMENT_NUMBER - msg.next_remote_commitment_number + 1) != data_loss.your_last_per_commitment_secret {
27472747
return Err(ChannelError::Close("Peer sent a garbage channel_reestablish with secret key not matching the commitment height provided"));
27482748
}
27492749
if msg.next_remote_commitment_number > INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number {

lightning/src/ln/functional_tests.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,29 +1599,29 @@ fn test_fee_spike_violation_fails_htlc() {
15991599

16001600
let feerate_per_kw = get_feerate!(nodes[0], chan.2);
16011601

1602+
const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
1603+
16021604
// Get the EnforcingChannelKeys for each channel, which will be used to (1) get the keys
16031605
// needed to sign the new commitment tx and (2) sign the new commitment tx.
1604-
let (local_revocation_basepoint, local_htlc_basepoint, local_payment_point, local_chan_commitment_seed) = {
1606+
let (local_revocation_basepoint, local_htlc_basepoint, local_payment_point, local_secret, local_secret2) = {
16051607
let chan_lock = nodes[0].node.channel_state.lock().unwrap();
16061608
let local_chan = chan_lock.by_id.get(&chan.2).unwrap();
16071609
let chan_keys = local_chan.get_local_keys();
16081610
let pubkeys = chan_keys.pubkeys();
1609-
(pubkeys.revocation_basepoint, pubkeys.htlc_basepoint, pubkeys.payment_point, *chan_keys.commitment_seed())
1611+
(pubkeys.revocation_basepoint, pubkeys.htlc_basepoint, pubkeys.payment_point,
1612+
chan_keys.commitment_secret(INITIAL_COMMITMENT_NUMBER), chan_keys.commitment_secret(INITIAL_COMMITMENT_NUMBER - 2))
16101613
};
1611-
let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_payment_point, remote_chan_commitment_seed) = {
1614+
let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_payment_point, remote_secret1) = {
16121615
let chan_lock = nodes[1].node.channel_state.lock().unwrap();
16131616
let remote_chan = chan_lock.by_id.get(&chan.2).unwrap();
16141617
let chan_keys = remote_chan.get_local_keys();
16151618
let pubkeys = chan_keys.pubkeys();
1616-
(pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint, pubkeys.payment_point, *chan_keys.commitment_seed())
1619+
(pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint, pubkeys.payment_point,
1620+
chan_keys.commitment_secret(INITIAL_COMMITMENT_NUMBER - 1))
16171621
};
16181622

16191623
// Assemble the set of keys we can use for signatures for our commitment_signed message.
1620-
const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
1621-
let commitment_secret = {
1622-
let res = chan_utils::build_commitment_secret(&remote_chan_commitment_seed, INITIAL_COMMITMENT_NUMBER - 1);
1623-
SecretKey::from_slice(&res).unwrap()
1624-
};
1624+
let commitment_secret = SecretKey::from_slice(&remote_secret1).unwrap();
16251625
let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &commitment_secret);
16261626
let commit_tx_keys = chan_utils::TxCreationKeys::new(&secp_ctx, &per_commitment_point, &remote_delayed_payment_basepoint,
16271627
&remote_htlc_basepoint, &local_revocation_basepoint, &local_htlc_basepoint).unwrap();
@@ -1706,8 +1706,8 @@ fn test_fee_spike_violation_fails_htlc() {
17061706
let _ = nodes[1].node.get_and_clear_pending_msg_events();
17071707

17081708
// Send the RAA to nodes[1].
1709-
let per_commitment_secret = chan_utils::build_commitment_secret(&local_chan_commitment_seed, INITIAL_COMMITMENT_NUMBER);
1710-
let next_secret = SecretKey::from_slice(&chan_utils::build_commitment_secret(&local_chan_commitment_seed, INITIAL_COMMITMENT_NUMBER - 2)).unwrap();
1709+
let per_commitment_secret = local_secret;
1710+
let next_secret = SecretKey::from_slice(&local_secret2).unwrap();
17111711
let next_per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &next_secret);
17121712
let raa_msg = msgs::RevokeAndACK{ channel_id: chan.2, per_commitment_secret, next_per_commitment_point};
17131713
nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa_msg);
@@ -8125,11 +8125,12 @@ fn test_counterparty_raa_skip_no_crash() {
81258125
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
81268126
let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
81278127

8128-
let commitment_seed = nodes[0].node.channel_state.lock().unwrap().by_id.get_mut(&channel_id).unwrap().local_keys.commitment_seed().clone();
8128+
let mut guard = nodes[0].node.channel_state.lock().unwrap();
8129+
let local_keys = &guard.by_id.get_mut(&channel_id).unwrap().local_keys;
81298130
const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
81308131
let next_per_commitment_point = PublicKey::from_secret_key(&Secp256k1::new(),
8131-
&SecretKey::from_slice(&chan_utils::build_commitment_secret(&commitment_seed, INITIAL_COMMITMENT_NUMBER - 2)).unwrap());
8132-
let per_commitment_secret = chan_utils::build_commitment_secret(&commitment_seed, INITIAL_COMMITMENT_NUMBER);
8132+
&SecretKey::from_slice(&local_keys.commitment_secret(INITIAL_COMMITMENT_NUMBER - 2)).unwrap());
8133+
let per_commitment_secret = local_keys.commitment_secret(INITIAL_COMMITMENT_NUMBER);
81338134

81348135
nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(),
81358136
&msgs::RevokeAndACK { channel_id, per_commitment_secret, next_per_commitment_point });

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl EnforcingChannelKeys {
4848
}
4949

5050
impl ChannelKeys for EnforcingChannelKeys {
51-
fn commitment_seed(&self) -> &[u8; 32] { self.inner.commitment_seed() }
51+
fn commitment_secret(&self, idx: u64) -> [u8; 32] { self.inner.commitment_secret(idx) }
5252
fn pubkeys(&self) -> &ChannelPublicKeys { self.inner.pubkeys() }
5353
fn key_derivation_params(&self) -> (u64, u64) { self.inner.key_derivation_params() }
5454

0 commit comments

Comments
 (0)