@@ -30,12 +30,13 @@ use lightning::routing::router::{PaymentParameters, RouteParameters};
3030
3131use lightning_types:: payment:: { PaymentHash , PaymentPreimage } ;
3232
33- use lightning_invoice:: { Bolt11Invoice , Bolt11InvoiceDescription , Description } ;
33+ use lightning_invoice:: { Bolt11Invoice , Bolt11InvoiceDescription , Description } ;
3434
35- use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
3635use bitcoin:: hashes:: Hash ;
36+ use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
3737
3838use std:: sync:: { Arc , RwLock } ;
39+ use std:: str:: FromStr ;
3940
4041/// A payment handler allowing to create and pay [BOLT 11] invoices.
4142///
@@ -403,12 +404,22 @@ impl Bolt11Payment {
403404 /// given.
404405 ///
405406 /// The inbound payment will be automatically claimed upon arrival.
407+ #[ cfg( not( feature = "uniffi" ) ) ]
406408 pub fn receive (
407- & self , amount_msat : u64 , description : & str , expiry_secs : u32 ,
409+ & self , amount_msat : u64 , description : & Bolt11InvoiceDescription , expiry_secs : u32 ,
408410 ) -> Result < Bolt11Invoice , Error > {
409411 self . receive_inner ( Some ( amount_msat) , description, expiry_secs, None )
410412 }
411413
414+ #[ cfg( feature = "uniffi" ) ]
415+ pub fn receive (
416+ & self , amount_msat : u64 , description : & Bolt11InvoiceStringDescription , expiry_secs : u32 ,
417+ ) -> Result < Bolt11Invoice , Error > {
418+ let invoice_description: Bolt11InvoiceDescription = description. try_into ( ) ?;
419+ self . receive_inner ( Some ( amount_msat) , & invoice_description, expiry_secs, None )
420+ }
421+
422+
412423 /// Returns a payable invoice that can be used to request a payment of the amount
413424 /// given for the given payment hash.
414425 ///
@@ -423,22 +434,40 @@ impl Bolt11Payment {
423434 /// [`PaymentClaimable`]: crate::Event::PaymentClaimable
424435 /// [`claim_for_hash`]: Self::claim_for_hash
425436 /// [`fail_for_hash`]: Self::fail_for_hash
437+ #[ cfg( not( feature = "uniffi" ) ) ]
426438 pub fn receive_for_hash (
427- & self , amount_msat : u64 , description : & str , expiry_secs : u32 , payment_hash : PaymentHash ,
439+ & self , amount_msat : u64 , description : & Bolt11InvoiceDescription , expiry_secs : u32 , payment_hash : PaymentHash ,
428440 ) -> Result < Bolt11Invoice , Error > {
429441 self . receive_inner ( Some ( amount_msat) , description, expiry_secs, Some ( payment_hash) )
430442 }
443+
444+ #[ cfg( feature = "uniffi" ) ]
445+ pub fn receive_for_hash (
446+ & self , amount_msat : u64 , description : & Bolt11InvoiceStringDescription , expiry_secs : u32 , payment_hash : PaymentHash ,
447+ ) -> Result < Bolt11Invoice , Error > {
448+ let invoice_description: Bolt11InvoiceDescription = description. try_into ( ) ?;
449+ self . receive_inner ( Some ( amount_msat) , & invoice_description, expiry_secs, Some ( payment_hash) )
450+ }
431451
432452 /// Returns a payable invoice that can be used to request and receive a payment for which the
433453 /// amount is to be determined by the user, also known as a "zero-amount" invoice.
434454 ///
435455 /// The inbound payment will be automatically claimed upon arrival.
456+ #[ cfg( not( feature = "uniffi" ) ) ]
436457 pub fn receive_variable_amount (
437- & self , description : & str , expiry_secs : u32 ,
458+ & self , description : & Bolt11InvoiceDescription , expiry_secs : u32 ,
438459 ) -> Result < Bolt11Invoice , Error > {
439460 self . receive_inner ( None , description, expiry_secs, None )
440461 }
441462
463+ #[ cfg( feature = "uniffi" ) ]
464+ pub fn receive_variable_amount (
465+ & self , description : & Bolt11InvoiceStringDescription , expiry_secs : u32 ,
466+ ) -> Result < Bolt11Invoice , Error > {
467+ let invoice_description: Bolt11InvoiceDescription = description. try_into ( ) ?;
468+ self . receive_inner ( None , & invoice_description, expiry_secs, None )
469+ }
470+
442471 /// Returns a payable invoice that can be used to request a payment for the given payment hash
443472 /// and the amount to be determined by the user, also known as a "zero-amount" invoice.
444473 ///
@@ -453,24 +482,29 @@ impl Bolt11Payment {
453482 /// [`PaymentClaimable`]: crate::Event::PaymentClaimable
454483 /// [`claim_for_hash`]: Self::claim_for_hash
455484 /// [`fail_for_hash`]: Self::fail_for_hash
485+ #[ cfg( not( feature = "uniffi" ) ) ]
456486 pub fn receive_variable_amount_for_hash (
457- & self , description : & str , expiry_secs : u32 , payment_hash : PaymentHash ,
487+ & self , description : & Bolt11InvoiceDescription , expiry_secs : u32 , payment_hash : PaymentHash ,
458488 ) -> Result < Bolt11Invoice , Error > {
459489 self . receive_inner ( None , description, expiry_secs, Some ( payment_hash) )
460490 }
461491
462- fn receive_inner (
463- & self , amount_msat : Option < u64 > , description : & str , expiry_secs : u32 ,
464- manual_claim_payment_hash : Option < PaymentHash > ,
492+ # [ cfg ( feature = "uniffi" ) ]
493+ pub fn receive_variable_amount_for_hash (
494+ & self , description : & Bolt11InvoiceStringDescription , expiry_secs : u32 , payment_hash : PaymentHash ,
465495 ) -> Result < Bolt11Invoice , Error > {
466- let invoice_description = Bolt11InvoiceDescription :: Direct (
467- Description :: new ( description . to_string ( ) ) . map_err ( |_| Error :: InvoiceCreationFailed ) ? ,
468- ) ;
496+ let invoice_description: Bolt11InvoiceDescription = description . try_into ( ) ? ;
497+ self . receive_inner ( None , & invoice_description , expiry_secs , Some ( payment_hash ) )
498+ }
469499
500+ pub ( crate ) fn receive_inner (
501+ & self , amount_msat : Option < u64 > , invoice_description : & Bolt11InvoiceDescription , expiry_secs : u32 ,
502+ manual_claim_payment_hash : Option < PaymentHash > ,
503+ ) -> Result < Bolt11Invoice , Error > {
470504 let invoice = {
471505 let invoice_params = Bolt11InvoiceParameters {
472506 amount_msats : amount_msat,
473- description : invoice_description,
507+ description : invoice_description. clone ( ) ,
474508 invoice_expiry_delta_secs : Some ( expiry_secs) ,
475509 payment_hash : manual_claim_payment_hash,
476510 ..Default :: default ( )
@@ -740,3 +774,33 @@ impl Bolt11Payment {
740774 Ok ( ( ) )
741775 }
742776}
777+
778+ /// Represents the description of an invoice which has to be either a directly included string or
779+ /// a hash of a description provided out of band.
780+ pub enum Bolt11InvoiceStringDescription { // use same name (no string)
781+ /// Contains a full description.
782+ Direct {
783+ /// Description of what the invoice is for
784+ description : String
785+ } ,
786+ /// Contains a hash.
787+ Hash {
788+ /// Hash of the description of what the invoice is for
789+ hash : String
790+ }
791+ }
792+
793+ impl TryInto < Bolt11InvoiceDescription > for & Bolt11InvoiceStringDescription {
794+ type Error = Error ;
795+
796+ fn try_into ( self ) -> Result < Bolt11InvoiceDescription , Self :: Error > {
797+ match self {
798+ Bolt11InvoiceStringDescription :: Direct { description} => {
799+ Description :: new ( description. clone ( ) ) . map ( Bolt11InvoiceDescription :: Direct ) . map_err ( |_| Error :: InvalidInvoice )
800+ } ,
801+ Bolt11InvoiceStringDescription :: Hash { hash} => {
802+ Sha256 :: from_str ( & hash) . map ( lightning_invoice:: Sha256 ) . map ( Bolt11InvoiceDescription :: Hash ) . map_err ( |_| Error :: InvalidInvoice )
803+ } ,
804+ }
805+ }
806+ }
0 commit comments