Skip to content

Commit f72f774

Browse files
committed
WIP: three-hop blinded paths
1 parent 9e73e5e commit f72f774

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ where
349349
&self, recipient: PublicKey, peers: Vec<PublicKey>, entropy_source: &ES,
350350
secp_ctx: &Secp256k1<T>
351351
) -> Result<Vec<BlindedPath>, ()> {
352+
let recipient_node_id = NodeId::from_pubkey(&recipient);
353+
352354
// Limit the number of blinded paths that are computed.
353355
const MAX_PATHS: usize = 3;
354356

@@ -358,15 +360,35 @@ where
358360

359361
let network_graph = self.network_graph.deref().read_only();
360362
let paths = peers.iter()
363+
.map(|pubkey| (pubkey, NodeId::from_pubkey(&pubkey)))
361364
// Limit to peers with announced channels
362-
.filter(|pubkey|
365+
.filter_map(|(pubkey, node_id)|
363366
network_graph
364-
.node(&NodeId::from_pubkey(pubkey))
367+
.node(&node_id)
365368
.map(|info| &info.channels[..])
366-
.map(|channels| channels.len() >= MIN_PEER_CHANNELS)
367-
.unwrap_or(false)
369+
.and_then(|channels| (channels.len() >= MIN_PEER_CHANNELS).then(|| channels))
370+
.map(|channels| (pubkey, node_id, channels))
371+
)
372+
// Pair peers with their other peers
373+
.flat_map(|(pubkey, node_id, channels)|
374+
channels
375+
.iter()
376+
.filter_map(|scid| network_graph.channels().get(scid))
377+
.filter_map(move |info| info
378+
.as_directed_to(&node_id)
379+
.map(|(_, source)| source)
380+
)
381+
.filter(|source| **source != recipient_node_id)
382+
.filter(|source| network_graph
383+
.node(source)
384+
.and_then(|info| info.announcement_info.as_ref())
385+
.map(|info| info.features.supports_onion_messages())
386+
.unwrap_or(false)
387+
)
388+
.filter_map(|source| source.as_pubkey().ok())
389+
.map(move |source_pubkey| (source_pubkey, *pubkey))
368390
)
369-
.map(|pubkey| vec![*pubkey, recipient])
391+
.map(|(source_pubkey, pubkey)| vec![source_pubkey, pubkey, recipient])
370392
.map(|node_pks| BlindedPath::new_for_message(&node_pks, entropy_source, secp_ctx))
371393
.take(MAX_PATHS)
372394
.collect::<Result<Vec<_>, _>>();

0 commit comments

Comments
 (0)