diff --git a/common/Makefile b/common/Makefile index 7bd094b30..a8a682210 100644 --- a/common/Makefile +++ b/common/Makefile @@ -26,6 +26,7 @@ COMMON_SRC_NOGEN := \ common/io_lock.c \ common/json.c \ common/json_escaped.c \ + common/json_helpers.c \ common/json_tok.c \ common/key_derive.c \ common/keyset.c \ diff --git a/common/json.c b/common/json.c index 0e9d0f3c2..678f1b5de 100644 --- a/common/json.c +++ b/common/json.c @@ -1,6 +1,7 @@ /* JSON core and helpers */ #include "json.h" #include +#include #include #include #include @@ -127,34 +128,6 @@ bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b) 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) { if (tok->type != JSMN_PRIMITIVE) diff --git a/common/json.h b/common/json.h index 954eebf72..d0e5f985b 100644 --- a/common/json.h +++ b/common/json.h @@ -1,8 +1,6 @@ #ifndef LIGHTNING_COMMON_JSON_H #define LIGHTNING_COMMON_JSON_H #include "config.h" -#include -#include #include #include #include @@ -11,9 +9,6 @@ #define JSMN_STRICT 1 # include -struct json_escaped; -struct short_channel_id; - /* Include " if it's a string. */ 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 */ 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]+ */ bool json_tok_is_num(const char *buffer, const jsmntok_t *tok); diff --git a/common/json_helpers.c b/common/json_helpers.c new file mode 100644 index 000000000..42ace5b15 --- /dev/null +++ b/common/json_helpers.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include + +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)); +} diff --git a/common/json_helpers.h b/common/json_helpers.h new file mode 100644 index 000000000..3a54ce387 --- /dev/null +++ b/common/json_helpers.h @@ -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 + +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 */ diff --git a/common/json_tok.c b/common/json_tok.c index 6174aa60f..b0b0d1cc2 100644 --- a/common/json_tok.c +++ b/common/json_tok.c @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/common/json_tok.h b/common/json_tok.h index ff6434f38..0a0e33d1c 100644 --- a/common/json_tok.h +++ b/common/json_tok.h @@ -2,10 +2,13 @@ #ifndef LIGHTNING_COMMON_JSON_TOK_H #define LIGHTNING_COMMON_JSON_TOK_H #include "config.h" +#include #include struct command; struct command_result; +struct json_escaped; +struct sha256; /* Extract json array token */ struct command_result *param_array(struct command *cmd, const char *name, diff --git a/common/test/run-json.c b/common/test/run-json.c index 71e4b7069..dff25ad61 100644 --- a/common/test/run-json.c +++ b/common/test/run-json.c @@ -1,4 +1,5 @@ #include "../json.c" +#include "../json_helpers.c" #include #include #include diff --git a/lightningd/Makefile b/lightningd/Makefile index e491e534e..eaf3a5e1e 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -35,6 +35,7 @@ LIGHTNINGD_COMMON_OBJS := \ common/io_lock.o \ common/json.o \ common/json_escaped.o \ + common/json_helpers.o \ common/json_tok.o \ common/memleak.o \ common/msg_queue.o \ diff --git a/lightningd/bitcoind.c b/lightningd/bitcoind.c index 9baa22e71..6a3b986dd 100644 --- a/lightningd/bitcoind.c +++ b/lightningd/bitcoind.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c index 0648419a9..1b405d5f7 100644 --- a/lightningd/connect_control.c +++ b/lightningd/connect_control.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 30e8c2256..544ba0af6 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include diff --git a/lightningd/json.c b/lightningd/json.c index a4d79b553..88614747e 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -97,13 +98,6 @@ void json_add_txid(struct json_stream *result, const char *fieldname, 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, const char *buffer, const jsmntok_t *tok, 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)); } -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, const char *name, const char *buffer, diff --git a/lightningd/json.h b/lightningd/json.h index 984f0379a..8d02a7712 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -47,18 +47,10 @@ void json_add_pubkey(struct json_stream *response, void json_add_txid(struct json_stream *result, const char *fieldname, 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, const char *buffer, const jsmntok_t *tok, 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, const char *name, const char *buffer, diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index e28c65f7c..b11fe80b9 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include diff --git a/lightningd/test/run-jsonrpc.c b/lightningd/test/run-jsonrpc.c index 09055b6e3..1d35b022b 100644 --- a/lightningd/test/run-jsonrpc.c +++ b/lightningd/test/run-jsonrpc.c @@ -21,6 +21,14 @@ const char *feerate_name(enum feerate feerate UNNEEDED) /* Generated stub for fmt_wireaddr_without_port */ char *fmt_wireaddr_without_port(const tal_t *ctx UNNEEDED, const struct wireaddr *a UNNEEDED) { 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_ */ void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED, const char *fmt UNNEEDED, ...)