@@ -21,14 +21,15 @@ use crate::ln::msgs::{self, OnionMessageHandler};
2121use crate :: ln:: onion_utils;
2222use crate :: ln:: peer_handler:: IgnoringMessageHandler ;
2323use super :: blinded_route:: { BlindedRoute , ForwardTlvs , ReceiveTlvs } ;
24- pub use super :: packet:: { CustomOnionMessageContents , OnionMessageContents } ;
24+ use super :: packet:: { CustomOnionMessageContents , OnionMessageContents } ;
2525use super :: packet:: { BIG_PACKET_HOP_DATA_LEN , ForwardControlTlvs , Packet , Payload , ReceiveControlTlvs , SMALL_PACKET_HOP_DATA_LEN } ;
2626use super :: utils;
2727use crate :: util:: events:: OnionMessageProvider ;
2828use crate :: util:: logger:: Logger ;
2929use crate :: util:: ser:: Writeable ;
3030
3131use core:: ops:: Deref ;
32+ use crate :: io;
3233use crate :: sync:: { Arc , Mutex } ;
3334use crate :: prelude:: * ;
3435
@@ -45,9 +46,11 @@ use crate::prelude::*;
4546/// # use lightning::chain::keysinterface::{InMemorySigner, KeysManager, KeysInterface};
4647/// # use lightning::ln::msgs::DecodeError;
4748/// # use lightning::ln::peer_handler::IgnoringMessageHandler;
48- /// # use lightning::onion_message::{BlindedRoute, CustomOnionMessageContents, Destination, OnionMessageContents, OnionMessenger};
49+ /// # use lightning::onion_message::messenger::{Destination, OnionMessenger};
50+ /// # use lightning::onion_message::packet::CustomOnionMessageContents;
51+ /// # use lightning::onion_message::blinded_route::BlindedRoute;
4952/// # use lightning::util::logger::{Logger, Record};
50- /// # use lightning::util::ser::{MaybeReadableArgs, Writeable, Writer};
53+ /// # use lightning::util::ser::{Writeable, Writer};
5154/// # use lightning::io;
5255/// # use std::sync::Arc;
5356/// # struct FakeLogger {};
@@ -81,19 +84,11 @@ use crate::prelude::*;
8184/// your_custom_message_type
8285/// }
8386/// }
84- /// impl MaybeReadableArgs<u64> for YourCustomMessage {
85- /// fn read<R: io::Read>(r: &mut R, message_type: u64) -> Result<Option<Self>, DecodeError> {
86- /// # unreachable!()
87- /// // Read your custom onion message of type `message_type` from `r`, or return `None`
88- /// // if the message type is unknown
89- /// }
90- /// }
9187/// // Send a custom onion message to a node id.
9288/// let intermediate_hops = [hop_node_id1, hop_node_id2];
9389/// let reply_path = None;
9490/// # let your_custom_message = YourCustomMessage {};
95- /// let message = OnionMessageContents::Custom(your_custom_message);
96- /// onion_messenger.send_onion_message(&intermediate_hops, Destination::Node(destination_node_id), message, reply_path);
91+ /// onion_messenger.send_custom_onion_message(&intermediate_hops, Destination::Node(destination_node_id), your_custom_message, reply_path);
9792///
9893/// // Create a blinded route to yourself, for someone to send an onion message to.
9994/// # let your_node_id = hop_node_id1;
@@ -104,8 +99,7 @@ use crate::prelude::*;
10499/// # let intermediate_hops = [hop_node_id1, hop_node_id2];
105100/// let reply_path = None;
106101/// # let your_custom_message = YourCustomMessage {};
107- /// let message = OnionMessageContents::Custom(your_custom_message);
108- /// onion_messenger.send_onion_message(&intermediate_hops, Destination::BlindedRoute(blinded_route), message, reply_path);
102+ /// onion_messenger.send_custom_onion_message(&intermediate_hops, Destination::BlindedRoute(blinded_route), your_custom_message, reply_path);
109103/// ```
110104///
111105/// [offers]: <https://github.com/lightning/bolts/pull/798>
@@ -143,7 +137,7 @@ impl Destination {
143137
144138/// Errors that may occur when [sending an onion message].
145139///
146- /// [sending an onion message]: OnionMessenger::send_onion_message
140+ /// [sending an onion message]: OnionMessenger::send_custom_onion_message
147141#[ derive( Debug , PartialEq , Eq ) ]
148142pub enum SendError {
149143 /// Errored computing onion message packet keys.
@@ -178,6 +172,9 @@ pub trait CustomOnionMessageHandler {
178172 type CustomMessage : CustomOnionMessageContents ;
179173 /// Called with the custom message that was received.
180174 fn handle_custom_message ( & self , msg : Self :: CustomMessage ) ;
175+ /// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
176+ /// message type is unknown.
177+ fn read_custom_message < R : io:: Read > ( & self , message_type : u64 , buffer : & mut R ) -> Result < Option < Self :: CustomMessage > , msgs:: DecodeError > ;
181178}
182179
183180impl < Signer : Sign , K : Deref , L : Deref , CMH : Deref > OnionMessenger < Signer , K , L , CMH >
@@ -201,13 +198,19 @@ impl<Signer: Sign, K: Deref, L: Deref, CMH: Deref> OnionMessenger<Signer, K, L,
201198
202199 /// Send an onion message with contents `message` to `destination`, routing it through `intermediate_nodes`.
203200 /// See [`OnionMessenger`] for example usage.
204- pub fn send_onion_message < T : CustomOnionMessageContents > ( & self , intermediate_nodes : & [ PublicKey ] , destination : Destination , message : OnionMessageContents < T > , reply_path : Option < BlindedRoute > ) -> Result < ( ) , SendError > {
201+ pub ( crate ) fn send_onion_message < T : CustomOnionMessageContents > ( & self , intermediate_nodes : & [ PublicKey ] , destination : Destination , msg : OnionMessageContents < T > , reply_path : Option < BlindedRoute > ) -> Result < ( ) , SendError > {
202+ let OnionMessageContents :: Custom ( message) = msg;
203+ self . send_custom_onion_message ( intermediate_nodes, destination, message, reply_path)
204+ }
205+
206+ /// Send an onion message with contents `message` to `destination`, routing it through `intermediate_nodes`.
207+ /// See [`OnionMessenger`] for example usage.
208+ pub fn send_custom_onion_message < T : CustomOnionMessageContents > ( & self , intermediate_nodes : & [ PublicKey ] , destination : Destination , msg : T , reply_path : Option < BlindedRoute > ) -> Result < ( ) , SendError > {
205209 if let Destination :: BlindedRoute ( BlindedRoute { ref blinded_hops, .. } ) = destination {
206210 if blinded_hops. len ( ) < 2 {
207211 return Err ( SendError :: TooFewBlindedHops ) ;
208212 }
209213 }
210- let OnionMessageContents :: Custom ( ref msg) = message;
211214 if msg. tlv_type ( ) < 64 { return Err ( SendError :: InvalidMessage ) }
212215
213216 let blinding_secret_bytes = self . keys_manager . get_secure_random_bytes ( ) ;
@@ -222,7 +225,7 @@ impl<Signer: Sign, K: Deref, L: Deref, CMH: Deref> OnionMessenger<Signer, K, L,
222225 }
223226 } ;
224227 let ( packet_payloads, packet_keys) = packet_payloads_and_keys (
225- & self . secp_ctx , intermediate_nodes, destination, message , reply_path, & blinding_secret)
228+ & self . secp_ctx , intermediate_nodes, destination, OnionMessageContents :: Custom ( msg ) , reply_path, & blinding_secret)
226229 . map_err ( |e| SendError :: Secp256k1 ( e) ) ?;
227230
228231 let prng_seed = self . keys_manager . get_secure_random_bytes ( ) ;
@@ -279,7 +282,7 @@ fn outbound_buffer_full(peer_node_id: &PublicKey, buffer: &HashMap<PublicKey, Ve
279282impl < Signer : Sign , K : Deref , L : Deref , CMH : Deref > OnionMessageHandler for OnionMessenger < Signer , K , L , CMH >
280283 where K :: Target : KeysInterface < Signer = Signer > ,
281284 L :: Target : Logger ,
282- CMH :: Target : CustomOnionMessageHandler ,
285+ CMH :: Target : CustomOnionMessageHandler + Sized ,
283286{
284287 /// Handle an incoming onion message. Currently, if a message was destined for us we will log, but
285288 /// soon we'll delegate the onion message to a handler that can generate invoices or send
@@ -308,8 +311,8 @@ impl<Signer: Sign, K: Deref, L: Deref, CMH: Deref> OnionMessageHandler for Onion
308311 }
309312 }
310313 } ;
311- match onion_utils:: decode_next_hop ( onion_decode_ss, & msg. onion_routing_packet . hop_data [ ..] ,
312- msg. onion_routing_packet . hmac , control_tlvs_ss)
314+ match onion_utils:: decode_next_untagged_hop ( onion_decode_ss, & msg. onion_routing_packet . hop_data [ ..] ,
315+ msg. onion_routing_packet . hmac , ( control_tlvs_ss, & * self . custom_handler ) )
313316 {
314317 Ok ( ( Payload :: Receive :: < <<CMH as Deref >:: Target as CustomOnionMessageHandler >:: CustomMessage > {
315318 message, control_tlvs : ReceiveControlTlvs :: Unblinded ( ReceiveTlvs { path_id } ) , reply_path,
0 commit comments