MCPcopy Create free account
hub / github.com/ElementsProject/lightning / invoice_check_payment

Function invoice_check_payment

lightningd/invoice.c:332–442  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

330 struct invoice_payment_hook_payload *);
331
332const struct invoice_details *
333invoice_check_payment(const tal_t *ctx,
334 struct lightningd *ld,
335 const struct sha256 *payment_hash,
336 const struct amount_msat msat,
337 const struct amount_msat *expected_msat_override,
338 const struct secret *payment_secret,
339 const char **err)
340{
341 u64 inv_dbid;
342 const struct invoice_details *details;
343 bool bolt12_payment;
344
345 /* BOLT #4:
346 * - if the payment hash has already been paid:
347 * - MAY treat the payment hash as unknown.
348 * - MAY succeed in accepting the HTLC.
349 *...
350 * - if the payment hash is unknown:
351 * - MUST fail the HTLC.
352 * - MUST return an `incorrect_or_unknown_payment_details` error.
353 */
354 if (!invoices_find_unpaid(ld->wallet->invoices, &inv_dbid, payment_hash)) {
355 if (invoices_find_by_rhash(ld->wallet->invoices, &inv_dbid, payment_hash)) {
356 *err = tal_fmt(ctx, "Already paid or expired invoice %s",
357 fmt_sha256(tmpctx, payment_hash));
358 } else {
359 *err = tal_fmt(ctx, "Unknown invoice %s",
360 fmt_sha256(tmpctx, payment_hash));
361 }
362 return NULL;
363 }
364
365 details = invoices_get_details(ctx, ld->wallet->invoices, inv_dbid);
366 bolt12_payment = details->invstring && strstarts(details->invstring, "lni1");
367
368 /* BOLT #4:
369 * - if the `payment_secret` doesn't match the expected value for that
370 * `payment_hash`, or the `payment_secret` is required and is not
371 * present:
372 * - MUST fail the HTLC.
373 */
374 /* For BOLT12: we stash path_secret in this field if it's of
375 * correct length, so it must be present */
376 if ((feature_is_set(details->features, COMPULSORY_FEATURE(OPT_VAR_ONION))
377 || bolt12_payment)
378 && !payment_secret) {
379 *err = tal_fmt(ctx, "Attempt to pay %s without secret",
380 fmt_sha256(tmpctx, &details->rhash));
381 return tal_free(details);
382 }
383
384 if (payment_secret) {
385 struct secret expected;
386
387 /* FIXME: BOLT 12 should require a *real* secret: a signature of the
388 * request using the payer_id key. This just means they've
389 * *seen* the invoice! */

Callers 2

self_paymentFunction · 0.70
htlc_set_add_Function · 0.70

Calls 13

fmt_sha256Function · 0.85
feature_is_setFunction · 0.85
tal_freeFunction · 0.85
bolt12_path_secretFunction · 0.85
invoice_secretFunction · 0.85
secret_eq_consttimeFunction · 0.85
amount_msat_addFunction · 0.85
amount_msat_greaterFunction · 0.85
invoices_find_unpaidFunction · 0.50
invoices_find_by_rhashFunction · 0.50
invoices_get_detailsFunction · 0.50
amount_msat_lessFunction · 0.50

Tested by

no test coverage detected