@@ -273,6 +273,15 @@ impl Responder {
273
273
path_id : self . path_id ,
274
274
} )
275
275
}
276
+
277
+ /// Creates the appropriate [`ResponseInstruction`] for a given response.
278
+ pub fn respond_with_reply_path < T : OnionMessageContents > ( self , response : T ) -> ResponseInstruction < T > {
279
+ ResponseInstruction :: WithReplyPath ( OnionMessageResponse {
280
+ message : response,
281
+ reply_path : self . reply_path ,
282
+ path_id : self . path_id ,
283
+ } )
284
+ }
276
285
}
277
286
278
287
/// This struct contains the information needed to reply to a received message.
@@ -284,6 +293,9 @@ pub struct OnionMessageResponse<T: OnionMessageContents> {
284
293
285
294
/// `ResponseInstruction` represents instructions for responding to received messages.
286
295
pub enum ResponseInstruction < T : OnionMessageContents > {
296
+ /// Indicates that a response should be sent including a reply path for
297
+ /// the recipient to respond back.
298
+ WithReplyPath ( OnionMessageResponse < T > ) ,
287
299
/// Indicates that a response should be sent without including a reply path
288
300
/// for the recipient to respond back.
289
301
WithoutReplyPath ( OnionMessageResponse < T > ) ,
@@ -926,6 +938,24 @@ where
926
938
. map_err ( |_| SendError :: PathNotFound )
927
939
}
928
940
941
+ fn create_blinded_path ( & self ) -> Result < BlindedPath , SendError > {
942
+ let recipient = self . node_signer
943
+ . get_node_id ( Recipient :: Node )
944
+ . map_err ( |_| SendError :: GetNodeIdFailed ) ?;
945
+ let secp_ctx = & self . secp_ctx ;
946
+
947
+ let peers = self . message_recipients . lock ( ) . unwrap ( )
948
+ . iter ( )
949
+ . filter ( |( _, peer) | matches ! ( peer, OnionMessageRecipient :: ConnectedPeer ( _) ) )
950
+ . map ( |( node_id, _) | * node_id)
951
+ . collect ( ) ;
952
+
953
+ self . message_router
954
+ . create_blinded_paths ( recipient, peers, secp_ctx)
955
+ . and_then ( |paths| paths. into_iter ( ) . next ( ) . ok_or ( ( ) ) )
956
+ . map_err ( |_| SendError :: PathNotFound )
957
+ }
958
+
929
959
fn enqueue_onion_message < T : OnionMessageContents > (
930
960
& self , path : OnionMessagePath , contents : T , reply_path : Option < BlindedPath > ,
931
961
log_suffix : fmt:: Arguments
@@ -982,17 +1012,21 @@ where
982
1012
pub fn handle_onion_message_response < T : OnionMessageContents > (
983
1013
& self , response : ResponseInstruction < T >
984
1014
) {
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
- }
1015
+ let ( response, reply_path) = match response {
1016
+ ResponseInstruction :: WithReplyPath ( response) => ( response, self . create_blinded_path ( ) . ok ( ) ) ,
1017
+ ResponseInstruction :: WithoutReplyPath ( response) => ( response, None ) ,
1018
+ ResponseInstruction :: NoResponse => return ,
1019
+ } ;
1020
+
1021
+ let message_type = response. message . msg_type ( ) ;
1022
+ let _ = self . find_path_and_enqueue_onion_message (
1023
+ response. message , Destination :: BlindedPath ( response. reply_path ) , reply_path,
1024
+ format_args ! (
1025
+ "when responding to {} onion message with path_id {:02x?}" ,
1026
+ message_type,
1027
+ response. path_id
1028
+ )
1029
+ ) ;
996
1030
}
997
1031
998
1032
#[ cfg( test) ]
0 commit comments