@@ -14,19 +14,32 @@ use core::ops::Deref;
14
14
use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
15
15
use core:: time:: Duration ;
16
16
17
- use bitcoin:: block:: Header ;
18
- use bitcoin:: constants:: ChainHash ;
19
- use bitcoin:: secp256k1:: { self , PublicKey , Secp256k1 } ;
17
+ use crate :: blinded_path:: message:: {
18
+ BlindedMessagePath , MessageContext , MessageForwardNode , OffersContext ,
19
+ } ;
20
+ use crate :: blinded_path:: payment:: {
21
+ BlindedPaymentPath , PaymentConstraints , PaymentContext , UnauthenticatedReceiveTlvs ,
22
+ } ;
23
+ use crate :: chain:: channelmonitor:: LATENCY_GRACE_PERIOD_BLOCKS ;
20
24
21
25
#[ allow( unused_imports) ]
22
26
use crate :: prelude:: * ;
23
27
24
28
use crate :: chain:: BestBlock ;
29
+ use crate :: ln:: channel_state:: ChannelDetails ;
30
+ use crate :: ln:: channelmanager:: { CLTV_FAR_FAR_AWAY , MAX_SHORT_LIVED_RELATIVE_EXPIRY } ;
25
31
use crate :: ln:: inbound_payment;
32
+ use crate :: offers:: nonce:: Nonce ;
26
33
use crate :: onion_message:: async_payments:: AsyncPaymentsMessage ;
27
34
use crate :: onion_message:: messenger:: { MessageRouter , MessageSendInstructions } ;
28
35
use crate :: onion_message:: offers:: OffersMessage ;
36
+ use crate :: routing:: router:: Router ;
37
+ use crate :: sign:: EntropySource ;
29
38
use crate :: sync:: { Mutex , RwLock } ;
39
+ use bitcoin:: block:: Header ;
40
+ use bitcoin:: constants:: ChainHash ;
41
+ use bitcoin:: secp256k1:: { self , PublicKey , Secp256k1 } ;
42
+ use lightning_invoice:: PaymentSecret ;
30
43
31
44
#[ cfg( feature = "dnssec" ) ]
32
45
use crate :: onion_message:: dns_resolution:: { DNSResolverMessage , OMNameResolver } ;
@@ -139,3 +152,129 @@ where
139
152
}
140
153
}
141
154
}
155
+
156
+ impl < MR : Deref > OffersMessageFlow < MR >
157
+ where
158
+ MR :: Target : MessageRouter ,
159
+ {
160
+ /// Creates a collection of blinded paths by delegating to [`MessageRouter`] based on
161
+ /// the path's intended lifetime.
162
+ ///
163
+ /// Whether or not the path is compact depends on whether the path is short-lived or long-lived,
164
+ /// respectively, based on the given `absolute_expiry` as seconds since the Unix epoch. See
165
+ /// [`MAX_SHORT_LIVED_RELATIVE_EXPIRY`].
166
+ fn create_blinded_paths_using_absolute_expiry (
167
+ & self , context : OffersContext , absolute_expiry : Option < Duration > ,
168
+ peers : Vec < MessageForwardNode > ,
169
+ ) -> Result < Vec < BlindedMessagePath > , ( ) > {
170
+ let now = self . duration_since_epoch ( ) ;
171
+ let max_short_lived_absolute_expiry = now. saturating_add ( MAX_SHORT_LIVED_RELATIVE_EXPIRY ) ;
172
+
173
+ if absolute_expiry. unwrap_or ( Duration :: MAX ) <= max_short_lived_absolute_expiry {
174
+ self . create_compact_blinded_paths ( peers, context)
175
+ } else {
176
+ self . create_blinded_paths ( peers, MessageContext :: Offers ( context) )
177
+ }
178
+ }
179
+
180
+ /// Creates a collection of blinded paths by delegating to
181
+ /// [`MessageRouter::create_blinded_paths`].
182
+ ///
183
+ /// Errors if the `MessageRouter` errors.
184
+ fn create_blinded_paths (
185
+ & self , peers : Vec < MessageForwardNode > , context : MessageContext ,
186
+ ) -> Result < Vec < BlindedMessagePath > , ( ) > {
187
+ let recipient = self . get_our_node_id ( ) ;
188
+ let secp_ctx = & self . secp_ctx ;
189
+
190
+ let peers = peers. into_iter ( ) . map ( |node| node. node_id ) . collect ( ) ;
191
+
192
+ self . message_router
193
+ . create_blinded_paths ( recipient, context, peers, secp_ctx)
194
+ . and_then ( |paths| ( !paths. is_empty ( ) ) . then ( || paths) . ok_or ( ( ) ) )
195
+ }
196
+
197
+ /// Creates a collection of blinded paths by delegating to
198
+ /// [`MessageRouter::create_compact_blinded_paths`].
199
+ ///
200
+ /// Errors if the `MessageRouter` errors.
201
+ fn create_compact_blinded_paths (
202
+ & self , peers : Vec < MessageForwardNode > , context : OffersContext ,
203
+ ) -> Result < Vec < BlindedMessagePath > , ( ) > {
204
+ let recipient = self . get_our_node_id ( ) ;
205
+ let secp_ctx = & self . secp_ctx ;
206
+
207
+ self . message_router
208
+ . create_compact_blinded_paths (
209
+ recipient,
210
+ MessageContext :: Offers ( context) ,
211
+ peers,
212
+ secp_ctx,
213
+ )
214
+ . and_then ( |paths| ( !paths. is_empty ( ) ) . then ( || paths) . ok_or ( ( ) ) )
215
+ }
216
+
217
+ /// Creates multi-hop blinded payment paths for the given `amount_msats` by delegating to
218
+ /// [`Router::create_blinded_payment_paths`].
219
+ fn create_blinded_payment_paths < ES : Deref , R : Deref > (
220
+ & self , router : & R , entropy_source : ES , usable_channels : Vec < ChannelDetails > ,
221
+ amount_msats : Option < u64 > , payment_secret : PaymentSecret , payment_context : PaymentContext ,
222
+ relative_expiry_seconds : u32 ,
223
+ ) -> Result < Vec < BlindedPaymentPath > , ( ) >
224
+ where
225
+ ES :: Target : EntropySource ,
226
+ R :: Target : Router ,
227
+ {
228
+ let expanded_key = & self . inbound_payment_key ;
229
+ let entropy = & * entropy_source;
230
+ let secp_ctx = & self . secp_ctx ;
231
+
232
+ let payee_node_id = self . get_our_node_id ( ) ;
233
+
234
+ // Assume shorter than usual block times to avoid spuriously failing payments too early.
235
+ const SECONDS_PER_BLOCK : u32 = 9 * 60 ;
236
+ let relative_expiry_blocks = relative_expiry_seconds / SECONDS_PER_BLOCK ;
237
+ let max_cltv_expiry = core:: cmp:: max ( relative_expiry_blocks, CLTV_FAR_FAR_AWAY )
238
+ . saturating_add ( LATENCY_GRACE_PERIOD_BLOCKS )
239
+ . saturating_add ( self . best_block . read ( ) . unwrap ( ) . height ) ;
240
+
241
+ let payee_tlvs = UnauthenticatedReceiveTlvs {
242
+ payment_secret,
243
+ payment_constraints : PaymentConstraints { max_cltv_expiry, htlc_minimum_msat : 1 } ,
244
+ payment_context,
245
+ } ;
246
+ let nonce = Nonce :: from_entropy_source ( entropy) ;
247
+ let payee_tlvs = payee_tlvs. authenticate ( nonce, expanded_key) ;
248
+
249
+ router. create_blinded_payment_paths (
250
+ payee_node_id,
251
+ usable_channels,
252
+ payee_tlvs,
253
+ amount_msats,
254
+ secp_ctx,
255
+ )
256
+ }
257
+
258
+ #[ cfg( all( test, async_payments) ) ]
259
+ /// Creates multi-hop blinded payment paths for the given `amount_msats` by delegating to
260
+ /// [`Router::create_blinded_payment_paths`].
261
+ pub ( crate ) fn test_create_blinded_payment_paths < ES : Deref , R : Deref > (
262
+ & self , router : & R , entropy_source : ES , usable_channels : Vec < ChannelDetails > ,
263
+ amount_msats : Option < u64 > , payment_secret : PaymentSecret , payment_context : PaymentContext ,
264
+ relative_expiry_seconds : u32 ,
265
+ ) -> Result < Vec < BlindedPaymentPath > , ( ) >
266
+ where
267
+ ES :: Target : EntropySource ,
268
+ R :: Target : Router ,
269
+ {
270
+ self . create_blinded_payment_paths (
271
+ router,
272
+ entropy_source,
273
+ usable_channels,
274
+ amount_msats,
275
+ payment_secret,
276
+ payment_context,
277
+ relative_expiry_seconds,
278
+ )
279
+ }
280
+ }
0 commit comments