Skip to content

Commit e90dc58

Browse files
committed
WIP: Split out KeysInterface into EntropySource, NodeSigner, and SignerProvider.
1 parent 5e14c24 commit e90dc58

File tree

6 files changed

+263
-261
lines changed

6 files changed

+263
-261
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use lightning::chain::{BestBlock, ChannelMonitorUpdateStatus, chainmonitor, chan
3636
use lightning::chain::channelmonitor::{ChannelMonitor, MonitorEvent};
3737
use lightning::chain::transaction::OutPoint;
3838
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
39-
use lightning::chain::keysinterface::{KeyMaterial, KeysInterface, InMemorySigner, Recipient};
39+
use lightning::chain::keysinterface::{KeyMaterial, KeysInterface, InMemorySigner, Recipient, EntropySource, NodeSigner, SignerProvider};
4040
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
4141
use lightning::ln::channelmanager::{self, ChainParameters, ChannelManager, PaymentSendFailure, ChannelManagerReadArgs, PaymentId};
4242
use lightning::ln::channel::FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
@@ -160,38 +160,24 @@ struct KeyProvider {
160160
rand_bytes_id: atomic::AtomicU32,
161161
enforcement_states: Mutex<HashMap<[u8;32], Arc<Mutex<EnforcementState>>>>,
162162
}
163-
impl KeysInterface for KeyProvider {
164-
type Signer = EnforcingSigner;
165-
166-
fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> {
167-
Ok(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, self.node_id]).unwrap())
168-
}
169163

170-
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
171-
let mut node_secret = self.get_node_secret(recipient)?;
172-
if let Some(tweak) = tweak {
173-
node_secret = node_secret.mul_tweak(tweak).unwrap();
174-
}
175-
Ok(SharedSecret::new(other_key, &node_secret))
176-
}
177-
178-
fn get_inbound_payment_key_material(&self) -> KeyMaterial {
179-
KeyMaterial([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, self.node_id])
164+
impl EntropySource for KeyProvider {
165+
fn get_secure_random_bytes(&self) -> [u8; 32] {
166+
let id = self.rand_bytes_id.fetch_add(1, atomic::Ordering::Relaxed);
167+
let mut res = [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, self.node_id];
168+
res[30-4..30].copy_from_slice(&id.to_le_bytes());
169+
res
180170
}
171+
}
181172

182-
fn get_destination_script(&self) -> Script {
183-
let secp_ctx = Secp256k1::signing_only();
184-
let channel_monitor_claim_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, self.node_id]).unwrap();
185-
let our_channel_monitor_claim_key_hash = WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
186-
Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script()
173+
impl NodeSigner for KeyProvider {
174+
fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> {
175+
Ok(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, self.node_id]).unwrap())
187176
}
177+
}
188178

189-
fn get_shutdown_scriptpubkey(&self) -> ShutdownScript {
190-
let secp_ctx = Secp256k1::signing_only();
191-
let secret_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, self.node_id]).unwrap();
192-
let pubkey_hash = WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &secret_key).serialize());
193-
ShutdownScript::new_p2wpkh(&pubkey_hash)
194-
}
179+
impl SignerProvider for KeyProvider {
180+
type Signer = EnforcingSigner;
195181

196182
fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] {
197183
let id = self.rand_bytes_id.fetch_add(1, atomic::Ordering::Relaxed) as u8;
@@ -217,13 +203,6 @@ impl KeysInterface for KeyProvider {
217203
EnforcingSigner::new_with_revoked(keys, revoked_commitment, false)
218204
}
219205

220-
fn get_secure_random_bytes(&self) -> [u8; 32] {
221-
let id = self.rand_bytes_id.fetch_add(1, atomic::Ordering::Relaxed);
222-
let mut res = [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, self.node_id];
223-
res[30-4..30].copy_from_slice(&id.to_le_bytes());
224-
res
225-
}
226-
227206
fn read_chan_signer(&self, buffer: &[u8]) -> Result<Self::Signer, DecodeError> {
228207
let mut reader = std::io::Cursor::new(buffer);
229208

@@ -236,9 +215,25 @@ impl KeysInterface for KeyProvider {
236215
disable_revocation_policy_check: false,
237216
})
238217
}
218+
}
219+
220+
impl KeysInterface for KeyProvider {
221+
fn get_inbound_payment_key_material(&self) -> KeyMaterial {
222+
KeyMaterial([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, self.node_id])
223+
}
224+
225+
fn get_destination_script(&self) -> Script {
226+
let secp_ctx = Secp256k1::signing_only();
227+
let channel_monitor_claim_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, self.node_id]).unwrap();
228+
let our_channel_monitor_claim_key_hash = WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
229+
Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script()
230+
}
239231

