From 6d1fe7e599e69f2da5623cd60849b21e08347882 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 8 Jan 2021 05:05:47 +1030 Subject: [PATCH] plugin/offers: hoist send_onion_reply into core. We want to reuse it. Signed-off-by: Rusty Russell --- plugins/Makefile | 2 +- plugins/offers.c | 64 +++++++++++++++++++++++++++++++++ plugins/offers.h | 16 +++++++++ plugins/offers_invreq_hook.c | 68 +++--------------------------------- 4 files changed, 85 insertions(+), 65 deletions(-) create mode 100644 plugins/offers.h diff --git a/plugins/Makefile b/plugins/Makefile index 59c389966..f83f78429 100644 --- a/plugins/Makefile +++ b/plugins/Makefile @@ -27,7 +27,7 @@ PLUGIN_PAY_LIB_OBJS := $(PLUGIN_PAY_LIB_SRC:.c=.o) PLUGIN_OFFERS_SRC := plugins/offers.c plugins/offers_offer.c plugins/offers_invreq_hook.c PLUGIN_OFFERS_OBJS := $(PLUGIN_OFFERS_SRC:.c=.o) -PLUGIN_OFFERS_HEADER := plugins/offers_offer.h plugins/offers_invreq_hook.h +PLUGIN_OFFERS_HEADER := $(PLUGIN_OFFERS_SRC:.c=.h) PLUGIN_FETCHINVOICE_SRC := plugins/fetchinvoice.c PLUGIN_FETCHINVOICE_OBJS := $(PLUGIN_FETCHINVOICE_SRC:.c=.o) diff --git a/plugins/offers.c b/plugins/offers.c index e31d1d9a1..c9bde41b4 100644 --- a/plugins/offers.c +++ b/plugins/offers.c @@ -1,12 +1,76 @@ /* This plugin covers both sending and receiving offers */ #include +#include #include +#include #include #include struct pubkey32 id; u32 cltv_final; +static struct command_result *finished(struct command *cmd, + const char *buf, + const jsmntok_t *result, + void *unused) +{ + return command_hook_success(cmd); +} + +static struct command_result *sendonionmessage_error(struct command *cmd, + const char *buf, + const jsmntok_t *err, + void *unused) +{ + plugin_log(cmd->plugin, LOG_BROKEN, + "sendoniomessage gave JSON error: %.*s", + json_tok_full_len(err), + json_tok_full(buf, err)); + return command_hook_success(cmd); +} + +struct command_result *WARN_UNUSED_RESULT +send_onion_reply(struct command *cmd, + const char *jsonbuf, + const jsmntok_t *replytok, + const char *replyfield, + const u8 *replydata) +{ + struct out_req *req; + size_t i; + const jsmntok_t *t; + + plugin_log(cmd->plugin, LOG_DBG, "sending reply %s = %s", + replyfield, tal_hex(tmpctx, replydata)); + + /* Send to requester, using return route. */ + req = jsonrpc_request_start(cmd->plugin, cmd, "sendonionmessage", + finished, sendonionmessage_error, NULL); + + /* Add reply into last hop. */ + json_array_start(req->js, "hops"); + json_for_each_arr(i, t, replytok) { + size_t j; + const jsmntok_t *t2; + + plugin_log(cmd->plugin, LOG_DBG, "hops[%zu/%i]", + i, replytok->size); + json_object_start(req->js, NULL); + json_for_each_obj(j, t2, t) + json_add_tok(req->js, + json_strdup(tmpctx, jsonbuf, t2), + t2+1, jsonbuf); + if (i == replytok->size - 1) { + plugin_log(cmd->plugin, LOG_DBG, "... adding %s", + replyfield); + json_add_hex_talarr(req->js, replyfield, replydata); + } + json_object_end(req->js); + } + json_array_end(req->js); + return send_outreq(cmd->plugin, req); +} + static struct command_result *onion_message_call(struct command *cmd, const char *buf, const jsmntok_t *params) diff --git a/plugins/offers.h b/plugins/offers.h new file mode 100644 index 000000000..bc9783972 --- /dev/null +++ b/plugins/offers.h @@ -0,0 +1,16 @@ +#ifndef LIGHTNING_PLUGINS_OFFERS_H +#define LIGHTNING_PLUGINS_OFFERS_H +#include "config.h" +#include + +struct command_result; +struct command; + +/* Helper to send a reply */ +struct command_result *WARN_UNUSED_RESULT +send_onion_reply(struct command *cmd, + const char *jsonbuf, + const jsmntok_t *replytok, + const char *replyfield, + const u8 *replydata); +#endif /* LIGHTNING_PLUGINS_OFFERS_H */ diff --git a/plugins/offers_invreq_hook.c b/plugins/offers_invreq_hook.c index 5a36259c8..090086f9f 100644 --- a/plugins/offers_invreq_hook.c +++ b/plugins/offers_invreq_hook.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -26,68 +27,6 @@ struct invreq { struct preimage preimage; }; -static struct command_result *finished(struct command *cmd, - const char *buf, - const jsmntok_t *result, - void *unused) -{ - return command_hook_success(cmd); -} - -/* If we get an error trying to reply, don't try again! */ -static struct command_result *error_noloop(struct command *cmd, - const char *buf, - const jsmntok_t *err, - void *unused) -{ - plugin_log(cmd->plugin, LOG_BROKEN, - "sendoniomessage gave JSON error: %.*s", - json_tok_full_len(err), - json_tok_full(buf, err)); - return command_hook_success(cmd); -} - -static struct command_result *WARN_UNUSED_RESULT -send_onion_reply(struct command *cmd, - const struct invreq *ir, - const char *replyfield, - const u8 *replydata) -{ - struct out_req *req; - size_t i; - const jsmntok_t *t; - - plugin_log(cmd->plugin, LOG_DBG, "sending reply %s = %s", - replyfield, tal_hex(tmpctx, replydata)); - - /* Send to requester, using return route. */ - req = jsonrpc_request_start(cmd->plugin, cmd, "sendonionmessage", - finished, error_noloop, NULL); - - /* Add reply into last hop. */ - json_array_start(req->js, "hops"); - json_for_each_arr(i, t, ir->replytok) { - size_t j; - const jsmntok_t *t2; - - plugin_log(cmd->plugin, LOG_DBG, "hops[%zu/%i]", - i, ir->replytok->size); - json_object_start(req->js, NULL); - json_for_each_obj(j, t2, t) - json_add_tok(req->js, - json_strdup(tmpctx, ir->buf, t2), - t2+1, ir->buf); - if (i == ir->replytok->size - 1) { - plugin_log(cmd->plugin, LOG_DBG, "... adding %s", - replyfield); - json_add_hex_talarr(req->js, replyfield, replydata); - } - json_object_end(req->js); - } - json_array_end(req->js); - return send_outreq(cmd->plugin, req); -} - static struct command_result *WARN_UNUSED_RESULT fail_invreq_level(struct command *cmd, const struct invreq *invreq, @@ -120,7 +59,8 @@ fail_invreq_level(struct command *cmd, errdata = tal_arr(cmd, u8, 0); towire_invoice_error(&errdata, err); - return send_onion_reply(cmd, invreq, "invoice_error", errdata); + return send_onion_reply(cmd, invreq->buf, invreq->replytok, + "invoice_error", errdata); } static struct command_result *WARN_UNUSED_RESULT @@ -238,7 +178,7 @@ static struct command_result *createinvoice_done(struct command *cmd, json_tok_full(buf, t)); } - return send_onion_reply(cmd, ir, "invoice", rawinv); + return send_onion_reply(cmd, ir->buf, ir->replytok, "invoice", rawinv); } static struct command_result *create_invoicereq(struct command *cmd,