From 1fb80a360865acc5728f8120a2724dccc4b1218d Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 27 Feb 2024 11:53:05 +1030 Subject: [PATCH] pay: move preapproveinvoice command out so it can be called bu `check`. Signed-off-by: Rusty Russell Changelog-Added: JSON-RPC: `check` `keysend` now checks with HSM that it will approve it (use instead of `preapprovekeysend`). --- plugins/libplugin-pay.h | 1 - plugins/pay.c | 43 ++++++++++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/plugins/libplugin-pay.h b/plugins/libplugin-pay.h index 77783c6274b8..f04bb17efd27 100644 --- a/plugins/libplugin-pay.h +++ b/plugins/libplugin-pay.h @@ -453,7 +453,6 @@ REGISTER_PAYMENT_MODIFIER_HEADER(local_channel_hints, void); * each of those channels can bear. */ REGISTER_PAYMENT_MODIFIER_HEADER(payee_incoming_limit, void); REGISTER_PAYMENT_MODIFIER_HEADER(route_exclusions, struct route_exclusions_data); -REGISTER_PAYMENT_MODIFIER_HEADER(check_preapproveinvoice, void); struct payment *payment_new(tal_t *ctx, struct command *cmd, diff --git a/plugins/pay.c b/plugins/pay.c index fa5d3e4ece26..f684f595f331 100644 --- a/plugins/pay.c +++ b/plugins/pay.c @@ -969,7 +969,6 @@ payment_listsendpays_previous(struct command *cmd, const char *buf, } struct payment_modifier *paymod_mods[] = { - &check_preapproveinvoice_pay_mod, /* NOTE: The order in which these four paymods are executed is * significant! * local_channel_hints *must* execute first before route_exclusions @@ -1009,6 +1008,32 @@ static void destroy_payment(struct payment *p) list_del(&p->list); } +static struct command_result * +preapproveinvoice_succeed(struct command *cmd, + const char *buf, + const jsmntok_t *result, + struct payment *p) +{ + struct out_req *req; + + /* Now we can conclude `check` command */ + if (command_check_only(cmd)) { + return command_check_done(cmd); + } + + list_add_tail(&payments, &p->list); + tal_add_destructor(p, destroy_payment); + /* We're keeping this around now */ + tal_steal(cmd->plugin, p); + + req = jsonrpc_request_start(cmd->plugin, cmd, "listsendpays", + payment_listsendpays_previous, + payment_listsendpays_previous, p); + + json_add_sha256(req->js, "payment_hash", p->payment_hash); + return send_outreq(cmd->plugin, req); +} + static struct command_result *json_pay(struct command *cmd, const char *buf, const jsmntok_t *params) @@ -1035,7 +1060,7 @@ static struct command_result *json_pay(struct command *cmd, /* If any of the modifiers need to add params to the JSON-RPC call we * would add them to the `param()` call below, and have them be * initialized directly that way. */ - if (!param(cmd, buf, params, + if (!param_check(cmd, buf, params, /* FIXME: parameter should be invstring now */ p_req("bolt11", param_invstring, &b11str), p_opt("amount_msat", param_msat, &msat), @@ -1256,16 +1281,12 @@ static struct command_result *json_pay(struct command *cmd, tal_free(dev_use_shadow); p->label = tal_steal(p, label); - list_add_tail(&payments, &p->list); - tal_add_destructor(p, destroy_payment); - /* We're keeping this around now */ - tal_steal(cmd->plugin, p); - req = jsonrpc_request_start(cmd->plugin, cmd, "listsendpays", - payment_listsendpays_previous, - payment_listsendpays_previous, p); - - json_add_sha256(req->js, "payment_hash", p->payment_hash); + /* Now preapprove, then start payment. */ + req = jsonrpc_request_start(p->plugin, cmd, "preapproveinvoice", + &preapproveinvoice_succeed, + &forward_error, p); + json_add_string(req->js, "bolt11", p->invstring); return send_outreq(cmd->plugin, req); }