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