@@ -21,14 +21,15 @@ use crate::ln::msgs::{self, OnionMessageHandler};
21
21
use crate :: ln:: onion_utils;
22
22
use crate :: ln:: peer_handler:: IgnoringMessageHandler ;
23
23
use super :: blinded_route:: { BlindedRoute , ForwardTlvs , ReceiveTlvs } ;
24
- pub use super :: packet:: { CustomOnionMessageContents , OnionMessageContents } ;
24
+ use super :: packet:: { CustomOnionMessageContents , OnionMessageContents } ;
25
25
use super :: packet:: { BIG_PACKET_HOP_DATA_LEN , ForwardControlTlvs , Packet , Payload , ReceiveControlTlvs , SMALL_PACKET_HOP_DATA_LEN } ;
26
26
use super :: utils;
27
27
use crate :: util:: events:: OnionMessageProvider ;
28
28
use crate :: util:: logger:: Logger ;
29
29
use crate :: util:: ser:: Writeable ;
30
30
31
31
use core:: ops:: Deref ;
32
+ use crate :: io;
32
33
use crate :: sync:: { Arc , Mutex } ;
33
34
use crate :: prelude:: * ;
34
35
@@ -45,9 +46,11 @@ use crate::prelude::*;
45
46
/// # use lightning::chain::keysinterface::{InMemorySigner, KeysManager, KeysInterface};
46
47
/// # use lightning::ln::msgs::DecodeError;
47
48
/// # 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;
49
52
/// # use lightning::util::logger::{Logger, Record};
50
- /// # use lightning::util::ser::{MaybeReadableArgs, Writeable, Writer};
53
+ /// # use lightning::util::ser::{Writeable, Writer};
51
54
/// # use lightning::io;
52
55
/// # use std::sync::Arc;
53
56
/// # struct FakeLogger {};
@@ -81,19 +84,11 @@ use crate::prelude::*;
81
84
/// your_custom_message_type
82
85
/// }
83
86
/// }
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
- /// }
91
87
/// // Send a custom onion message to a node id.
92
88
/// let intermediate_hops = [hop_node_id1, hop_node_id2];
93
89
/// let reply_path = None;
94
90
/// # 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);
97
92
///
98
93
/// // Create a blinded route to yourself, for someone to send an onion message to.
99
94
/// # let your_node_id = hop_node_id1;
@@ -104,8 +99,7 @@ use crate::prelude::*;
104
99
/// # let intermediate_hops = [hop_node_id1, hop_node_id2];
105
100
/// let reply_path = None;
106
101
/// # 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);
109
103
/// ```
110
104
///
111
105
/// [offers]: <https://github.com/lightning/bolts/pull/798>
@@ -143,7 +137,7 @@ impl Destination {
143
137
144
138
/// Errors that may occur when [sending an onion message].
145
139
///
146
- /// [sending an onion message]: OnionMessenger::send_onion_message
140
+ /// [sending an onion message]: OnionMessenger::send_custom_onion_message
147
141
#[ derive( Debug , PartialEq , Eq ) ]
148
142
pub enum SendError {
149
143
/// Errored computing onion message packet keys.
@@ -178,6 +172,9 @@ pub trait CustomOnionMessageHandler {
178
172
type CustomMessage : CustomOnionMessageContents ;
179
173
/// Called with the custom message that was received.
180
174
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 > ;
181
178
}
182
179
183
180
impl < 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,
201
198
202
199
/// Send an onion message with contents `message` to `destination`, routing it through `intermediate_nodes`.
203
200
/// 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 > {
205
209
if let Destination :: BlindedRoute ( BlindedRoute { ref blinded_hops, .. } ) = destination {
206
210
if blinded_hops. len ( ) < 2 {
207
211
return Err ( SendError :: TooFewBlindedHops ) ;
208
212
}
209
213
}
210
- let OnionMessageContents :: Custom ( ref msg) = message;
211
214
if msg. tlv_type ( ) < 64 { return Err ( SendError :: InvalidMessage ) }
212
215
213
216
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,
222
225
}
223
226
} ;
224
227
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)
226
229
. map_err ( |e| SendError :: Secp256k1 ( e) ) ?;
227
230
228
231
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
279
282
impl < Signer : Sign , K : Deref , L : Deref , CMH : Deref > OnionMessageHandler for OnionMessenger < Signer , K , L , CMH >
280
283
where K :: Target : KeysInterface < Signer = Signer > ,
281
284
L :: Target : Logger ,
282
- CMH :: Target : CustomOnionMessageHandler ,
285
+ CMH :: Target : CustomOnionMessageHandler + Sized ,
283
286
{
284
287
/// Handle an incoming onion message. Currently, if a message was destined for us we will log, but
285
288
/// 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
308
311
}
309
312
}
310
313
} ;
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 ) )
313
316
{
314
317
Ok ( ( Payload :: Receive :: < <<CMH as Deref >:: Target as CustomOnionMessageHandler >:: CustomMessage > {
315
318
message, control_tlvs : ReceiveControlTlvs :: Unblinded ( ReceiveTlvs { path_id } ) , reply_path,
0 commit comments