@@ -26,7 +26,6 @@ use bitcoin_payment_instructions::amount::Amount as BPIAmount;
2626use bitcoin_payment_instructions:: { PaymentInstructions , PaymentMethod } ;
2727use lightning:: ln:: channelmanager:: PaymentId ;
2828use lightning:: offers:: offer:: Offer ;
29- use lightning:: onion_message:: dns_resolution:: HumanReadableName ;
3029use lightning:: routing:: router:: RouteParametersConfig ;
3130use lightning_invoice:: { Bolt11Invoice , Bolt11InvoiceDescription , Description } ;
3231
@@ -40,6 +39,11 @@ use crate::Config;
4039
4140type Uri < ' a > = bip21:: Uri < ' a , NetworkChecked , Extras > ;
4241
42+ #[ cfg( not( feature = "uniffi" ) ) ]
43+ type HumanReadableName = lightning:: onion_message:: dns_resolution:: HumanReadableName ;
44+ #[ cfg( feature = "uniffi" ) ]
45+ type HumanReadableName = crate :: ffi:: HumanReadableName ;
46+
4347#[ derive( Debug , Clone ) ]
4448struct Extras {
4549 bolt11_invoice : Option < Bolt11Invoice > ,
@@ -159,15 +163,30 @@ impl UnifiedPayment {
159163 /// [BIP 353]: https://github.com/bitcoin/bips/blob/master/bip-0353.mediawiki
160164 pub async fn send (
161165 & self , uri_str : & str , amount_msat : Option < u64 > ,
162- route_parameters : Option < RouteParametersConfig > ,
166+ route_parameters : Option < RouteParametersConfig > , # [ cfg ( hrn_tests ) ] test_offer : & Offer ,
163167 ) -> Result < UnifiedPaymentResult , Error > {
164168 let resolver = self . hrn_resolver . as_ref ( ) . clone ( ) . ok_or_else ( || {
165169 log_error ! ( self . logger, "No HRN resolver configured. Cannot resolve HRNs." ) ;
166170 Error :: HrnResolverNotConfigured
167171 } ) ?;
168172
173+ let target_network;
174+
175+ target_network = if let Ok ( _) = HumanReadableName :: from_encoded ( uri_str) {
176+ #[ cfg( hrn_tests) ]
177+ {
178+ bitcoin:: Network :: Bitcoin
179+ }
180+ #[ cfg( not( hrn_tests) ) ]
181+ {
182+ self . config . network
183+ }
184+ } else {
185+ self . config . network
186+ } ;
187+
169188 let parse_fut =
170- PaymentInstructions :: parse ( uri_str, self . config . network , resolver. as_ref ( ) , false ) ;
189+ PaymentInstructions :: parse ( uri_str, target_network , resolver. as_ref ( ) , false ) ;
171190
172191 let instructions =
173192 tokio:: time:: timeout ( Duration :: from_secs ( HRN_RESOLUTION_TIMEOUT_SECS ) , parse_fut)
@@ -231,20 +250,26 @@ impl UnifiedPayment {
231250 for method in sorted_payment_methods {
232251 match method {
233252 PaymentMethod :: LightningBolt12 ( offer) => {
234- let offer = maybe_wrap ( offer. clone ( ) ) ;
235-
236- let payment_result = if let Ok ( hrn) = HumanReadableName :: from_encoded ( uri_str) {
237- let hrn = maybe_wrap ( hrn. clone ( ) ) ;
238- self . bolt12_payment . send_using_amount_inner ( & offer, amount_msat. unwrap_or ( 0 ) , None , None , route_parameters, Some ( hrn) )
239- } else if let Some ( amount_msat) = amount_msat {
240- self . bolt12_payment . send_using_amount ( & offer, amount_msat, None , None , route_parameters)
241- } else {
242- self . bolt12_payment . send ( & offer, None , None , route_parameters)
243- }
244- . map_err ( |e| {
245- log_error ! ( self . logger, "Failed to send BOLT12 offer: {:?}. This is part of a unified payment. Falling back to the BOLT11 invoice." , e) ;
246- e
247- } ) ;
253+ #[ cfg( not( hrn_tests) ) ]
254+ let offer = maybe_wrap ( offer) ;
255+
256+ #[ cfg( hrn_tests) ]
257+ let offer = maybe_wrap ( test_offer. clone ( ) ) ;
258+
259+ let payment_result = {
260+ if let Ok ( hrn) = HumanReadableName :: from_encoded ( uri_str) {
261+ let hrn = maybe_wrap ( hrn. clone ( ) ) ;
262+ self . bolt12_payment . send_using_amount_inner ( & offer, amount_msat. unwrap_or ( 0 ) , None , None , route_parameters, Some ( hrn) )
263+ } else if let Some ( amount_msat) = amount_msat {
264+ self . bolt12_payment . send_using_amount ( & offer, amount_msat, None , None , route_parameters)
265+ } else {
266+ self . bolt12_payment . send ( & offer, None , None , route_parameters)
267+ }
268+ . map_err ( |e| {
269+ log_error ! ( self . logger, "Failed to send BOLT12 offer: {:?}. This is part of a unified payment. Falling back to the BOLT11 invoice." , e) ;
270+ e
271+ } )
272+ } ;
248273
249274 if let Ok ( payment_id) = payment_result {
250275 return Ok ( UnifiedPaymentResult :: Bolt12 { payment_id } ) ;
0 commit comments