@@ -267,14 +267,27 @@ impl Responder {
267267 }
268268 }
269269
270- /// Creates the appropriate [`ResponseInstruction`] for a given response.
270+ /// Creates a [`ResponseInstruction::WithoutReplyPath`] for a given response.
271+ ///
272+ /// Use when the recipient doesn't need to send back a reply to us.
271273 pub fn respond < T : OnionMessageContents > ( self , response : T ) -> ResponseInstruction < T > {
272274 ResponseInstruction :: WithoutReplyPath ( OnionMessageResponse {
273275 message : response,
274276 reply_path : self . reply_path ,
275277 path_id : self . path_id ,
276278 } )
277279 }
280+
281+ /// Creates a [`ResponseInstruction::WithReplyPath`] for a given response.
282+ ///
283+ /// Use when the recipient needs to send back a reply to us.
284+ pub fn respond_with_reply_path < T : OnionMessageContents > ( self , response : T ) -> ResponseInstruction < T > {
285+ ResponseInstruction :: WithReplyPath ( OnionMessageResponse {
286+ message : response,
287+ reply_path : self . reply_path ,
288+ path_id : self . path_id ,
289+ } )
290+ }
278291}
279292
280293/// This struct contains the information needed to reply to a received message.
@@ -286,6 +299,9 @@ pub struct OnionMessageResponse<T: OnionMessageContents> {
286299
287300/// `ResponseInstruction` represents instructions for responding to received messages.
288301pub enum ResponseInstruction < T : OnionMessageContents > {
302+ /// Indicates that a response should be sent including a reply path for
303+ /// the recipient to respond back.
304+ WithReplyPath ( OnionMessageResponse < T > ) ,
289305 /// Indicates that a response should be sent without including a reply path
290306 /// for the recipient to respond back.
291307 WithoutReplyPath ( OnionMessageResponse < T > ) ,
@@ -971,6 +987,24 @@ where
971987 . map_err ( |_| SendError :: PathNotFound )
972988 }
973989
990+ fn create_blinded_path ( & self ) -> Result < BlindedPath , SendError > {
991+ let recipient = self . node_signer
992+ . get_node_id ( Recipient :: Node )
993+ . map_err ( |_| SendError :: GetNodeIdFailed ) ?;
994+ let secp_ctx = & self . secp_ctx ;
995+
996+ let peers = self . message_recipients . lock ( ) . unwrap ( )
997+ . iter ( )
998+ . filter ( |( _, peer) | matches ! ( peer, OnionMessageRecipient :: ConnectedPeer ( _) ) )
999+ . map ( |( node_id, _) | * node_id)
1000+ . collect ( ) ;
1001+
1002+ self . message_router
1003+ . create_blinded_paths ( recipient, peers, secp_ctx)
1004+ . and_then ( |paths| paths. into_iter ( ) . next ( ) . ok_or ( ( ) ) )
1005+ . map_err ( |_| SendError :: PathNotFound )
1006+ }
1007+
9741008 fn enqueue_onion_message < T : OnionMessageContents > (
9751009 & self , path : OnionMessagePath , contents : T , reply_path : Option < BlindedPath > ,
9761010 log_suffix : fmt:: Arguments
@@ -1047,18 +1081,37 @@ where
10471081 /// enqueueing any response for sending.
10481082 pub fn handle_onion_message_response < T : OnionMessageContents > (
10491083 & self , response : ResponseInstruction < T >
1050- ) {
1051- if let ResponseInstruction :: WithoutReplyPath ( response) = response {
1052- let message_type = response. message . msg_type ( ) ;
1053- let _ = self . find_path_and_enqueue_onion_message (
1054- response. message , Destination :: BlindedPath ( response. reply_path ) , None ,
1055- format_args ! (
1056- "when responding with {} to an onion message with path_id {:02x?}" ,
1057- message_type,
1058- response. path_id
1059- )
1060- ) ;
1061- }
1084+ ) -> Result < Option < SendSuccess > , SendError > {
1085+ let ( response, create_reply_path) = match response {
1086+ ResponseInstruction :: WithReplyPath ( response) => ( response, true ) ,
1087+ ResponseInstruction :: WithoutReplyPath ( response) => ( response, false ) ,
1088+ ResponseInstruction :: NoResponse => return Ok ( None ) ,
1089+ } ;
1090+
1091+ let message_type = response. message . msg_type ( ) ;
1092+ let reply_path = if create_reply_path {
1093+ match self . create_blinded_path ( ) {
1094+ Ok ( reply_path) => Some ( reply_path) ,
1095+ Err ( err) => {
1096+ log_trace ! (
1097+ self . logger,
1098+ "Failed to create reply_path when responding with {} to an onion message \
1099+ with path_id {:02x?}: {:?}",
1100+ message_type, response. path_id, err
1101+ ) ;
1102+ return Err ( err) ;
1103+ }
1104+ }
1105+ } else { None } ;
1106+
1107+ self . find_path_and_enqueue_onion_message (
1108+ response. message , Destination :: BlindedPath ( response. reply_path ) , reply_path,
1109+ format_args ! (
1110+ "when responding with {} to an onion message with path_id {:02x?}" ,
1111+ message_type,
1112+ response. path_id
1113+ )
1114+ ) . map ( |result| Some ( result) )
10621115 }
10631116
10641117 #[ cfg( test) ]
@@ -1164,14 +1217,14 @@ where
11641217 |reply_path| Responder :: new ( reply_path, path_id)
11651218 ) ;
11661219 let response_instructions = self . offers_handler . handle_message ( msg, responder) ;
1167- self . handle_onion_message_response ( response_instructions) ;
1220+ let _ = self . handle_onion_message_response ( response_instructions) ;
11681221 } ,
11691222 ParsedOnionMessageContents :: Custom ( msg) => {
11701223 let responder = reply_path. map (
11711224 |reply_path| Responder :: new ( reply_path, path_id)
11721225 ) ;
11731226 let response_instructions = self . custom_handler . handle_custom_message ( msg, responder) ;
1174- self . handle_onion_message_response ( response_instructions) ;
1227+ let _ = self . handle_onion_message_response ( response_instructions) ;
11751228 } ,
11761229 }
11771230 } ,
0 commit comments