@@ -14,23 +14,23 @@ use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
14
14
#[ allow( unused_imports) ]
15
15
use crate :: prelude:: * ;
16
16
17
- use bitcoin:: hashes:: hmac:: Hmac ;
18
- use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
19
- use crate :: blinded_path:: { BlindedHop , BlindedPath , Direction , IntroductionNode , NodeIdLookUp } ;
20
17
use crate :: blinded_path:: utils;
18
+ use crate :: blinded_path:: { BlindedHop , BlindedPath , Direction , IntroductionNode , NodeIdLookUp } ;
19
+ use crate :: crypto:: streams:: ChaChaPolyReadAdapter ;
21
20
use crate :: io;
22
21
use crate :: io:: Cursor ;
23
22
use crate :: ln:: channelmanager:: PaymentId ;
24
23
use crate :: ln:: msgs:: DecodeError ;
25
24
use crate :: ln:: onion_utils;
26
- use crate :: types:: payment:: PaymentHash ;
27
25
use crate :: offers:: nonce:: Nonce ;
28
26
use crate :: onion_message:: packet:: ControlTlvs ;
29
27
use crate :: routing:: gossip:: { NodeId , ReadOnlyNetworkGraph } ;
30
28
use crate :: sign:: { EntropySource , NodeSigner , Recipient } ;
31
- use crate :: crypto :: streams :: ChaChaPolyReadAdapter ;
29
+ use crate :: types :: payment :: PaymentHash ;
32
30
use crate :: util:: scid_utils;
33
31
use crate :: util:: ser:: { FixedLengthReader , LengthReadableArgs , Readable , Writeable , Writer } ;
32
+ use bitcoin:: hashes:: hmac:: Hmac ;
33
+ use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
34
34
35
35
use core:: mem;
36
36
use core:: ops:: Deref ;
@@ -55,8 +55,12 @@ impl Readable for BlindedMessagePath {
55
55
impl BlindedMessagePath {
56
56
/// Create a one-hop blinded path for a message.
57
57
pub fn one_hop < ES : Deref , T : secp256k1:: Signing + secp256k1:: Verification > (
58
- recipient_node_id : PublicKey , context : MessageContext , entropy_source : ES , secp_ctx : & Secp256k1 < T >
59
- ) -> Result < Self , ( ) > where ES :: Target : EntropySource {
58
+ recipient_node_id : PublicKey , context : MessageContext , entropy_source : ES ,
59
+ secp_ctx : & Secp256k1 < T > ,
60
+ ) -> Result < Self , ( ) >
61
+ where
62
+ ES :: Target : EntropySource ,
63
+ {
60
64
Self :: new ( & [ ] , recipient_node_id, context, entropy_source, secp_ctx)
61
65
}
62
66
@@ -68,20 +72,28 @@ impl BlindedMessagePath {
68
72
pub fn new < ES : Deref , T : secp256k1:: Signing + secp256k1:: Verification > (
69
73
intermediate_nodes : & [ MessageForwardNode ] , recipient_node_id : PublicKey ,
70
74
context : MessageContext , entropy_source : ES , secp_ctx : & Secp256k1 < T > ,
71
- ) -> Result < Self , ( ) > where ES :: Target : EntropySource {
75
+ ) -> Result < Self , ( ) >
76
+ where
77
+ ES :: Target : EntropySource ,
78
+ {
72
79
let introduction_node = IntroductionNode :: NodeId (
73
- intermediate_nodes. first ( ) . map_or ( recipient_node_id, |n| n. node_id )
80
+ intermediate_nodes. first ( ) . map_or ( recipient_node_id, |n| n. node_id ) ,
74
81
) ;
75
82
let blinding_secret_bytes = entropy_source. get_secure_random_bytes ( ) ;
76
- let blinding_secret = SecretKey :: from_slice ( & blinding_secret_bytes[ ..] ) . expect ( "RNG is busted" ) ;
83
+ let blinding_secret =
84
+ SecretKey :: from_slice ( & blinding_secret_bytes[ ..] ) . expect ( "RNG is busted" ) ;
77
85
78
86
Ok ( Self ( BlindedPath {
79
87
introduction_node,
80
88
blinding_point : PublicKey :: from_secret_key ( secp_ctx, & blinding_secret) ,
81
89
blinded_hops : blinded_hops (
82
- secp_ctx, intermediate_nodes, recipient_node_id,
83
- context, & blinding_secret,
84
- ) . map_err ( |_| ( ) ) ?,
90
+ secp_ctx,
91
+ intermediate_nodes,
92
+ recipient_node_id,
93
+ context,
94
+ & blinding_secret,
95
+ )
96
+ . map_err ( |_| ( ) ) ?,
85
97
} ) )
86
98
}
87
99
@@ -97,9 +109,9 @@ impl BlindedMessagePath {
97
109
if let Some ( node_info) = network_graph. node ( & node_id) {
98
110
if let Some ( ( scid, channel_info) ) = node_info
99
111
. channels
100
- . iter ( )
101
- . filter_map ( |scid| network_graph. channel ( * scid) . map ( |info| ( * scid, info) ) )
102
- . min_by_key ( |( scid, _) | scid_utils:: block_from_scid ( * scid) )
112
+ . iter ( )
113
+ . filter_map ( |scid| network_graph. channel ( * scid) . map ( |info| ( * scid, info) ) )
114
+ . min_by_key ( |( scid, _) | scid_utils:: block_from_scid ( * scid) )
103
115
{
104
116
let direction = if node_id == channel_info. node_one {
105
117
Direction :: NodeOne
@@ -117,7 +129,7 @@ impl BlindedMessagePath {
117
129
/// Returns the introduction [`NodeId`] of the blinded path, if it is publicly reachable (i.e.,
118
130
/// it is found in the network graph).
119
131
pub fn public_introduction_node_id < ' a > (
120
- & self , network_graph : & ' a ReadOnlyNetworkGraph
132
+ & self , network_graph : & ' a ReadOnlyNetworkGraph ,
121
133
) -> Option < & ' a NodeId > {
122
134
self . 0 . public_introduction_node_id ( network_graph)
123
135
}
@@ -144,7 +156,7 @@ impl BlindedMessagePath {
144
156
///
145
157
/// Will only modify `self` when returning `Ok`.
146
158
pub fn advance_path_by_one < NS : Deref , NL : Deref , T > (
147
- & mut self , node_signer : & NS , node_id_lookup : & NL , secp_ctx : & Secp256k1 < T >
159
+ & mut self , node_signer : & NS , node_id_lookup : & NL , secp_ctx : & Secp256k1 < T > ,
148
160
) -> Result < ( ) , ( ) >
149
161
where
150
162
NS :: Target : NodeSigner ,
@@ -158,28 +170,31 @@ impl BlindedMessagePath {
158
170
let mut reader = FixedLengthReader :: new ( & mut s, encrypted_control_tlvs. len ( ) as u64 ) ;
159
171
match ChaChaPolyReadAdapter :: read ( & mut reader, rho) {
160
172
Ok ( ChaChaPolyReadAdapter {
161
- readable : ControlTlvs :: Forward ( ForwardTlvs { next_hop, next_blinding_override } )
173
+ readable : ControlTlvs :: Forward ( ForwardTlvs { next_hop, next_blinding_override } ) ,
162
174
} ) => {
163
175
let next_node_id = match next_hop {
164
176
NextMessageHop :: NodeId ( pubkey) => pubkey,
165
- NextMessageHop :: ShortChannelId ( scid) => match node_id_lookup. next_node_id ( scid) {
177
+ NextMessageHop :: ShortChannelId ( scid) => match node_id_lookup. next_node_id ( scid)
178
+ {
166
179
Some ( pubkey) => pubkey,
167
180
None => return Err ( ( ) ) ,
168
181
} ,
169
182
} ;
170
183
let mut new_blinding_point = match next_blinding_override {
171
184
Some ( blinding_point) => blinding_point,
172
- None => {
173
- onion_utils:: next_hop_pubkey ( secp_ctx, self . 0 . blinding_point ,
174
- control_tlvs_ss. as_ref ( ) ) . map_err ( |_| ( ) ) ?
175
- }
185
+ None => onion_utils:: next_hop_pubkey (
186
+ secp_ctx,
187
+ self . 0 . blinding_point ,
188
+ control_tlvs_ss. as_ref ( ) ,
189
+ )
190
+ . map_err ( |_| ( ) ) ?,
176
191
} ;
177
192
mem:: swap ( & mut self . 0 . blinding_point , & mut new_blinding_point) ;
178
193
self . 0 . introduction_node = IntroductionNode :: NodeId ( next_node_id) ;
179
194
self . 0 . blinded_hops . remove ( 0 ) ;
180
195
Ok ( ( ) )
181
196
} ,
182
- _ => Err ( ( ) )
197
+ _ => Err ( ( ) ) ,
183
198
}
184
199
}
185
200
@@ -189,7 +204,7 @@ impl BlindedMessagePath {
189
204
190
205
#[ cfg( test) ]
191
206
pub fn from_raw (
192
- introduction_node_id : PublicKey , blinding_point : PublicKey , blinded_hops : Vec < BlindedHop >
207
+ introduction_node_id : PublicKey , blinding_point : PublicKey , blinded_hops : Vec < BlindedHop > ,
193
208
) -> Self {
194
209
Self ( BlindedPath {
195
210
introduction_node : IntroductionNode :: NodeId ( introduction_node_id) ,
@@ -241,7 +256,7 @@ pub(crate) struct ReceiveTlvs {
241
256
/// If `context` is `Some`, it is used to identify the blinded path that this onion message is
242
257
/// sending to. This is useful for receivers to check that said blinded path is being used in
243
258
/// the right context.
244
- pub context : Option < MessageContext >
259
+ pub context : Option < MessageContext > ,
245
260
}
246
261
247
262
impl Writeable for ForwardTlvs {
@@ -480,20 +495,24 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
480
495
secp_ctx : & Secp256k1 < T > , intermediate_nodes : & [ MessageForwardNode ] ,
481
496
recipient_node_id : PublicKey , context : MessageContext , session_priv : & SecretKey ,
482
497
) -> Result < Vec < BlindedHop > , secp256k1:: Error > {
483
- let pks = intermediate_nodes. iter ( ) . map ( |node| node. node_id )
498
+ let pks = intermediate_nodes
499
+ . iter ( )
500
+ . map ( |node| node. node_id )
484
501
. chain ( core:: iter:: once ( recipient_node_id) ) ;
485
- let tlvs = pks. clone ( )
502
+ let tlvs = pks
503
+ . clone ( )
486
504
. skip ( 1 ) // The first node's TLVs contains the next node's pubkey
487
505
. zip ( intermediate_nodes. iter ( ) . map ( |node| node. short_channel_id ) )
488
506
. map ( |( pubkey, scid) | match scid {
489
507
Some ( scid) => NextMessageHop :: ShortChannelId ( scid) ,
490
508
None => NextMessageHop :: NodeId ( pubkey) ,
491
509
} )
492
- . map ( |next_hop| ControlTlvs :: Forward ( ForwardTlvs { next_hop, next_blinding_override : None } ) )
493
- . chain ( core:: iter:: once ( ControlTlvs :: Receive ( ReceiveTlvs { context : Some ( context) } ) ) ) ;
510
+ . map ( |next_hop| {
511
+ ControlTlvs :: Forward ( ForwardTlvs { next_hop, next_blinding_override : None } )
512
+ } )
513
+ . chain ( core:: iter:: once ( ControlTlvs :: Receive ( ReceiveTlvs { context : Some ( context) } ) ) ) ;
494
514
495
515
let path = pks. zip ( tlvs) ;
496
516
497
517
utils:: construct_blinded_hops ( secp_ctx, path, session_priv)
498
518
}
499
-
0 commit comments