12
12
use crate :: config:: { Config , LDK_PAYMENT_RETRY_TIMEOUT } ;
13
13
use crate :: connection:: ConnectionManager ;
14
14
use crate :: error:: Error ;
15
- use crate :: hex_utils;
16
15
use crate :: liquidity:: LiquiditySource ;
17
16
use crate :: logger:: { log_error, log_info, FilesystemLogger , Logger } ;
18
17
use crate :: payment:: store:: {
@@ -31,14 +30,32 @@ use lightning::routing::router::{PaymentParameters, RouteParameters};
31
30
32
31
use lightning_types:: payment:: { PaymentHash , PaymentPreimage } ;
33
32
34
- use lightning_invoice:: { Bolt11Invoice , Description } ;
33
+ use lightning_invoice:: Bolt11Invoice ;
34
+ use lightning_invoice:: Bolt11InvoiceDescription as LdkBolt11InvoiceDescription ;
35
35
36
36
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
37
37
use bitcoin:: hashes:: Hash ;
38
38
39
- use std:: str:: FromStr ;
40
39
use std:: sync:: { Arc , RwLock } ;
41
40
41
+ #[ cfg( not( feature = "uniffi" ) ) ]
42
+ type Bolt11InvoiceDescription = LdkBolt11InvoiceDescription ;
43
+ #[ cfg( feature = "uniffi" ) ]
44
+ type Bolt11InvoiceDescription = crate :: uniffi_types:: Bolt11InvoiceDescription ;
45
+
46
+ macro_rules! maybe_convert_description {
47
+ ( $description: expr) => { {
48
+ #[ cfg( not( feature = "uniffi" ) ) ]
49
+ {
50
+ $description
51
+ }
52
+ #[ cfg( feature = "uniffi" ) ]
53
+ {
54
+ & LdkBolt11InvoiceDescription :: try_from( $description) ?
55
+ }
56
+ } } ;
57
+ }
58
+
42
59
/// A payment handler allowing to create and pay [BOLT 11] invoices.
43
60
///
44
61
/// Should be retrieved by calling [`Node::bolt11_payment`].
@@ -405,21 +422,11 @@ impl Bolt11Payment {
405
422
/// given.
406
423
///
407
424
/// The inbound payment will be automatically claimed upon arrival.
408
- #[ cfg( not( feature = "uniffi" ) ) ]
409
- pub fn receive (
410
- & self , amount_msat : u64 , description : & lightning_invoice:: Bolt11InvoiceDescription ,
411
- expiry_secs : u32 ,
412
- ) -> Result < Bolt11Invoice , Error > {
413
- self . receive_inner ( Some ( amount_msat) , description, expiry_secs, None )
414
- }
415
-
416
- #[ cfg( feature = "uniffi" ) ]
417
425
pub fn receive (
418
426
& self , amount_msat : u64 , description : & Bolt11InvoiceDescription , expiry_secs : u32 ,
419
427
) -> Result < Bolt11Invoice , Error > {
420
- let invoice_description =
421
- lightning_invoice:: Bolt11InvoiceDescription :: try_from ( description) ?;
422
- self . receive_inner ( Some ( amount_msat) , & invoice_description, expiry_secs, None )
428
+ let description = maybe_convert_description ! ( description) ;
429
+ self . receive_inner ( Some ( amount_msat) , description, expiry_secs, None )
423
430
}
424
431
425
432
/// Returns a payable invoice that can be used to request a payment of the amount
@@ -436,42 +443,23 @@ impl Bolt11Payment {
436
443
/// [`PaymentClaimable`]: crate::Event::PaymentClaimable
437
444
/// [`claim_for_hash`]: Self::claim_for_hash
438
445
/// [`fail_for_hash`]: Self::fail_for_hash
439
- #[ cfg( not( feature = "uniffi" ) ) ]
440
- pub fn receive_for_hash (
441
- & self , amount_msat : u64 , description : & lightning_invoice:: Bolt11InvoiceDescription ,
442
- expiry_secs : u32 , payment_hash : PaymentHash ,
443
- ) -> Result < Bolt11Invoice , Error > {
444
- self . receive_inner ( Some ( amount_msat) , description, expiry_secs, Some ( payment_hash) )
445
- }
446
-
447
- #[ cfg( feature = "uniffi" ) ]
448
446
pub fn receive_for_hash (
449
447
& self , amount_msat : u64 , description : & Bolt11InvoiceDescription , expiry_secs : u32 ,
450
448
payment_hash : PaymentHash ,
451
449
) -> Result < Bolt11Invoice , Error > {
452
- let invoice_description =
453
- lightning_invoice:: Bolt11InvoiceDescription :: try_from ( description) ?;
454
- self . receive_inner ( Some ( amount_msat) , & invoice_description, expiry_secs, Some ( payment_hash) )
450
+ let description = maybe_convert_description ! ( description) ;
451
+ self . receive_inner ( Some ( amount_msat) , description, expiry_secs, Some ( payment_hash) )
455
452
}
456
453
457
454
/// Returns a payable invoice that can be used to request and receive a payment for which the
458
455
/// amount is to be determined by the user, also known as a "zero-amount" invoice.
459
456
///
460
457
/// The inbound payment will be automatically claimed upon arrival.
461
- #[ cfg( not( feature = "uniffi" ) ) ]
462
- pub fn receive_variable_amount (
463
- & self , description : & lightning_invoice:: Bolt11InvoiceDescription , expiry_secs : u32 ,
464
- ) -> Result < Bolt11Invoice , Error > {
465
- self . receive_inner ( None , description, expiry_secs, None )
466
- }
467
-
468
- #[ cfg( feature = "uniffi" ) ]
469
458
pub fn receive_variable_amount (
470
459
& self , description : & Bolt11InvoiceDescription , expiry_secs : u32 ,
471
460
) -> Result < Bolt11Invoice , Error > {
472
- let invoice_description =
473
- lightning_invoice:: Bolt11InvoiceDescription :: try_from ( description) ?;
474
- self . receive_inner ( None , & invoice_description, expiry_secs, None )
461
+ let description = maybe_convert_description ! ( description) ;
462
+ self . receive_inner ( None , description, expiry_secs, None )
475
463
}
476
464
477
465
/// Returns a payable invoice that can be used to request a payment for the given payment hash
@@ -488,27 +476,16 @@ impl Bolt11Payment {
488
476
/// [`PaymentClaimable`]: crate::Event::PaymentClaimable
489
477
/// [`claim_for_hash`]: Self::claim_for_hash
490
478
/// [`fail_for_hash`]: Self::fail_for_hash
491
- #[ cfg( not( feature = "uniffi" ) ) ]
492
- pub fn receive_variable_amount_for_hash (
493
- & self , description : & lightning_invoice:: Bolt11InvoiceDescription , expiry_secs : u32 ,
494
- payment_hash : PaymentHash ,
495
- ) -> Result < Bolt11Invoice , Error > {
496
- self . receive_inner ( None , description, expiry_secs, Some ( payment_hash) )
497
- }
498
-
499
- #[ cfg( feature = "uniffi" ) ]
500
479
pub fn receive_variable_amount_for_hash (
501
480
& self , description : & Bolt11InvoiceDescription , expiry_secs : u32 , payment_hash : PaymentHash ,
502
481
) -> Result < Bolt11Invoice , Error > {
503
- let invoice_description =
504
- lightning_invoice:: Bolt11InvoiceDescription :: try_from ( description) ?;
505
- self . receive_inner ( None , & invoice_description, expiry_secs, Some ( payment_hash) )
482
+ let description = maybe_convert_description ! ( description) ;
483
+ self . receive_inner ( None , description, expiry_secs, Some ( payment_hash) )
506
484
}
507
485
508
486
pub ( crate ) fn receive_inner (
509
- & self , amount_msat : Option < u64 > ,
510
- invoice_description : & lightning_invoice:: Bolt11InvoiceDescription , expiry_secs : u32 ,
511
- manual_claim_payment_hash : Option < PaymentHash > ,
487
+ & self , amount_msat : Option < u64 > , invoice_description : & LdkBolt11InvoiceDescription ,
488
+ expiry_secs : u32 , manual_claim_payment_hash : Option < PaymentHash > ,
512
489
) -> Result < Bolt11Invoice , Error > {
513
490
let invoice = {
514
491
let invoice_params = Bolt11InvoiceParameters {
@@ -573,30 +550,14 @@ impl Bolt11Payment {
573
550
/// channel to us. We'll use its cheapest offer otherwise.
574
551
///
575
552
/// [LSPS2]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md
576
- #[ cfg( not( feature = "uniffi" ) ) ]
577
- pub fn receive_via_jit_channel (
578
- & self , amount_msat : u64 , description : & lightning_invoice:: Bolt11InvoiceDescription ,
579
- expiry_secs : u32 , max_total_lsp_fee_limit_msat : Option < u64 > ,
580
- ) -> Result < Bolt11Invoice , Error > {
581
- self . receive_via_jit_channel_inner (
582
- Some ( amount_msat) ,
583
- description,
584
- expiry_secs,
585
- max_total_lsp_fee_limit_msat,
586
- None ,
587
- )
588
- }
589
-
590
- #[ cfg( feature = "uniffi" ) ]
591
553
pub fn receive_via_jit_channel (
592
554
& self , amount_msat : u64 , description : & Bolt11InvoiceDescription , expiry_secs : u32 ,
593
555
max_total_lsp_fee_limit_msat : Option < u64 > ,
594
556
) -> Result < Bolt11Invoice , Error > {
595
- let invoice_description =
596
- lightning_invoice:: Bolt11InvoiceDescription :: try_from ( description) ?;
557
+ let description = maybe_convert_description ! ( description) ;
597
558
self . receive_via_jit_channel_inner (
598
559
Some ( amount_msat) ,
599
- & invoice_description ,
560
+ description ,
600
561
expiry_secs,
601
562
max_total_lsp_fee_limit_msat,
602
563
None ,
@@ -614,38 +575,22 @@ impl Bolt11Payment {
614
575
/// We'll use its cheapest offer otherwise.
615
576
///
616
577
/// [LSPS2]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md
617
- #[ cfg( not( feature = "uniffi" ) ) ]
618
- pub fn receive_variable_amount_via_jit_channel (
619
- & self , description : & lightning_invoice:: Bolt11InvoiceDescription , expiry_secs : u32 ,
620
- max_proportional_lsp_fee_limit_ppm_msat : Option < u64 > ,
621
- ) -> Result < Bolt11Invoice , Error > {
622
- self . receive_via_jit_channel_inner (
623
- None ,
624
- description,
625
- expiry_secs,
626
- None ,
627
- max_proportional_lsp_fee_limit_ppm_msat,
628
- )
629
- }
630
-
631
- #[ cfg( feature = "uniffi" ) ]
632
578
pub fn receive_variable_amount_via_jit_channel (
633
579
& self , description : & Bolt11InvoiceDescription , expiry_secs : u32 ,
634
580
max_proportional_lsp_fee_limit_ppm_msat : Option < u64 > ,
635
581
) -> Result < Bolt11Invoice , Error > {
636
- let invoice_description =
637
- lightning_invoice:: Bolt11InvoiceDescription :: try_from ( description) ?;
582
+ let description = maybe_convert_description ! ( description) ;
638
583
self . receive_via_jit_channel_inner (
639
584
None ,
640
- & invoice_description ,
585
+ description ,
641
586
expiry_secs,
642
587
None ,
643
588
max_proportional_lsp_fee_limit_ppm_msat,
644
589
)
645
590
}
646
591
647
592
fn receive_via_jit_channel_inner (
648
- & self , amount_msat : Option < u64 > , description : & lightning_invoice :: Bolt11InvoiceDescription ,
593
+ & self , amount_msat : Option < u64 > , description : & LdkBolt11InvoiceDescription ,
649
594
expiry_secs : u32 , max_total_lsp_fee_limit_msat : Option < u64 > ,
650
595
max_proportional_lsp_fee_limit_ppm_msat : Option < u64 > ,
651
596
) -> Result < Bolt11Invoice , Error > {
@@ -817,49 +762,3 @@ impl Bolt11Payment {
817
762
Ok ( ( ) )
818
763
}
819
764
}
820
-
821
- /// Represents the description of an invoice which has to be either a directly included string or
822
- /// a hash of a description provided out of band.
823
- pub enum Bolt11InvoiceDescription {
824
- /// Contains a full description.
825
- Direct {
826
- /// Description of what the invoice is for
827
- description : String ,
828
- } ,
829
- /// Contains a hash.
830
- Hash {
831
- /// Hash of the description of what the invoice is for
832
- hash : String ,
833
- } ,
834
- }
835
-
836
- impl TryFrom < & Bolt11InvoiceDescription > for lightning_invoice:: Bolt11InvoiceDescription {
837
- type Error = Error ;
838
-
839
- fn try_from ( value : & Bolt11InvoiceDescription ) -> Result < Self , Self :: Error > {
840
- match value {
841
- Bolt11InvoiceDescription :: Direct { description } => {
842
- Description :: new ( description. clone ( ) )
843
- . map ( lightning_invoice:: Bolt11InvoiceDescription :: Direct )
844
- . map_err ( |_| Error :: InvoiceCreationFailed )
845
- } ,
846
- Bolt11InvoiceDescription :: Hash { hash } => Sha256 :: from_str ( & hash)
847
- . map ( lightning_invoice:: Sha256 )
848
- . map ( lightning_invoice:: Bolt11InvoiceDescription :: Hash )
849
- . map_err ( |_| Error :: InvoiceCreationFailed ) ,
850
- }
851
- }
852
- }
853
-
854
- impl From < lightning_invoice:: Bolt11InvoiceDescription > for Bolt11InvoiceDescription {
855
- fn from ( value : lightning_invoice:: Bolt11InvoiceDescription ) -> Self {
856
- match value {
857
- lightning_invoice:: Bolt11InvoiceDescription :: Direct ( description) => {
858
- Bolt11InvoiceDescription :: Direct { description : description. to_string ( ) }
859
- } ,
860
- lightning_invoice:: Bolt11InvoiceDescription :: Hash ( hash) => {
861
- Bolt11InvoiceDescription :: Hash { hash : hex_utils:: to_string ( hash. 0 . as_ref ( ) ) }
862
- } ,
863
- }
864
- }
865
- }
0 commit comments