Skip to content

Commit 35e3bfb

Browse files
committed
Add CompactMessageRouter and NullMessageRouter
Introduced two new default routers: - `CompactMessageRouter` – for creating compact blinded paths - `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 b0a5e5a commit 35e3bfb

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

lightning/src/onion_message/messenger.rs

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

707+
/// A [`MessageRouter`] that can only route to a directly connected [`Destination`],
708+
/// and tries to create compact blinded path for that purpose.
709+
///
710+
/// # Privacy
711+
///
712+
/// Creating [`BlindedMessagePath`]s may affect privacy since, if a suitable path cannot be found,
713+
/// it will create a one-hop path using the recipient as the introduction node if it is a announced
714+
/// node. Otherwise, there is no way to find a path to the introduction node in order to send a
715+
/// message, and thus an `Err` is returned.
716+
pub struct CompactMessageRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref>
717+
where
718+
L::Target: Logger,
719+
ES::Target: EntropySource,
720+
{
721+
network_graph: G,
722+
entropy_source: ES,
723+
}
724+
725+
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref> CompactMessageRouter<G, L, ES>
726+
where
727+
L::Target: Logger,
728+
ES::Target: EntropySource,
729+
{
730+
/// Creates a [`DefaultMessageRouter`] using the given [`NetworkGraph`].
731+
pub fn new(network_graph: G, entropy_source: ES) -> Self {
732+
Self { network_graph, entropy_source }
733+
}
734+
}
735+
736+
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref> MessageRouter
737+
for CompactMessageRouter<G, L, ES>
738+
where
739+
L::Target: Logger,
740+
ES::Target: EntropySource,
741+
{
742+
fn find_path(
743+
&self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination,
744+
) -> Result<OnionMessagePath, ()> {
745+
DefaultMessageRouter::<G, L, ES>::find_path(&self.network_graph, sender, peers, destination)
746+
}
747+
748+
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
749+
&self, recipient: PublicKey, context: MessageContext, peers: Vec<MessageForwardNode>,
750+
secp_ctx: &Secp256k1<T>,
751+
) -> Result<Vec<BlindedMessagePath>, ()> {
752+
DefaultMessageRouter::create_blinded_paths_from_iter(
753+
&self.network_graph,
754+
recipient,
755+
context,
756+
peers.into_iter(),
757+
&self.entropy_source,
758+
secp_ctx,
759+
true,
760+
)
761+
}
762+
763+
fn create_compact_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
764+
&self, recipient: PublicKey, context: MessageContext, peers: Vec<MessageForwardNode>,
765+
secp_ctx: &Secp256k1<T>,
766+
) -> Result<Vec<BlindedMessagePath>, ()> {
767+
DefaultMessageRouter::create_blinded_paths_from_iter(
768+
&self.network_graph,
769+
recipient,
770+
context,
771+
peers.into_iter(),
772+
&self.entropy_source,
773+
secp_ctx,
774+
true,
775+
)
776+
}
777+
}
778+
779+
/// A special [`MessageRouter`] that can doesn't route.
780+
pub struct NullMessageRouter {}
781+
782+
impl MessageRouter for NullMessageRouter {
783+
fn find_path(
784+
&self, _sender: PublicKey, _peers: Vec<PublicKey>, _destination: Destination,
785+
) -> Result<OnionMessagePath, ()> {
786+
unreachable!()
787+
}
788+
789+
fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
790+
&self, _recipient: PublicKey, _context: MessageContext, _peers: Vec<MessageForwardNode>,
791+
_secp_ctx: &Secp256k1<T>,
792+
) -> Result<Vec<BlindedMessagePath>, ()> {
793+
Ok(vec![])
794+
}
795+
796+
fn create_compact_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
797+
&self, _recipient: PublicKey, _context: MessageContext, _peers: Vec<MessageForwardNode>,
798+
_secp_ctx: &Secp256k1<T>,
799+
) -> Result<Vec<BlindedMessagePath>, ()> {
800+
Ok(vec![])
801+
}
802+
}
803+
707804
/// A path for sending an [`OnionMessage`].
708805
#[derive(Clone)]
709806
pub struct OnionMessagePath {

0 commit comments

Comments
 (0)