2018-12-08 01:39:28 +01:00
|
|
|
/* Helpers for use with param parsing. */
|
2022-07-04 05:49:38 +02:00
|
|
|
#ifndef LIGHTNING_COMMON_JSON_PARAM_H
|
|
|
|
#define LIGHTNING_COMMON_JSON_PARAM_H
|
2018-12-08 01:39:28 +01:00
|
|
|
#include "config.h"
|
2019-01-15 04:54:27 +01:00
|
|
|
#include <ccan/short_types/short_types.h>
|
2021-06-18 17:21:41 +02:00
|
|
|
#include <common/bolt11.h>
|
2022-07-04 05:49:38 +02:00
|
|
|
#include <common/json_parse.h>
|
2021-07-02 23:00:17 +02:00
|
|
|
#include <common/lease_rates.h>
|
2019-08-28 05:23:14 +02:00
|
|
|
#include <common/node_id.h>
|
2019-11-24 19:09:19 +01:00
|
|
|
#include <common/sphinx.h>
|
2019-09-30 18:31:27 +02:00
|
|
|
#include <wire/wire.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
|
2022-07-04 05:49:38 +02:00
|
|
|
/*~ Greetings adventurer!
|
|
|
|
*
|
|
|
|
* Do you want to automatically validate json input and unmarshal it into
|
|
|
|
* local variables, all using typesafe callbacks? And on error,
|
|
|
|
* call command_fail with a proper error message? Then you've come to the
|
|
|
|
* right place!
|
|
|
|
*
|
|
|
|
* Here is a simple example of using the system:
|
|
|
|
*
|
|
|
|
* unsigned *cltv;
|
|
|
|
* u64 *msatoshi;
|
|
|
|
* u64 *expiry;
|
|
|
|
*
|
|
|
|
* if (!param(cmd, buffer, params,
|
2022-12-24 12:56:40 +01:00
|
|
|
* p_req("cltv", param_number, &cltv),
|
|
|
|
* p_opt("msatoshi", param_u64, &msatoshi),
|
|
|
|
* p_opt_def("expiry", param_u64, &expiry, 3600),
|
2022-07-04 05:49:38 +02:00
|
|
|
* NULL))
|
|
|
|
* return;
|
|
|
|
*
|
|
|
|
* If param() returns true then you're good to go.
|
|
|
|
*
|
|
|
|
* All the command handlers throughout the code use this system.
|
|
|
|
* json_invoice() is a great example. The common callbacks can be found in
|
2022-12-24 12:56:40 +01:00
|
|
|
* common/json_param.c. Use them directly or feel free to write your own.
|
2022-07-04 05:49:38 +02:00
|
|
|
*/
|
|
|
|
struct command;
|
|
|
|
|
|
|
|
/* A dummy type returned by command_ functions, to ensure you return them
|
|
|
|
* immediately */
|
|
|
|
struct command_result;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse the json tokens. @params can be an array of values or an object
|
|
|
|
* of named values.
|
|
|
|
*/
|
|
|
|
bool param(struct command *cmd, const char *buffer,
|
|
|
|
const jsmntok_t params[], ...) LAST_ARG_NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The callback signature.
|
|
|
|
*
|
|
|
|
* Callbacks must return NULL on success. On failure they
|
|
|
|
* must return command_fail(...).
|
|
|
|
*/
|
|
|
|
typedef struct command_result *(*param_cbx)(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
void **arg);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the first json value.
|
|
|
|
*
|
|
|
|
* name...: NULL-terminated array of valid values.
|
|
|
|
*
|
|
|
|
* Returns subcommand: if it returns NULL if you should return
|
|
|
|
* command_param_failed() immediately.
|
|
|
|
*/
|
|
|
|
const char *param_subcommand(struct command *cmd, const char *buffer,
|
|
|
|
const jsmntok_t tokens[],
|
|
|
|
const char *name, ...) LAST_ARG_NULL;
|
|
|
|
|
|
|
|
enum param_style {
|
|
|
|
PARAM_REQUIRED,
|
|
|
|
PARAM_REQUIRED_ALLOW_DUPS,
|
|
|
|
PARAM_OPTIONAL,
|
|
|
|
PARAM_OPTIONAL_WITH_DEFAULT,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a required parameter.
|
|
|
|
*/
|
|
|
|
#define p_req(name, cbx, arg) \
|
|
|
|
name"", \
|
|
|
|
PARAM_REQUIRED, \
|
|
|
|
(param_cbx)(cbx), \
|
|
|
|
(arg) + 0*sizeof((cbx)((struct command *)NULL, \
|
|
|
|
(const char *)NULL, \
|
|
|
|
(const char *)NULL, \
|
|
|
|
(const jsmntok_t *)NULL, \
|
|
|
|
(arg)) == (struct command_result *)NULL)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add an optional parameter. *arg is set to NULL if it isn't found.
|
|
|
|
*/
|
|
|
|
#define p_opt(name, cbx, arg) \
|
|
|
|
name"", \
|
|
|
|
PARAM_OPTIONAL, \
|
|
|
|
(param_cbx)(cbx), \
|
|
|
|
({ *arg = NULL; \
|
|
|
|
(arg) + 0*sizeof((cbx)((struct command *)NULL, \
|
|
|
|
(const char *)NULL, \
|
|
|
|
(const char *)NULL, \
|
|
|
|
(const jsmntok_t *)NULL, \
|
|
|
|
(arg)) == (struct command_result *)NULL); })
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add an required parameter, like p_req, but ignore duplicates.
|
|
|
|
*/
|
|
|
|
#define p_req_dup_ok(name, cbx, arg) \
|
|
|
|
name"", \
|
|
|
|
PARAM_REQUIRED_ALLOW_DUPS, \
|
|
|
|
(param_cbx)(cbx), \
|
|
|
|
({ *arg = NULL; \
|
|
|
|
(arg) + 0*sizeof((cbx)((struct command *)NULL, \
|
|
|
|
(const char *)NULL, \
|
|
|
|
(const char *)NULL, \
|
|
|
|
(const jsmntok_t *)NULL, \
|
|
|
|
(arg)) == (struct command_result *)NULL); })
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add an optional parameter. *arg is set to @def if it isn't found.
|
|
|
|
*/
|
|
|
|
#define p_opt_def(name, cbx, arg, def) \
|
|
|
|
name"", \
|
|
|
|
PARAM_OPTIONAL_WITH_DEFAULT, \
|
|
|
|
(param_cbx)(cbx), \
|
|
|
|
({ (*arg) = tal((cmd), typeof(**arg)); \
|
|
|
|
(**arg) = (def); \
|
|
|
|
(arg) + 0*sizeof((cbx)((struct command *)NULL, \
|
|
|
|
(const char *)NULL, \
|
|
|
|
(const char *)NULL, \
|
|
|
|
(const jsmntok_t *)NULL, \
|
|
|
|
(arg)) == (struct command_result *)NULL); })
|
|
|
|
|
|
|
|
/* Special flag for 'check' which allows any parameters. */
|
|
|
|
#define p_opt_any() "", PARAM_OPTIONAL, NULL, NULL
|
|
|
|
|
|
|
|
/* All the helper routines. */
|
2019-02-21 01:45:57 +01:00
|
|
|
struct amount_msat;
|
|
|
|
struct amount_sat;
|
2020-07-07 22:50:28 +02:00
|
|
|
struct bitcoin_txid;
|
2020-12-04 11:24:14 +01:00
|
|
|
struct bitcoin_outpoint;
|
2020-05-15 12:30:25 +02:00
|
|
|
struct channel_id;
|
2019-06-12 02:38:54 +02:00
|
|
|
struct json_escape;
|
2021-11-04 22:06:01 +01:00
|
|
|
struct route_exclusion;
|
2019-01-15 04:54:27 +01:00
|
|
|
struct sha256;
|
2020-09-03 19:59:55 +02:00
|
|
|
struct wally_psbt;
|
2018-12-08 01:39:28 +01:00
|
|
|
|
|
|
|
/* Extract json array token */
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *param_array(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
const jsmntok_t **arr);
|
2018-12-08 01:39:28 +01:00
|
|
|
|
|
|
|
/* Extract boolean this (must be a true or false) */
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *param_bool(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
bool **b);
|
2018-12-08 01:39:28 +01:00
|
|
|
|
2020-01-29 12:30:00 +01:00
|
|
|
/*
|
|
|
|
* Extract a non-negative (either 0 or positive) floating-point number from this
|
|
|
|
* (must be a number literal), multiply it by 1 million and return it as an
|
|
|
|
* integer.
|
|
|
|
*/
|
|
|
|
struct command_result *param_millionths(struct command *cmd, const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok, uint64_t **num);
|
2018-12-08 01:39:28 +01:00
|
|
|
|
|
|
|
/* Extract an escaped string (and unescape it) */
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *param_escaped_string(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
const char **str);
|
2018-12-08 01:39:28 +01:00
|
|
|
|
|
|
|
/* Extract a string */
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *param_string(struct command *cmd, const char *name,
|
|
|
|
const char * buffer, const jsmntok_t *tok,
|
|
|
|
const char **str);
|
2018-12-08 01:39:28 +01:00
|
|
|
|
|
|
|
/* Extract a label. It is either an escaped string or a number. */
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *param_label(struct command *cmd, const char *name,
|
|
|
|
const char * buffer, const jsmntok_t *tok,
|
2019-06-12 02:38:54 +02:00
|
|
|
struct json_escape **label);
|
2018-12-08 01:39:28 +01:00
|
|
|
|
|
|
|
/* Extract number from this (may be a string, or a number literal) */
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *param_number(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
unsigned int **num);
|
2018-12-08 01:39:28 +01:00
|
|
|
|
|
|
|
/* Extract sha256 hash */
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *param_sha256(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct sha256 **hash);
|
2018-12-08 01:39:28 +01:00
|
|
|
|
2022-05-19 13:51:49 +02:00
|
|
|
/* Extract number from this (may be a string, or a number literal) */
|
|
|
|
struct command_result *param_u32(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
uint32_t **num);
|
|
|
|
|
2018-12-08 01:39:28 +01:00
|
|
|
/* Extract number from this (may be a string, or a number literal) */
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *param_u64(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
uint64_t **num);
|
2018-12-08 01:39:28 +01:00
|
|
|
|
2019-05-22 01:48:30 +02:00
|
|
|
/* Extract msatoshi amount from this string */
|
2019-02-21 01:45:57 +01:00
|
|
|
struct command_result *param_msat(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct amount_msat **msat);
|
|
|
|
|
2019-05-22 01:48:30 +02:00
|
|
|
/* Extract satoshi amount from this string */
|
2019-02-21 01:45:57 +01:00
|
|
|
struct command_result *param_sat(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct amount_sat **sat);
|
|
|
|
|
2019-08-15 19:41:23 +02:00
|
|
|
/* Extract satoshi amount from this string. */
|
|
|
|
/* If the string is "all", set amonut as AMOUNT_SAT(-1ULL). */
|
|
|
|
struct command_result *param_sat_or_all(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct amount_sat **sat);
|
|
|
|
|
2019-08-28 05:23:14 +02:00
|
|
|
|
|
|
|
/* Extract node_id from this string. Makes sure *id is valid. */
|
|
|
|
struct command_result *param_node_id(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
struct node_id **id);
|
|
|
|
|
2019-09-30 18:31:27 +02:00
|
|
|
struct command_result *param_channel_id(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
struct channel_id **cid);
|
2021-05-22 07:00:22 +02:00
|
|
|
|
|
|
|
struct command_result *param_short_channel_id(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
struct short_channel_id **scid);
|
|
|
|
|
2019-07-16 03:41:51 +02:00
|
|
|
/* Ignore the token. Not usually used. */
|
|
|
|
struct command_result *param_ignore(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
const void *unused);
|
2019-09-30 18:31:27 +02:00
|
|
|
|
2019-11-07 17:56:06 +01:00
|
|
|
/* Extract a secret from this string */
|
|
|
|
struct command_result *param_secret(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct secret **secret);
|
|
|
|
|
|
|
|
/* Extract a binary value from the param and unhexlify it. */
|
|
|
|
struct command_result *param_bin_from_hex(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
u8 **bin);
|
|
|
|
|
2019-11-24 19:09:19 +01:00
|
|
|
struct command_result *param_hops_array(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct sphinx_hop **hops);
|
|
|
|
|
2019-11-25 13:42:23 +01:00
|
|
|
struct command_result *param_secrets_array(struct command *cmd,
|
|
|
|
const char *name, const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
struct secret **secrets);
|
|
|
|
|
2020-07-07 22:50:28 +02:00
|
|
|
struct command_result *param_txid(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
struct bitcoin_txid **txid);
|
|
|
|
|
2020-07-07 22:50:26 +02:00
|
|
|
enum address_parse_result {
|
|
|
|
/* Not recognized as an onchain address */
|
|
|
|
ADDRESS_PARSE_UNRECOGNIZED,
|
|
|
|
/* Recognized as an onchain address, but targets wrong network */
|
|
|
|
ADDRESS_PARSE_WRONG_NETWORK,
|
|
|
|
/* Recognized and succeeds */
|
|
|
|
ADDRESS_PARSE_SUCCESS,
|
|
|
|
};
|
|
|
|
/* Return result of address parsing and fills in *scriptpubkey
|
|
|
|
* allocated off ctx if ADDRESS_PARSE_SUCCESS
|
|
|
|
*/
|
|
|
|
enum address_parse_result json_to_address_scriptpubkey(const tal_t *ctx,
|
|
|
|
const struct chainparams *chainparams,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok, const u8 **scriptpubkey);
|
2020-08-07 03:21:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
struct command_result *param_bitcoin_address(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
const u8 **scriptpubkey);
|
2020-09-03 19:59:55 +02:00
|
|
|
|
|
|
|
struct command_result *param_psbt(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
struct wally_psbt **psbt);
|
2020-12-04 11:24:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a list of `txid:output` outpoints.
|
|
|
|
*/
|
|
|
|
struct command_result *param_outpoint_arr(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
struct bitcoin_outpoint **outpoints);
|
2021-06-17 18:06:43 +02:00
|
|
|
|
|
|
|
struct command_result *param_extra_tlvs(struct command *cmd, const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
struct tlv_field **fields);
|
2021-06-18 17:21:41 +02:00
|
|
|
|
|
|
|
struct command_result *
|
|
|
|
param_routehint_array(struct command *cmd, const char *name, const char *buffer,
|
|
|
|
const jsmntok_t *tok, struct route_info ***ris);
|
|
|
|
|
2021-11-04 22:06:01 +01:00
|
|
|
struct command_result *param_route_exclusion(struct command *cmd,
|
|
|
|
const char *name, const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct route_exclusion **re);
|
|
|
|
|
|
|
|
struct command_result *
|
|
|
|
param_route_exclusion_array(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct route_exclusion ***res);
|
|
|
|
|
2021-07-02 23:00:17 +02:00
|
|
|
/**
|
|
|
|
* Parse a 'compact-lease' (serialized lease_rates) back into lease_rates
|
|
|
|
*/
|
|
|
|
struct command_result *param_lease_hex(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
struct lease_rates **rates);
|
2022-07-04 05:52:34 +02:00
|
|
|
|
|
|
|
struct command_result *param_pubkey(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct pubkey **pubkey);
|
|
|
|
|
2022-07-04 05:49:38 +02:00
|
|
|
#endif /* LIGHTNING_COMMON_JSON_PARAM_H */
|