2019-01-15 05:13:27 +01:00
|
|
|
/* Helper library for C plugins. */
|
|
|
|
#ifndef LIGHTNING_PLUGINS_LIBPLUGIN_H
|
|
|
|
#define LIGHTNING_PLUGINS_LIBPLUGIN_H
|
|
|
|
#include "config.h"
|
|
|
|
|
2020-01-26 23:54:33 +01:00
|
|
|
#include <ccan/intmap/intmap.h>
|
|
|
|
#include <ccan/membuf/membuf.h>
|
2020-01-24 14:57:12 +01:00
|
|
|
#include <ccan/strmap/strmap.h>
|
2019-08-28 05:36:50 +02:00
|
|
|
#include <ccan/time/time.h>
|
2020-01-24 14:57:12 +01:00
|
|
|
#include <ccan/timer/timer.h>
|
2020-01-26 13:52:29 +01:00
|
|
|
#include <common/errcode.h>
|
2020-03-31 15:34:02 +02:00
|
|
|
#include <common/features.h>
|
2019-01-15 05:13:27 +01:00
|
|
|
#include <common/json.h>
|
|
|
|
#include <common/json_command.h>
|
|
|
|
#include <common/json_helpers.h>
|
|
|
|
#include <common/jsonrpc_errors.h>
|
|
|
|
#include <common/param.h>
|
2019-05-21 09:32:37 +02:00
|
|
|
#include <common/status_levels.h>
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2019-06-12 02:38:54 +02:00
|
|
|
struct json_out;
|
2020-02-05 00:21:22 +01:00
|
|
|
struct plugin;
|
2020-02-03 10:14:43 +01:00
|
|
|
struct rpc_conn;
|
2019-02-23 04:11:14 +01:00
|
|
|
|
2020-02-05 00:16:01 +01:00
|
|
|
extern bool deprecated_apis;
|
|
|
|
|
2019-07-29 13:46:48 +02:00
|
|
|
enum plugin_restartability {
|
|
|
|
PLUGIN_STATIC,
|
|
|
|
PLUGIN_RESTARTABLE
|
|
|
|
};
|
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
struct out_req {
|
|
|
|
/* The unique id of this request. */
|
|
|
|
u64 id;
|
|
|
|
/* The command which is why we're calling this rpc. */
|
|
|
|
struct command *cmd;
|
|
|
|
/* The request stream. */
|
|
|
|
struct json_stream *js;
|
|
|
|
/* The callback when we get a response. */
|
|
|
|
struct command_result *(*cb)(struct command *command,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *result,
|
|
|
|
void *arg);
|
|
|
|
/* The callback when we get an error. */
|
|
|
|
struct command_result *(*errcb)(struct command *command,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *error,
|
|
|
|
void *arg);
|
|
|
|
void *arg;
|
|
|
|
};
|
|
|
|
|
2020-01-27 00:42:00 +01:00
|
|
|
struct command {
|
|
|
|
u64 *id;
|
|
|
|
const char *methodname;
|
|
|
|
bool usage_only;
|
|
|
|
struct plugin *plugin;
|
|
|
|
};
|
|
|
|
|
2019-01-15 05:13:27 +01:00
|
|
|
/* Create an array of these, one for each command you support. */
|
|
|
|
struct plugin_command {
|
|
|
|
const char *name;
|
2019-05-22 16:27:34 +02:00
|
|
|
const char *category;
|
2019-01-15 05:13:27 +01:00
|
|
|
const char *description;
|
|
|
|
const char *long_description;
|
|
|
|
struct command_result *(*handle)(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *params);
|
2020-08-06 02:30:51 +02:00
|
|
|
/* If true, this command *disabled* if allow-deprecated-apis = false */
|
|
|
|
bool deprecated;
|
2019-01-15 05:13:27 +01:00
|
|
|
};
|
|
|
|
|
2019-05-20 13:09:45 +02:00
|
|
|
/* Create an array of these, one for each --option you support. */
|
|
|
|
struct plugin_option {
|
|
|
|
const char *name;
|
2019-05-26 23:19:00 +02:00
|
|
|
const char *type;
|
2019-05-20 13:09:45 +02:00
|
|
|
const char *description;
|
|
|
|
char *(*handle)(const char *str, void *arg);
|
|
|
|
void *arg;
|
2020-08-06 02:30:51 +02:00
|
|
|
/* If true, this options *disabled* if allow-deprecated-apis = false */
|
|
|
|
bool deprecated;
|
2019-05-20 13:09:45 +02:00
|
|
|
};
|
|
|
|
|
2019-12-04 18:19:14 +01:00
|
|
|
/* Create an array of these, one for each notification you subscribe to. */
|
|
|
|
struct plugin_notification {
|
|
|
|
const char *name;
|
|
|
|
void (*handle)(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *params);
|
|
|
|
};
|
|
|
|
|
2019-12-05 11:28:32 +01:00
|
|
|
/* Create an array of these, one for each hook you subscribe to. */
|
|
|
|
struct plugin_hook {
|
|
|
|
const char *name;
|
|
|
|
struct command_result *(*handle)(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *params);
|
2020-10-30 02:13:42 +01:00
|
|
|
/* If non-NULL, these are NULL-terminated arrays of deps */
|
|
|
|
const char **before, **after;
|
2019-12-05 11:28:32 +01:00
|
|
|
};
|
|
|
|
|
2020-04-02 06:06:47 +02:00
|
|
|
/* Return the feature set of the current lightning node */
|
|
|
|
const struct feature_set *plugin_feature_set(const struct plugin *p);
|
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
/* Helper to create a JSONRPC2 request stream. Send it with `send_outreq`. */
|
2021-05-21 07:23:05 +02:00
|
|
|
struct out_req *jsonrpc_request_start_(struct plugin *plugin,
|
|
|
|
struct command *cmd,
|
|
|
|
const char *method,
|
|
|
|
struct command_result *(*cb)(struct command *command,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *result,
|
|
|
|
void *arg),
|
|
|
|
struct command_result *(*errcb)(struct command *command,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *result,
|
|
|
|
void *arg),
|
|
|
|
void *arg);
|
2020-01-30 00:55:55 +01:00
|
|
|
|
|
|
|
#define jsonrpc_request_start(plugin, cmd, method, cb, errcb, arg) \
|
|
|
|
jsonrpc_request_start_((plugin), (cmd), (method), \
|
|
|
|
typesafe_cb_preargs(struct command_result *, void *, \
|
|
|
|
(cb), (arg), \
|
|
|
|
struct command *command, \
|
|
|
|
const char *buf, \
|
|
|
|
const jsmntok_t *result), \
|
|
|
|
typesafe_cb_preargs(struct command_result *, void *, \
|
|
|
|
(errcb), (arg), \
|
|
|
|
struct command *command, \
|
|
|
|
const char *buf, \
|
|
|
|
const jsmntok_t *result), \
|
|
|
|
(arg))
|
|
|
|
|
|
|
|
|
2020-01-28 12:28:25 +01:00
|
|
|
/* Helper to create a JSONRPC2 response stream with a "result" object. */
|
|
|
|
struct json_stream *jsonrpc_stream_success(struct command *cmd);
|
|
|
|
|
|
|
|
/* Helper to create a JSONRPC2 response stream with an "error" object. */
|
|
|
|
struct json_stream *jsonrpc_stream_fail(struct command *cmd,
|
|
|
|
int code,
|
|
|
|
const char *err);
|
|
|
|
|
|
|
|
/* Helper to create a JSONRPC2 response stream with an "error" object,
|
|
|
|
* to which will be added a "data" object. */
|
|
|
|
struct json_stream *jsonrpc_stream_fail_data(struct command *cmd,
|
|
|
|
int code,
|
|
|
|
const char *err);
|
|
|
|
|
|
|
|
/* This command is finished, here's the response (the content of the
|
|
|
|
* "result" or "error" field) */
|
2021-05-21 07:23:05 +02:00
|
|
|
WARN_UNUSED_RESULT
|
|
|
|
struct command_result *command_finished(struct command *cmd, struct json_stream *response);
|
2020-01-28 12:28:25 +01:00
|
|
|
|
2020-02-02 14:20:23 +01:00
|
|
|
/* Helper for a command that'll be finished in a callback. */
|
2021-05-21 07:23:05 +02:00
|
|
|
WARN_UNUSED_RESULT
|
|
|
|
struct command_result *command_still_pending(struct command *cmd);
|
2020-02-02 14:20:23 +01:00
|
|
|
|
2019-06-12 02:38:54 +02:00
|
|
|
/* Helper to create a zero or single-value JSON object; if @str is NULL,
|
|
|
|
* object is empty. */
|
|
|
|
struct json_out *json_out_obj(const tal_t *ctx,
|
|
|
|
const char *fieldname,
|
|
|
|
const char *str);
|
|
|
|
|
2019-01-15 05:13:27 +01:00
|
|
|
/* Return this iff the param() call failed in your handler. */
|
|
|
|
struct command_result *command_param_failed(void);
|
|
|
|
|
|
|
|
/* Call this on fatal error. */
|
2020-01-26 15:44:16 +01:00
|
|
|
void NORETURN plugin_err(struct plugin *p, const char *fmt, ...);
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2019-05-21 09:32:31 +02:00
|
|
|
/* This command is finished, here's a detailed error; @cmd cannot be
|
2019-06-12 02:38:54 +02:00
|
|
|
* NULL, data can be NULL; otherwise it must be a JSON object. */
|
2019-01-15 05:13:27 +01:00
|
|
|
struct command_result *WARN_UNUSED_RESULT
|
|
|
|
command_done_err(struct command *cmd,
|
2020-01-26 13:52:29 +01:00
|
|
|
errcode_t code,
|
2019-01-15 05:13:27 +01:00
|
|
|
const char *errmsg,
|
2019-06-12 02:38:54 +02:00
|
|
|
const struct json_out *data);
|
|
|
|
|
2019-08-28 05:38:12 +02:00
|
|
|
/* Send a raw error response. Useful for forwarding a previous
|
|
|
|
* error after cleanup */
|
|
|
|
struct command_result *command_err_raw(struct command *cmd,
|
|
|
|
const char *json_str);
|
|
|
|
|
2019-06-12 02:38:54 +02:00
|
|
|
/* This command is finished, here's the result object; @cmd cannot be NULL. */
|
|
|
|
struct command_result *WARN_UNUSED_RESULT
|
|
|
|
command_success(struct command *cmd, const struct json_out *result);
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2020-11-25 01:18:46 +01:00
|
|
|
/* End a hook normally (with "result": "continue") */
|
|
|
|
struct command_result *WARN_UNUSED_RESULT
|
|
|
|
command_hook_success(struct command *cmd);
|
|
|
|
|
2021-01-06 06:41:20 +01:00
|
|
|
/* Synchronous helper to send command and extract fields from
|
2019-01-15 05:13:27 +01:00
|
|
|
* response; can only be used in init callback. */
|
2021-01-06 06:41:20 +01:00
|
|
|
void rpc_scan(struct plugin *plugin,
|
|
|
|
const char *method,
|
|
|
|
const struct json_out *params TAKES,
|
|
|
|
const char *guide,
|
|
|
|
...);
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
/* Send an async rpc request to lightningd. */
|
2021-05-21 07:23:05 +02:00
|
|
|
struct command_result *send_outreq(struct plugin *plugin,
|
|
|
|
const struct out_req *req);
|
2019-01-15 05:13:27 +01:00
|
|
|
|
2019-05-21 09:32:31 +02:00
|
|
|
/* Callback to just forward error and close request; @cmd cannot be NULL */
|
2019-01-15 05:13:27 +01:00
|
|
|
struct command_result *forward_error(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *error,
|
|
|
|
void *arg);
|
|
|
|
|
2019-05-21 09:32:31 +02:00
|
|
|
/* Callback to just forward result and close request; @cmd cannot be NULL */
|
2019-01-15 05:13:27 +01:00
|
|
|
struct command_result *forward_result(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *result,
|
|
|
|
void *arg);
|
|
|
|
|
2019-05-21 09:32:31 +02:00
|
|
|
/* Callback for timer where we expect a 'command_result'. All timers
|
|
|
|
* must return this eventually, though they may do so via a convoluted
|
|
|
|
* send_req() path. */
|
2020-01-24 14:57:12 +01:00
|
|
|
struct command_result *timer_complete(struct plugin *p);
|
2019-05-20 13:10:54 +02:00
|
|
|
|
2021-04-19 21:26:23 +02:00
|
|
|
/* Signals that we've completed a command. Useful for when
|
|
|
|
* there's no `cmd` present */
|
|
|
|
struct command_result *command_done(void);
|
|
|
|
|
2019-05-20 13:10:54 +02:00
|
|
|
/* Access timer infrastructure to add a timer.
|
|
|
|
*
|
2019-05-21 09:32:31 +02:00
|
|
|
* Freeing this releases the timer, otherwise it's freed after @cb
|
|
|
|
* if it hasn't been freed already.
|
2019-05-20 13:10:54 +02:00
|
|
|
*/
|
2020-02-16 12:31:47 +01:00
|
|
|
struct plugin_timer *plugin_timer_(struct plugin *p,
|
|
|
|
struct timerel t,
|
|
|
|
void (*cb)(void *cb_arg),
|
|
|
|
void *cb_arg);
|
|
|
|
|
|
|
|
#define plugin_timer(plugin, time, cb, cb_arg) \
|
|
|
|
plugin_timer_((plugin), (time), \
|
|
|
|
typesafe_cb(void, void *, \
|
|
|
|
(cb), (cb_arg)), \
|
|
|
|
(cb_arg)) \
|
2019-05-20 13:10:54 +02:00
|
|
|
|
2019-05-21 09:32:37 +02:00
|
|
|
/* Log something */
|
2020-01-26 15:44:16 +01:00
|
|
|
void plugin_log(struct plugin *p, enum log_level l, const char *fmt, ...) PRINTF_FMT(3, 4);
|
2019-05-21 09:32:37 +02:00
|
|
|
|
2020-10-12 07:33:50 +02:00
|
|
|
/* Notify the caller of something. */
|
|
|
|
struct json_stream *plugin_notify_start(struct command *cmd, const char *method);
|
|
|
|
void plugin_notify_end(struct command *cmd, struct json_stream *js);
|
|
|
|
|
2021-04-28 18:36:19 +02:00
|
|
|
/* Send a notification for a custom notification topic. These are sent
|
|
|
|
* to lightningd and distributed to subscribing plugins. */
|
|
|
|
struct json_stream *plugin_notification_start(struct plugin *plugins,
|
|
|
|
const char *method);
|
|
|
|
void plugin_notification_end(struct plugin *plugin,
|
|
|
|
struct json_stream *stream TAKES);
|
|
|
|
|
2020-10-12 07:33:50 +02:00
|
|
|
/* Convenience wrapper for notify "message" */
|
|
|
|
void plugin_notify_message(struct command *cmd,
|
|
|
|
enum log_level level,
|
|
|
|
const char *fmt, ...)
|
|
|
|
PRINTF_FMT(3, 4);
|
|
|
|
|
|
|
|
/* Convenience wrapper for progress: num_stages is normally 0. */
|
|
|
|
void plugin_notify_progress(struct command *cmd,
|
|
|
|
u32 num_stages, u32 stage,
|
|
|
|
u32 num_progress, u32 progress);
|
|
|
|
|
2019-05-20 13:09:45 +02:00
|
|
|
/* Macro to define arguments */
|
2020-08-06 02:30:51 +02:00
|
|
|
#define plugin_option_(name, type, description, set, arg, deprecated) \
|
2019-05-20 13:09:45 +02:00
|
|
|
(name), \
|
2019-05-26 23:19:00 +02:00
|
|
|
(type), \
|
2019-05-20 13:09:45 +02:00
|
|
|
(description), \
|
|
|
|
typesafe_cb_preargs(char *, void *, (set), (arg), const char *), \
|
2020-08-06 02:30:51 +02:00
|
|
|
(arg), \
|
|
|
|
(deprecated)
|
|
|
|
|
|
|
|
#define plugin_option(name, type, description, set, arg) \
|
|
|
|
plugin_option_((name), (type), (description), (set), (arg), false)
|
|
|
|
|
|
|
|
#define plugin_option_deprecated(name, type, description, set, arg) \
|
|
|
|
plugin_option_((name), (type), (description), (set), (arg), true)
|
2019-05-20 13:09:45 +02:00
|
|
|
|
|
|
|
/* Standard helpers */
|
|
|
|
char *u64_option(const char *arg, u64 *i);
|
2020-03-11 13:36:05 +01:00
|
|
|
char *u32_option(const char *arg, u32 *i);
|
2020-07-10 14:48:00 +02:00
|
|
|
char *bool_option(const char *arg, bool *i);
|
2019-05-20 13:09:45 +02:00
|
|
|
char *charp_option(const char *arg, char **p);
|
2020-07-10 14:48:00 +02:00
|
|
|
char *flag_option(const char *arg, bool *i);
|
2019-05-20 13:09:45 +02:00
|
|
|
|
|
|
|
/* The main plugin runner: append with 0 or more plugin_option(), then NULL. */
|
|
|
|
void NORETURN LAST_ARG_NULL plugin_main(char *argv[],
|
2021-01-13 04:00:24 +01:00
|
|
|
const char *(*init)(struct plugin *p,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *),
|
2019-07-29 13:46:48 +02:00
|
|
|
const enum plugin_restartability restartability,
|
2020-07-20 14:39:32 +02:00
|
|
|
bool init_rpc,
|
2020-03-31 15:34:02 +02:00
|
|
|
struct feature_set *features,
|
2019-05-20 13:09:45 +02:00
|
|
|
const struct plugin_command *commands,
|
2019-12-04 18:19:14 +01:00
|
|
|
size_t num_commands,
|
|
|
|
const struct plugin_notification *notif_subs,
|
|
|
|
size_t num_notif_subs,
|
2019-12-05 11:28:32 +01:00
|
|
|
const struct plugin_hook *hook_subs,
|
|
|
|
size_t num_hook_subs,
|
2021-04-28 17:28:27 +02:00
|
|
|
const char **notif_topics,
|
|
|
|
size_t num_notif_topics,
|
2019-12-04 18:19:14 +01:00
|
|
|
...);
|
2020-06-09 18:30:14 +02:00
|
|
|
|
|
|
|
struct listpeers_channel {
|
|
|
|
bool private;
|
|
|
|
struct bitcoin_txid funding_txid;
|
|
|
|
const char *state;
|
|
|
|
struct short_channel_id *scid;
|
|
|
|
int *direction;
|
|
|
|
struct amount_msat total_msat;
|
|
|
|
struct amount_msat spendable_msat;
|
|
|
|
/* TODO Add fields as we need them. */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct listpeers_peer {
|
|
|
|
struct node_id id;
|
|
|
|
bool connected;
|
|
|
|
const char **netaddr;
|
|
|
|
struct feature_set *features;
|
|
|
|
struct listpeers_channel **channels;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct listpeers_result {
|
|
|
|
struct listpeers_peer **peers;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct listpeers_result *json_to_listpeers_result(const tal_t *ctx,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok);
|
|
|
|
|
|
|
|
struct createonion_response {
|
|
|
|
u8 *onion;
|
|
|
|
struct secret *shared_secrets;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct createonion_response *json_to_createonion_response(const tal_t *ctx,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *toks);
|
|
|
|
|
|
|
|
struct route_hop *json_to_route(const tal_t *ctx, const char *buffer,
|
|
|
|
const jsmntok_t *toks);
|
|
|
|
|
2019-01-15 05:13:27 +01:00
|
|
|
#endif /* LIGHTNING_PLUGINS_LIBPLUGIN_H */
|