240-
fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient) -> Result<RecoverableSignature, ()> {
241-
unreachable!()
232+
fn get_shutdown_scriptpubkey(&self) -> ShutdownScript {
233+
let secp_ctx = Secp256k1::signing_only();
234+
let secret_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, self.node_id]).unwrap();
235+
let pubkey_hash = WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &secret_key).serialize());
236+
ShutdownScript::new_p2wpkh(&pubkey_hash)
242237
}
243238
}
244239

fuzz/src/full_stack.rs

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use lightning::chain::{BestBlock, ChannelMonitorUpdateStatus, Confirm, Listen};
3333
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
3434
use lightning::chain::chainmonitor;
3535
use lightning::chain::transaction::OutPoint;
36-
use lightning::chain::keysinterface::{InMemorySigner, Recipient, KeyMaterial, KeysInterface};
36+
use lightning::chain::keysinterface::{InMemorySigner, Recipient, KeyMaterial, KeysInterface, EntropySource, NodeSigner, SignerProvider};
3737
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
3838
use lightning::ln::channelmanager::{ChainParameters, ChannelManager, PaymentId};
3939
use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor,IgnoringMessageHandler};
@@ -265,38 +265,23 @@ struct KeyProvider {
265265
counter: AtomicU64,
266266
signer_state: RefCell<HashMap<u8, (bool, Arc<Mutex<EnforcementState>>)>>
267267
}
268-
impl KeysInterface for KeyProvider {
269-
type Signer = EnforcingSigner;
270-
271-
fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> {
272-
Ok(self.node_secret.clone())
273-
}
274-
275-
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
276-
let mut node_secret = self.get_node_secret(recipient)?;
277-
if let Some(tweak) = tweak {
278-
node_secret = node_secret.mul_tweak(tweak).unwrap();
279-
}
280-
Ok(SharedSecret::new(other_key, &node_secret))
281-
}
282268

283-
fn get_inbound_payment_key_material(&self) -> KeyMaterial {
284-
self.inbound_payment_key.clone()
269+
impl EntropySource for KeyProvider {
270+
fn get_secure_random_bytes(&self) -> [u8; 32] {
271+
let ctr = self.counter.fetch_add(1, Ordering::Relaxed);
272+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
273+
(ctr >> 8*7) as u8, (ctr >> 8*6) as u8, (ctr >> 8*5) as u8, (ctr >> 8*4) as u8, (ctr >> 8*3) as u8, (ctr >> 8*2) as u8, (ctr >> 8*1) as u8, 14, (ctr >> 8*0) as u8]
285274
}
275+
}
286276

287-
fn get_destination_script(&self) -> Script {
288-
let secp_ctx = Secp256k1::signing_only();
289-
let channel_monitor_claim_key = SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap();
290-
let our_channel_monitor_claim_key_hash = WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
291-
Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script()
277+
impl NodeSigner for KeyProvider {
278+
fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> {
279+
Ok(self.node_secret.clone())
292280
}
281+
}
293282

294-
fn get_shutdown_scriptpubkey(&self) -> ShutdownScript {
295-
let secp_ctx = Secp256k1::signing_only();
296-
let secret_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, 0, 1]).unwrap();
297-
let pubkey_hash = WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &secret_key).serialize());
298-
ShutdownScript::new_p2wpkh(&pubkey_hash)
299-
}
283+
impl SignerProvider for KeyProvider {
284+
type Signer = EnforcingSigner;
300285

301286
fn generate_channel_keys_id(&self, inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] {
302287
let ctr = self.counter.fetch_add(1, Ordering::Relaxed) as u8;
@@ -337,12 +322,6 @@ impl KeysInterface for KeyProvider {
337322
}, state, false)
338323
}
339324

340-
fn get_secure_random_bytes(&self) -> [u8; 32] {
341-
let ctr = self.counter.fetch_add(1, Ordering::Relaxed);
342-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
343-
(ctr >> 8*7) as u8, (ctr >> 8*6) as u8, (ctr >> 8*5) as u8, (ctr >> 8*4) as u8, (ctr >> 8*3) as u8, (ctr >> 8*2) as u8, (ctr >> 8*1) as u8, 14, (ctr >> 8*0) as u8]
344-
}
345-
346325
fn read_chan_signer(&self, mut data: &[u8]) -> Result<EnforcingSigner, DecodeError> {
347326
let inner: InMemorySigner = ReadableArgs::read(&mut data, self.node_secret.clone())?;
348327
let state = Arc::new(Mutex::new(EnforcementState::new()));
@@ -353,9 +332,25 @@ impl KeysInterface for KeyProvider {
353332
false
354333
))
355334
}
335+
}
336+
337+
impl KeysInterface for KeyProvider {
338+
fn get_inbound_payment_key_material(&self) -> KeyMaterial {
339+
self.inbound_payment_key.clone()
340+
}
356341

357-
fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient) -> Result<RecoverableSignature, ()> {
358-
unreachable!()
342+
fn get_destination_script(&self) -> Script {
343+
let secp_ctx = Secp256k1::signing_only();
344+
let channel_monitor_claim_key = SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap();
345+
let our_channel_monitor_claim_key_hash = WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
346+
Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script()
347+
}
348+
349+
fn get_shutdown_scriptpubkey(&self) -> ShutdownScript {
350+
let secp_ctx = Secp256k1::signing_only();
351+
let secret_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, 0, 1]).unwrap();
352+
let pubkey_hash = WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &secret_key).serialize());
353+
ShutdownScript::new_p2wpkh(&pubkey_hash)
359354
}
360355
}
361356

