Skip to content

Commit 572c455

Browse files
nepetrustyrussell
authored andcommitted
lightningd: add override amt to invoice_check_payment
Adds `expected_msat_override` to the `invoice_check_payment` check. If it's set, it will be used to override the invoice amount as the expected amount of the payment check. This enables us to charge a different amount for a payment than the amount stated on the invoice. Signed-off-by: Peter Neuroth <pet.v.ne@gmail.com>
1 parent 193f24a commit 572c455

File tree

5 files changed

+14
-6
lines changed

5 files changed

+14
-6
lines changed

lightningd/htlc_set.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ void htlc_set_add_(struct lightningd *ld,
150150
* [Failure Messages](#failure-messages)
151151
* - Note: "amount paid" specified there is the `total_msat` field.
152152
*/
153-
details = invoice_check_payment(tmpctx, ld, payment_hash,
154-
total_msat, payment_secret, &err);
153+
details = invoice_check_payment(tmpctx, ld, payment_hash, total_msat,
154+
NULL, payment_secret, &err);
155155
if (!details) {
156156
log_debug(log, "payment failed: %s", err);
157157
fail(arg, take(failmsg_incorrect_or_unknown(NULL, ld, msat)));

lightningd/invoice.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ invoice_check_payment(const tal_t *ctx,
332332
struct lightningd *ld,
333333
const struct sha256 *payment_hash,
334334
const struct amount_msat msat,
335+
const struct amount_msat *expected_msat_override,
335336
const struct secret *payment_secret,
336337
const char **err)
337338
{
@@ -408,15 +409,19 @@ invoice_check_payment(const tal_t *ctx,
408409
if (details->msat != NULL) {
409410
struct amount_msat twice;
410411

411-
if (amount_msat_less(msat, *details->msat)) {
412+
/* Override the expected amount. */
413+
struct amount_msat expected_msat =
414+
expected_msat_override ? *expected_msat_override : *details->msat;
415+
416+
if (amount_msat_less(msat, expected_msat)) {
412417
*err = tal_fmt(ctx, "Attempt to pay %s with amount %s < %s",
413418
fmt_sha256(tmpctx, &details->rhash),
414419
fmt_amount_msat(tmpctx, msat),
415-
fmt_amount_msat(tmpctx, *details->msat));
420+
fmt_amount_msat(tmpctx, expected_msat));
416421
return tal_free(details);
417422
}
418423

419-
if (amount_msat_add(&twice, *details->msat, *details->msat)
424+
if (amount_msat_add(&twice, expected_msat, expected_msat)
420425
&& amount_msat_greater(msat, twice)) {
421426
*err = tal_fmt(ctx, "Attempt to pay %s with amount %s > %s",
422427
fmt_sha256(tmpctx, &details->rhash),

lightningd/invoice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct invoice_details {
5050
* @ld: lightningd
5151
* @payment_hash: hash of preimage they want.
5252
* @msat: amount they offer to pay.
53+
* @expected_msat_override: if set: overrides the amount we expect to be payed.
5354
* @payment_secret: they payment secret they sent, if any.
5455
* @err: error string if it returns NULL.
5556
*
@@ -59,6 +60,7 @@ const struct invoice_details *invoice_check_payment(const tal_t *ctx,
5960
struct lightningd *ld,
6061
const struct sha256 *payment_hash,
6162
const struct amount_msat msat,
63+
const struct amount_msat *expected_msat_override,
6264
const struct secret *payment_secret,
6365
const char **err);
6466

lightningd/pay.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ static struct command_result *self_payment(struct lightningd *ld,
14821482
local_invreq_id);
14831483

14841484
/* Now, resolve the invoice */
1485-
inv = invoice_check_payment(tmpctx, ld, rhash, msat, payment_secret, &err);
1485+
inv = invoice_check_payment(tmpctx, ld, rhash, msat, NULL, payment_secret, &err);
14861486
if (!inv) {
14871487
struct routing_failure *fail;
14881488
wallet_payment_set_status(ld->wallet, rhash, partid, groupid,

wallet/test/run-wallet.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ const struct invoice_details *invoice_check_payment(const tal_t *ctx UNNEEDED,
391391
struct lightningd *ld UNNEEDED,
392392
const struct sha256 *payment_hash UNNEEDED,
393393
const struct amount_msat msat UNNEEDED,
394+
const struct amount_msat *expected_msat_override UNNEEDED,
394395
const struct secret *payment_secret UNNEEDED,
395396
const char **err UNNEEDED)
396397
{ fprintf(stderr, "invoice_check_payment called!\n"); abort(); }

0 commit comments

Comments
 (0)