1212use crate :: config:: { Config , LDK_PAYMENT_RETRY_TIMEOUT } ;
1313use crate :: connection:: ConnectionManager ;
1414use crate :: error:: Error ;
15+ use crate :: hex_utils;
1516use crate :: liquidity:: LiquiditySource ;
1617use crate :: logger:: { log_error, log_info, FilesystemLogger , Logger } ;
1718use crate :: payment:: store:: {
@@ -30,11 +31,12 @@ use lightning::routing::router::{PaymentParameters, RouteParameters};
3031
3132use lightning_types:: payment:: { PaymentHash , PaymentPreimage } ;
3233
33- use lightning_invoice:: { Bolt11Invoice , Bolt11InvoiceDescription , Description } ;
34+ use lightning_invoice:: { Bolt11Invoice , Description } ;
3435
3536use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
3637use bitcoin:: hashes:: Hash ;
3738
39+ use std:: str:: FromStr ;
3840use std:: sync:: { Arc , RwLock } ;
3941
4042/// A payment handler allowing to create and pay [BOLT 11] invoices.
@@ -403,12 +405,23 @@ impl Bolt11Payment {
403405 /// given.
404406 ///
405407 /// The inbound payment will be automatically claimed upon arrival.
408+ #[ cfg( not( feature = "uniffi" ) ) ]
406409 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 ,
408412 ) -> Result < Bolt11Invoice , Error > {
409413 self . receive_inner ( Some ( amount_msat) , description, expiry_secs, None )
410414 }
411415
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+
412425 /// Returns a payable invoice that can be used to request a payment of the amount
413426 /// given for the given payment hash.
414427 ///
@@ -423,22 +436,44 @@ impl Bolt11Payment {
423436 /// [`PaymentClaimable`]: crate::Event::PaymentClaimable
424437 /// [`claim_for_hash`]: Self::claim_for_hash
425438 /// [`fail_for_hash`]: Self::fail_for_hash
439+ #[ cfg( not( feature = "uniffi" ) ) ]
426440 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 ,
428443 ) -> Result < Bolt11Invoice , Error > {
429444 self . receive_inner ( Some ( amount_msat) , description, expiry_secs, Some ( payment_hash) )
430445 }
431446
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+
432457 /// Returns a payable invoice that can be used to request and receive a payment for which the
433458 /// amount is to be determined by the user, also known as a "zero-amount" invoice.
434459 ///
435460 /// The inbound payment will be automatically claimed upon arrival.
461+ #[ cfg( not( feature = "uniffi" ) ) ]
436462 pub fn receive_variable_amount (
437- & self , description : & str , expiry_secs : u32 ,
463+ & self , description : & lightning_invoice :: Bolt11InvoiceDescription , expiry_secs : u32 ,
438464 ) -> Result < Bolt11Invoice , Error > {
439465 self . receive_inner ( None , description, expiry_secs, None )
440466 }
441467
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+
442477 /// Returns a payable invoice that can be used to request a payment for the given payment hash
443478 /// and the amount to be determined by the user, also known as a "zero-amount" invoice.
444479 ///
@@ -453,24 +488,32 @@ impl Bolt11Payment {
453488 /// [`PaymentClaimable`]: crate::Event::PaymentClaimable
454489 /// [`claim_for_hash`]: Self::claim_for_hash
455490 /// [`fail_for_hash`]: Self::fail_for_hash
491+ #[ cfg( not( feature = "uniffi" ) ) ]
456492 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 ,
458495 ) -> Result < Bolt11Invoice , Error > {
459496 self . receive_inner ( None , description, expiry_secs, Some ( payment_hash) )
460497 }
461498
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 ,
465502 ) -> 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+ }
469507
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 > {
470513 let invoice = {
471514 let invoice_params = Bolt11InvoiceParameters {
472515 amount_msats : amount_msat,
473- description : invoice_description,
516+ description : invoice_description. clone ( ) ,
474517 invoice_expiry_delta_secs : Some ( expiry_secs) ,
475518 payment_hash : manual_claim_payment_hash,
476519 ..Default :: default ( )
@@ -530,9 +573,10 @@ impl Bolt11Payment {
530573 /// channel to us. We'll use its cheapest offer otherwise.
531574 ///
532575 /// [LSPS2]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md
576+ #[ cfg( not( feature = "uniffi" ) ) ]
533577 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 > ,
536580 ) -> Result < Bolt11Invoice , Error > {
537581 self . receive_via_jit_channel_inner (
538582 Some ( amount_msat) ,
@@ -543,6 +587,22 @@ impl Bolt11Payment {
543587 )
544588 }
545589
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+
546606 /// Returns a payable invoice that can be used to request a variable amount payment (also known
547607 /// as "zero-amount" invoice) and receive it via a newly created just-in-time (JIT) channel.
548608 ///
@@ -554,8 +614,9 @@ impl Bolt11Payment {
554614 /// We'll use its cheapest offer otherwise.
555615 ///
556616 /// [LSPS2]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md
617+ #[ cfg( not( feature = "uniffi" ) ) ]
557618 pub fn receive_variable_amount_via_jit_channel (
558- & self , description : & str , expiry_secs : u32 ,
619+ & self , description : & lightning_invoice :: Bolt11InvoiceDescription , expiry_secs : u32 ,
559620 max_proportional_lsp_fee_limit_ppm_msat : Option < u64 > ,
560621 ) -> Result < Bolt11Invoice , Error > {
561622 self . receive_via_jit_channel_inner (
@@ -567,9 +628,25 @@ impl Bolt11Payment {
567628 )
568629 }
569630
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+
570647 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 > ,
573650 max_proportional_lsp_fee_limit_ppm_msat : Option < u64 > ,
574651 ) -> Result < Bolt11Invoice , Error > {
575652 let liquidity_source =
@@ -740,3 +817,49 @@ impl Bolt11Payment {
740817 Ok ( ( ) )
741818 }
742819}
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