@@ -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 ;
@@ -131,3 +144,106 @@ where
131
144
}
132
145
}
133
146
}
147
+
148
+ impl < MR : Deref > OffersMessageFlow < MR >
149
+ where
150
+ MR :: Target : MessageRouter ,
151
+ {
152
+ /// Creates a collection of blinded paths by delegating to [`MessageRouter`] based on
153
+ /// the path's intended lifetime.
154
+ ///
155
+ /// Whether or not the path is compact depends on whether the path is short-lived or long-lived,
156
+ /// respectively, based on the given `absolute_expiry` as seconds since the Unix epoch. See
157
+ /// [`MAX_SHORT_LIVED_RELATIVE_EXPIRY`].
158
+ fn create_blinded_paths_using_absolute_expiry (
159
+ & self , context : OffersContext , absolute_expiry : Option < Duration > ,
160
+ peers : Vec < MessageForwardNode > ,
161
+ ) -> Result < Vec < BlindedMessagePath > , ( ) > {
162
+ let now = self . duration_since_epoch ( ) ;
163
+ let max_short_lived_absolute_expiry = now. saturating_add ( MAX_SHORT_LIVED_RELATIVE_EXPIRY ) ;
164
+
165
+ if absolute_expiry. unwrap_or ( Duration :: MAX ) <= max_short_lived_absolute_expiry {
166
+ self . create_compact_blinded_paths ( peers, context)
167
+ } else {
168
+ self . create_blinded_paths ( peers, MessageContext :: Offers ( context) )
169
+ }
170
+ }
171
+
172
+ /// Creates a collection of blinded paths by delegating to
173
+ /// [`MessageRouter::create_blinded_paths`].
174
+ ///
175
+ /// Errors if the `MessageRouter` errors.
176
+ fn create_blinded_paths (
177
+ & self , peers : Vec < MessageForwardNode > , context : MessageContext ,
178
+ ) -> Result < Vec < BlindedMessagePath > , ( ) > {
179
+ let recipient = self . get_our_node_id ( ) ;
180
+ let secp_ctx = & self . secp_ctx ;
181
+
182
+ let peers = peers. into_iter ( ) . map ( |node| node. node_id ) . collect ( ) ;
183
+
184
+ self . message_router
185
+ . create_blinded_paths ( recipient, context, peers, secp_ctx)
186
+ . and_then ( |paths| ( !paths. is_empty ( ) ) . then ( || paths) . ok_or ( ( ) ) )
187
+ }
188
+
189
+ /// Creates a collection of blinded paths by delegating to
190
+ /// [`MessageRouter::create_compact_blinded_paths`].
191
+ ///
192
+ /// Errors if the `MessageRouter` errors.
193
+ fn create_compact_blinded_paths (
194
+ & self , peers : Vec < MessageForwardNode > , context : OffersContext ,
195
+ ) -> Result < Vec < BlindedMessagePath > , ( ) > {
196
+ let recipient = self . get_our_node_id ( ) ;
197
+ let secp_ctx = & self . secp_ctx ;
198
+
199
+ self . message_router
200
+ . create_compact_blinded_paths (
201
+ recipient,
202
+ MessageContext :: Offers ( context) ,
203
+ peers,
204
+ secp_ctx,
205
+ )
206
+ . and_then ( |paths| ( !paths. is_empty ( ) ) . then ( || paths) . ok_or ( ( ) ) )
207
+ }
208
+
209
+ /// Creates multi-hop blinded payment paths for the given `amount_msats` by delegating to
210
+ /// [`Router::create_blinded_payment_paths`].
211
+ fn create_blinded_payment_paths < ES : Deref , R : Deref > (
212
+ & self , router : & R , entropy_source : ES , usable_channels : Vec < ChannelDetails > ,
213
+ amount_msats : Option < u64 > , payment_secret : PaymentSecret , payment_context : PaymentContext ,
214
+ relative_expiry_seconds : u32 ,
215
+ ) -> Result < Vec < BlindedPaymentPath > , ( ) >
216
+ where
217
+ ES :: Target : EntropySource ,
218
+ R :: Target : Router ,
219
+ {
220
+ let expanded_key = & self . inbound_payment_key ;
221
+ let entropy = & * entropy_source;
222
+ let secp_ctx = & self . secp_ctx ;
223
+
224
+ let payee_node_id = self . get_our_node_id ( ) ;
225
+
226
+ // Assume shorter than usual block times to avoid spuriously failing payments too early.
227
+ const SECONDS_PER_BLOCK : u32 = 9 * 60 ;
228
+ let relative_expiry_blocks = relative_expiry_seconds / SECONDS_PER_BLOCK ;
229
+ let max_cltv_expiry = core:: cmp:: max ( relative_expiry_blocks, CLTV_FAR_FAR_AWAY )
230
+ . saturating_add ( LATENCY_GRACE_PERIOD_BLOCKS )
231
+ . saturating_add ( self . best_block . read ( ) . unwrap ( ) . height ) ;
232
+
233
+ let payee_tlvs = UnauthenticatedReceiveTlvs {
234
+ payment_secret,
235
+ payment_constraints : PaymentConstraints { max_cltv_expiry, htlc_minimum_msat : 1 } ,
236
+ payment_context,
237
+ } ;
238
+ let nonce = Nonce :: from_entropy_source ( entropy) ;
239
+ let payee_tlvs = payee_tlvs. authenticate ( nonce, expanded_key) ;
240
+
241
+ router. create_blinded_payment_paths (
242
+ payee_node_id,
243
+ usable_channels,
244
+ payee_tlvs,
245
+ amount_msats,
246
+ secp_ctx,
247
+ )
248
+ }
249
+ }
0 commit comments