@@ -23,7 +23,8 @@ use crate::ln::features::{InitFeatures, NodeFeatures};
23
23
use crate :: ln:: msgs:: { self , OnionMessageHandler } ;
24
24
use crate :: ln:: onion_utils;
25
25
use crate :: ln:: peer_handler:: IgnoringMessageHandler ;
26
- pub use super :: packet:: { CustomOnionMessageContents , ParsedOnionMessageContents } ;
26
+ pub use super :: packet:: OnionMessageContents ;
27
+ use super :: packet:: ParsedOnionMessageContents ;
27
28
use super :: offers:: OffersMessageHandler ;
28
29
use super :: packet:: { BIG_PACKET_HOP_DATA_LEN , ForwardControlTlvs , Packet , Payload , ReceiveControlTlvs , SMALL_PACKET_HOP_DATA_LEN } ;
29
30
use crate :: util:: logger:: Logger ;
@@ -60,7 +61,7 @@ use crate::prelude::*;
60
61
/// # use lightning::blinded_path::BlindedPath;
61
62
/// # use lightning::sign::KeysManager;
62
63
/// # use lightning::ln::peer_handler::IgnoringMessageHandler;
63
- /// # use lightning::onion_message::{CustomOnionMessageContents , Destination, MessageRouter, ParsedOnionMessageContents , OnionMessagePath, OnionMessenger};
64
+ /// # use lightning::onion_message::{OnionMessageContents , Destination, MessageRouter, OnionMessagePath, OnionMessenger};
64
65
/// # use lightning::util::logger::{Logger, Record};
65
66
/// # use lightning::util::ser::{Writeable, Writer};
66
67
/// # use lightning::io;
@@ -101,7 +102,7 @@ use crate::prelude::*;
101
102
/// // Write your custom onion message to `w`
102
103
/// }
103
104
/// }
104
- /// impl CustomOnionMessageContents for YourCustomMessage {
105
+ /// impl OnionMessageContents for YourCustomMessage {
105
106
/// fn tlv_type(&self) -> u64 {
106
107
/// # let your_custom_message_type = 42;
107
108
/// your_custom_message_type
@@ -113,8 +114,7 @@ use crate::prelude::*;
113
114
/// destination: Destination::Node(destination_node_id),
114
115
/// };
115
116
/// let reply_path = None;
116
- /// # let your_custom_message = YourCustomMessage {};
117
- /// let message = ParsedOnionMessageContents::Custom(your_custom_message);
117
+ /// # let message = YourCustomMessage {};
118
118
/// onion_messenger.send_onion_message(path, message, reply_path);
119
119
///
120
120
/// // Create a blinded path to yourself, for someone to send an onion message to.
@@ -128,8 +128,7 @@ use crate::prelude::*;
128
128
/// destination: Destination::BlindedPath(blinded_path),
129
129
/// };
130
130
/// let reply_path = None;
131
- /// # let your_custom_message = YourCustomMessage {};
132
- /// let message = ParsedOnionMessageContents::Custom(your_custom_message);
131
+ /// # let message = YourCustomMessage {};
133
132
/// onion_messenger.send_onion_message(path, message, reply_path);
134
133
/// ```
135
134
///
@@ -249,7 +248,7 @@ pub enum SendError {
249
248
pub trait CustomOnionMessageHandler {
250
249
/// The message known to the handler. To support multiple message types, you may want to make this
251
250
/// an enum with a variant for each supported message.
252
- type CustomMessage : CustomOnionMessageContents ;
251
+ type CustomMessage : OnionMessageContents ;
253
252
254
253
/// Called with the custom message that was received, returning a response to send, if any.
255
254
fn handle_custom_message ( & self , msg : Self :: CustomMessage ) -> Option < Self :: CustomMessage > ;
@@ -292,9 +291,8 @@ where
292
291
/// Send an onion message with contents `message` to the destination of `path`.
293
292
///
294
293
/// See [`OnionMessenger`] for example usage.
295
- pub fn send_onion_message < T : CustomOnionMessageContents > (
296
- & self , path : OnionMessagePath , message : ParsedOnionMessageContents < T > ,
297
- reply_path : Option < BlindedPath >
294
+ pub fn send_onion_message < T : OnionMessageContents > (
295
+ & self , path : OnionMessagePath , message : T , reply_path : Option < BlindedPath >
298
296
) -> Result < ( ) , SendError > {
299
297
let OnionMessagePath { intermediate_nodes, mut destination } = path;
300
298
if let Destination :: BlindedPath ( BlindedPath { ref blinded_hops, .. } ) = destination {
@@ -348,9 +346,25 @@ where
348
346
}
349
347
}
350
348
351
- fn find_path_and_enqueue_onion_message < T : CustomOnionMessageContents > (
352
- & self , contents : ParsedOnionMessageContents < T > , destination : Destination ,
353
- log_suffix : fmt:: Arguments
349
+ fn handle_onion_message_response < T : OnionMessageContents > (
350
+ & self , response : Option < T > , reply_path : Option < BlindedPath > , log_suffix : fmt:: Arguments
351
+ ) {
352
+ if let Some ( response) = response {
353
+ match reply_path {
354
+ Some ( reply_path) => {
355
+ self . find_path_and_enqueue_onion_message (
356
+ response, Destination :: BlindedPath ( reply_path) , log_suffix
357
+ ) ;
358
+ } ,
359
+ None => {
360
+ log_trace ! ( self . logger, "Missing reply path {}" , log_suffix) ;
361
+ } ,
362
+ }
363
+ }
364
+ }
365
+
366
+ fn find_path_and_enqueue_onion_message < T : OnionMessageContents > (
367
+ & self , contents : T , destination : Destination , log_suffix : fmt:: Arguments
354
368
) {
355
369
let sender = match self . node_signer . get_node_id ( Recipient :: Node ) {
356
370
Ok ( node_id) => node_id,
@@ -454,41 +468,32 @@ where
454
468
onion_decode_ss, & msg. onion_routing_packet . hop_data [ ..] , msg. onion_routing_packet . hmac ,
455
469
( control_tlvs_ss, & * self . custom_handler , & * self . logger )
456
470
) {
457
- Ok ( ( Payload :: Receive :: < << CMH as Deref >:: Target as CustomOnionMessageHandler >:: CustomMessage > {
471
+ Ok ( ( Payload :: Receive :: < ParsedOnionMessageContents < << CMH as Deref >:: Target as CustomOnionMessageHandler >:: CustomMessage > > {
458
472
message, control_tlvs : ReceiveControlTlvs :: Unblinded ( ReceiveTlvs { path_id } ) , reply_path,
459
473
} , None ) ) => {
460
474
log_trace!( self . logger,
461
475
"Received an onion message with path_id {:02x?} and {} reply_path" ,
462
476
path_id, if reply_path. is_some ( ) { "a" } else { "no" } ) ;
463
477
464
- let response = match message {
478
+ match message {
465
479
ParsedOnionMessageContents :: Offers ( msg) => {
466
- self . offers_handler . handle_message ( msg)
467
- . map ( |msg| ParsedOnionMessageContents :: Offers ( msg) )
480
+ let response = self . offers_handler . handle_message ( msg) ;
481
+ self . handle_onion_message_response (
482
+ response, reply_path, format_args ! (
483
+ "when responding to Offers onion message with path_id {:02x?}" ,
484
+ path_id
485
+ )
486
+ ) ;
468
487
} ,
469
488
ParsedOnionMessageContents :: Custom ( msg) => {
470
- self . custom_handler . handle_custom_message ( msg)
471
- . map ( |msg| ParsedOnionMessageContents :: Custom ( msg) )
472
- } ,
473
- } ;
474
-
475
- if let Some ( response) = response {
476
- match reply_path {
477
- Some ( reply_path) => {
478
- self . find_path_and_enqueue_onion_message (
479
- response, Destination :: BlindedPath ( reply_path) , format_args ! (
480
- "when responding to onion message with path_id {:02x?}" , path_id
481
- )
482
- ) ;
483
- } ,
484
- None => {
485
- log_trace ! (
486
- self . logger,
487
- "Missing reply path when responding to onion message with path_id {:02x?}" ,
489
+ let response = self . custom_handler . handle_custom_message ( msg) ;
490
+ self . handle_onion_message_response (
491
+ response, reply_path, format_args ! (
492
+ "when responding to Custom onion message with path_id {:02x?}" ,
488
493
path_id
489
- ) ;
490
- } ,
491
- }
494
+ )
495
+ ) ;
496
+ } ,
492
497
}
493
498
} ,
494
499
Ok ( ( Payload :: Forward ( ForwardControlTlvs :: Unblinded ( ForwardTlvs {
@@ -629,9 +634,9 @@ pub type SimpleRefOnionMessenger<'a, 'b, 'c, L> = OnionMessenger<
629
634
630
635
/// Construct onion packet payloads and keys for sending an onion message along the given
631
636
/// `unblinded_path` to the given `destination`.
632
- fn packet_payloads_and_keys < T : CustomOnionMessageContents , S : secp256k1:: Signing + secp256k1:: Verification > (
633
- secp_ctx : & Secp256k1 < S > , unblinded_path : & [ PublicKey ] , destination : Destination ,
634
- message : ParsedOnionMessageContents < T > , mut reply_path : Option < BlindedPath > , session_priv : & SecretKey
637
+ fn packet_payloads_and_keys < T : OnionMessageContents , S : secp256k1:: Signing + secp256k1:: Verification > (
638
+ secp_ctx : & Secp256k1 < S > , unblinded_path : & [ PublicKey ] , destination : Destination , message : T ,
639
+ mut reply_path : Option < BlindedPath > , session_priv : & SecretKey
635
640
) -> Result < ( Vec < ( Payload < T > , [ u8 ; 32 ] ) > , Vec < onion_utils:: OnionKeys > ) , secp256k1:: Error > {
636
641
let num_hops = unblinded_path. len ( ) + destination. num_hops ( ) ;
637
642
let mut payloads = Vec :: with_capacity ( num_hops) ;
@@ -707,7 +712,7 @@ fn packet_payloads_and_keys<T: CustomOnionMessageContents, S: secp256k1::Signing
707
712
}
708
713
709
714
/// Errors if the serialized payload size exceeds onion_message::BIG_PACKET_HOP_DATA_LEN
710
- fn construct_onion_message_packet < T : CustomOnionMessageContents > ( payloads : Vec < ( Payload < T > , [ u8 ; 32 ] ) > , onion_keys : Vec < onion_utils:: OnionKeys > , prng_seed : [ u8 ; 32 ] ) -> Result < Packet , ( ) > {
715
+ fn construct_onion_message_packet < T : OnionMessageContents > ( payloads : Vec < ( Payload < T > , [ u8 ; 32 ] ) > , onion_keys : Vec < onion_utils:: OnionKeys > , prng_seed : [ u8 ; 32 ] ) -> Result < Packet , ( ) > {
711
716
// Spec rationale:
712
717
// "`len` allows larger messages to be sent than the standard 1300 bytes allowed for an HTLC
713
718
// onion, but this should be used sparingly as it is reduces anonymity set, hence the
0 commit comments