@@ -267,6 +267,14 @@ impl Responder {
267
267
reply_path : self . reply_path
268
268
} )
269
269
}
270
+
271
+ /// Creates the appropriate [`ResponseInstruction`] for a given response.
272
+ pub fn respond_with_reply_path < T : OnionMessageContents > ( self , response : T ) -> ResponseInstruction < T > {
273
+ ResponseInstruction :: WithReplyPath ( OnionMessageResponse {
274
+ message : response,
275
+ reply_path : self . reply_path
276
+ } )
277
+ }
270
278
}
271
279
272
280
/// This struct contains the information needed to reply to a received message.
@@ -277,6 +285,8 @@ pub struct OnionMessageResponse<T: OnionMessageContents> {
277
285
278
286
/// `ResponseInstruction` represents instructions for responding to received messages.
279
287
pub enum ResponseInstruction < T : OnionMessageContents > {
288
+ /// Indicates that a response should be sent including a reply path for the receiver to respond back.
289
+ WithReplyPath ( OnionMessageResponse < T > ) ,
280
290
/// Indicates that a response should be sent without including a reply path for the receiver to respond back.
281
291
WithoutReplyPath ( OnionMessageResponse < T > ) ,
282
292
/// Indicates that there's no response to send back.
@@ -837,6 +847,24 @@ where
837
847
. map_err ( |_| SendError :: PathNotFound )
838
848
}
839
849
850
+ fn create_blinded_path ( & self ) -> Result < BlindedPath , SendError > {
851
+ let recipient = self . node_signer
852
+ . get_node_id ( Recipient :: Node )
853
+ . map_err ( |_| SendError :: GetNodeIdFailed ) ?;
854
+ let secp_ctx = & self . secp_ctx ;
855
+
856
+ let peers = self . message_recipients . lock ( ) . unwrap ( )
857
+ . iter ( )
858
+ . filter ( |( _, peer) | matches ! ( peer, OnionMessageRecipient :: ConnectedPeer ( _) ) )
859
+ . map ( |( node_id, _) | * node_id)
860
+ . collect ( ) ;
861
+
862
+ self . message_router
863
+ . create_blinded_paths ( recipient, peers, secp_ctx)
864
+ . and_then ( |paths| paths. into_iter ( ) . next ( ) . ok_or ( ( ) ) )
865
+ . map_err ( |_| SendError :: PathNotFound )
866
+ }
867
+
840
868
fn enqueue_onion_message < T : OnionMessageContents > (
841
869
& self , path : OnionMessagePath , contents : T , reply_path : Option < BlindedPath > ,
842
870
log_suffix : fmt:: Arguments
@@ -892,16 +920,26 @@ where
892
920
pub fn handle_onion_message_response < T : OnionMessageContents > (
893
921
& self , response : ResponseInstruction < T > , message_type : & str , path_id : Option < [ u8 ; 32 ] >
894
922
) {
895
- if let ResponseInstruction :: WithoutReplyPath ( response) = response {
896
- let _ = self . find_path_and_enqueue_onion_message (
897
- response. message , Destination :: BlindedPath ( response. reply_path ) , None ,
898
- format_args ! (
899
- "when responding to {} onion message with path_id {:02x?}" ,
900
- message_type,
901
- path_id
902
- )
903
- ) ;
904
- }
923
+ let ( response, reply_path) = match response {
924
+ ResponseInstruction :: WithReplyPath ( response) => ( response, self . create_blinded_path ( ) . ok ( ) ) ,
925
+ ResponseInstruction :: WithoutReplyPath ( response) => ( response, None ) ,
926
+ ResponseInstruction :: NoResponse => {
927
+ log_trace ! ( self . logger,
928
+ "Missing reply path when responding to {} onion message with path_id {:02x?}" ,
929
+ message_type, path_id) ;
930
+ return
931
+ }
932
+ } ;
933
+
934
+ let _ = self . find_path_and_enqueue_onion_message (
935
+ response. message , Destination :: BlindedPath ( response. reply_path ) , reply_path,
936
+ format_args ! (
937
+ "when responding to {} onion message with path_id {:02x?}" ,
938
+ message_type,
939
+ path_id
940
+ )
941
+ ) ;
942
+
905
943
}
906
944
907
945
#[ cfg( test) ]
0 commit comments