Skip to content

Commit 58b8cc3

Browse files
committed
Add ChannelKeys to ChannelMonitor
1 parent 12e89f6 commit 58b8cc3

11 files changed

+300
-179
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Writer for VecWriter {
7575

7676
static mut IN_RESTORE: bool = false;
7777
pub struct TestChannelMonitor {
78-
pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
78+
pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys>>,
7979
pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
8080
pub latest_good_update: Mutex<HashMap<OutPoint, Vec<u8>>>,
8181
pub latest_update_good: Mutex<HashMap<OutPoint, bool>>,
@@ -94,8 +94,8 @@ impl TestChannelMonitor {
9494
}
9595
}
9696
}
97-
impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
98-
fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
97+
impl channelmonitor::ManyChannelMonitor<EnforcingChannelKeys> for TestChannelMonitor {
98+
fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor<EnforcingChannelKeys>) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
9999
let ret = self.update_ret.lock().unwrap().clone();
100100
if let Ok(()) = ret {
101101
let mut ser = VecWriter(Vec::new());
@@ -151,16 +151,17 @@ impl KeysInterface for KeyProvider {
151151
}
152152

153153
fn get_channel_keys(&self, _inbound: bool, channel_value_satoshis: u64) -> EnforcingChannelKeys {
154-
EnforcingChannelKeys::new(InMemoryChannelKeys {
155-
funding_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, self.node_id]).unwrap(),
156-
revocation_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, self.node_id]).unwrap(),
157-
payment_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, self.node_id]).unwrap(),
158-
delayed_payment_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, self.node_id]).unwrap(),
159-
htlc_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, self.node_id]).unwrap(),
160-
commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, self.node_id],
161-
remote_channel_pubkeys: None,
154+
let secp_ctx = Secp256k1::signing_only();
155+
EnforcingChannelKeys::new(InMemoryChannelKeys::new(
156+
&secp_ctx,
157+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, self.node_id]).unwrap(),
158+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, self.node_id]).unwrap(),
159+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, self.node_id]).unwrap(),
160+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, self.node_id]).unwrap(),
161+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, self.node_id]).unwrap(),
162+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, self.node_id],
162163
channel_value_satoshis,
163-
})
164+
))
164165
}
165166

