Merge pull request #1935 from TheBlueMatt/2022-12-no-non-time-panic
[rust-lightning] / lightning / src / util / test_utils.rs
index 17bdb06c73f9047368e746edfd07afbbf1d37036..4f74791a173d120f7f84e3c68c3951dd0a08af8b 100644 (file)
@@ -48,7 +48,7 @@ use crate::sync::{Mutex, Arc};
 use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
 use core::mem;
 use bitcoin::bech32::u5;
-use crate::chain::keysinterface::{InMemorySigner, Recipient, KeyMaterial};
+use crate::chain::keysinterface::{InMemorySigner, Recipient, KeyMaterial, EntropySource, NodeSigner, SignerProvider};
 
 #[cfg(feature = "std")]
 use std::time::{SystemTime, UNIX_EPOCH};
@@ -72,17 +72,27 @@ impl chaininterface::FeeEstimator for TestFeeEstimator {
 }
 
 pub struct OnlyReadsKeysInterface {}
-impl keysinterface::KeysInterface for OnlyReadsKeysInterface {
-       type Signer = EnforcingSigner;
 
+impl EntropySource for OnlyReadsKeysInterface {
+       fn get_secure_random_bytes(&self) -> [u8; 32] { [0; 32] }}
+
+impl NodeSigner for OnlyReadsKeysInterface {
        fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> { unreachable!(); }
+       fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
+               let secp_ctx = Secp256k1::signing_only();
+               Ok(PublicKey::from_secret_key(&secp_ctx, &self.get_node_secret(recipient)?))
+       }
        fn ecdh(&self, _recipient: Recipient, _other_key: &PublicKey, _tweak: Option<&Scalar>) -> Result<SharedSecret, ()> { unreachable!(); }
        fn get_inbound_payment_key_material(&self) -> KeyMaterial { unreachable!(); }
-       fn get_destination_script(&self) -> Script { unreachable!(); }
-       fn get_shutdown_scriptpubkey(&self) -> ShutdownScript { unreachable!(); }
+       fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient) -> Result<RecoverableSignature, ()> { unreachable!(); }
+}
+
+impl SignerProvider for OnlyReadsKeysInterface {
+       type Signer = EnforcingSigner;
+
        fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] { unreachable!(); }
+
        fn derive_channel_signer(&self, _channel_value_satoshis: u64, _channel_keys_id: [u8; 32]) -> Self::Signer { unreachable!(); }
-       fn get_secure_random_bytes(&self) -> [u8; 32] { [0; 32] }
 
        fn read_chan_signer(&self, mut reader: &[u8]) -> Result<Self::Signer, msgs::DecodeError> {
                let dummy_sk = SecretKey::from_slice(&[42; 32]).unwrap();
@@ -95,7 +105,12 @@ impl keysinterface::KeysInterface for OnlyReadsKeysInterface {
                        false
                ))
        }
-       fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient) -> Result<RecoverableSignature, ()> { unreachable!(); }
+
+       fn get_destination_script(&self) -> Script { unreachable!(); }
+       fn get_shutdown_scriptpubkey(&self) -> ShutdownScript { unreachable!(); }
+}
+
+impl keysinterface::KeysInterface for OnlyReadsKeysInterface {
 }
 
 pub struct TestChainMonitor<'a> {
@@ -603,32 +618,40 @@ pub struct TestKeysInterface {
        expectations: Mutex<Option<VecDeque<OnGetShutdownScriptpubkey>>>,
 }
 
-impl keysinterface::KeysInterface for TestKeysInterface {
-       type Signer = EnforcingSigner;
+impl EntropySource for TestKeysInterface {
+       fn get_secure_random_bytes(&self) -> [u8; 32] {
+               let override_random_bytes = self.override_random_bytes.lock().unwrap();
+               if let Some(bytes) = &*override_random_bytes {
+                       return *bytes;
+               }
+               self.backing.get_secure_random_bytes()
+       }
+}
 
+impl NodeSigner for TestKeysInterface {
        fn get_node_secret(&self, recipient: Recipient) -> Result<SecretKey, ()> {
                self.backing.get_node_secret(recipient)
        }
+
        fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
                self.backing.get_node_id(recipient)
        }
+
        fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
                self.backing.ecdh(recipient, other_key, tweak)
        }
