@@ -40,7 +40,7 @@ use crate::ln::channelmanager::Verification;
4040use crate :: ln:: msgs:: {
4141 self , BaseMessageHandler , MessageSendEvent , OnionMessage , OnionMessageHandler , SocketAddress ,
4242} ;
43- use crate :: ln:: onion_utils;
43+ use crate :: ln:: { inbound_payment , onion_utils} ;
4444use crate :: routing:: gossip:: { NetworkGraph , NodeId , ReadOnlyNetworkGraph } ;
4545use crate :: sign:: { EntropySource , NodeSigner , Recipient } ;
4646use crate :: types:: features:: { InitFeatures , NodeFeatures } ;
@@ -541,6 +541,7 @@ where
541541{
542542 network_graph : G ,
543543 entropy_source : ES ,
544+ expanded_key : inbound_payment:: ExpandedKey ,
544545}
545546
546547impl < G : Deref < Target = NetworkGraph < L > > , L : Deref , ES : Deref > DefaultMessageRouter < G , L , ES >
@@ -549,16 +550,19 @@ where
549550 ES :: Target : EntropySource ,
550551{
551552 /// Creates a [`DefaultMessageRouter`] using the given [`NetworkGraph`].
552- pub fn new ( network_graph : G , entropy_source : ES ) -> Self {
553- Self { network_graph, entropy_source }
553+ pub fn new (
554+ network_graph : G , entropy_source : ES , expanded_key : inbound_payment:: ExpandedKey ,
555+ ) -> Self {
556+ Self { network_graph, entropy_source, expanded_key }
554557 }
555558
556559 fn create_blinded_paths_from_iter <
557560 I : ExactSizeIterator < Item = MessageForwardNode > ,
558561 T : secp256k1:: Signing + secp256k1:: Verification ,
559562 > (
560563 network_graph : & G , recipient : PublicKey , context : MessageContext , peers : I ,
561- entropy_source : & ES , secp_ctx : & Secp256k1 < T > , compact_paths : bool ,
564+ entropy_source : & ES , expanded_key : & inbound_payment:: ExpandedKey , secp_ctx : & Secp256k1 < T > ,
565+ compact_paths : bool ,
562566 ) -> Result < Vec < BlindedMessagePath > , ( ) > {
563567 // Limit the number of blinded paths that are computed.
564568 const MAX_PATHS : usize = 3 ;
@@ -567,6 +571,21 @@ where
567571 // recipient's node_id.
568572 const MIN_PEER_CHANNELS : usize = 3 ;
569573
574+ // Add a random number (0 to 5) of dummy hops to each non-compact blinded path
575+ // to make it harder to infer the recipient's position.
576+ //
577+ // # Note on compact paths:
578+ //
579+ // Compact paths are optimized for minimal size. Adding dummy hops to them
580+ // would increase their size and negate their primary advantage.
581+ // Therefore, we avoid adding dummy hops to compact paths.
582+ let dummy_hops_count = if compact_paths {
583+ 0
584+ } else {
585+ let random_byte = entropy_source. get_secure_random_bytes ( ) [ 0 ] ;
586+ random_byte % 6
587+ } ;
588+
570589 let network_graph = network_graph. deref ( ) . read_only ( ) ;
571590 let is_recipient_announced =
572591 network_graph. nodes ( ) . contains_key ( & NodeId :: from_pubkey ( & recipient) ) ;
@@ -597,7 +616,15 @@ where
597616 let paths = peer_info
598617 . into_iter ( )
599618 . map ( |( peer, _, _) | {
600- BlindedMessagePath :: new ( & [ peer] , recipient, context. clone ( ) , entropy, secp_ctx)
619+ BlindedMessagePath :: new_with_dummy_hops (
620+ & [ peer] ,
621+ dummy_hops_count,
622+ recipient,
623+ context. clone ( ) ,
624+ entropy,
625+ expanded_key,
626+ secp_ctx,
627+ )
601628 } )
602629 . take ( MAX_PATHS )
603630 . collect :: < Result < Vec < _ > , _ > > ( ) ;
@@ -666,7 +693,7 @@ where
666693
667694 pub ( crate ) fn create_blinded_paths < T : secp256k1:: Signing + secp256k1:: Verification > (
668695 network_graph : & G , recipient : PublicKey , context : MessageContext , peers : Vec < PublicKey > ,
669- entropy_source : & ES , secp_ctx : & Secp256k1 < T > ,
696+ entropy_source : & ES , expanded_key : & inbound_payment :: ExpandedKey , secp_ctx : & Secp256k1 < T > ,
670697 ) -> Result < Vec < BlindedMessagePath > , ( ) > {
671698 let peers =
672699 peers. into_iter ( ) . map ( |node_id| MessageForwardNode { node_id, short_channel_id : None } ) ;
@@ -676,21 +703,24 @@ where
676703 context,
677704 peers. into_iter ( ) ,
678705 entropy_source,
706+ expanded_key,
679707 secp_ctx,
680708 false ,
681709 )
682710 }
683711
684712 pub ( crate ) fn create_compact_blinded_paths < T : secp256k1:: Signing + secp256k1:: Verification > (
685713 network_graph : & G , recipient : PublicKey , context : MessageContext ,
686- peers : Vec < MessageForwardNode > , entropy_source : & ES , secp_ctx : & Secp256k1 < T > ,
714+ peers : Vec < MessageForwardNode > , entropy_source : & ES ,
715+ expanded_key : & inbound_payment:: ExpandedKey , secp_ctx : & Secp256k1 < T > ,
687716 ) -> Result < Vec < BlindedMessagePath > , ( ) > {
688717 Self :: create_blinded_paths_from_iter (
689718 network_graph,
690719 recipient,
691720 context,
692721 peers. into_iter ( ) ,
693722 entropy_source,
723+ expanded_key,
694724 secp_ctx,
695725 true ,
696726 )
@@ -719,6 +749,7 @@ where
719749 context,
720750 peers,
721751 & self . entropy_source ,
752+ & self . expanded_key ,
722753 secp_ctx,
723754 )
724755 }
@@ -733,6 +764,7 @@ where
733764 context,
734765 peers,
735766 & self . entropy_source ,
767+ & self . expanded_key ,
736768 secp_ctx,
737769 )
738770 }
0 commit comments