166167
fn get_onion_rand(&self) -> (SecretKey, [u8; 32]) {
@@ -191,7 +192,7 @@ pub fn do_test(data: &[u8]) {
191192
config.channel_options.fee_proportional_millionths = 0;
192193
config.channel_options.announced_channel = true;
193194
config.peer_channel_config_limits.min_dust_limit_satoshis = 0;
194-
(Arc::new(ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone() as Arc<channelmonitor::ManyChannelMonitor>, broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0).unwrap()),
195+
(Arc::new(ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone() as Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>, broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0).unwrap()),
195196
monitor)
196197
} }
197198
}
@@ -211,7 +212,7 @@ pub fn do_test(data: &[u8]) {
211212
let mut monitors = HashMap::new();
212213
let mut old_monitors = $old_monitors.latest_good_update.lock().unwrap();
213214
for (outpoint, monitor_ser) in old_monitors.drain() {
214-
monitors.insert(outpoint, <(Sha256d, ChannelMonitor)>::read(&mut Cursor::new(&monitor_ser), Arc::clone(&logger)).expect("Failed to read monitor").1);
215+
monitors.insert(outpoint, <(Sha256d, ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&monitor_ser), Arc::clone(&logger)).expect("Failed to read monitor").1);
215216
monitor.latest_good_update.lock().unwrap().insert(outpoint, monitor_ser);
216217
}
217218
let mut monitor_refs = HashMap::new();
@@ -222,14 +223,14 @@ pub fn do_test(data: &[u8]) {
222223
let read_args = ChannelManagerReadArgs {
223224
keys_manager,
224225
fee_estimator: fee_est.clone(),
225-
monitor: monitor.clone() as Arc<channelmonitor::ManyChannelMonitor>,
226+
monitor: monitor.clone() as Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>,
226227
tx_broadcaster: broadcast.clone(),
227228
logger,
228229
default_config: config,
229230
channel_monitors: &mut monitor_refs,
230231
};
231232

232-
let res = (<(Sha256d, ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor>>)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor);
233+
let res = (<(Sha256d, ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>>)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor);
233234
for (_, was_good) in $old_monitors.latest_updates_good_at_last_ser.lock().unwrap().iter() {
234235
if !was_good {
235236
// If the last time we updated a monitor we didn't successfully update (and we

fuzz/src/chanmon_deser.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
55

6+
use lightning::util::enforcing_trait_impls::EnforcingChannelKeys;
67
use lightning::ln::channelmonitor;
78
use lightning::util::ser::{ReadableArgs, Writer};
89

@@ -25,10 +26,10 @@ impl Writer for VecWriter {
2526
#[inline]
2627
pub fn do_test(data: &[u8]) {
2728
let logger = Arc::new(test_logger::TestLogger::new("".to_owned()));
28-
if let Ok((latest_block_hash, monitor)) = <(Sha256dHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(data), logger.clone()) {
29+
if let Ok((latest_block_hash, monitor)) = <(Sha256dHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(data), logger.clone()) {
2930
let mut w = VecWriter(Vec::new());
3031
monitor.write_for_disk(&mut w).unwrap();
31-
let deserialized_copy = <(Sha256dHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(&w.0), logger.clone()).unwrap();
32+
let deserialized_copy = <(Sha256dHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&w.0), logger.clone()).unwrap();
3233
assert!(latest_block_hash == deserialized_copy.0);
3334
assert!(monitor == deserialized_copy.1);
3435
w.0.clear();

fuzz/src/full_stack.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ impl<'a> Hash for Peer<'a> {
136136
}
137137

138138
struct MoneyLossDetector<'a> {
139-
manager: Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor>>>,
140-
monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
141-
handler: PeerManager<Peer<'a>, Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor>>>>,
139+
manager: Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>>>,
140+
monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys>>,
141+
handler: PeerManager<Peer<'a>, Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>>>>,
142142

143143
peers: &'a RefCell<[bool; 256]>,
144144
funding_txn: Vec<Transaction>,
@@ -149,7 +149,10 @@ struct MoneyLossDetector<'a> {
149149
blocks_connected: u32,
150150
}
151151
impl<'a> MoneyLossDetector<'a> {
152-
pub fn new(peers: &'a RefCell<[bool; 256]>, manager: Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor>>>, monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>, handler: PeerManager<Peer<'a>, Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor>>>>) -> Self {
152+
pub fn new(peers: &'a RefCell<[bool; 256]>,
153+
manager: Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>>>,
154+
monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys>>,
155+
handler: PeerManager<Peer<'a>, Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>>>>) -> Self {
153156
MoneyLossDetector {
154157
manager,
155158
monitor,
@@ -249,28 +252,29 @@ impl KeysInterface for KeyProvider {
249252

250253
fn get_channel_keys(&self, inbound: bool, channel_value_satoshis: u64) -> EnforcingChannelKeys {
251254
let ctr = self.counter.fetch_add(1, Ordering::Relaxed) as u8;
255+
let secp_ctx = Secp256k1::signing_only();
252256
EnforcingChannelKeys::new(if inbound {
253-
InMemoryChannelKeys {
254-
funding_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ctr]).unwrap(),
255-
revocation_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, ctr]).unwrap(),
256-
payment_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, ctr]).unwrap(),
257-
delayed_payment_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, ctr]).unwrap(),
258-
htlc_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, ctr]).unwrap(),
259-
commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, ctr],
260-
remote_channel_pubkeys: None,
261-
channel_value_satoshis: channel_value_satoshis,
262-
}
257+
InMemoryChannelKeys::new(
258+
&secp_ctx,
259+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ctr]).unwrap(),
260+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, ctr]).unwrap(),
261+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, ctr]).unwrap(),
262+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, ctr]).unwrap(),
263+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, ctr]).unwrap(),
264+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, ctr],
265+
channel_value_satoshis,
266+
)
263267
} else {
264-
InMemoryChannelKeys {
265-
funding_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, ctr]).unwrap(),
266-
revocation_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, ctr]).unwrap(),
267-
payment_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, ctr]).unwrap(),
268-
delayed_payment_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, ctr]).unwrap(),
269-
htlc_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, ctr]).unwrap(),
270-
commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, ctr],
271-
remote_channel_pubkeys: None,
272-
channel_value_satoshis: channel_value_satoshis,
273-
}
268+
InMemoryChannelKeys::new(
269+
&secp_ctx,
270+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, ctr]).unwrap(),
271+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, ctr]).unwrap(),
272+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, ctr]).unwrap(),
273+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, ctr]).unwrap(),
274+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, ctr]).unwrap(),
275+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, ctr],
276+
channel_value_satoshis,
277+
)
274278
})
275279
}
276280

