Expose send_payment_for_bolt12_invoice
[rust-lightning] / fuzz / src / onion_message.rs
index f2bae246fabeb09f6a2ab827c078b6046c2cd8e5..a8a290fefb3379a176ff781e8b80c8df1036804f 100644 (file)
@@ -1,24 +1,27 @@
 // Imports that need to be added manually
-use bitcoin::bech32::u5;
+use bech32::u5;
 use bitcoin::blockdata::script::ScriptBuf;
-use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, SecretKey, self};
 use bitcoin::secp256k1::ecdh::SharedSecret;
 use bitcoin::secp256k1::ecdsa::RecoverableSignature;
 use bitcoin::secp256k1::schnorr;
+use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
 
-use lightning::blinded_path::BlindedPath;
+use lightning::blinded_path::{BlindedPath, EmptyNodeIdLookUp};
 use lightning::ln::features::InitFeatures;
 use lightning::ln::msgs::{self, DecodeError, OnionMessageHandler};
 use lightning::ln::script::ShutdownScript;
 use lightning::offers::invoice::UnsignedBolt12Invoice;
 use lightning::offers::invoice_request::UnsignedInvoiceRequest;
-use lightning::sign::{Recipient, KeyMaterial, EntropySource, NodeSigner, SignerProvider};
-use lightning::util::test_channel_signer::TestChannelSigner;
-use lightning::util::logger::Logger;
-use lightning::util::ser::{Readable, Writeable, Writer};
-use lightning::onion_message::messenger::{CustomOnionMessageHandler, Destination, MessageRouter, OnionMessagePath, OnionMessenger, PendingOnionMessage};
+use lightning::onion_message::messenger::{
+       CustomOnionMessageHandler, Destination, MessageRouter, OnionMessagePath, OnionMessenger,
+       PendingOnionMessage, Responder, ResponseInstruction,
+};
 use lightning::onion_message::offers::{OffersMessage, OffersMessageHandler};
 use lightning::onion_message::packet::OnionMessageContents;
+use lightning::sign::{EntropySource, KeyMaterial, NodeSigner, Recipient, SignerProvider};
+use lightning::util::logger::Logger;
+use lightning::util::ser::{Readable, Writeable, Writer};
+use lightning::util::test_channel_signer::TestChannelSigner;
 
 use crate::utils::test_logger;
 
