json: move bitcoin/lightning specific helpers into common/json_helpers.

We don't need them in common/json, since lightning-cli doesn't need these,
but plugins want them.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2019-01-15 14:24:27 +10:30
committed by Christian Decker
parent ea7e13b5a7
commit e65b680807
16 changed files with 89 additions and 60 deletions

View File

@@ -26,6 +26,7 @@ COMMON_SRC_NOGEN := \
common/io_lock.c \ common/io_lock.c \
common/json.c \ common/json.c \
common/json_escaped.c \ common/json_escaped.c \
common/json_helpers.c \
common/json_tok.c \ common/json_tok.c \
common/key_derive.c \ common/key_derive.c \
common/keyset.c \ common/keyset.c \

View File

@@ -1,6 +1,7 @@
/* JSON core and helpers */ /* JSON core and helpers */
#include "json.h" #include "json.h"
#include <assert.h> #include <assert.h>
#include <bitcoin/pubkey.h>
#include <ccan/build_assert/build_assert.h> #include <ccan/build_assert/build_assert.h>
#include <ccan/mem/mem.h> #include <ccan/mem/mem.h>
#include <ccan/str/hex/hex.h> #include <ccan/str/hex/hex.h>
@@ -127,34 +128,6 @@ bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b)
return false; return false;
} }
bool json_to_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
uint64_t *satoshi)
{
char *end;
unsigned long btc, sat;
btc = strtoul(buffer + tok->start, &end, 10);
if (btc == ULONG_MAX && errno == ERANGE)
return false;
if (end != buffer + tok->end) {
/* Expect always 8 decimal places. */
if (*end != '.' || buffer + tok->end - end != 9)
return false;
sat = strtoul(end+1, &end, 10);
if (sat == ULONG_MAX && errno == ERANGE)
return false;
if (end != buffer + tok->end)
return false;
} else
sat = 0;
*satoshi = btc * (uint64_t)100000000 + sat;
if (*satoshi != btc * (uint64_t)100000000 + sat)
return false;
return true;
}
bool json_tok_is_num(const char *buffer, const jsmntok_t *tok) bool json_tok_is_num(const char *buffer, const jsmntok_t *tok)
{ {
if (tok->type != JSMN_PRIMITIVE) if (tok->type != JSMN_PRIMITIVE)

View File

@@ -1,8 +1,6 @@
#ifndef LIGHTNING_COMMON_JSON_H #ifndef LIGHTNING_COMMON_JSON_H
#define LIGHTNING_COMMON_JSON_H #define LIGHTNING_COMMON_JSON_H
#include "config.h" #include "config.h"
#include <bitcoin/pubkey.h>
#include <ccan/take/take.h>
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
@@ -11,9 +9,6 @@
#define JSMN_STRICT 1 #define JSMN_STRICT 1
# include <external/jsmn/jsmn.h> # include <external/jsmn/jsmn.h>
struct json_escaped;
struct short_channel_id;
/* Include " if it's a string. */ /* Include " if it's a string. */
const char *json_tok_full(const char *buffer, const jsmntok_t *t); const char *json_tok_full(const char *buffer, const jsmntok_t *t);
@@ -43,10 +38,6 @@ bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num);
/* Extract boolean from this */ /* Extract boolean from this */
bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b); bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b);
/* Extract satoshis from this (may be a string, or a decimal number literal) */
bool json_to_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
uint64_t *satoshi);
/* Is this a number? [0..9]+ */ /* Is this a number? [0..9]+ */
bool json_tok_is_num(const char *buffer, const jsmntok_t *tok); bool json_tok_is_num(const char *buffer, const jsmntok_t *tok);

46
common/json_helpers.c Normal file
View File

@@ -0,0 +1,46 @@
#include <bitcoin/pubkey.h>
#include <bitcoin/short_channel_id.h>
#include <common/json_helpers.h>
#include <errno.h>
bool json_to_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
uint64_t *satoshi)
{
char *end;
unsigned long btc, sat;
btc = strtoul(buffer + tok->start, &end, 10);
if (btc == ULONG_MAX && errno == ERANGE)
return false;
if (end != buffer + tok->end) {
/* Expect always 8 decimal places. */
if (*end != '.' || buffer + tok->end - end != 9)
return false;
sat = strtoul(end+1, &end, 10);
if (sat == ULONG_MAX && errno == ERANGE)
return false;
if (end != buffer + tok->end)
return false;
} else
sat = 0;
*satoshi = btc * (uint64_t)100000000 + sat;
if (*satoshi != btc * (uint64_t)100000000 + sat)
return false;
return true;
}
bool json_to_pubkey(const char *buffer, const jsmntok_t *tok,
struct pubkey *pubkey)
{
return pubkey_from_hexstr(buffer + tok->start,
tok->end - tok->start, pubkey);
}
bool json_to_short_channel_id(const char *buffer, const jsmntok_t *tok,
struct short_channel_id *scid)
{
return (short_channel_id_from_str(buffer + tok->start,
tok->end - tok->start, scid));
}

