Skip to content

Commit 4f99f01

Browse files
committed
Introduce NodeIdMessageRouter and NullMessageRouter
To make the purpose of each `MessageRouter` implementation unambiguous, this commit sets a direction where the type of `MessageRouter` used deterministically defines the kind of blinded paths created. As a step toward this goal, two new default routers are introduced: - `NodeIdMessageRouter` – creates full-length blinded paths using the peer's node ID. - `NullMessageRouter` – intentionally creates no blinded paths.
1 parent b138165 commit 4f99f01

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,118 @@ where
712712
}
713713
}
714714

715+
/// This message router is similar to [`DefaultMessageRouter`], but it always creates
716+
/// full-length blinded paths, using the peer's [`NodeId`].
717+
///
718+
/// This message router can only route to a directly connected [`Destination`].
719+
///
720+
/// # Privacy
721+
///
722+
/// Creating [`BlindedMessagePath`]s may affect privacy since, if a suitable path cannot be found,
723+
/// it will create a one-hop path using the recipient as the introduction node if it is an announced
724+
/// node. Otherwise, there is no way to find a path to the introduction node in order to send a
725+
/// message, and thus an `Err` is returned.
726+
pub struct NodeIdMessageRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref>
727+
where
728+
L::Target: Logger,
729+
ES::Target: EntropySource,
730+
{
731+
network_graph: G,
732+
entropy_source: ES,
733+
}
734+
735+
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref> NodeIdMessageRouter<G, L, ES>
736+
where
737+
L::Target: Logger,
738+
ES::Target: EntropySource,
739+
{
740+
/// Creates a [`NodeIdMessageRouter`] using the given [`NetworkGraph`].
741+
pub fn new(network_graph: G, entropy_source: ES) -> Self {
742+
Self { network_graph, entropy_source }
743+
}
744+
}
745+
746+
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref> MessageRouter
747+
for NodeIdMessageRouter<G, L, ES>
748+
where
749+
L::Target: Logger,
750+
ES::Target: EntropySource,
751+
{
752+
fn find_path(
753+
&self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination,
754+
) -> Result<OnionMessagePath, ()> {
755+
DefaultMessageRouter::<G, L, ES>::find_path(&self.network_graph, sender, peers, destination)
756+
}
757+
758+
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
759+
&self, recipient: PublicKey, context: MessageContext, peers: Vec<MessageForwardNode>,
760+
secp_ctx: &Secp256k1<T>,
761+
) -> Result<Vec<BlindedMessagePath>, ()> {
762+
DefaultMessageRouter::create_blinded_paths_from_iter(
763+
&self.network_graph,
764+
recipient,
765+
context,
766+
peers.into_iter(),
767+
&self.entropy_source,
768+
secp_ctx,
769+
false,
770+
)
771+
}
772+
773+
fn create_compact_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
774+
&self, recipient: PublicKey, context: MessageContext, peers: Vec<MessageForwardNode>,
775+
secp_ctx: &Secp256k1<T>,
776+
) -> Result<Vec<BlindedMessagePath>, ()> {
777+
DefaultMessageRouter::create_blinded_paths_from_iter(
778+
&self.network_graph,
779+
recipient,
780+
context,
781+
peers.into_iter(),
782+
&self.entropy_source,
783+
secp_ctx,
784+
false,
785+
)
786+
}
787+
}
788+
789+
/// A special [`MessageRouter`] that performs no routing and does not create blinded paths.
790+
/// Its purpose is to enable the creation of [`Offer`]s and [`Refund`]s without blinded paths,
791+
/// where the user's `node_id` is used directly as the [`Destination`].
792+
///
793+
/// # Note
794+
/// [`NullMessageRouter`] **must not** be used as the type parameter for [`ChannelManager`],
795+
/// since [`ChannelManager`] requires a functioning [`MessageRouter`] to create blinded paths,
796+
/// which are necessary for constructing reply paths in onion message communication.
797+
/// However, [`NullMessageRouter`] *can* still be passed as an argument to [`ChannelManager`]
798+
/// methods that accept a router when blinded paths are not needed.
799+
///
800+
/// [`Offer`]: crate::offers::offer::Offer
801+
/// [`Refund`]: crate::offers::refund::Refund
802+
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
803+
pub struct NullMessageRouter {}
804+
805+
impl MessageRouter for NullMessageRouter {
806+
fn find_path(
807+
&self, _sender: PublicKey, _peers: Vec<PublicKey>, _destination: Destination,
808+
) -> Result<OnionMessagePath, ()> {
809+
Err(())
810+
}
811+
812+
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
813+
&self, _recipient: PublicKey, _context: MessageContext, _peers: Vec<MessageForwardNode>,
814+
_secp_ctx: &Secp256k1<T>,
815+
) -> Result<Vec<BlindedMessagePath>, ()> {
816+
Ok(vec![])
817+
}
818+
819+
fn create_compact_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
820+
&self, _recipient: PublicKey, _context: MessageContext, _peers: Vec<MessageForwardNode>,
821+
_secp_ctx: &Secp256k1<T>,
822+
) -> Result<Vec<BlindedMessagePath>, ()> {
823+
Ok(vec![])
824+
}
825+
}
826+
715827
/// A path for sending an [`OnionMessage`].
716828
#[derive(Clone)]
717829
pub struct OnionMessagePath {

0 commit comments

Comments
 (0)