From 81f578de767eaa4a673a6d2444081fcfa4d33052 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 10 Jul 2024 12:27:25 +0930 Subject: [PATCH] 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 --- plugins/fetchinvoice.c | 31 +++++++++---------------------- plugins/fetchinvoice.h | 10 +++++++--- plugins/offers.c | 35 +++++++++++++++++++++++------------ 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/plugins/fetchinvoice.c b/plugins/fetchinvoice.c index 8dafab0f1..1ddcfd224 100644 --- a/plugins/fetchinvoice.c +++ b/plugins/fetchinvoice.c @@ -333,34 +333,21 @@ badinv: return command_hook_success(cmd); } -struct command_result *recv_modern_onion_message(struct command *cmd, - const char *buf, - const jsmntok_t *params) +struct command_result *handle_invoice_onion_message(struct command *cmd, + const char *buf, + const jsmntok_t *om, + const struct secret *pathsecret) { - const jsmntok_t *om, *secrettok; struct sent *sent; - struct secret pathsecret; struct command_result *err; - om = json_get_member(buf, params, "onion_message"); - - secrettok = json_get_member(buf, om, "pathsecret"); - if (!secrettok) + sent = find_sent_by_secret(pathsecret); + if (!sent) return NULL; - json_to_secret(buf, secrettok, &pathsecret); - sent = find_sent_by_secret(&pathsecret); - if (!sent) { - 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)); + plugin_log(cmd->plugin, LOG_DBG, "Received onion message reply for invoice_request: %.*s", + json_tok_full_len(om), + json_tok_full(buf, om)); err = handle_error(cmd, sent, buf, om); if (err) diff --git a/plugins/fetchinvoice.h b/plugins/fetchinvoice.h index 1f5ef14a3..6ab7340ce 100644 --- a/plugins/fetchinvoice.h +++ b/plugins/fetchinvoice.h @@ -17,9 +17,13 @@ struct command_result *json_dev_rawrequest(struct command *cmd, const char *buffer, const jsmntok_t *params); -struct command_result *recv_modern_onion_message(struct command *cmd, - const char *buf, - const jsmntok_t *params); +/* Returns NULL if this wasn't one of ours. */ +struct command_result *handle_invoice_onion_message(struct command *cmd, + const char *buf, + const jsmntok_t *om, + const struct secret *pathsecret); + +/* invoice_payment hook */ struct command_result *invoice_payment(struct command *cmd, const char *buf, const jsmntok_t *params); diff --git a/plugins/offers.c b/plugins/offers.c index 84f6f01d6..dd0eaa93f 100644 --- a/plugins/offers.c +++ b/plugins/offers.c @@ -157,23 +157,34 @@ send_onion_reply(struct command *cmd, return send_outreq(cmd->plugin, req); } -static struct command_result *onion_message_modern_call(struct command *cmd, - const char *buf, - const jsmntok_t *params) +static struct command_result *onion_message_recv(struct command *cmd, + const char *buf, + 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 command_result *res; + struct secret *secret; if (!offers_enabled) 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"); + 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"); if (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[] = { { "onion_message_recv", - onion_message_modern_call + onion_message_recv }, { "onion_message_recv_secret", - onion_message_modern_call + onion_message_recv }, { "invoice_payment",