From: Matt Corallo Date: Mon, 17 Dec 2018 19:20:27 +0000 (-0500) Subject: Add test_util for overriding session privs for onion crypt X-Git-Tag: v0.0.12~254^2~2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=8783a748bb952168623cb674f8ea3230d7c94b25;p=rust-lightning Add test_util for overriding session privs for onion crypt --- diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index 217a57bc4..5216882d4 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -3570,6 +3570,7 @@ mod tests { chain_monitor: Arc, tx_broadcaster: Arc, chan_monitor: Arc, + keys_manager: Arc, node: Arc, router: Router, node_seed: [u8; 32], @@ -4303,14 +4304,14 @@ mod tests { let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())}); let mut seed = [0; 32]; rng.fill_bytes(&mut seed); - let keys_manager = Arc::new(keysinterface::KeysManager::new(&seed, Network::Testnet, Arc::clone(&logger))); + let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, Arc::clone(&logger))); let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone())); let mut config = UserConfig::new(); config.channel_options.announced_channel = true; config.channel_limits.force_announced_channel_preference = false; let node = ChannelManager::new(Network::Testnet, feeest.clone(), chan_monitor.clone(), chain_monitor.clone(), tx_broadcaster.clone(), Arc::clone(&logger), keys_manager.clone(), config).unwrap(); let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &keys_manager.get_node_secret()), chain_monitor.clone(), Arc::clone(&logger)); - nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, node_seed: seed, + nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, keys_manager, node_seed: seed, network_payment_count: payment_count.clone(), network_chan_count: chan_count.clone(), }); diff --git a/src/util/test_utils.rs b/src/util/test_utils.rs index 3acb0d02e..b04f728b0 100644 --- a/src/util/test_utils.rs +++ b/src/util/test_utils.rs @@ -1,6 +1,7 @@ use chain::chaininterface; use chain::chaininterface::ConfirmationTarget; use chain::transaction::OutPoint; +use chain::keysinterface; use ln::channelmonitor; use ln::msgs; use ln::msgs::{HandleError}; @@ -10,9 +11,11 @@ use util::logger::{Logger, Level, Record}; use util::ser::{ReadableArgs, Writer}; use bitcoin::blockdata::transaction::Transaction; +use bitcoin::blockdata::script::Script; use bitcoin::util::hash::Sha256dHash; +use bitcoin::network::constants::Network; -use secp256k1::PublicKey; +use secp256k1::{SecretKey, PublicKey}; use std::sync::{Arc,Mutex}; use std::{mem}; @@ -208,3 +211,31 @@ impl Logger for TestLogger { } } } + +pub struct TestKeysInterface { + backing: keysinterface::KeysManager, + pub override_session_priv: Mutex>, +} + +impl keysinterface::KeysInterface for TestKeysInterface { + fn get_node_secret(&self) -> SecretKey { self.backing.get_node_secret() } + fn get_destination_script(&self) -> Script { self.backing.get_destination_script() } + fn get_shutdown_pubkey(&self) -> PublicKey { self.backing.get_shutdown_pubkey() } + fn get_channel_keys(&self, inbound: bool) -> keysinterface::ChannelKeys { self.backing.get_channel_keys(inbound) } + + fn get_session_key(&self) -> SecretKey { + match *self.override_session_priv.lock().unwrap() { + Some(key) => key.clone(), + None => self.backing.get_session_key() + } + } +} + +impl TestKeysInterface { + pub fn new(seed: &[u8; 32], network: Network, logger: Arc) -> Self { + Self { + backing: keysinterface::KeysManager::new(seed, network, logger), + override_session_priv: Mutex::new(None), + } + } +}