Skip to content

Commit 45d4d26

Browse files
committed
Add a new method read_chan_signer to KeysInterface
This adds a new method to the general cross-channel `KeysInterface` which requires it to handle the deserialization of per-channel signer objects. This allows the deserialization of per-channel signers to have more context available, which, in the case of the C bindings, includes the actual KeysInterface information itself.
1 parent 0f5580a commit 45d4d26

File tree

6 files changed

+31
-4
lines changed

6 files changed

+31
-4
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget,
3737
use lightning::chain::keysinterface::{KeysInterface, InMemoryChannelKeys};
3838
use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, PaymentSecret, PaymentSendFailure, ChannelManagerReadArgs};
3939
use lightning::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
40-
use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, UpdateAddHTLC, Init};
40+
use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, DecodeError, ErrorAction, UpdateAddHTLC, Init};
4141
use lightning::util::enforcing_trait_impls::EnforcingChannelKeys;
4242
use lightning::util::errors::APIError;
4343
use lightning::util::events;
@@ -184,6 +184,10 @@ impl KeysInterface for KeyProvider {
184184
let id = self.rand_bytes_id.fetch_add(1, atomic::Ordering::Relaxed);
185185
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, id, 11, self.node_id]
186186
}
187+
188+
fn read_chan_signer(&self, data: &[u8]) -> Result<EnforcingChannelKeys, DecodeError> {
189+
EnforcingChannelKeys::read(&mut std::io::Cursor::new(data))
190+
}
187191
}
188192

189193
#[inline]

fuzz/src/full_stack.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ use lightning::chain::transaction::OutPoint;
3333
use lightning::chain::keysinterface::{InMemoryChannelKeys, KeysInterface};
3434
use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, PaymentSecret};
3535
use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
36+
use lightning::ln::msgs::DecodeError;
3637
use lightning::routing::router::get_route;
3738
use lightning::routing::network_graph::NetGraphMsgHandler;
39+
use lightning::util::config::UserConfig;
3840
use lightning::util::events::{EventsProvider,Event};
3941
use lightning::util::enforcing_trait_impls::EnforcingChannelKeys;
4042
use lightning::util::logger::Logger;
41-
use lightning::util::config::UserConfig;
43+
use lightning::util::ser::Readable;
4244

4345
use utils::test_logger;
4446
use utils::test_persister::TestPersister;
@@ -299,6 +301,10 @@ impl KeysInterface for KeyProvider {
299301
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
300302
(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]
301303
}
304+
305+
fn read_chan_signer(&self, data: &[u8]) -> Result<EnforcingChannelKeys, DecodeError> {
306+
EnforcingChannelKeys::read(&mut std::io::Cursor::new(data))
307+
}
302308
}
303309

304310
#[inline]

lightning/src/chain/keysinterface.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,14 @@ pub trait KeysInterface: Send + Sync {
344344
/// onion packets and for temporary channel IDs. There is no requirement that these be
345345
/// persisted anywhere, though they must be unique across restarts.
346346
fn get_secure_random_bytes(&self) -> [u8; 32];
347+
348+
/// Reads a `ChanKeySigner` for this `KeysInterface` from the given input stream.
349+
/// This is only called during deserialization of other objects which contain
350+
/// `ChannelKeys`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s).
351+
/// The bytes are exactly those which `<Self::ChanKeySigner as Writeable>::write()` writes, and
352+
/// contain no versioning scheme. You may wish to include your own version prefix and ensure
353+
/// you've read all of the provided bytes to ensure no corruption occurred.
354+
fn read_chan_signer(&self, reader: &[u8]) -> Result<Self::ChanKeySigner, DecodeError>;
347355
}
348356

349357
#[derive(Clone)]
@@ -809,4 +817,8 @@ impl KeysInterface for KeysManager {
809817
sha.input(b"Unique Secure Random Bytes Salt");
810818
Sha256::from_engine(sha).into_inner()
811819
}
820+
821+
fn read_chan_signer(&self, reader: &[u8]) -> Result<Self::ChanKeySigner, DecodeError> {
822+
InMemoryChannelKeys::read(&mut std::io::Cursor::new(reader))
823+
}
812824
}

lightning/src/ln/channel.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4472,7 +4472,7 @@ mod tests {
44724472
use ln::channel::{Channel,ChannelKeys,InboundHTLCOutput,OutboundHTLCOutput,InboundHTLCState,OutboundHTLCState,HTLCOutputInCommitment,TxCreationKeys};
44734473
use ln::channel::MAX_FUNDING_SATOSHIS;
44744474
use ln::features::InitFeatures;
4475-
use ln::msgs::{OptionalField, DataLossProtect};
4475+
use ln::msgs::{OptionalField, DataLossProtect, DecodeError};
44764476
use ln::chan_utils;
44774477
use ln::chan_utils::{ChannelPublicKeys, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
44784478
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
@@ -4528,6 +4528,7 @@ mod tests {
45284528
self.chan_keys.clone()
45294529
}
45304530
fn get_secure_random_bytes(&self) -> [u8; 32] { [0; 32] }
4531+
fn read_chan_signer(&self, data: &[u8]) -> Result<Self::ChanKeySigner, DecodeError> { panic!(); }
45314532
}
45324533

45334534
fn public_from_secret_hex(secp_ctx: &Secp256k1<All>, hex: &str) -> PublicKey {

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7322,7 +7322,7 @@ fn test_user_configurable_csv_delay() {
73227322
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
73237323

73247324
// We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in Channel::new_outbound()
7325-
let keys_manager: Arc<KeysInterface<ChanKeySigner = EnforcingChannelKeys>> = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet));
7325+
let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet));
73267326
if let Err(error) = Channel::new_outbound(&&test_utils::TestFeeEstimator { sat_per_kw: 253 }, &keys_manager, nodes[1].node.get_our_node_id(), 1000000, 1000000, 0, &low_our_to_self_config) {
73277327
match error {
73287328
APIError::APIMisuseError { err } => { assert!(regex::Regex::new(r"Configured with an unreasonable our_to_self_delay \(\d+\) putting user funds at risks").unwrap().is_match(err.as_str())); },

lightning/src/util/test_utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ impl keysinterface::KeysInterface for TestKeysInterface {
430430
}
431431
self.backing.get_secure_random_bytes()
432432
}
433+
434+
fn read_chan_signer(&self, reader: &[u8]) -> Result<Self::ChanKeySigner, msgs::DecodeError> {
435+
EnforcingChannelKeys::read(&mut std::io::Cursor::new(reader))
436+
}
433437
}
434438

435439
impl TestKeysInterface {

0 commit comments

Comments
 (0)