Skip to content

Commit 1512e9d

Browse files
committed
Impl receive_payment
1 parent c032a10 commit 1512e9d

File tree

2 files changed

+63
-9
lines changed

2 files changed

+63
-9
lines changed

src/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub enum LdkLiteError {
2626
Decode(msgs::DecodeError),
2727
/// A wrapped LDK `PaymentError`
2828
Payment(payment::PaymentError),
29+
/// A wrapped LDK `SignOrCreationError`
30+
InvoiceCreation(lightning_invoice::SignOrCreationError),
2931
/// A wrapped BDK error
3032
Bdk(bdk::Error),
3133
/// A wrapped `Bip32` error
@@ -46,6 +48,7 @@ impl fmt::Display for LdkLiteError {
4648
LdkLiteError::Decode(ref e) => write!(f, "LDK decode error: {}", e),
4749
// TODO: print more sensible things based on the type of payment error
4850
LdkLiteError::Payment(ref e) => write!(f, "LDK payment error: {:?}", e),
51+
LdkLiteError::InvoiceCreation(ref e) => write!(f, "LDK invoice sign or creation error: {:?}", e),
4952
LdkLiteError::Bdk(ref e) => write!(f, "BDK error: {}", e),
5053
LdkLiteError::Bip32(ref e) => write!(f, "Bitcoin error: {}", e),
5154
LdkLiteError::Io(ref e) => write!(f, "IO error: {}", e),
@@ -74,6 +77,12 @@ impl From<payment::PaymentError> for LdkLiteError {
7477
}
7578
}
7679

80+
impl From<lightning_invoice::SignOrCreationError> for LdkLiteError {
81+
fn from(e: lightning_invoice::SignOrCreationError) -> Self {
82+
Self::InvoiceCreation(e)
83+
}
84+
}
85+
7786
impl From<bdk::Error> for LdkLiteError {
7887
fn from(e: bdk::Error) -> Self {
7988
Self::Bdk(e)

src/lib.rs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use lightning_persister::FilesystemPersister;
6363
use lightning_net_tokio::SocketDescriptor;
6464

6565
use lightning_invoice::utils::DefaultRouter;
66-
use lightning_invoice::{payment, Invoice};
66+
use lightning_invoice::{payment, Currency, Invoice};
6767

6868
use bdk::blockchain::esplora::EsploraBlockchain;
6969
use bdk::blockchain::{GetBlockHash, GetHeight};
@@ -606,7 +606,8 @@ impl LdkLite {
606606
Ok(_payment_id) => {
607607
let payee_pubkey = invoice.recover_payee_pub_key();
608608
// 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?
610611
let amt_msat = invoice.amount_milli_satoshis().unwrap();
611612
log_info!(self.logger, "initiated sending {} msats to {}", amt_msat, payee_pubkey);
612613
PaymentStatus::Pending
@@ -685,12 +686,56 @@ impl LdkLite {
685686

686687
Ok(payment_hash)
687688
}
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+
692737
/// 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> {
694739
let payment_hash = PaymentHash(*payment_hash);
695740

696741
{
@@ -712,10 +757,10 @@ impl LdkLite {
712757

713758
//
714759
// // Query for information about our channels
715-
// pub channel_info(&mut self) -> ChannelInfo;
760+
// pub channel_info(&self) -> ChannelInfo;
716761
//
717762
// // Query for information about our on-chain/funding status.
718-
// pub funding_info(&mut self) -> FundingInfo;
763+
// pub funding_info(&self) -> FundingInfo;
719764
//}
720765
}
721766

0 commit comments

Comments
 (0)