+
        fn get_inbound_payment_key_material(&self) -> keysinterface::KeyMaterial {
                self.backing.get_inbound_payment_key_material()
        }
-       fn get_destination_script(&self) -> Script { self.backing.get_destination_script() }
 
-       fn get_shutdown_scriptpubkey(&self) -> ShutdownScript {
-               match &mut *self.expectations.lock().unwrap() {
-                       None => self.backing.get_shutdown_scriptpubkey(),
-                       Some(expectations) => match expectations.pop_front() {
-                               None => panic!("Unexpected get_shutdown_scriptpubkey"),
-                               Some(expectation) => expectation.returns,
-                       },
-               }
+       fn sign_invoice(&self, hrp_bytes: &[u8], invoice_data: &[u5], recipient: Recipient) -> Result<RecoverableSignature, ()> {
+               self.backing.sign_invoice(hrp_bytes, invoice_data, recipient)
        }
+}
+
+impl SignerProvider for TestKeysInterface {
+       type Signer = EnforcingSigner;
 
        fn generate_channel_keys_id(&self, inbound: bool, channel_value_satoshis: u64, user_channel_id: u128) -> [u8; 32] {
                self.backing.generate_channel_keys_id(inbound, channel_value_satoshis, user_channel_id)
@@ -640,14 +663,6 @@ impl keysinterface::KeysInterface for TestKeysInterface {
                EnforcingSigner::new_with_revoked(keys, state, self.disable_revocation_policy_check)
        }
 
-       fn get_secure_random_bytes(&self) -> [u8; 32] {
-               let override_random_bytes = self.override_random_bytes.lock().unwrap();
-               if let Some(bytes) = &*override_random_bytes {
-                       return *bytes;
-               }
-               self.backing.get_secure_random_bytes()
-       }
-
        fn read_chan_signer(&self, buffer: &[u8]) -> Result<Self::Signer, msgs::DecodeError> {
                let mut reader = io::Cursor::new(buffer);
 
@@ -661,11 +676,21 @@ impl keysinterface::KeysInterface for TestKeysInterface {
                ))
        }
 
-       fn sign_invoice(&self, hrp_bytes: &[u8], invoice_data: &[u5], recipient: Recipient) -> Result<RecoverableSignature, ()> {
-               self.backing.sign_invoice(hrp_bytes, invoice_data, recipient)
+       fn get_destination_script(&self) -> Script { self.backing.get_destination_script() }
+
+       fn get_shutdown_scriptpubkey(&self) -> ShutdownScript {
+               match &mut *self.expectations.lock().unwrap() {
+                       None => self.backing.get_shutdown_scriptpubkey(),
+                       Some(expectations) => match expectations.pop_front() {
+                               None => panic!("Unexpected get_shutdown_scriptpubkey"),
+                               Some(expectation) => expectation.returns,
+                       },
+               }
        }
 }
 
+impl keysinterface::KeysInterface for TestKeysInterface {}
+
 impl TestKeysInterface {
        pub fn new(seed: &[u8; 32], network: Network) -> Self {
                let now = Duration::from_secs(genesis_block(network).header.time as u64);
@@ -678,7 +703,7 @@ impl TestKeysInterface {
                }
        }
 
-       /// Sets an expectation that [`keysinterface::KeysInterface::get_shutdown_scriptpubkey`] is
+       /// Sets an expectation that [`keysinterface::SignerProvider::get_shutdown_scriptpubkey`] is
        /// called.
        pub fn expect(&self, expectation: OnGetShutdownScriptpubkey) -> &Self {
                self.expectations.lock().unwrap()
@@ -726,7 +751,7 @@ impl Drop for TestKeysInterface {
        }
 }
 
-/// An expectation that [`keysinterface::KeysInterface::get_shutdown_scriptpubkey`] was called and
+/// An expectation that [`keysinterface::SignerProvider::get_shutdown_scriptpubkey`] was called and
 /// returns a [`ShutdownScript`].
 pub struct OnGetShutdownScriptpubkey {
        /// A shutdown script used to close a channel.