@@ -32,16 +35,19 @@ pub fn do_test<L: Logger>(data: &[u8], logger: &L) {
                let mut secret_bytes = [1; 32];
                secret_bytes[31] = 2;
                let secret = SecretKey::from_slice(&secret_bytes).unwrap();
-               let keys_manager = KeyProvider {
-                       node_secret: secret,
-                       counter: AtomicU64::new(0),
-               };
+               let keys_manager = KeyProvider { node_secret: secret, counter: AtomicU64::new(0) };
+               let node_id_lookup = EmptyNodeIdLookUp {};
                let message_router = TestMessageRouter {};
                let offers_msg_handler = TestOffersMessageHandler {};
                let custom_msg_handler = TestCustomMessageHandler {};
                let onion_messenger = OnionMessenger::new(
-                       &keys_manager, &keys_manager, logger, &message_router, &offers_msg_handler,
-                       &custom_msg_handler
+                       &keys_manager,
+                       &keys_manager,
+                       logger,
+                       &node_id_lookup,
+                       &message_router,
+                       &offers_msg_handler,
+                       &custom_msg_handler,
                );
 
                let peer_node_id = {
@@ -77,13 +83,9 @@ struct TestMessageRouter {}
 
 impl MessageRouter for TestMessageRouter {
        fn find_path(
-               &self, _sender: PublicKey, _peers: Vec<PublicKey>, destination: Destination
+               &self, _sender: PublicKey, _peers: Vec<PublicKey>, destination: Destination,
        ) -> Result<OnionMessagePath, ()> {
-               Ok(OnionMessagePath {
-                       intermediate_nodes: vec![],
-                       destination,
-                       first_node_addresses: None,
-               })
+               Ok(OnionMessagePath { intermediate_nodes: vec![], destination, first_node_addresses: None })
        }
 
        fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
@@ -96,8 +98,10 @@ impl MessageRouter for TestMessageRouter {
 struct TestOffersMessageHandler {}
 
 impl OffersMessageHandler for TestOffersMessageHandler {
-       fn handle_message(&self, _message: OffersMessage) -> Option<OffersMessage> {
-               None
+       fn handle_message(
+               &self, _message: OffersMessage, _responder: Option<Responder>,
+       ) -> ResponseInstruction<OffersMessage> {
+               ResponseInstruction::NoResponse
        }
 }
 
@@ -111,6 +115,9 @@ impl OnionMessageContents for TestCustomMessage {
        fn tlv_type(&self) -> u64 {
                CUSTOM_MESSAGE_TYPE
        }
+       fn msg_type(&self) -> &'static str {
+               "Custom Message"
+       }
 }
 
 impl Writeable for TestCustomMessage {
@@ -123,13 +130,20 @@ struct TestCustomMessageHandler {}
 
 impl CustomOnionMessageHandler for TestCustomMessageHandler {
        type CustomMessage = TestCustomMessage;
-       fn handle_custom_message(&self, _msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
-               Some(TestCustomMessage {})
+       fn handle_custom_message(
+               &self, message: Self::CustomMessage, responder: Option<Responder>,
+       ) -> ResponseInstruction<Self::CustomMessage> {
+               match responder {
+                       Some(responder) => responder.respond(message),
+                       None => ResponseInstruction::NoResponse,
+               }
        }
-       fn read_custom_message<R: io::Read>(&self, _message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, msgs::DecodeError> {
+       fn read_custom_message<R: io::Read>(
+               &self, _message_type: u64, buffer: &mut R,
+       ) -> Result<Option<Self::CustomMessage>, msgs::DecodeError> {
                let mut buf = Vec::new();
                buffer.read_to_end(&mut buf)?;
-               return Ok(Some(TestCustomMessage {}))
+               return Ok(Some(TestCustomMessage {}));
        }
        fn release_pending_custom_messages(&self) -> Vec<PendingOnionMessage<Self::CustomMessage>> {
                vec![]
@@ -151,8 +165,11 @@ struct KeyProvider {
 impl EntropySource for KeyProvider {
        fn get_secure_random_bytes(&self) -> [u8; 32] {
                let ctr = self.counter.fetch_add(1, Ordering::Relaxed);
-               [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-                       (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]
+               #[rustfmt::skip]
+               let random_bytes = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                       (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];
+               random_bytes
        }
 }
 
@@ -160,15 +177,17 @@ impl NodeSigner for KeyProvider {
        fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
                let node_secret = match recipient {
                        Recipient::Node => Ok(&self.node_secret),
-                       Recipient::PhantomNode => Err(())
+                       Recipient::PhantomNode => Err(()),
                }?;
                Ok(PublicKey::from_secret_key(&Secp256k1::signing_only(), node_secret))
        }
 
-       fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
+       fn ecdh(
+               &self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>,
+       ) -> Result<SharedSecret, ()> {
                let mut node_secret = match recipient {
                        Recipient::Node => Ok(self.node_secret.clone()),
-                       Recipient::PhantomNode => Err(())
+                       Recipient::PhantomNode => Err(()),
                }?;
                if let Some(tweak) = tweak {
                        node_secret = node_secret.mul_tweak(tweak).map_err(|_| ())?;
@@ -176,14 +195,18 @@ impl NodeSigner for KeyProvider {
                Ok(SharedSecret::new(other_key, &node_secret))
        }
 
-       fn get_inbound_payment_key_material(&self) -> KeyMaterial { unreachable!() }
+       fn get_inbound_payment_key_material(&self) -> KeyMaterial {
+               unreachable!()
+       }
 
-       fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient) -> Result<RecoverableSignature, ()> {
+       fn sign_invoice(
+               &self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient,
+       ) -> Result<RecoverableSignature, ()> {
                unreachable!()
        }
 
        fn sign_bolt12_invoice_request(
-               &self, _invoice_request: &UnsignedInvoiceRequest
+               &self, _invoice_request: &UnsignedInvoiceRequest,
        ) -> Result<schnorr::Signature, ()> {
                unreachable!()
        }
@@ -194,7 +217,9 @@ impl NodeSigner for KeyProvider {
                unreachable!()
        }
 
-       fn sign_gossip_message(&self, _msg: lightning::ln::msgs::UnsignedGossipMessage) -> Result<bitcoin::secp256k1::ecdsa::Signature, ()> {
+       fn sign_gossip_message(
+               &self, _msg: lightning::ln::msgs::UnsignedGossipMessage,
+       ) -> Result<bitcoin::secp256k1::ecdsa::Signature, ()> {
                unreachable!()
        }
 }
@@ -204,17 +229,29 @@ impl SignerProvider for KeyProvider {
        #[cfg(taproot)]
        type TaprootSigner = TestChannelSigner;
 
-       fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] { unreachable!() }
+       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::EcdsaSigner {
+       fn derive_channel_signer(
+               &self, _channel_value_satoshis: u64, _channel_keys_id: [u8; 32],
+       ) -> Self::EcdsaSigner {
                unreachable!()
        }
 
-       fn read_chan_signer(&self, _data: &[u8]) -> Result<TestChannelSigner, DecodeError> { unreachable!() }
+       fn read_chan_signer(&self, _data: &[u8]) -> Result<TestChannelSigner, DecodeError> {
+               unreachable!()
+       }
 
-       fn get_destination_script(&self, _channel_keys_id: [u8; 32]) -> Result<ScriptBuf, ()> { unreachable!() }
+       fn get_destination_script(&self, _channel_keys_id: [u8; 32]) -> Result<ScriptBuf, ()> {
+               unreachable!()
+       }
 
-       fn get_shutdown_scriptpubkey(&self) -> Result<ShutdownScript, ()> { unreachable!() }
+       fn get_shutdown_scriptpubkey(&self) -> Result<ShutdownScript, ()> {
+               unreachable!()
+       }
 }
 
 #[cfg(test)]
@@ -230,8 +267,17 @@ mod tests {
        }
        impl Logger for TrackingLogger {
                fn log(&self, record: Record) {
-                       *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1;
-                       println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
+                       let mut lines_lock = self.lines.lock().unwrap();
+                       let key = (record.module_path.to_string(), format!("{}", record.args));
+                       *lines_lock.entry(key).or_insert(0) += 1;
+                       println!(
+                               "{:<5} [{} : {}, {}] {}",
+                               record.level.to_string(),
+                               record.module_path,
+                               record.file,
+                               record.line,
+                               record.args
+                       );
                }
        }
 
@@ -279,9 +325,9 @@ mod tests {
                                                "Received an onion message with path_id None and a reply_path: Custom(TestCustomMessage)"
                                                .to_string())), Some(&1));
                        assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
-                                               "Constructing onion message when responding to Custom onion message with path_id None: TestCustomMessage".to_string())), Some(&1));
+                                               "Constructing onion message when responding with Custom Message to an onion message with path_id None: TestCustomMessage".to_string())), Some(&1));
                        assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
-                                               "Buffered onion message when responding to Custom onion message with path_id None".to_string())), Some(&1));
+                                               "Buffered onion message when responding with Custom Message to an onion message with path_id None".to_string())), Some(&1));
                }
 
                let two_unblinded_hops_om = "\