plugins/offers: neaten fetchinvoice integration.

We already parse some fields, so hand them directly rather than
having fetchinvoice behave as if it's a raw hook.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2024-07-17 12:53:24 +09:30 committed by Vincenzo Palazzo
parent a782ea75b5
commit d664d52342
3 changed files with 39 additions and 37 deletions

View File

@ -333,34 +333,21 @@ badinv:
return command_hook_success(cmd); return command_hook_success(cmd);
} }
struct command_result *recv_modern_onion_message(struct command *cmd, struct command_result *handle_invoice_onion_message(struct command *cmd,
const char *buf, const char *buf,
const jsmntok_t *params) const jsmntok_t *om,
const struct secret *pathsecret)
{ {
const jsmntok_t *om, *secrettok;
struct sent *sent; struct sent *sent;
struct secret pathsecret;
struct command_result *err; struct command_result *err;
om = json_get_member(buf, params, "onion_message"); sent = find_sent_by_secret(pathsecret);
if (!sent)
secrettok = json_get_member(buf, om, "pathsecret");
if (!secrettok)
return NULL; return NULL;
json_to_secret(buf, secrettok, &pathsecret); plugin_log(cmd->plugin, LOG_DBG, "Received onion message reply for invoice_request: %.*s",
sent = find_sent_by_secret(&pathsecret); json_tok_full_len(om),
if (!sent) { json_tok_full(buf, om));
plugin_log(cmd->plugin, LOG_DBG,
"No match for modern onion %.*s",
json_tok_full_len(om),
json_tok_full(buf, om));
return NULL;
}
plugin_log(cmd->plugin, LOG_DBG, "Received modern onion message: %.*s",
json_tok_full_len(params),
json_tok_full(buf, params));
err = handle_error(cmd, sent, buf, om); err = handle_error(cmd, sent, buf, om);
if (err) if (err)

View File

@ -17,9 +17,13 @@ struct command_result *json_dev_rawrequest(struct command *cmd,
const char *buffer, const char *buffer,
const jsmntok_t *params); const jsmntok_t *params);
struct command_result *recv_modern_onion_message(struct command *cmd, /* Returns NULL if this wasn't one of ours. */
const char *buf, struct command_result *handle_invoice_onion_message(struct command *cmd,
const jsmntok_t *params); const char *buf,
const jsmntok_t *om,
const struct secret *pathsecret);
/* invoice_payment hook */
struct command_result *invoice_payment(struct command *cmd, struct command_result *invoice_payment(struct command *cmd,
const char *buf, const char *buf,
const jsmntok_t *params); const jsmntok_t *params);

View File

@ -157,23 +157,34 @@ send_onion_reply(struct command *cmd,
return send_outreq(cmd->plugin, req); return send_outreq(cmd->plugin, req);
} }
static struct command_result *onion_message_modern_call(struct command *cmd, static struct command_result *onion_message_recv(struct command *cmd,
const char *buf, const char *buf,
const jsmntok_t *params) const jsmntok_t *params)
{ {
const jsmntok_t *om, *replytok, *invreqtok, *invtok; const jsmntok_t *om, *secrettok, *replytok, *invreqtok, *invtok;
struct blinded_path *reply_path = NULL; struct blinded_path *reply_path = NULL;
struct command_result *res; struct secret *secret;
if (!offers_enabled) if (!offers_enabled)
return command_hook_success(cmd); return command_hook_success(cmd);
/* FIXME: unify parsing! */
res = recv_modern_onion_message(cmd, buf, params);
if (res)
return res;
om = json_get_member(buf, params, "onion_message"); om = json_get_member(buf, params, "onion_message");
secrettok = json_get_member(buf, om, "pathsecret");
if (secrettok) {
secret = tal(tmpctx, struct secret);
json_to_secret(buf, secrettok, secret);
} else
secret = NULL;
/* Might be reply for fetchinvoice (which always has a secret,
* so we can tell it's a response). */
if (secret) {
struct command_result *res;
res = handle_invoice_onion_message(cmd, buf, om, secret);
if (res)
return res;
}
replytok = json_get_member(buf, om, "reply_blindedpath"); replytok = json_get_member(buf, om, "reply_blindedpath");
if (replytok) { if (replytok) {
reply_path = json_to_blinded_path(cmd, buf, replytok); reply_path = json_to_blinded_path(cmd, buf, replytok);
@ -208,11 +219,11 @@ static struct command_result *onion_message_modern_call(struct command *cmd,
static const struct plugin_hook hooks[] = { static const struct plugin_hook hooks[] = {
{ {
"onion_message_recv", "onion_message_recv",
onion_message_modern_call onion_message_recv
}, },
{ {
"onion_message_recv_secret", "onion_message_recv_secret",
onion_message_modern_call onion_message_recv
}, },
{ {
"invoice_payment", "invoice_payment",