@@ -329,7 +333,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
329333
config.channel_options.fee_proportional_millionths = slice_to_be32(get_slice!(4));
330334
config.channel_options.announced_channel = get_slice!(1)[0] != 0;
331335
config.peer_channel_config_limits.min_dust_limit_satoshis = 0;
332-
let channelmanager = Arc::new(ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone() as Arc<channelmonitor::ManyChannelMonitor>, broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0).unwrap());
336+
let channelmanager = Arc::new(ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone() as Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>, broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0).unwrap());
333337
let router = Arc::new(Router::new(PublicKey::from_secret_key(&Secp256k1::signing_only(), &keys_manager.get_node_secret()), watch.clone(), Arc::clone(&logger)));
334338

335339
let peers = RefCell::new([false; 256]);

lightning/src/chain/keysinterface.rs

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use bitcoin_hashes::sha256d::Hash as Sha256dHash;
1616
use bitcoin_hashes::hash160::Hash as Hash160;
1717

1818
use secp256k1::key::{SecretKey, PublicKey};
19-
use secp256k1::{Secp256k1, Signature};
19+
use secp256k1::{Secp256k1, Signature, Signing};
2020
use secp256k1;
2121

2222
use util::byte_utils;
@@ -133,7 +133,8 @@ pub trait KeysInterface: Send + Sync {
133133
/// (TODO: We shouldn't require that, and should have an API to get them at deser time, due mostly
134134
/// to the possibility of reentrancy issues by calling the user's code during our deserialization
135135
/// routine).
136-
pub trait ChannelKeys : Send {
136+
/// TODO: remove Clone once we start returning ChannelUpdate objects instead of copying ChannelMonitor
137+
pub trait ChannelKeys : Send+Clone {
137138
/// Gets the private key for the anchor tx
138139
fn funding_key<'a>(&'a self) -> &'a SecretKey;
139140
/// Gets the local secret key for blinded revocation pubkey
@@ -147,6 +148,8 @@ pub trait ChannelKeys : Send {
147148
fn htlc_base_key<'a>(&'a self) -> &'a SecretKey;
148149
/// Gets the commitment seed
149150
fn commitment_seed<'a>(&'a self) -> &'a [u8; 32];
151+
/// Gets the local channel public keys and basepoints
152+
fn pubkeys<'a>(&'a self) -> &'a ChannelPublicKeys;
150153

