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

Function invoice_check_payment

lightningd/invoice.c:331–441  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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