Skip to content

Commit 1782ec7

Browse files
committed
WIP: revocation enforcement in signer
1 parent ebcddd9 commit 1782ec7

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ pub trait ChannelKeys : Send+Clone {
202202
/// Gets the commitment seed for a specific commitment number, thereby revoking the commitment
203203
///
204204
/// After this is called, it is an error to try to sign the relevant local commitment transaction.
205-
/// May be called more than once (e.g. if re-establishing the channel).
205+
/// Must be called in monotonic order, without gaps. May be called more than once (e.g. if
206+
/// re-establishing the channel).
206207
/// Note that the commitment number starts at (1 << 48) - 1 and counts backwards.
207208
fn get_revoke_commitment_secret(&self, idx: u64) -> [u8; 32];
208209
/// Gets the local channel public keys and basepoints

lightning/src/ln/functional_tests.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,14 +1608,20 @@ fn test_fee_spike_violation_fails_htlc() {
16081608
let local_chan = chan_lock.by_id.get(&chan.2).unwrap();
16091609
let chan_keys = local_chan.get_local_keys();
16101610
let pubkeys = chan_keys.pubkeys();
1611+
let local_secret = chan_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER);
1612+
// Must revoke without gaps
1613+
chan_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER - 1);
1614+
let local_secret2 = chan_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER - 2);
16111615
(pubkeys.revocation_basepoint, pubkeys.htlc_basepoint, pubkeys.payment_point,
1612-
chan_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER), chan_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER - 2))
1616+
local_secret, local_secret2)
16131617
};
16141618
let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_payment_point, remote_secret1) = {
16151619
let chan_lock = nodes[1].node.channel_state.lock().unwrap();
16161620
let remote_chan = chan_lock.by_id.get(&chan.2).unwrap();
16171621
let chan_keys = remote_chan.get_local_keys();
16181622
let pubkeys = chan_keys.pubkeys();
1623+
// Must revoke without gaps
1624+
chan_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER);
16191625
(pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint, pubkeys.payment_point,
16201626
chan_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER - 1))
16211627
};
@@ -8128,9 +8134,11 @@ fn test_counterparty_raa_skip_no_crash() {
81288134
let mut guard = nodes[0].node.channel_state.lock().unwrap();
81298135
let local_keys = &guard.by_id.get_mut(&channel_id).unwrap().local_keys;
81308136
const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
8137+
let per_commitment_secret = local_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER);
8138+
// Must revoke without gaps
8139+
local_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER - 1);
81318140
let next_per_commitment_point = PublicKey::from_secret_key(&Secp256k1::new(),
81328141
&SecretKey::from_slice(&local_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER - 2)).unwrap());
8133-
let per_commitment_secret = local_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER);
81348142

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

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ use util::ser::{Writeable, Writer, Readable};
1515
use std::io::Error;
1616
use ln::msgs::DecodeError;
1717

18+
const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
19+
1820
/// Enforces some rules on ChannelKeys calls. Eventually we will probably want to expose a variant
1921
/// of this which would essentially be what you'd want to run on a hardware wallet.
2022
#[derive(Clone)]
2123
pub struct EnforcingChannelKeys {
2224
pub inner: InMemoryChannelKeys,
25+
revoked_commitment: Arc<Mutex<u64>>,
2326
commitment_number_obscure_and_last: Arc<Mutex<(Option<u64>, u64)>>,
2427
}
2528

@@ -28,6 +31,7 @@ impl EnforcingChannelKeys {
2831
Self {
2932
inner,
3033
commitment_number_obscure_and_last: Arc::new(Mutex::new((None, 0))),
34+
revoked_commitment: Arc::new(Mutex::new(INITIAL_COMMITMENT_NUMBER + 1)),
3135
}
3236
}
3337
}
@@ -52,7 +56,14 @@ impl ChannelKeys for EnforcingChannelKeys {
5256
self.inner.get_per_commitment_point(idx, secp_ctx)
5357
}
5458

55-
fn get_revoke_commitment_secret(&self, idx: u64) -> [u8; 32] { self.inner.get_revoke_commitment_secret(idx) }
59+
fn get_revoke_commitment_secret(&self, idx: u64) -> [u8; 32] {
60+
let mut revoked = self.revoked_commitment.lock().unwrap();
61+
if idx != *revoked && idx != *revoked - 1 {
62+
panic!("can only revoke the current or next unrevoked commitment - trying {}, revoked {}", idx, *revoked)
63+
}
64+
*revoked = idx;
65+
self.inner.get_revoke_commitment_secret(idx)
66+
}
5667
fn pubkeys(&self) -> &ChannelPublicKeys { self.inner.pubkeys() }
5768
fn key_derivation_params(&self) -> (u64, u64) { self.inner.key_derivation_params() }
5869

@@ -124,6 +135,8 @@ impl ChannelKeys for EnforcingChannelKeys {
124135
impl Writeable for EnforcingChannelKeys {
125136
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
126137
self.inner.write(writer)?;
138+
let revoked = *self.revoked_commitment.lock().unwrap();
139+
revoked.write(writer)?;
127140
let (obscure, last) = *self.commitment_number_obscure_and_last.lock().unwrap();
128141
obscure.write(writer)?;
129142
last.write(writer)?;
@@ -134,9 +147,11 @@ impl Writeable for EnforcingChannelKeys {
134147
impl Readable for EnforcingChannelKeys {
135148
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
136149
let inner = Readable::read(reader)?;
150+
let revoked = Readable::read(reader)?;
137151
let obscure_and_last = Readable::read(reader)?;
138152
Ok(EnforcingChannelKeys {
139153
inner: inner,
154+
revoked_commitment: Arc::new(Mutex::new(revoked)),
140155
commitment_number_obscure_and_last: Arc::new(Mutex::new(obscure_and_last))
141156
})
142157
}

0 commit comments

Comments
 (0)