Browse Source

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>
plugin-timeout-inc
Rusty Russell 6 years ago
committed by Christian Decker
parent
commit
e65b680807
  1. 1
      common/Makefile
  2. 29
      common/json.c
  3. 9
      common/json.h
  4. 46
      common/json_helpers.c
  5. 22
      common/json_helpers.h
  6. 1
      common/json_tok.c
  7. 3
      common/json_tok.h
  8. 1
      common/test/run-json.c
  9. 1
      lightningd/Makefile
  10. 2
      lightningd/bitcoind.c
  11. 1
      lightningd/connect_control.c
  12. 1
      lightningd/gossip_control.c
  13. 15
      lightningd/json.c
  14. 8
      lightningd/json.h
  15. 1
      lightningd/peer_control.c
  16. 8
      lightningd/test/run-jsonrpc.c

1
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 \

29
common/json.c

@ -1,6 +1,7 @@
/* JSON core and helpers */
#include "json.h"
#include <assert.h>
#include <bitcoin/pubkey.h>
#include <ccan/build_assert/build_assert.h>
#include <ccan/mem/mem.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;
}
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)

9
common/json.h

@ -1,8 +1,6 @@
#ifndef LIGHTNING_COMMON_JSON_H
#define LIGHTNING_COMMON_JSON_H
#include "config.h"
#include <bitcoin/pubkey.h>
#include <ccan/take/take.h>
#include <ccan/tal/tal.h>
#include <stdbool.h>
#include <stdint.h>
@ -11,9 +9,6 @@
#define JSMN_STRICT 1
# include <external/jsmn/jsmn.h>
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);

46
common/json_helpers.c

@ -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

@ -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 */

1
common/json_tok.c

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

3
common/json_tok.h

@ -2,10 +2,13 @@
#ifndef LIGHTNING_COMMON_JSON_TOK_H
#define LIGHTNING_COMMON_JSON_TOK_H
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <common/json.h>
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,

1
common/test/run-json.c

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

1
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 \

2
lightningd/bitcoind.c

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

1
lightningd/connect_control.c

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

1
lightningd/gossip_control.c

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

15
lightningd/json.c

@ -5,6 +5,7 @@
#include <common/json.h>
#include <common/json_command.h>
#include <common/json_escaped.h>
#include <common/json_helpers.h>
#include <common/jsonrpc_errors.h>
#include <common/memleak.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);
}
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,

8
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,

1
lightningd/peer_control.c

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

8
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, ...)

Loading…
Cancel
Save