Skip to content

Commit 8231ed5

Browse files
committed
WIP: three-hop blinded paths
1 parent 75fbf9a commit 8231ed5

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
@@ -347,6 +347,8 @@ where
347347
&self, recipient: PublicKey, peers: Vec<PublicKey>, entropy_source: &ES,
348348
secp_ctx: &Secp256k1<T>
349349
) -> Result<Vec<BlindedPath>, ()> {
350+
let recipient_node_id = NodeId::from_pubkey(&recipient);
351+
350352
// Limit the number of blinded paths that are computed.
351353
const MAX_PATHS: usize = 3;
352354

@@ -356,15 +358,35 @@ where
356358

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

0 commit comments

Comments
 (0)