From: Jeffrey Czyz Date: Wed, 15 Feb 2023 22:10:59 +0000 (-0600) Subject: OffersMessageHandler trait for OnionMessenger X-Git-Tag: v0.0.116-alpha1~11^2~9 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=f521e4cceb022768a459285e7ab3922f0f59f9d5;p=rust-lightning OffersMessageHandler trait for OnionMessenger Add a trait for handling BOLT 12 Offers messages to OnionMessenger and a skeleton implementation of it for ChannelManager. This allows users to either provide their own custom handling Offers messages or rely on a version provided by LDK using stateless verification. --- diff --git a/fuzz/src/onion_message.rs b/fuzz/src/onion_message.rs index 5fb2122ce..ec467e22d 100644 --- a/fuzz/src/onion_message.rs +++ b/fuzz/src/onion_message.rs @@ -11,7 +11,7 @@ use lightning::ln::script::ShutdownScript; use lightning::util::enforcing_trait_impls::EnforcingSigner; use lightning::util::logger::Logger; use lightning::util::ser::{Readable, Writeable, Writer}; -use lightning::onion_message::{CustomOnionMessageContents, CustomOnionMessageHandler, OnionMessenger}; +use lightning::onion_message::{CustomOnionMessageContents, CustomOnionMessageHandler, OffersMessage, OffersMessageHandler, OnionMessenger}; use crate::utils::test_logger; @@ -29,8 +29,11 @@ pub fn do_test(data: &[u8], logger: &L) { node_secret: secret, counter: AtomicU64::new(0), }; + let offers_msg_handler = TestOffersMessageHandler {}; let custom_msg_handler = TestCustomMessageHandler {}; - let onion_messenger = OnionMessenger::new(&keys_manager, &keys_manager, logger, &custom_msg_handler); + let onion_messenger = OnionMessenger::new( + &keys_manager, &keys_manager, logger, &offers_msg_handler, &custom_msg_handler + ); let mut pk = [2; 33]; pk[1] = 0xff; let peer_node_id_not_used = PublicKey::from_slice(&pk).unwrap(); onion_messenger.handle_onion_message(&peer_node_id_not_used, &msg); @@ -50,6 +53,12 @@ pub extern "C" fn onion_message_run(data: *const u8, datalen: usize) { do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, &logger); } +struct TestOffersMessageHandler {} + +impl OffersMessageHandler for TestOffersMessageHandler { + fn handle_message(&self, _message: OffersMessage) {} +} + struct TestCustomMessage {} const CUSTOM_MESSAGE_TYPE: u64 = 4242; diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index 06c6932cc..f5c6341bb 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -28,7 +28,7 @@ use crate::util::ser::{VecWriter, Writeable, Writer}; use crate::ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep}; use crate::ln::wire; use crate::ln::wire::{Encode, Type}; -use crate::onion_message::{CustomOnionMessageContents, CustomOnionMessageHandler, SimpleArcOnionMessenger, SimpleRefOnionMessenger}; +use crate::onion_message::{CustomOnionMessageContents, CustomOnionMessageHandler, OffersMessage, OffersMessageHandler, SimpleArcOnionMessenger, SimpleRefOnionMessenger}; use crate::routing::gossip::{NetworkGraph, P2PGossipSync, NodeId, NodeAlias}; use crate::util::atomic_counter::AtomicCounter; use crate::util::logger::Logger; @@ -118,6 +118,9 @@ impl OnionMessageHandler for IgnoringMessageHandler { InitFeatures::empty() } } +impl OffersMessageHandler for IgnoringMessageHandler { + fn handle_message(&self, _msg: OffersMessage) {} +} impl CustomOnionMessageHandler for IgnoringMessageHandler { type CustomMessage = Infallible; fn handle_custom_message(&self, _msg: Infallible) { diff --git a/lightning/src/onion_message/functional_tests.rs b/lightning/src/onion_message/functional_tests.rs index 96e0c44b2..cc83f2d2c 100644 --- a/lightning/src/onion_message/functional_tests.rs +++ b/lightning/src/onion_message/functional_tests.rs @@ -13,7 +13,7 @@ use crate::blinded_path::BlindedPath; use crate::sign::{NodeSigner, Recipient}; use crate::ln::features::InitFeatures; use crate::ln::msgs::{self, DecodeError, OnionMessageHandler}; -use super::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, OnionMessageContents, OnionMessenger, SendError}; +use super::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, OffersMessage, OffersMessageHandler, OnionMessageContents, OnionMessenger, SendError}; use crate::util::ser::{Writeable, Writer}; use crate::util::test_utils; @@ -27,7 +27,7 @@ use crate::sync::Arc; struct MessengerNode { keys_manager: Arc, - messenger: OnionMessenger, Arc, Arc, Arc>, + messenger: OnionMessenger, Arc, Arc, Arc, Arc>, custom_message_handler: Arc, logger: Arc, } @@ -38,6 +38,14 @@ impl MessengerNode { } } +struct TestOffersMessageHandler {} + +impl OffersMessageHandler for TestOffersMessageHandler { + fn handle_message(&self, _message: OffersMessage) { + todo!() + } +} + #[derive(Clone)] struct TestCustomMessage {} @@ -98,10 +106,11 @@ fn create_nodes(num_messengers: u8) -> Vec { let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i))); let seed = [i as u8; 32]; let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet)); + let offers_message_handler = Arc::new(TestOffersMessageHandler {}); let custom_message_handler = Arc::new(TestCustomMessageHandler::new()); nodes.push(MessengerNode { keys_manager: keys_manager.clone(), - messenger: OnionMessenger::new(keys_manager.clone(), keys_manager.clone(), logger.clone(), custom_message_handler.clone()), + messenger: OnionMessenger::new(keys_manager.clone(), keys_manager, logger.clone(), offers_message_handler, custom_message_handler.clone()), custom_message_handler, logger, }); diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index dbfa0bc63..23c9db0c4 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -23,6 +23,7 @@ use crate::ln::msgs::{self, OnionMessageHandler}; use crate::ln::onion_utils; use crate::ln::peer_handler::IgnoringMessageHandler; pub use super::packet::{CustomOnionMessageContents, OnionMessageContents}; +use super::offers::OffersMessageHandler; use super::packet::{BIG_PACKET_HOP_DATA_LEN, ForwardControlTlvs, Packet, Payload, ReceiveControlTlvs, SMALL_PACKET_HOP_DATA_LEN}; use crate::util::logger::Logger; use crate::util::ser::Writeable; @@ -63,10 +64,11 @@ use crate::prelude::*; /// # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret); /// # let (hop_node_id2, hop_node_id3, hop_node_id4) = (hop_node_id1, hop_node_id1, hop_node_id1); /// # let destination_node_id = hop_node_id1; -/// # let your_custom_message_handler = IgnoringMessageHandler {}; +/// # let custom_message_handler = IgnoringMessageHandler {}; +/// # let offers_message_handler = IgnoringMessageHandler {}; /// // Create the onion messenger. This must use the same `keys_manager` as is passed to your /// // ChannelManager. -/// let onion_messenger = OnionMessenger::new(&keys_manager, &keys_manager, logger, &your_custom_message_handler); +/// let onion_messenger = OnionMessenger::new(&keys_manager, &keys_manager, logger, &offers_message_handler, &custom_message_handler); /// /// # struct YourCustomMessage {} /// impl Writeable for YourCustomMessage { @@ -103,20 +105,21 @@ use crate::prelude::*; /// /// [offers]: /// [`OnionMessenger`]: crate::onion_message::OnionMessenger -pub struct OnionMessenger - where ES::Target: EntropySource, - NS::Target: NodeSigner, - L::Target: Logger, - CMH:: Target: CustomOnionMessageHandler, +pub struct OnionMessenger +where + ES::Target: EntropySource, + NS::Target: NodeSigner, + L::Target: Logger, + OMH::Target: OffersMessageHandler, + CMH:: Target: CustomOnionMessageHandler, { entropy_source: ES, node_signer: NS, logger: L, pending_messages: Mutex>>, secp_ctx: Secp256k1, + offers_handler: OMH, custom_handler: CMH, - // Coming soon: - // invoice_handler: InvoiceHandler, } /// The destination of an onion message. @@ -187,15 +190,19 @@ pub trait CustomOnionMessageHandler { fn read_custom_message(&self, message_type: u64, buffer: &mut R) -> Result, msgs::DecodeError>; } -impl OnionMessenger - where ES::Target: EntropySource, - NS::Target: NodeSigner, - L::Target: Logger, - CMH::Target: CustomOnionMessageHandler, +impl OnionMessenger +where + ES::Target: EntropySource, + NS::Target: NodeSigner, + L::Target: Logger, + OMH::Target: OffersMessageHandler, + CMH::Target: CustomOnionMessageHandler, { /// Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to /// their respective handlers. - pub fn new(entropy_source: ES, node_signer: NS, logger: L, custom_handler: CMH) -> Self { + pub fn new( + entropy_source: ES, node_signer: NS, logger: L, offers_handler: OMH, custom_handler: CMH + ) -> Self { let mut secp_ctx = Secp256k1::new(); secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes()); OnionMessenger { @@ -204,6 +211,7 @@ impl OnionMessenger pending_messages: Mutex::new(HashMap::new()), secp_ctx, logger, + offers_handler, custom_handler, } } @@ -298,11 +306,14 @@ fn outbound_buffer_full(peer_node_id: &PublicKey, buffer: &HashMap OnionMessageHandler for OnionMessenger - where ES::Target: EntropySource, - NS::Target: NodeSigner, - L::Target: Logger + Sized, - CMH::Target: CustomOnionMessageHandler + Sized, +impl OnionMessageHandler +for OnionMessenger +where + ES::Target: EntropySource, + NS::Target: NodeSigner, + L::Target: Logger, + OMH::Target: OffersMessageHandler, + CMH::Target: CustomOnionMessageHandler + Sized, { /// Handle an incoming onion message. Currently, if a message was destined for us we will log, but /// soon we'll delegate the onion message to a handler that can generate invoices or send @@ -342,7 +353,7 @@ impl OnionMessageHandler for OnionMe "Received an onion message with path_id {:02x?} and {} reply_path", path_id, if reply_path.is_some() { "a" } else { "no" }); match message { - OnionMessageContents::Offers(_msg) => todo!(), + OnionMessageContents::Offers(msg) => self.offers_handler.handle_message(msg), OnionMessageContents::Custom(msg) => self.custom_handler.handle_custom_message(msg), } }, @@ -445,11 +456,14 @@ impl OnionMessageHandler for OnionMe } } -impl OnionMessageProvider for OnionMessenger - where ES::Target: EntropySource, - NS::Target: NodeSigner, - L::Target: Logger, - CMH::Target: CustomOnionMessageHandler, +impl OnionMessageProvider +for OnionMessenger +where + ES::Target: EntropySource, + NS::Target: NodeSigner, + L::Target: Logger, + OMH::Target: OffersMessageHandler, + CMH::Target: CustomOnionMessageHandler, { fn next_onion_message_for_peer(&self, peer_node_id: PublicKey) -> Option { let mut pending_msgs = self.pending_messages.lock().unwrap(); @@ -469,7 +483,14 @@ impl OnionMessageProvider for OnionM /// /// [`SimpleArcChannelManager`]: crate::ln::channelmanager::SimpleArcChannelManager /// [`SimpleArcPeerManager`]: crate::ln::peer_handler::SimpleArcPeerManager -pub type SimpleArcOnionMessenger = OnionMessenger, Arc, Arc, IgnoringMessageHandler>; +pub type SimpleArcOnionMessenger = OnionMessenger< + Arc, + Arc, + Arc, + IgnoringMessageHandler, + IgnoringMessageHandler +>; + /// Useful for simplifying the parameters of [`SimpleRefChannelManager`] and /// [`SimpleRefPeerManager`]. See their docs for more details. /// @@ -477,7 +498,13 @@ pub type SimpleArcOnionMessenger = OnionMessenger, Arc = OnionMessenger<&'a KeysManager, &'a KeysManager, &'b L, IgnoringMessageHandler>; +pub type SimpleRefOnionMessenger<'a, 'b, L> = OnionMessenger< + &'a KeysManager, + &'a KeysManager, + &'b L, + IgnoringMessageHandler, + IgnoringMessageHandler +>; /// Construct onion packet payloads and keys for sending an onion message along the given /// `unblinded_path` to the given `destination`. diff --git a/lightning/src/onion_message/mod.rs b/lightning/src/onion_message/mod.rs index 0fcf3cc79..e0b3f5001 100644 --- a/lightning/src/onion_message/mod.rs +++ b/lightning/src/onion_message/mod.rs @@ -28,4 +28,5 @@ mod functional_tests; // Re-export structs so they can be imported with just the `onion_message::` module prefix. pub use self::messenger::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, OnionMessageContents, OnionMessenger, SendError, SimpleArcOnionMessenger, SimpleRefOnionMessenger}; +pub use self::offers::{OffersMessage, OffersMessageHandler}; pub(crate) use self::packet::{ControlTlvs, Packet}; diff --git a/lightning/src/onion_message/offers.rs b/lightning/src/onion_message/offers.rs index 36363c90a..98276d1bf 100644 --- a/lightning/src/onion_message/offers.rs +++ b/lightning/src/onion_message/offers.rs @@ -26,6 +26,15 @@ const INVOICE_REQUEST_TLV_TYPE: u64 = 64; const INVOICE_TLV_TYPE: u64 = 66; const INVOICE_ERROR_TLV_TYPE: u64 = 68; +/// A handler for an [`OnionMessage`] containing a BOLT 12 Offers message as its payload. +/// +/// [`OnionMessage`]: crate::ln::msgs::OnionMessage +pub trait OffersMessageHandler { + /// Handles the given message by either responding with an [`Invoice`], sending a payment, or + /// replying with an error. + fn handle_message(&self, message: OffersMessage); +} + /// Possible BOLT 12 Offers messages sent and received via an [`OnionMessage`]. /// /// [`OnionMessage`]: crate::ln::msgs::OnionMessage @@ -82,7 +91,7 @@ impl Writeable for OffersMessage { } } -impl ReadableArgs<(u64, &L)> for OffersMessage { +impl ReadableArgs<(u64, &L)> for OffersMessage { fn read(r: &mut R, read_args: (u64, &L)) -> Result { let (tlv_type, logger) = read_args; if tlv_type == INVOICE_ERROR_TLV_TYPE { diff --git a/lightning/src/onion_message/packet.rs b/lightning/src/onion_message/packet.rs index e5fb93f45..663a0b0cd 100644 --- a/lightning/src/onion_message/packet.rs +++ b/lightning/src/onion_message/packet.rs @@ -203,7 +203,7 @@ impl Writeable for (Payload, [u8; 32]) { } // Uses the provided secret to simultaneously decode and decrypt the control TLVs and data TLV. -impl +impl ReadableArgs<(SharedSecret, &H, &L)> for Payload<::CustomMessage> { fn read(r: &mut R, args: (SharedSecret, &H, &L)) -> Result { let (encrypted_tlvs_ss, handler, logger) = args;