lightningd: extend preapproveinvoice and preapprovekeysend to check with HSM.

If they support it, we can actually ask hsmd when we are called in
check mode.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2024-04-04 14:06:13 +10:30
parent 6ea95da342
commit bd6cf99e4d

View file

@ -1873,17 +1873,25 @@ static struct command_result *json_preapproveinvoice(struct command *cmd,
u8 *req;
const u8 *msg;
if (!param(cmd, buffer, params,
/* FIXME: parameter should be invstring now */
p_req("bolt11", param_invstring, &invstring),
if (!param_check(cmd, buffer, params,
/* FIXME: parameter should be invstring now */
p_req("bolt11", param_invstring, &invstring),
NULL))
return command_param_failed();
/* Old version didn't have `bool check_only` at end */
if (!hsm_capable(cmd->ld, WIRE_HSMD_PREAPPROVE_INVOICE_CHECK))
if (!hsm_capable(cmd->ld, WIRE_HSMD_PREAPPROVE_INVOICE_CHECK)) {
/* We can't just check this, so "succeed". Log message for
* tests though */
if (command_check_only(cmd)) {
log_debug(cmd->ld->log, "hsmd too old to check preapprove");
return command_check_done(cmd);
}
req = towire_hsmd_preapprove_invoice(NULL, invstring);
else
req = towire_hsmd_preapprove_invoice_check(NULL, invstring, false);
} else {
req = towire_hsmd_preapprove_invoice_check(NULL, invstring,
command_check_only(cmd));
}
msg = hsm_sync_req(tmpctx, cmd->ld, take(req));
@ -1897,6 +1905,9 @@ static struct command_result *json_preapproveinvoice(struct command *cmd,
if (!approved)
return command_fail(cmd, PAY_INVOICE_PREAPPROVAL_DECLINED, "invoice was declined");
if (command_check_only(cmd))
return command_check_done(cmd);
response = json_stream_success(cmd);
return command_success(cmd, response);
}
@ -1922,18 +1933,25 @@ static struct command_result *json_preapprovekeysend(struct command *cmd,
const u8 *msg;
u8 *req;
if (!param(cmd, buffer, params,
p_req("destination", param_node_id, &destination),
p_req("payment_hash", param_sha256, &payment_hash),
p_req("amount_msat", param_msat, &amount),
NULL))
if (!param_check(cmd, buffer, params,
p_req("destination", param_node_id, &destination),
p_req("payment_hash", param_sha256, &payment_hash),
p_req("amount_msat", param_msat, &amount),
NULL))
return command_param_failed();
if (!hsm_capable(cmd->ld, WIRE_HSMD_PREAPPROVE_KEYSEND_CHECK))
if (!hsm_capable(cmd->ld, WIRE_HSMD_PREAPPROVE_KEYSEND_CHECK)) {
/* We can't just check this, so "succeed". Log message for
* tests though */
if (command_check_only(cmd)) {
log_debug(cmd->ld->log, "hsmd too old to check preapprove");
return command_check_done(cmd);
}
req = towire_hsmd_preapprove_keysend(NULL, destination, payment_hash, *amount);
else
} else {
req = towire_hsmd_preapprove_keysend_check(NULL, destination, payment_hash,
*amount, false);
*amount, command_check_only(cmd));
}
msg = hsm_sync_req(tmpctx, cmd->ld, take(req));
@ -1947,6 +1965,9 @@ static struct command_result *json_preapprovekeysend(struct command *cmd,
if (!approved)
return command_fail(cmd, PAY_KEYSEND_PREAPPROVAL_DECLINED, "keysend was declined");
if (command_check_only(cmd))
return command_check_done(cmd);
response = json_stream_success(cmd);
return command_success(cmd, response);
}