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;
15
16
use crate :: liquidity:: LiquiditySource ;
16
17
use crate :: logger:: { log_error, log_info, FilesystemLogger , Logger } ;
17
18
use crate :: payment:: store:: {
@@ -30,11 +31,12 @@ use lightning::routing::router::{PaymentParameters, RouteParameters};
30
31
31
32
use lightning_types:: payment:: { PaymentHash , PaymentPreimage } ;
32
33
33
- use lightning_invoice:: { Bolt11Invoice , Bolt11InvoiceDescription , Description } ;
34
+ use lightning_invoice:: { Bolt11Invoice , Description } ;
34
35
35
36
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
36
37
use bitcoin:: hashes:: Hash ;
37
38
39
+ use std:: str:: FromStr ;
38
40
use std:: sync:: { Arc , RwLock } ;
39
41
40
42
/// A payment handler allowing to create and pay [BOLT 11] invoices.
@@ -403,12 +405,23 @@ impl Bolt11Payment {
403
405
/// given.
404
406
///
405
407
/// The inbound payment will be automatically claimed upon arrival.
408
+ #[ cfg( not( feature = "uniffi" ) ) ]
406
409
pub fn receive (
407
- & self , amount_msat : u64 , description : & str , expiry_secs : u32 ,
410
+ & self , amount_msat : u64 , description : & lightning_invoice:: Bolt11InvoiceDescription ,
411
+ expiry_secs : u32 ,
408
412
) -> Result < Bolt11Invoice , Error > {
409
413
self . receive_inner ( Some ( amount_msat) , description, expiry_secs, None )
410
414
}
411
415
416
+ #[ cfg( feature = "uniffi" ) ]
417
+ pub fn receive (
418
+ & self , amount_msat : u64 , description : & Bolt11InvoiceDescription , expiry_secs : u32 ,
419
+ ) -> 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 )
423
+ }
424
+
412
425
/// Returns a payable invoice that can be used to request a payment of the amount
413
426
/// given for the given payment hash.
414
427
///
@@ -423,22 +436,44 @@ impl Bolt11Payment {
423
436
/// [`PaymentClaimable`]: crate::Event::PaymentClaimable
424
437
/// [`claim_for_hash`]: Self::claim_for_hash
425
438
/// [`fail_for_hash`]: Self::fail_for_hash
439
+ #[ cfg( not( feature = "uniffi" ) ) ]
426
440
pub fn receive_for_hash (
427
- & self , amount_msat : u64 , description : & str , expiry_secs : u32 , payment_hash : PaymentHash ,
441
+ & self , amount_msat : u64 , description : & lightning_invoice:: Bolt11InvoiceDescription ,
442
+ expiry_secs : u32 , payment_hash : PaymentHash ,
428
443
) -> Result < Bolt11Invoice , Error > {
429
444
self . receive_inner ( Some ( amount_msat) , description, expiry_secs, Some ( payment_hash) )
430
445
}
431
446
447
+ #[ cfg( feature = "uniffi" ) ]
448
+ pub fn receive_for_hash (
449
+ & self , amount_msat : u64 , description : & Bolt11InvoiceDescription , expiry_secs : u32 ,
450
+ payment_hash : PaymentHash ,
451
+ ) -> 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) )
455
+ }
456
+
432
457
/// Returns a payable invoice that can be used to request and receive a payment for which the
433
458
/// amount is to be determined by the user, also known as a "zero-amount" invoice.
434
459
///
435
460
/// The inbound payment will be automatically claimed upon arrival.
461
+ #[ cfg( not( feature = "uniffi" ) ) ]
436
462
pub fn receive_variable_amount (
437
- & self , description : & str , expiry_secs : u32 ,
463
+ & self , description : & lightning_invoice :: Bolt11InvoiceDescription , expiry_secs : u32 ,
438
464
) -> Result < Bolt11Invoice , Error > {
439
465
self . receive_inner ( None , description, expiry_secs, None )
440
466
}
441
467
468
+ #[ cfg( feature = "uniffi" ) ]
469
+ pub fn receive_variable_amount (
470
+ & self , description : & Bolt11InvoiceDescription , expiry_secs : u32 ,
471
+ ) -> Result < Bolt11Invoice , Error > {
472
+ let invoice_description =
473
+ lightning_invoice:: Bolt11InvoiceDescription :: try_from ( description) ?;
474
+ self . receive_inner ( None , & invoice_description, expiry_secs, None )
475
+ }
476
+
442
477
/// Returns a payable invoice that can be used to request a payment for the given payment hash
443
478
/// and the amount to be determined by the user, also known as a "zero-amount" invoice.
444
479
///
@@ -453,24 +488,32 @@ impl Bolt11Payment {
453
488
/// [`PaymentClaimable`]: crate::Event::PaymentClaimable
454
489
/// [`claim_for_hash`]: Self::claim_for_hash
455
490
/// [`fail_for_hash`]: Self::fail_for_hash
491
+ #[ cfg( not( feature = "uniffi" ) ) ]
456
492
pub fn receive_variable_amount_for_hash (
457
- & self , description : & str , expiry_secs : u32 , payment_hash : PaymentHash ,
493
+ & self , description : & lightning_invoice:: Bolt11InvoiceDescription , expiry_secs : u32 ,
494
+ payment_hash : PaymentHash ,
458
495
) -> Result < Bolt11Invoice , Error > {
459
496
self . receive_inner ( None , description, expiry_secs, Some ( payment_hash) )
460
497
}
461
498
462
- fn receive_inner (
463
- & self , amount_msat : Option < u64 > , description : & str , expiry_secs : u32 ,
464
- manual_claim_payment_hash : Option < PaymentHash > ,
499
+ # [ cfg ( feature = "uniffi" ) ]
500
+ pub fn receive_variable_amount_for_hash (
501
+ & self , description : & Bolt11InvoiceDescription , expiry_secs : u32 , payment_hash : PaymentHash ,
465
502
) -> Result < Bolt11Invoice , Error > {
466
- let invoice_description = Bolt11InvoiceDescription :: Direct (
467
- Description :: new ( description. to_string ( ) ) . map_err ( |_| Error :: InvoiceCreationFailed ) ?,
468
- ) ;
503
+ let invoice_description =
504
+ lightning_invoice:: Bolt11InvoiceDescription :: try_from ( description) ?;
505
+ self . receive_inner ( None , & invoice_description, expiry_secs, Some ( payment_hash) )
506
+ }
469
507
508
+ 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 > ,
512
+ ) -> Result < Bolt11Invoice , Error > {
470
513
let invoice = {
471
514
let invoice_params = Bolt11InvoiceParameters {
472
515
amount_msats : amount_msat,
473
- description : invoice_description,
516
+ description : invoice_description. clone ( ) ,
474
517
invoice_expiry_delta_secs : Some ( expiry_secs) ,
475
518
payment_hash : manual_claim_payment_hash,
476
519
..Default :: default ( )
@@ -530,9 +573,10 @@ impl Bolt11Payment {
530
573
/// channel to us. We'll use its cheapest offer otherwise.
531
574
///
532
575
/// [LSPS2]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md
576
+ #[ cfg( not( feature = "uniffi" ) ) ]
533
577
pub fn receive_via_jit_channel (
534
- & self , amount_msat : u64 , description : & str , expiry_secs : u32 ,
535
- max_total_lsp_fee_limit_msat : Option < u64 > ,
578
+ & self , amount_msat : u64 , description : & lightning_invoice :: Bolt11InvoiceDescription ,
579
+ expiry_secs : u32 , max_total_lsp_fee_limit_msat : Option < u64 > ,
536
580
) -> Result < Bolt11Invoice , Error > {
537
581
self . receive_via_jit_channel_inner (
538
582
Some ( amount_msat) ,
@@ -543,6 +587,22 @@ impl Bolt11Payment {
543
587
)
544
588
}
545
589
590
+ #[ cfg( feature = "uniffi" ) ]
591
+ pub fn receive_via_jit_channel (
592
+ & self , amount_msat : u64 , description : & Bolt11InvoiceDescription , expiry_secs : u32 ,
593
+ max_total_lsp_fee_limit_msat : Option < u64 > ,
594
+ ) -> Result < Bolt11Invoice , Error > {
595
+ let invoice_description =
596
+ lightning_invoice:: Bolt11InvoiceDescription :: try_from ( description) ?;
597
+ self . receive_via_jit_channel_inner (
598
+ Some ( amount_msat) ,
599
+ & invoice_description,
600
+ expiry_secs,
601
+ max_total_lsp_fee_limit_msat,
602
+ None ,
603
+ )
604
+ }
605
+
546
606
/// Returns a payable invoice that can be used to request a variable amount payment (also known
547
607
/// as "zero-amount" invoice) and receive it via a newly created just-in-time (JIT) channel.
548
608
///
@@ -554,8 +614,9 @@ impl Bolt11Payment {
554
614
/// We'll use its cheapest offer otherwise.
555
615
///
556
616
/// [LSPS2]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md
617
+ #[ cfg( not( feature = "uniffi" ) ) ]
557
618
pub fn receive_variable_amount_via_jit_channel (
558
- & self , description : & str , expiry_secs : u32 ,
619
+ & self , description : & lightning_invoice :: Bolt11InvoiceDescription , expiry_secs : u32 ,
559
620
max_proportional_lsp_fee_limit_ppm_msat : Option < u64 > ,
560
621
) -> Result < Bolt11Invoice , Error > {
561
622
self . receive_via_jit_channel_inner (
@@ -567,9 +628,25 @@ impl Bolt11Payment {
567
628
)
568
629
}
569
630
631
+ #[ cfg( feature = "uniffi" ) ]
632
+ pub fn receive_variable_amount_via_jit_channel (
633
+ & self , description : & Bolt11InvoiceDescription , expiry_secs : u32 ,
634
+ max_proportional_lsp_fee_limit_ppm_msat : Option < u64 > ,
635
+ ) -> Result < Bolt11Invoice , Error > {
636
+ let invoice_description =
637
+ lightning_invoice:: Bolt11InvoiceDescription :: try_from ( description) ?;
638
+ self . receive_via_jit_channel_inner (
639
+ None ,
640
+ & invoice_description,
641
+ expiry_secs,
642
+ None ,
643
+ max_proportional_lsp_fee_limit_ppm_msat,
644
+ )
645
+ }
646
+
570
647
fn receive_via_jit_channel_inner (
571
- & self , amount_msat : Option < u64 > , description : & str , expiry_secs : u32 ,
572
- max_total_lsp_fee_limit_msat : Option < u64 > ,
648
+ & self , amount_msat : Option < u64 > , description : & lightning_invoice :: Bolt11InvoiceDescription ,
649
+ expiry_secs : u32 , max_total_lsp_fee_limit_msat : Option < u64 > ,
573
650
max_proportional_lsp_fee_limit_ppm_msat : Option < u64 > ,
574
651
) -> Result < Bolt11Invoice , Error > {
575
652
let liquidity_source =
@@ -740,3 +817,49 @@ impl Bolt11Payment {
740
817
Ok ( ( ) )
741
818
}
742
819
}
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