Skip to content

Commit 88c21a5

Browse files
committed
Introduce ResponseInstruction::WithReplyPath variant.
1. Introduce a new function in OnionMessenger to create blinded paths. 2. Use it in handle_onion_message_response to create a reply_path for the right variant and use it in onion_message.
1 parent 8a0f468 commit 88c21a5

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::blinded_path::BlindedPath;
1919
use crate::blinded_path::message::{advance_path_by_one, ForwardTlvs, ReceiveTlvs};
2020
use crate::blinded_path::utils;
2121
use crate::events::{Event, EventHandler, EventsProvider};
22+
use crate::offers::parse::Bolt12SemanticError;
2223
use crate::sign::{EntropySource, NodeSigner, Recipient};
2324
use crate::ln::features::{InitFeatures, NodeFeatures};
2425
use crate::ln::msgs::{self, OnionMessage, OnionMessageHandler, SocketAddress};
@@ -267,6 +268,14 @@ impl Responder {
267268
reply_path: self.reply_path
268269
})
269270
}
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+
}
270279
}
271280

272281
/// This struct contains the information needed to reply to a received message.
@@ -277,6 +286,8 @@ pub struct OnionMessageResponse<T: OnionMessageContents> {
277286

278287
/// `ResponseInstruction` represents instructions for responding to received messages.
279288
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>),
280291
/// Indicates that a response should be sent without including a reply path for the receiver to respond back.
281292
WithoutReplyPath(OnionMessageResponse<T>),
282293
/// Indicates that there's no response to send back.
@@ -837,6 +848,24 @@ where
837848
.map_err(|_| SendError::PathNotFound)
838849
}
839850

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+
840869
fn enqueue_onion_message<T: OnionMessageContents>(
841870
&self, path: OnionMessagePath, contents: T, reply_path: Option<BlindedPath>,
842871
log_suffix: fmt::Arguments
@@ -890,16 +919,26 @@ where
890919
pub fn handle_onion_message_response<T: OnionMessageContents>(
891920
&self, response: ResponseInstruction<T>, message_type: &str, path_id: Option<[u8; 32]>
892921
) {
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+
903942
}
904943

905944
#[cfg(test)]

0 commit comments

Comments
 (0)