22
common/json_helpers.h Normal file
View File

@@ -0,0 +1,22 @@
/* More specialized (bitcoin, lightning-specific) JSON helpers. */
#ifndef LIGHTNING_COMMON_JSON_HELPERS_H
#define LIGHTNING_COMMON_JSON_HELPERS_H
#include "config.h"
#include <common/json.h>
struct pubkey;
struct short_channel_id;
/* Extract a pubkey from this */
bool json_to_pubkey(const char *buffer, const jsmntok_t *tok,
struct pubkey *pubkey);
/* Extract satoshis from this (may be a string, or a decimal number literal) */
bool json_to_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
uint64_t *satoshi);
/* Extract a short_channel_id from this */
bool json_to_short_channel_id(const char *buffer, const jsmntok_t *tok,
struct short_channel_id *scid);
#endif /* LIGHTNING_COMMON_JSON_HELPERS_H */

View File

@@ -1,3 +1,4 @@
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/str/hex/hex.h> #include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h> #include <ccan/tal/str/str.h>
#include <common/json_command.h> #include <common/json_command.h>

View File

@@ -2,10 +2,13 @@
#ifndef LIGHTNING_COMMON_JSON_TOK_H #ifndef LIGHTNING_COMMON_JSON_TOK_H
#define LIGHTNING_COMMON_JSON_TOK_H #define LIGHTNING_COMMON_JSON_TOK_H
#include "config.h" #include "config.h"
#include <ccan/short_types/short_types.h>
#include <common/json.h> #include <common/json.h>
struct command; struct command;
struct command_result; struct command_result;
struct json_escaped;
struct sha256;
/* Extract json array token */ /* Extract json array token */
struct command_result *param_array(struct command *cmd, const char *name, struct command_result *param_array(struct command *cmd, const char *name,

View File

@@ -1,4 +1,5 @@
#include "../json.c" #include "../json.c"
#include "../json_helpers.c"
#include <ccan/mem/mem.h> #include <ccan/mem/mem.h>
#include <common/utils.h> #include <common/utils.h>
#include <stdio.h> #include <stdio.h>

View File

@@ -35,6 +35,7 @@ LIGHTNINGD_COMMON_OBJS := \
common/io_lock.o \ common/io_lock.o \
common/json.o \ common/json.o \
common/json_escaped.o \ common/json_escaped.o \
common/json_helpers.o \
common/json_tok.o \ common/json_tok.o \
common/memleak.o \ common/memleak.o \
common/msg_queue.o \ common/msg_queue.o \

View File

@@ -14,7 +14,7 @@
#include <ccan/tal/grab_file/grab_file.h> #include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/path/path.h> #include <ccan/tal/path/path.h>
#include <ccan/tal/str/str.h> #include <ccan/tal/str/str.h>
#include <common/json.h> #include <common/json_helpers.h>
#include <common/memleak.h> #include <common/memleak.h>
#include <common/timeout.h> #include <common/timeout.h>
#include <common/utils.h> #include <common/utils.h>

View File

@@ -5,6 +5,7 @@
#include <ccan/tal/str/str.h> #include <ccan/tal/str/str.h>
#include <common/features.h> #include <common/features.h>
#include <common/json_command.h> #include <common/json_command.h>
#include <common/json_helpers.h>
#include <common/jsonrpc_errors.h> #include <common/jsonrpc_errors.h>
#include <common/memleak.h> #include <common/memleak.h>
#include <common/param.h> #include <common/param.h>

View File

@@ -13,6 +13,7 @@
#include <common/features.h> #include <common/features.h>
#include <common/json_command.h> #include <common/json_command.h>
#include <common/json_escaped.h> #include <common/json_escaped.h>
#include <common/json_helpers.h>
#include <common/jsonrpc_errors.h> #include <common/jsonrpc_errors.h>
#include <common/param.h> #include <common/param.h>
#include <common/type_to_string.h> #include <common/type_to_string.h>

View File

@@ -5,6 +5,7 @@
#include <common/json.h> #include <common/json.h>
#include <common/json_command.h> #include <common/json_command.h>
#include <common/json_escaped.h> #include <common/json_escaped.h>
#include <common/json_helpers.h>
#include <common/jsonrpc_errors.h> #include <common/jsonrpc_errors.h>
#include <common/memleak.h> #include <common/memleak.h>
#include <common/param.h> #include <common/param.h>
@@ -97,13 +98,6 @@ void json_add_txid(struct json_stream *result, const char *fieldname,
json_add_string(result, fieldname, hex); json_add_string(result, fieldname, hex);
} }
bool json_to_pubkey(const char *buffer, const jsmntok_t *tok,
struct pubkey *pubkey)
{
return pubkey_from_hexstr(buffer + tok->start,
tok->end - tok->start, pubkey);
}
struct command_result *param_pubkey(struct command *cmd, const char *name, struct command_result *param_pubkey(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok, const char *buffer, const jsmntok_t *tok,
struct pubkey **pubkey) struct pubkey **pubkey)
@@ -126,13 +120,6 @@ void json_add_short_channel_id(struct json_stream *response,
type_to_string(response, struct short_channel_id, id)); type_to_string(response, struct short_channel_id, id));
} }
bool json_to_short_channel_id(const char *buffer, const jsmntok_t *tok,
struct short_channel_id *scid)
{
return (short_channel_id_from_str(buffer + tok->start,
tok->end - tok->start, scid));
}
struct command_result *param_short_channel_id(struct command *cmd, struct command_result *param_short_channel_id(struct command *cmd,
const char *name, const char *name,
const char *buffer, const char *buffer,

View File

@@ -47,18 +47,10 @@ void json_add_pubkey(struct json_stream *response,
void json_add_txid(struct json_stream *result, const char *fieldname, void json_add_txid(struct json_stream *result, const char *fieldname,
const struct bitcoin_txid *txid); const struct bitcoin_txid *txid);
/* Extract a pubkey from this */
bool json_to_pubkey(const char *buffer, const jsmntok_t *tok,
struct pubkey *pubkey);
struct command_result *param_pubkey(struct command *cmd, const char *name, struct command_result *param_pubkey(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok, const char *buffer, const jsmntok_t *tok,
struct pubkey **pubkey); struct pubkey **pubkey);
/* Extract a short_channel_id from this */
bool json_to_short_channel_id(const char *buffer, const jsmntok_t *tok,
struct short_channel_id *scid);
struct command_result *param_short_channel_id(struct command *cmd, struct command_result *param_short_channel_id(struct command *cmd,
const char *name, const char *name,
const char *buffer, const char *buffer,

View File

@@ -16,6 +16,7 @@
#include <common/features.h> #include <common/features.h>
#include <common/initial_commit_tx.h> #include <common/initial_commit_tx.h>
#include <common/json_command.h> #include <common/json_command.h>
#include <common/json_helpers.h>
#include <common/jsonrpc_errors.h> #include <common/jsonrpc_errors.h>
#include <common/key_derive.h> #include <common/key_derive.h>
#include <common/param.h> #include <common/param.h>

View File

@@ -21,6 +21,14 @@ const char *feerate_name(enum feerate feerate UNNEEDED)
/* Generated stub for fmt_wireaddr_without_port */ /* Generated stub for fmt_wireaddr_without_port */
char *fmt_wireaddr_without_port(const tal_t *ctx UNNEEDED, const struct wireaddr *a UNNEEDED) char *fmt_wireaddr_without_port(const tal_t *ctx UNNEEDED, const struct wireaddr *a UNNEEDED)
{ fprintf(stderr, "fmt_wireaddr_without_port called!\n"); abort(); } { fprintf(stderr, "fmt_wireaddr_without_port called!\n"); abort(); }
/* Generated stub for json_to_pubkey */
bool json_to_pubkey(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
struct pubkey *pubkey UNNEEDED)
{ fprintf(stderr, "json_to_pubkey called!\n"); abort(); }
/* Generated stub for json_to_short_channel_id */
bool json_to_short_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
struct short_channel_id *scid UNNEEDED)
{ fprintf(stderr, "json_to_short_channel_id called!\n"); abort(); }
/* Generated stub for log_ */ /* Generated stub for log_ */
void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED, const char *fmt UNNEEDED, ...) void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED, const char *fmt UNNEEDED, ...)