@@ -265,14 +265,31 @@ impl Responder {
265
265
}
266
266
}
267
267
268
- /// Creates the appropriate [`ResponseInstruction`] for a given response.
268
+ /// Creates a [`ResponseInstruction::WithoutReplyPath`] variant of [`ResponseInstruction`]
269
+ /// for a given response.
270
+ ///
271
+ /// [`ResponseInstruction::WithoutReplyPath`] indicates that the response should be sent
272
+ /// without creating a reply path for the receiver to use to send back a response to us.
269
273
pub fn respond < T : OnionMessageContents > ( self , response : T ) -> ResponseInstruction < T > {
270
274
ResponseInstruction :: WithoutReplyPath ( OnionMessageResponse {
271
275
message : response,
272
276
reply_path : self . reply_path ,
273
277
path_id : self . path_id ,
274
278
} )
275
279
}
280
+
281
+ /// Creates a [`ResponseInstruction::WithReplyPath`] variant of [`ResponseInstruction`]
282
+ /// for a given response.
283
+ ///
284
+ /// [`ResponseInstruction::WithReplyPath`] indicates that the response should be sent with
285
+ /// a reply path for the receiver to use to send back a response to us.
286
+ pub fn respond_with_reply_path < T : OnionMessageContents > ( self , response : T ) -> ResponseInstruction < T > {
287
+ ResponseInstruction :: WithReplyPath ( OnionMessageResponse {
288
+ message : response,
289
+ reply_path : self . reply_path ,
290
+ path_id : self . path_id ,
291
+ } )
292
+ }
276
293
}
277
294
278
295
/// This struct contains the information needed to reply to a received message.
@@ -284,6 +301,9 @@ pub struct OnionMessageResponse<T: OnionMessageContents> {
284
301
285
302
/// `ResponseInstruction` represents instructions for responding to received messages.
286
303
pub enum ResponseInstruction < T : OnionMessageContents > {
304
+ /// Indicates that a response should be sent including a reply path for
305
+ /// the recipient to respond back.
306
+ WithReplyPath ( OnionMessageResponse < T > ) ,
287
307
/// Indicates that a response should be sent without including a reply path
288
308
/// for the recipient to respond back.
289
309
WithoutReplyPath ( OnionMessageResponse < T > ) ,
@@ -926,6 +946,24 @@ where
926
946
. map_err ( |_| SendError :: PathNotFound )
927
947
}
928
948
949
+ fn create_blinded_path ( & self ) -> Result < BlindedPath , SendError > {
950
+ let recipient = self . node_signer
951
+ . get_node_id ( Recipient :: Node )
952
+ . map_err ( |_| SendError :: GetNodeIdFailed ) ?;
953
+ let secp_ctx = & self . secp_ctx ;
954
+
955
+ let peers = self . message_recipients . lock ( ) . unwrap ( )
956
+ . iter ( )
957
+ . filter ( |( _, peer) | matches ! ( peer, OnionMessageRecipient :: ConnectedPeer ( _) ) )
958
+ . map ( |( node_id, _) | * node_id)
959
+ . collect ( ) ;
960
+
961
+ self . message_router
962
+ . create_blinded_paths ( recipient, peers, secp_ctx)
963
+ . and_then ( |paths| paths. into_iter ( ) . next ( ) . ok_or ( ( ) ) )
964
+ . map_err ( |_| SendError :: PathNotFound )
965
+ }
966
+
929
967
fn enqueue_onion_message < T : OnionMessageContents > (
930
968
& self , path : OnionMessagePath , contents : T , reply_path : Option < BlindedPath > ,
931
969
log_suffix : fmt:: Arguments
@@ -982,17 +1020,21 @@ where
982
1020
pub fn handle_onion_message_response < T : OnionMessageContents > (
983
1021
& self , response : ResponseInstruction < T >
984
1022
) {
985
- if let ResponseInstruction :: WithoutReplyPath ( response) = response {
986
- let message_type = response. message . msg_type ( ) ;
987
- let _ = self . find_path_and_enqueue_onion_message (
988
- response. message , Destination :: BlindedPath ( response. reply_path ) , None ,
989
- format_args ! (
990
- "when responding with {} to an onion message with path_id {:02x?}" ,
991
- message_type,
992
- response. path_id
993
- )
994
- ) ;
995
- }
1023
+ let ( response, reply_path) = match response {
1024
+ ResponseInstruction :: WithReplyPath ( response) => ( response, self . create_blinded_path ( ) . ok ( ) ) ,
1025
+ ResponseInstruction :: WithoutReplyPath ( response) => ( response, None ) ,
1026
+ ResponseInstruction :: NoResponse => return ,
1027
+ } ;
1028
+
1029
+ let message_type = response. message . msg_type ( ) ;
1030
+ let _ = self . find_path_and_enqueue_onion_message (
1031
+ response. message , Destination :: BlindedPath ( response. reply_path ) , reply_path,
1032
+ format_args ! (
1033
+ "when responding with {} to an onion message with path_id {:02x?}" ,
1034
+ message_type,
1035
+ response. path_id
1036
+ )
1037
+ ) ;
996
1038
}
997
1039
998
1040
#[ cfg( test) ]
0 commit comments