151154
/// Create a signature for a remote commitment transaction and associated HTLC transactions.
152155
///
@@ -182,21 +185,57 @@ pub trait ChannelKeys : Send {
182185
/// A simple implementation of ChannelKeys that just keeps the private keys in memory.
183186
pub struct InMemoryChannelKeys {
184187
/// Private key of anchor tx
185-
pub funding_key: SecretKey,
188+
funding_key: SecretKey,
186189
/// Local secret key for blinded revocation pubkey
187-
pub revocation_base_key: SecretKey,
190+
revocation_base_key: SecretKey,
188191
/// Local secret key used in commitment tx htlc outputs
189-
pub payment_base_key: SecretKey,
192+
payment_base_key: SecretKey,
190193
/// Local secret key used in HTLC tx
191-
pub delayed_payment_base_key: SecretKey,
194+
delayed_payment_base_key: SecretKey,
192195
/// Local htlc secret key used in commitment tx htlc outputs
193-
pub htlc_base_key: SecretKey,
196+
htlc_base_key: SecretKey,
194197
/// Commitment seed
195-
pub commitment_seed: [u8; 32],
198+
commitment_seed: [u8; 32],
199+
/// Local public keys and basepoints
200+
pub(crate) local_channel_pubkeys: ChannelPublicKeys,
196201
/// Remote public keys and base points
197-
pub remote_channel_pubkeys: Option<ChannelPublicKeys>,
202+
pub(crate) remote_channel_pubkeys: Option<ChannelPublicKeys>,
198203
/// The total value of this channel
199-
pub channel_value_satoshis: u64,
204+
channel_value_satoshis: u64,
205+
}
206+
207+
impl InMemoryChannelKeys {
208+
/// Create a new InMemoryChannelKeys
209+
pub fn new<C: Signing>(
210+
secp_ctx: &Secp256k1<C>,
211+
funding_key: SecretKey,
212+
revocation_base_key: SecretKey,
213+
payment_base_key: SecretKey,
214+
delayed_payment_base_key: SecretKey,
215+
htlc_base_key: SecretKey,
216+
commitment_seed: [u8; 32],
217+
channel_value_satoshis: u64) -> InMemoryChannelKeys {
218+
let from_secret = |s: &SecretKey| PublicKey::from_secret_key(secp_ctx, s);
219+
let local_keys = ChannelPublicKeys {
220+
funding_pubkey: from_secret(&funding_key),
221+
revocation_basepoint: from_secret(&revocation_base_key),
222+
payment_basepoint: from_secret(&payment_base_key),
223+
delayed_payment_basepoint: from_secret(&delayed_payment_base_key),
224+
htlc_basepoint: from_secret(&htlc_base_key),
225+
};
226+
InMemoryChannelKeys {
227+
funding_key,
228+
revocation_base_key,
229+
payment_base_key,
230+
delayed_payment_base_key,
231+
htlc_base_key,
232+
commitment_seed,
233+
channel_value_satoshis,
234+
remote_channel_pubkeys: None,
235+
local_channel_pubkeys: local_keys
236+
}
237+
}
238+
200239
}
201240

202241
impl ChannelKeys for InMemoryChannelKeys {
@@ -206,6 +245,7 @@ impl ChannelKeys for InMemoryChannelKeys {
206245
fn delayed_payment_base_key(&self) -> &SecretKey { &self.delayed_payment_base_key }
207246
fn htlc_base_key(&self) -> &SecretKey { &self.htlc_base_key }
208247
fn commitment_seed(&self) -> &[u8; 32] { &self.commitment_seed }
248+
fn pubkeys<'a>(&'a self) -> &'a ChannelPublicKeys { &self.local_channel_pubkeys }
209249

210250
fn sign_remote_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, feerate_per_kw: u64, commitment_tx: &Transaction, keys: &TxCreationKeys, htlcs: &[&HTLCOutputInCommitment], to_self_delay: u16, secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()> {
211251
if commitment_tx.input.len() != 1 { return Err(()); }
@@ -268,6 +308,7 @@ impl_writeable!(InMemoryChannelKeys, 0, {
268308
delayed_payment_base_key,
269309
htlc_base_key,
270310
commitment_seed,
311+
local_channel_pubkeys,
271312
remote_channel_pubkeys,
272313
channel_value_satoshis
273314
});
@@ -411,16 +452,16 @@ impl KeysInterface for KeysManager {
411452
let delayed_payment_base_key = key_step!(b"delayed payment base key", payment_base_key);
412453
let htlc_base_key = key_step!(b"HTLC base key", delayed_payment_base_key);
413454

414-
InMemoryChannelKeys {
455+
InMemoryChannelKeys::new(
456+
&self.secp_ctx,
415457
funding_key,
416458
revocation_base_key,
417459
payment_base_key,
418460
delayed_payment_base_key,
419461
htlc_base_key,
420462
commitment_seed,
421-
remote_channel_pubkeys: None,
422463
channel_value_satoshis,
423-
}
464+
)
424465
}
425466

426467
fn get_onion_rand(&self) -> (SecretKey, [u8; 32]) {

lightning/src/ln/chan_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ pub struct TxCreationKeys {
155155
}
156156

157157
/// One counterparty's public keys which do not change over the life of a channel.
158-
#[derive(Clone)]
158+
#[derive(Clone, PartialEq)]
159159
pub struct ChannelPublicKeys {
160160
/// The public key which is used to sign all commitment transactions, as it appears in the
161161
/// on-chain channel lock-in 2-of-2 multisig output.

0 commit comments

Comments
 (0)