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

Function invoice_check_payment

lightningd/invoice.c:370–465  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

368 struct invoice_payment_hook_payload *);
369
370const struct invoice_details *
371invoice_check_payment(const tal_t *ctx,
372 struct lightningd *ld,
373 const struct sha256 *payment_hash,
374 const struct amount_msat msat,
375 const struct secret *payment_secret)
376{
377 struct invoice invoice;
378 const struct invoice_details *details;
379
380 /* BOLT #4:
381 * - if the payment hash has already been paid:
382 * - MAY treat the payment hash as unknown.
383 * - MAY succeed in accepting the HTLC.
384 *...
385 * - if the payment hash is unknown:
386 * - MUST fail the HTLC.
387 * - MUST return an `incorrect_or_unknown_payment_details` error.
388 */
389 if (!wallet_invoice_find_unpaid(ld->wallet, &invoice, payment_hash)) {
390 log_debug(ld->log, "Unknown paid invoice %s",
391 type_to_string(tmpctx, struct sha256, payment_hash));
392 if (wallet_invoice_find_by_rhash(ld->wallet, &invoice, payment_hash)) {
393 log_debug(ld->log, "ALREADY paid invoice %s",
394 type_to_string(tmpctx, struct sha256, payment_hash));
395 }
396 return NULL;
397 }
398
399 details = wallet_invoice_details(ctx, ld->wallet, invoice);
400
401 /* BOLT #4:
402 * - if the `payment_secret` doesn't match the expected value for that
403 * `payment_hash`, or the `payment_secret` is required and is not
404 * present:
405 * - MUST fail the HTLC.
406 */
407 if (feature_is_set(details->features, COMPULSORY_FEATURE(OPT_VAR_ONION))
408 && !payment_secret) {
409 log_debug(ld->log, "Attept to pay %s without secret",
410 type_to_string(tmpctx, struct sha256, &details->rhash));
411 return tal_free(details);
412 }
413
414 if (payment_secret) {
415 struct secret expected;
416
417 if (details->invstring && strstarts(details->invstring, "lni1"))
418 invoice_secret_bolt12(ld, details->invstring, &expected);
419 else
420 invoice_secret(&details->r, &expected);
421 if (!secret_eq_consttime(payment_secret, &expected)) {
422 log_debug(ld->log, "Attept to pay %s with wrong secret",
423 type_to_string(tmpctx, struct sha256,
424 &details->rhash));
425 return tal_free(details);
426 }
427 }

Callers 1

htlc_set_addFunction · 0.85

Calls 12

tal_freeFunction · 0.85
invoice_secret_bolt12Function · 0.85
invoice_secretFunction · 0.85
secret_eq_consttimeFunction · 0.85
amount_msat_lessFunction · 0.85
amount_msat_addFunction · 0.85
amount_msat_greaterFunction · 0.85
type_to_stringClass · 0.50
wallet_invoice_detailsFunction · 0.50
feature_is_setFunction · 0.50

Tested by

no test coverage detected