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