Skip to content

Commit 2486093

Browse files
committed
Add NodeIdMessageRouter and NullMessageRouter
Introduced two new default routers: - `NodeIdMessageRouter` – for creating full-length blinded paths using peer's node id. - `NullMessageRouter` – for intentionally creating no blinded paths This makes the purpose of each router explicit and ensures their functions remain unique. It also builds on the flexibility introduced in the previous commit, where `create_blinded_paths` was updated to support a MessageForwardNode.
1 parent 7469bcf commit 2486093

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,114 @@ where
704704
}
705705
}
706706

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

0 commit comments

Comments
 (0)