From: Matt Corallo Date: Wed, 26 Oct 2022 21:58:46 +0000 (+0000) Subject: Make OnionMessageContents private for now X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=refs%2Fheads%2F2022-10-112-bindings-base;p=rust-lightning Make OnionMessageContents private for now This doesn't change the API expressibility as its currently the only type of message that can be sent, but avoids a generation failure as we can't call `&self` methods on complex enums unless we can `clone` all the inner fields. The correct solution to this is to simply not expose those methods (which sucks, but at least for `OnionMessageContents` they don't really matter), but that doesn't have to come now. --- diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 261f59c4..6284c47c 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -47,7 +47,7 @@ use crate::prelude::*; /// # use lightning::ln::msgs::DecodeError; /// # use lightning::ln::peer_handler::IgnoringMessageHandler; /// # use lightning::onion_message::messenger::{Destination, OnionMessenger}; -/// # use lightning::onion_message::packet::{CustomOnionMessageContents, OnionMessageContents}; +/// # use lightning::onion_message::packet::CustomOnionMessageContents; /// # use lightning::onion_message::blinded_route::BlindedRoute; /// # use lightning::util::logger::{Logger, Record}; /// # use lightning::util::ser::{Writeable, Writer}; @@ -88,8 +88,7 @@ use crate::prelude::*; /// let intermediate_hops = [hop_node_id1, hop_node_id2]; /// let reply_path = None; /// # let your_custom_message = YourCustomMessage {}; -/// let message = OnionMessageContents::Custom(your_custom_message); -/// onion_messenger.send_onion_message(&intermediate_hops, Destination::Node(destination_node_id), message, reply_path); +/// onion_messenger.send_custom_onion_message(&intermediate_hops, Destination::Node(destination_node_id), your_custom_message, reply_path); /// /// // Create a blinded route to yourself, for someone to send an onion message to. /// # let your_node_id = hop_node_id1; @@ -100,8 +99,7 @@ use crate::prelude::*; /// # let intermediate_hops = [hop_node_id1, hop_node_id2]; /// let reply_path = None; /// # let your_custom_message = YourCustomMessage {}; -/// let message = OnionMessageContents::Custom(your_custom_message); -/// onion_messenger.send_onion_message(&intermediate_hops, Destination::BlindedRoute(blinded_route), message, reply_path); +/// onion_messenger.send_custom_onion_message(&intermediate_hops, Destination::BlindedRoute(blinded_route), your_custom_message, reply_path); /// ``` /// /// [offers]: @@ -139,7 +137,7 @@ impl Destination { /// Errors that may occur when [sending an onion message]. /// -/// [sending an onion message]: OnionMessenger::send_onion_message +/// [sending an onion message]: OnionMessenger::send_custom_onion_message #[derive(Debug, PartialEq, Eq)] pub enum SendError { /// Errored computing onion message packet keys. @@ -200,13 +198,19 @@ impl OnionMessenger(&self, intermediate_nodes: &[PublicKey], destination: Destination, message: OnionMessageContents, reply_path: Option) -> Result<(), SendError> { + pub(crate) fn send_onion_message(&self, intermediate_nodes: &[PublicKey], destination: Destination, msg: OnionMessageContents, reply_path: Option) -> Result<(), SendError> { + let OnionMessageContents::Custom(message) = msg; + self.send_custom_onion_message(intermediate_nodes, destination, message, reply_path) + } + + /// Send an onion message with contents `message` to `destination`, routing it through `intermediate_nodes`. + /// See [`OnionMessenger`] for example usage. + pub fn send_custom_onion_message(&self, intermediate_nodes: &[PublicKey], destination: Destination, msg: T, reply_path: Option) -> Result<(), SendError> { if let Destination::BlindedRoute(BlindedRoute { ref blinded_hops, .. }) = destination { if blinded_hops.len() < 2 { return Err(SendError::TooFewBlindedHops); } } - let OnionMessageContents::Custom(ref msg) = message; if msg.tlv_type() < 64 { return Err(SendError::InvalidMessage) } let blinding_secret_bytes = self.keys_manager.get_secure_random_bytes(); @@ -221,7 +225,7 @@ impl OnionMessenger { #[derive(Debug)] /// The contents of an onion message. In the context of offers, this would be the invoice, invoice /// request, or invoice error. -pub enum OnionMessageContents { +pub(crate) enum OnionMessageContents { // Coming soon: // Invoice, // InvoiceRequest,