2018-03-16 04:45:08 +01:00
|
|
|
/* lightningd/json.h
|
|
|
|
* Helpers for outputting JSON results that are specific only for
|
|
|
|
* lightningd.
|
|
|
|
*/
|
|
|
|
#ifndef LIGHTNING_LIGHTNINGD_JSON_H
|
|
|
|
#define LIGHTNING_LIGHTNINGD_JSON_H
|
|
|
|
#include "config.h"
|
2018-08-28 22:46:32 +02:00
|
|
|
#include <ccan/short_types/short_types.h>
|
2018-10-19 03:17:48 +02:00
|
|
|
#include <ccan/tal/tal.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
2018-08-13 18:16:41 +02:00
|
|
|
#include <stdint.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
|
|
|
|
#define JSMN_STRICT 1
|
|
|
|
# include <external/jsmn/jsmn.h>
|
|
|
|
|
|
|
|
struct bitcoin_txid;
|
2018-11-20 02:46:32 +01:00
|
|
|
struct chainparams;
|
2018-04-30 14:54:39 +02:00
|
|
|
struct channel_id;
|
2018-08-10 17:00:34 +02:00
|
|
|
struct command;
|
2018-08-29 20:43:28 +02:00
|
|
|
struct json_escaped;
|
2018-10-19 03:17:49 +02:00
|
|
|
struct json_stream;
|
2018-03-16 04:45:08 +01:00
|
|
|
struct pubkey;
|
|
|
|
struct route_hop;
|
2018-08-14 22:16:49 +02:00
|
|
|
struct sha256;
|
2018-03-16 04:45:08 +01:00
|
|
|
struct short_channel_id;
|
|
|
|
struct wallet_payment;
|
2018-11-20 02:46:32 +01:00
|
|
|
struct wallet_tx;
|
2018-03-16 04:45:08 +01:00
|
|
|
struct wireaddr;
|
2018-05-07 06:29:21 +02:00
|
|
|
struct wireaddr_internal;
|
2018-03-16 04:45:08 +01:00
|
|
|
|
|
|
|
/* Output a route array. */
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_route(struct json_stream *r, char const *n,
|
2018-03-16 04:45:08 +01:00
|
|
|
const struct route_hop *hops, size_t hops_len);
|
|
|
|
|
|
|
|
/* Output the fields of a wallet payment.
|
|
|
|
* Should be used within an object context. */
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_payment_fields(struct json_stream *response,
|
2018-03-16 04:45:08 +01:00
|
|
|
const struct wallet_payment *t);
|
|
|
|
|
|
|
|
/* '"fieldname" : "0289abcdef..."' or "0289abcdef..." if fieldname is NULL */
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_pubkey(struct json_stream *response,
|
2018-03-16 04:45:08 +01:00
|
|
|
const char *fieldname,
|
|
|
|
const struct pubkey *key);
|
|
|
|
|
|
|
|
/* '"fieldname" : <hexrev>' or "<hexrev>" if fieldname is NULL */
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_txid(struct json_stream *result, const char *fieldname,
|
2018-03-16 04:45:08 +01:00
|
|
|
const struct bitcoin_txid *txid);
|
|
|
|
|
2018-08-29 23:58:07 +02:00
|
|
|
/* Extract json array token */
|
|
|
|
bool json_tok_array(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
const jsmntok_t **arr);
|
|
|
|
|
2018-08-13 22:31:40 +02:00
|
|
|
/* Extract boolean this (must be a true or false) */
|
|
|
|
bool json_tok_bool(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
bool **b);
|
|
|
|
|
2018-08-13 18:42:11 +02:00
|
|
|
/* Extract double from this (must be a number literal) */
|
|
|
|
bool json_tok_double(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
double **num);
|
|
|
|
|
2018-08-29 22:44:04 +02:00
|
|
|
/* Extract an escaped string (and unescape it) */
|
|
|
|
bool json_tok_escaped_string(struct command *cmd, const char *name,
|
|
|
|
const char * buffer, const jsmntok_t *tok,
|
|
|
|
const char **str);
|
|
|
|
|
2018-08-30 00:53:00 +02:00
|
|
|
/* Extract a string */
|
|
|
|
bool json_tok_string(struct command *cmd, const char *name,
|
|
|
|
const char * buffer, const jsmntok_t *tok,
|
|
|
|
const char **str);
|
|
|
|
|
2018-08-29 20:43:28 +02:00
|
|
|
/* Extract a label. It is either an escaped string or a number. */
|
|
|
|
bool json_tok_label(struct command *cmd, const char *name,
|
|
|
|
const char * buffer, const jsmntok_t *tok,
|
|
|
|
struct json_escaped **label);
|
|
|
|
|
2018-08-10 22:16:22 +02:00
|
|
|
/* Extract number from this (may be a string, or a number literal) */
|
|
|
|
bool json_tok_number(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
unsigned int **num);
|
|
|
|
|
2018-08-14 22:16:49 +02:00
|
|
|
/* Extract sha256 hash */
|
|
|
|
bool json_tok_sha256(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct sha256 **hash);
|
|
|
|
|
2018-08-27 22:58:50 +02:00
|
|
|
/* Extract positive integer, or NULL if tok is 'any'. */
|
|
|
|
bool json_tok_msat(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t * tok,
|
|
|
|
u64 **msatoshi_val);
|
|
|
|
|
2018-08-27 16:19:09 +02:00
|
|
|
/* Extract double in range [0.0, 100.0] */
|
|
|
|
bool json_tok_percent(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
double **num);
|
|
|
|
|
2018-03-16 04:45:08 +01:00
|
|
|
/* Extract a pubkey from this */
|
2018-08-14 23:19:31 +02:00
|
|
|
bool json_to_pubkey(const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct pubkey *pubkey);
|
|
|
|
|
|
|
|
bool json_tok_pubkey(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct pubkey **pubkey);
|
2018-03-16 04:45:08 +01:00
|
|
|
|
|
|
|
/* Extract a short_channel_id from this */
|
2018-08-15 16:20:40 +02:00
|
|
|
bool json_to_short_channel_id(const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct short_channel_id *scid);
|
|
|
|
|
|
|
|
bool json_tok_short_channel_id(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct short_channel_id **scid);
|
2018-03-16 04:45:08 +01:00
|
|
|
|
2018-08-13 18:16:41 +02:00
|
|
|
/* Extract number from this (may be a string, or a number literal) */
|
|
|
|
bool json_tok_u64(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
uint64_t **num);
|
|
|
|
|
2018-08-27 07:11:39 +02:00
|
|
|
enum feerate_style {
|
|
|
|
FEERATE_PER_KSIPA,
|
|
|
|
FEERATE_PER_KBYTE
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Extract a feerate style. */
|
|
|
|
bool json_tok_feerate_style(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
enum feerate_style **style);
|
|
|
|
|
|
|
|
const char *json_feerate_style_name(enum feerate_style style);
|
|
|
|
|
2018-08-28 22:46:32 +02:00
|
|
|
/* Extract a feerate with optional style suffix. */
|
|
|
|
bool json_tok_feerate(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
u32 **feerate);
|
|
|
|
|
2018-03-16 04:45:08 +01:00
|
|
|
/* '"fieldname" : "1234:5:6"' */
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_short_channel_id(struct json_stream *response,
|
2018-03-16 04:45:08 +01:00
|
|
|
const char *fieldname,
|
|
|
|
const struct short_channel_id *id);
|
|
|
|
|
2018-04-30 14:54:39 +02:00
|
|
|
bool json_tok_channel_id(const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct channel_id *cid);
|
|
|
|
|
2018-03-16 04:45:08 +01:00
|
|
|
/* JSON serialize a network address for a node */
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_address(struct json_stream *response, const char *fieldname,
|
2018-03-16 04:45:08 +01:00
|
|
|
const struct wireaddr *addr);
|
|
|
|
|
2018-05-07 06:29:21 +02:00
|
|
|
/* JSON serialize a network address for a node. */
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_address_internal(struct json_stream *response,
|
2018-05-07 06:29:21 +02:00
|
|
|
const char *fieldname,
|
|
|
|
const struct wireaddr_internal *addr);
|
2018-08-10 17:00:34 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the address of @out to @tok. Used as a callback by handlers that
|
|
|
|
* want to unmarshal @tok themselves.
|
|
|
|
*/
|
|
|
|
bool json_tok_tok(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t * tok,
|
|
|
|
const jsmntok_t **out);
|
|
|
|
|
2018-10-19 03:17:48 +02:00
|
|
|
|
|
|
|
/* '"fieldname" : "value"' or '"value"' if fieldname is NULL. Turns
|
|
|
|
* any non-printable chars into JSON escapes, but leaves existing escapes alone.
|
|
|
|
*/
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_string(struct json_stream *result, const char *fieldname, const char *value);
|
2018-10-19 03:17:48 +02:00
|
|
|
|
|
|
|
/* '"fieldname" : "value"' or '"value"' if fieldname is NULL. String must
|
|
|
|
* already be JSON escaped as necessary. */
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_escaped_string(struct json_stream *result,
|
2018-10-19 03:17:48 +02:00
|
|
|
const char *fieldname,
|
|
|
|
const struct json_escaped *esc TAKES);
|
|
|
|
|
|
|
|
/* '"fieldname" : literal' or 'literal' if fieldname is NULL*/
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_literal(struct json_stream *result, const char *fieldname,
|
2018-10-19 03:17:48 +02:00
|
|
|
const char *literal, int len);
|
|
|
|
/* '"fieldname" : value' or 'value' if fieldname is NULL */
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_double(struct json_stream *result, const char *fieldname,
|
2018-10-19 03:17:48 +02:00
|
|
|
double value);
|
|
|
|
/* '"fieldname" : value' or 'value' if fieldname is NULL */
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_num(struct json_stream *result, const char *fieldname,
|
2018-10-19 03:17:48 +02:00
|
|
|
unsigned int value);
|
|
|
|
/* '"fieldname" : value' or 'value' if fieldname is NULL */
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_u64(struct json_stream *result, const char *fieldname,
|
2018-10-19 03:17:48 +02:00
|
|
|
uint64_t value);
|
|
|
|
/* '"fieldname" : true|false' or 'true|false' if fieldname is NULL */
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_bool(struct json_stream *result, const char *fieldname,
|
2018-10-19 03:17:48 +02:00
|
|
|
bool value);
|
|
|
|
/* '"fieldname" : "0189abcdef..."' or "0189abcdef..." if fieldname is NULL */
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_hex(struct json_stream *result, const char *fieldname,
|
2018-10-19 03:17:48 +02:00
|
|
|
const void *data, size_t len);
|
|
|
|
/* '"fieldname" : "0189abcdef..."' or "0189abcdef..." if fieldname is NULL */
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_hex_talarr(struct json_stream *result,
|
2018-10-19 03:17:48 +02:00
|
|
|
const char *fieldname,
|
|
|
|
const tal_t *data);
|
2018-11-20 02:46:32 +01: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_tok_address_scriptpubkey(const tal_t *ctx,
|
|
|
|
const struct chainparams *chainparams,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok, const u8 **scriptpubkey);
|
|
|
|
|
|
|
|
/* Parse the satoshi token in wallet_tx. */
|
|
|
|
bool json_tok_wtx(struct wallet_tx * tx, const char * buffer,
|
|
|
|
const jsmntok_t * sattok, u64 max);
|
2018-10-19 03:17:48 +02:00
|
|
|
|
2018-03-22 11:36:25 +01:00
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_JSON_H */
|