| 2319 | }; |
| 2320 | |
| 2321 | static struct command_result * |
| 2322 | invoice_fetched(struct command *cmd, |
| 2323 | const char *method, |
| 2324 | const char *buf, |
| 2325 | const jsmntok_t *result, |
| 2326 | struct xpay_params *params) |
| 2327 | { |
| 2328 | const char *inv, *err; |
| 2329 | |
| 2330 | inv = json_strdup(tmpctx, buf, json_get_member(buf, result, "invoice")); |
| 2331 | inv = to_canonical_invstr(tmpctx, inv); |
| 2332 | |
| 2333 | /* BOLT #12: |
| 2334 | * - if `invreq_amount` is present: |
| 2335 | * - MUST reject the invoice if `invoice_amount` is not equal to `invreq_amount` |
| 2336 | * - otherwise: |
| 2337 | * - SHOULD confirm authorization if `invoice_amount`.`msat` is not within |
| 2338 | * the amount range authorized. |
| 2339 | */ |
| 2340 | /* invoice_amount is not one of the fields the invoice must copy from our |
| 2341 | * invoice_request, so it is set independently of what we asked for and |
| 2342 | * has to be checked here. We set invreq_amount iff we were given an |
| 2343 | * amount, which selects which of the two rules above applies. */ |
| 2344 | if (params->authorized_msat) { |
| 2345 | struct amount_msat invoice_msat; |
| 2346 | struct tlv_invoice *b12inv |
| 2347 | = invoice_decode(tmpctx, inv, strlen(inv), |
| 2348 | plugin_feature_set(cmd->plugin), |
| 2349 | chainparams, &err); |
| 2350 | if (!b12inv) |
| 2351 | return command_fail(cmd, OFFER_BAD_INVREQ_REPLY, |
| 2352 | "Invalid bolt12 invoice: %s", err); |
| 2353 | /* invoice_decode() has already insisted on invoice_amount. */ |
| 2354 | invoice_msat = amount_msat(*b12inv->invoice_amount); |
| 2355 | if (params->msat) { |
| 2356 | if (!amount_msat_eq(invoice_msat, *params->authorized_msat)) |
| 2357 | return command_fail(cmd, OFFER_BAD_INVREQ_REPLY, |
| 2358 | "Invoice amount is %s, but we asked for %s", |
| 2359 | fmt_amount_msat(tmpctx, invoice_msat), |
| 2360 | fmt_amount_msat(tmpctx, |
| 2361 | *params->authorized_msat)); |
| 2362 | } else if (amount_msat_greater(invoice_msat, |
| 2363 | *params->authorized_msat)) { |
| 2364 | return command_fail(cmd, OFFER_BAD_INVREQ_REPLY, |
| 2365 | "Invoice amount is %s, more than the %s we authorized", |
| 2366 | fmt_amount_msat(tmpctx, invoice_msat), |
| 2367 | fmt_amount_msat(tmpctx, |
| 2368 | *params->authorized_msat)); |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | return xpay_core(cmd, inv, |
| 2373 | NULL, params->maxfee, params->layers, |
| 2374 | params->retryfor, params->partial, params->maxdelay, |
| 2375 | params->label, NULL, false, false, |
| 2376 | params->includefees_msat); |
| 2377 | } |
| 2378 |
nothing calls this directly
no test coverage detected