fuzz/src/onion_message.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bitcoin::secp256k1::{PublicKey, Scalar, SecretKey};
55
use bitcoin::secp256k1::ecdh::SharedSecret;
66
use bitcoin::secp256k1::ecdsa::RecoverableSignature;
77

8-
use lightning::chain::keysinterface::{Recipient, KeyMaterial, KeysInterface};
8+
use lightning::chain::keysinterface::{Recipient, KeyMaterial, KeysInterface, EntropySource, NodeSigner, SignerProvider};
99
use lightning::ln::msgs::{self, DecodeError, OnionMessageHandler};
1010
use lightning::ln::script::ShutdownScript;
1111
use lightning::util::enforcing_trait_impls::EnforcingSigner;
@@ -90,44 +90,39 @@ struct KeyProvider {
9090
node_secret: SecretKey,
9191
counter: AtomicU64,
9292
}
93-
impl KeysInterface for KeyProvider {
94-
type Signer = EnforcingSigner;
9593

96-
fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> {
97-
Ok(self.node_secret.clone())
94+
impl EntropySource for KeyProvider {
95+
fn get_secure_random_bytes(&self) -> [u8; 32] {
96+
let ctr = self.counter.fetch_add(1, Ordering::Relaxed);
97+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98+
(ctr >> 8*7) as u8, (ctr >> 8*6) as u8, (ctr >> 8*5) as u8, (ctr >> 8*4) as u8, (ctr >> 8*3) as u8, (ctr >> 8*2) as u8, (ctr >> 8*1) as u8, 14, (ctr >> 8*0) as u8]
9899
}
100+
}
99101

100-
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
101-
let mut node_secret = self.get_node_secret(recipient)?;
102-
if let Some(tweak) = tweak {
103-
node_secret = node_secret.mul_tweak(tweak).map_err(|_| ())?;
104-
}
105-
Ok(SharedSecret::new(other_key, &node_secret))
102+
impl NodeSigner for KeyProvider {
103+
fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> {
104+
Ok(self.node_secret.clone())
106105
}
106+
}
107107

108-
fn get_inbound_payment_key_material(&self) -> KeyMaterial { unreachable!() }
109-
110-
fn get_destination_script(&self) -> Script { unreachable!() }
111-
112-
fn get_shutdown_scriptpubkey(&self) -> ShutdownScript { unreachable!() }
108+
impl SignerProvider for KeyProvider {
109+
type Signer = EnforcingSigner;
113110

114111
fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] { unreachable!() }
115112

116113
fn derive_channel_signer(&self, _channel_value_satoshis: u64, _channel_keys_id: [u8; 32]) -> Self::Signer {
117114
unreachable!()
118115
}
119116

120-
fn get_secure_random_bytes(&self) -> [u8; 32] {
121-
let ctr = self.counter.fetch_add(1, Ordering::Relaxed);
122-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123-
(ctr >> 8*7) as u8, (ctr >> 8*6) as u8, (ctr >> 8*5) as u8, (ctr >> 8*4) as u8, (ctr >> 8*3) as u8, (ctr >> 8*2) as u8, (ctr >> 8*1) as u8, 14, (ctr >> 8*0) as u8]
124-
}
125-
126117
fn read_chan_signer(&self, _data: &[u8]) -> Result<EnforcingSigner, DecodeError> { unreachable!() }
118+
}
127119

128-
fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient) -> Result<RecoverableSignature, ()> {
129-
unreachable!()
130-
}
120+
impl KeysInterface for KeyProvider {
121+
fn get_inbound_payment_key_material(&self) -> KeyMaterial { unreachable!() }
122+
123+
fn get_destination_script(&self) -> Script { unreachable!() }
124+
125+
fn get_shutdown_scriptpubkey(&self) -> ShutdownScript { unreachable!() }
131126
}
132127

133128
#[cfg(test)]

0 commit comments

Comments
 (0)