Skip to content

Commit 4538c8a

Browse files
committed
f Allow payment of "zero-amount" invoices
1 parent b423804 commit 4538c8a

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

src/lib.rs

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -848,9 +848,72 @@ impl Node {
848848
) {
849849
Ok(_payment_id) => {
850850
let payee_pubkey = invoice.recover_payee_pub_key();
851-
// TODO: is this unwrap safe? Would a payment to an invoice with None amount ever
852-
// succeed? Should we allow to set the amount in the interface or via a dedicated
853-
// method?
851+
let amt_msat = invoice.amount_milli_satoshis().unwrap();
852+
log_info!(self.logger, "Initiated sending {} msats to {}", amt_msat, payee_pubkey);
853+
854+
outbound_payments_lock.insert(
855+
payment_hash,
856+
PaymentInfo {
857+
preimage: None,
858+
secret: payment_secret,
859+
status: PaymentStatus::Pending,
860+
amount_msat: invoice.amount_milli_satoshis(),
861+
},
862+
);
863+
864+
Ok(payment_hash)
865+
}
866+
Err(payment::PaymentError::Invoice(e)) => {
867+
log_error!(self.logger, "Failed to send payment due to invalid invoice: {}", e);
868+
Err(Error::InvalidInvoice)
869+
}
870+
Err(payment::PaymentError::Sending(e)) => {
871+
log_error!(self.logger, "Failed to send payment: {:?}", e);
872+
873+
outbound_payments_lock.insert(
874+
payment_hash,
875+
PaymentInfo {
876+
preimage: None,
877+
secret: payment_secret,
878+
status: PaymentStatus::Failed,
879+
amount_msat: invoice.amount_milli_satoshis(),
880+
},
881+
);
882+
Err(Error::PaymentFailed)
883+
}
884+
}
885+
}
886+
887+
/// Send a payement given a so-called "zero amount" invoice, i.e., an invoice that leaves the
888+
/// amount paid to be determined by the user.
889+
pub fn send_adjustable_value_payment(
890+
&self, invoice: Invoice, amount_msat: u64,
891+
) -> Result<PaymentHash, Error> {
892+
if self.running.read().unwrap().is_none() {
893+
return Err(Error::NotRunning);
894+
}
895+
896+
let mut outbound_payments_lock = self.outbound_payments.lock().unwrap();
897+
898+
let payment_hash = PaymentHash((*invoice.payment_hash()).into_inner());
899+
let payment_secret = Some(*invoice.payment_secret());
900+
901+
if invoice.amount_milli_satoshis().is_some() {
902+
log_error!(
903+
self.logger,
904+
"Failed to pay the given invoice: expected \"zero-amount\" invoice, please use send_payment."
905+
);
906+
return Err(Error::InvalidInvoice);
907+
}
908+
909+
match lightning_invoice::payment::pay_zero_value_invoice(
910+
&invoice,
911+
amount_msat,
912+
Retry::Timeout(LDK_PAYMENT_RETRY_TIMEOUT),
913+
self.channel_manager.as_ref(),
914+
) {
915+
Ok(_payment_id) => {
916+
let payee_pubkey = invoice.recover_payee_pub_key();
854917
let amt_msat = invoice.amount_milli_satoshis().unwrap();
855918
log_info!(self.logger, "Initiated sending {} msats to {}", amt_msat, payee_pubkey);
856919

0 commit comments

Comments
 (0)