@@ -63,7 +63,7 @@ use lightning_persister::FilesystemPersister;
63
63
use lightning_net_tokio:: SocketDescriptor ;
64
64
65
65
use lightning_invoice:: utils:: DefaultRouter ;
66
- use lightning_invoice:: { payment, Invoice } ;
66
+ use lightning_invoice:: { payment, Currency , Invoice } ;
67
67
68
68
use bdk:: blockchain:: esplora:: EsploraBlockchain ;
69
69
use bdk:: blockchain:: { GetBlockHash , GetHeight } ;
@@ -606,7 +606,8 @@ impl LdkLite {
606
606
Ok ( _payment_id) => {
607
607
let payee_pubkey = invoice. recover_payee_pub_key ( ) ;
608
608
// TODO: is this unwrap safe? Would a payment to an invoice with None amount ever
609
- // succeed?
609
+ // succeed? Should we allow to set the amount in the interface or via a dedicated
610
+ // method?
610
611
let amt_msat = invoice. amount_milli_satoshis ( ) . unwrap ( ) ;
611
612
log_info ! ( self . logger, "initiated sending {} msats to {}" , amt_msat, payee_pubkey) ;
612
613
PaymentStatus :: Pending
@@ -685,12 +686,56 @@ impl LdkLite {
685
686
686
687
Ok ( payment_hash)
687
688
}
688
- //
689
- // // Create an invoice to receive a payment
690
- // pub receive_payment(&mut self, amount: Option<u64>) -> Invoice;
691
- //
689
+
690
+ // TODO: Should we provide a configurable default for the expiry, or force the user to supply it on every call?
691
+ /// Returns a payable invoice that can be used to request and receive a payment.
692
+ pub fn receive_payment (
693
+ & self , amount_msat : Option < u64 > , description : & str , expiry_secs : u32 ,
694
+ ) -> Result < Invoice , Error > {
695
+ let mut inbound_payments_lock = self . inbound_payments . lock ( ) . unwrap ( ) ;
696
+
697
+ let currency = match self . config . network {
698
+ bitcoin:: Network :: Bitcoin => Currency :: Bitcoin ,
699
+ bitcoin:: Network :: Testnet => Currency :: BitcoinTestnet ,
700
+ bitcoin:: Network :: Regtest => Currency :: Regtest ,
701
+ bitcoin:: Network :: Signet => Currency :: Signet ,
702
+ } ;
703
+ let keys_manager = Arc :: clone ( & self . keys_manager ) ;
704
+ let invoice = match lightning_invoice:: utils:: create_invoice_from_channelmanager (
705
+ & self . channel_manager ,
706
+ keys_manager,
707
+ currency,
708
+ amount_msat,
709
+ description. to_string ( ) ,
710
+ expiry_secs,
711
+ ) {
712
+ Ok ( inv) => {
713
+ log_info ! ( self . logger, "generated invoice: {}" , inv) ;
714
+ inv
715
+ }
716
+ Err ( e) => {
717
+ let err_str = & e. to_string ( ) ;
718
+ log_error ! ( self . logger, "failed to create invoice: {:?}" , err_str) ;
719
+ // TODO;
720
+ return Err ( Error :: InvoiceCreation ( e) ) ;
721
+ }
722
+ } ;
723
+
724
+ let payment_hash = PaymentHash ( invoice. payment_hash ( ) . clone ( ) . into_inner ( ) ) ;
725
+ inbound_payments_lock. insert (
726
+ payment_hash,
727
+ PaymentInfo {
728
+ preimage : None ,
729
+ secret : Some ( invoice. payment_secret ( ) . clone ( ) ) ,
730
+ status : PaymentStatus :: Pending ,
731
+ amount_msat,
732
+ } ,
733
+ ) ;
734
+ Ok ( invoice)
735
+ }
736
+
692
737
/// Query for information about the status of a specific payment.
693
- pub fn payment_info ( & mut self , payment_hash : & [ u8 ; 32 ] ) -> Option < PaymentInfo > {
738
+ pub fn payment_info ( & self , payment_hash : & [ u8 ; 32 ] ) -> Option < PaymentInfo > {
694
739
let payment_hash = PaymentHash ( * payment_hash) ;
695
740
696
741
{
@@ -712,10 +757,10 @@ impl LdkLite {
712
757
713
758
//
714
759
// // Query for information about our channels
715
- // pub channel_info(&mut self) -> ChannelInfo;
760
+ // pub channel_info(&self) -> ChannelInfo;
716
761
//
717
762
// // Query for information about our on-chain/funding status.
718
- // pub funding_info(&mut self) -> FundingInfo;
763
+ // pub funding_info(&self) -> FundingInfo;
719
764
//}
720
765
}
721
766
0 commit comments