From e625fd7e820c9f35e409ab36711cbe286eb0d24f Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 26 Nov 2018 19:54:06 +0100 Subject: [PATCH] plugin: Map results back to the incoming JSON-RPC request The final step in the JSON-RPC passthrough: map the result we got from the plugin back to the original request we got from the client. Signed-off-by: Christian Decker --- lightningd/jsonrpc_errors.h | 3 +++ lightningd/plugin.c | 25 +++++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lightningd/jsonrpc_errors.h b/lightningd/jsonrpc_errors.h index 1ba1ca21f..65757e734 100644 --- a/lightningd/jsonrpc_errors.h +++ b/lightningd/jsonrpc_errors.h @@ -19,6 +19,9 @@ /* Developer error in the parameters to param() call */ #define PARAM_DEV_ERROR -2 +/* Plugin returned an error */ +#define PLUGIN_ERROR -3 + /* Errors from `pay`, `sendpay`, or `waitsendpay` commands */ #define PAY_IN_PROGRESS 200 #define PAY_RHASH_ALREADY_USED 201 diff --git a/lightningd/plugin.c b/lightningd/plugin.c index f2114cfa9..21afb7654 100644 --- a/lightningd/plugin.c +++ b/lightningd/plugin.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -417,10 +418,26 @@ static void plugin_rpcmethod_destroy(struct json_command *cmd, static void plugin_rpcmethod_cb(const struct plugin_request *req, struct plugin_rpc_request *rpc_req) { - // Parse - // Extract results or error - // Construct reply - // Return result with appropriate return code. + struct json_stream *response; + const jsmntok_t *res; + assert(req->resulttok || req->errortok); + + if (req->errortok) { + res = req->errortok; + command_fail(rpc_req->cmd, PLUGIN_ERROR, "%.*s", + res->end - res->start, req->response + res->start); + tal_free(rpc_req); + return; + } + + res = req->resulttok; + response = json_stream_success(rpc_req->cmd); + + json_add_member(response, NULL, "%.*s", json_tok_len(res), + json_tok_contents(req->response, res)); + + command_success(rpc_req->cmd, response); + tal_free(rpc_req); } static void plugin_rpcmethod_dispatch(struct command *cmd, const char *buffer,