diff --git a/cli/Makefile b/cli/Makefile index f2eefe2b8..bd9c32a03 100644 --- a/cli/Makefile +++ b/cli/Makefile @@ -3,22 +3,11 @@ LIGHTNING_CLI_OBJS := $(LIGHTNING_CLI_SRC:.c=.o) LIGHTNING_CLI_COMMON_OBJS := \ bitcoin/chainparams.o \ - bitcoin/pubkey.o \ - bitcoin/shadouble.o \ - bitcoin/tx.o \ - common/amount.o \ - common/base32.o \ - common/bigsize.o \ common/configdir.o \ common/json.o \ common/json_stream.o \ - common/memleak.o \ - common/type_to_string.o \ common/utils.o \ - common/version.o \ - common/wireaddr.o \ - wire/fromwire.o \ - wire/towire.o + common/version.o lightning-cli-all: cli/lightning-cli diff --git a/cli/test/run-large-input.c b/cli/test/run-large-input.c index 812296b3a..e1b3cf794 100644 --- a/cli/test/run-large-input.c +++ b/cli/test/run-large-input.c @@ -51,19 +51,12 @@ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_sub called!\n"); abort(); } -/* Generated stub for amount_sat_to_msat */ - bool amount_sat_to_msat(struct amount_msat *msat UNNEEDED, - struct amount_sat sat UNNEEDED) -{ fprintf(stderr, "amount_sat_to_msat called!\n"); abort(); } /* Generated stub for bigsize_get */ size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED) { fprintf(stderr, "bigsize_get called!\n"); abort(); } /* Generated stub for bigsize_put */ size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED) { fprintf(stderr, "bigsize_put called!\n"); abort(); } -/* 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 fromwire_channel_id */ void fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct channel_id *channel_id UNNEEDED) diff --git a/cli/test/run-remove-hint.c b/cli/test/run-remove-hint.c index 19d0c7622..f535dc929 100644 --- a/cli/test/run-remove-hint.c +++ b/cli/test/run-remove-hint.c @@ -54,19 +54,12 @@ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_sub called!\n"); abort(); } -/* Generated stub for amount_sat_to_msat */ - bool amount_sat_to_msat(struct amount_msat *msat UNNEEDED, - struct amount_sat sat UNNEEDED) -{ fprintf(stderr, "amount_sat_to_msat called!\n"); abort(); } /* Generated stub for bigsize_get */ size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED) { fprintf(stderr, "bigsize_get called!\n"); abort(); } /* Generated stub for bigsize_put */ size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED) { fprintf(stderr, "bigsize_put called!\n"); abort(); } -/* 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 fromwire_channel_id */ void fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct channel_id *channel_id UNNEEDED) diff --git a/common/json.c b/common/json.c index b5af5e061..30ffe04a2 100644 --- a/common/json.c +++ b/common/json.c @@ -1,25 +1,15 @@ /* JSON core and helpers */ -#include #include -#include -#include -#include -#include -#include #include #include #include #include #include #include -#include #include #include -#include #include -#include #include -#include #include #include #include @@ -235,12 +225,6 @@ bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b) return false; } -bool json_to_secret(const char *buffer, const jsmntok_t *tok, struct secret *dest) -{ - return hex_decode(buffer + tok->start, tok->end - tok->start, - dest->data, sizeof(struct secret)); -} - u8 *json_tok_bin_from_hex(const tal_t *ctx, const char *buffer, const jsmntok_t *tok) { u8 *result; @@ -255,12 +239,6 @@ u8 *json_tok_bin_from_hex(const tal_t *ctx, const char *buffer, const jsmntok_t return result; } -bool json_to_preimage(const char *buffer, const jsmntok_t *tok, struct preimage *preimage) -{ - size_t hexlen = tok->end - tok->start; - return hex_decode(buffer + tok->start, hexlen, preimage->r, sizeof(preimage->r)); -} - bool json_tok_is_num(const char *buffer, const jsmntok_t *tok) { if (tok->type != JSMN_PRIMITIVE) @@ -474,112 +452,6 @@ const jsmntok_t *json_delve(const char *buffer, return tok; } -void json_add_node_id(struct json_stream *response, - const char *fieldname, - const struct node_id *id) -{ - json_add_hex(response, fieldname, id->k, sizeof(id->k)); -} - -void json_add_pubkey(struct json_stream *response, - const char *fieldname, - const struct pubkey *key) -{ - u8 der[PUBKEY_CMPR_LEN]; - - pubkey_to_der(der, key); - json_add_hex(response, fieldname, der, sizeof(der)); -} - -void json_add_txid(struct json_stream *result, const char *fieldname, - const struct bitcoin_txid *txid) -{ - char hex[hex_str_size(sizeof(*txid))]; - - bitcoin_txid_to_hex(txid, hex, sizeof(hex)); - json_add_string(result, fieldname, hex); -} - -void json_add_short_channel_id(struct json_stream *response, - const char *fieldname, - const struct short_channel_id *scid) -{ - json_add_member(response, fieldname, true, "%dx%dx%d", - short_channel_id_blocknum(scid), - short_channel_id_txnum(scid), - short_channel_id_outnum(scid)); -} - -void json_add_address(struct json_stream *response, const char *fieldname, - const struct wireaddr *addr) -{ - json_object_start(response, fieldname); - char *addrstr = tal_arr(response, char, INET6_ADDRSTRLEN); - if (addr->type == ADDR_TYPE_IPV4) { - inet_ntop(AF_INET, addr->addr, addrstr, INET_ADDRSTRLEN); - json_add_string(response, "type", "ipv4"); - json_add_string(response, "address", addrstr); - json_add_num(response, "port", addr->port); - } else if (addr->type == ADDR_TYPE_IPV6) { - inet_ntop(AF_INET6, addr->addr, addrstr, INET6_ADDRSTRLEN); - json_add_string(response, "type", "ipv6"); - json_add_string(response, "address", addrstr); - json_add_num(response, "port", addr->port); - } else if (addr->type == ADDR_TYPE_TOR_V2) { - json_add_string(response, "type", "torv2"); - json_add_string(response, "address", fmt_wireaddr_without_port(tmpctx, addr)); - json_add_num(response, "port", addr->port); - } else if (addr->type == ADDR_TYPE_TOR_V3) { - json_add_string(response, "type", "torv3"); - json_add_string(response, "address", fmt_wireaddr_without_port(tmpctx, addr)); - json_add_num(response, "port", addr->port); - } - json_object_end(response); -} - -void json_add_address_internal(struct json_stream *response, - const char *fieldname, - const struct wireaddr_internal *addr) -{ - switch (addr->itype) { - case ADDR_INTERNAL_SOCKNAME: - json_object_start(response, fieldname); - json_add_string(response, "type", "local socket"); - json_add_string(response, "socket", addr->u.sockname); - json_object_end(response); - return; - case ADDR_INTERNAL_ALLPROTO: - json_object_start(response, fieldname); - json_add_string(response, "type", "any protocol"); - json_add_num(response, "port", addr->u.port); - json_object_end(response); - return; - case ADDR_INTERNAL_AUTOTOR: - json_object_start(response, fieldname); - json_add_string(response, "type", "Tor generated address"); - json_add_address(response, "service", &addr->u.torservice.address); - json_object_end(response); - return; - case ADDR_INTERNAL_STATICTOR: - json_object_start(response, fieldname); - json_add_string(response, "type", "Tor from blob generated static address"); - json_add_address(response, "service", &addr->u.torservice.address); - json_object_end(response); - return; - case ADDR_INTERNAL_FORPROXY: - json_object_start(response, fieldname); - json_add_string(response, "type", "unresolved"); - json_add_string(response, "name", addr->u.unresolved.name); - json_add_num(response, "port", addr->u.unresolved.port); - json_object_end(response); - return; - case ADDR_INTERNAL_WIREADDR: - json_add_address(response, fieldname, &addr->u.wireaddr); - return; - } - abort(); -} - void json_add_num(struct json_stream *result, const char *fieldname, unsigned int value) { json_add_member(result, fieldname, false, "%u", value); @@ -658,13 +530,6 @@ void json_add_hex_talarr(struct json_stream *result, json_add_hex(result, fieldname, data, tal_bytelen(data)); } -void json_add_tx(struct json_stream *result, - const char *fieldname, - const struct bitcoin_tx *tx) -{ - json_add_hex_talarr(result, fieldname, linearize_tx(tmpctx, tx)); -} - void json_add_escaped_string(struct json_stream *result, const char *fieldname, const struct json_escape *esc TAKES) { @@ -681,42 +546,6 @@ void json_add_escaped_string(struct json_stream *result, const char *fieldname, tal_free(esc); } -void json_add_amount_msat_compat(struct json_stream *result, - struct amount_msat msat, - const char *rawfieldname, - const char *msatfieldname) -{ - json_add_u64(result, rawfieldname, msat.millisatoshis); /* Raw: low-level helper */ - json_add_amount_msat_only(result, msatfieldname, msat); -} - -void json_add_amount_msat_only(struct json_stream *result, - const char *msatfieldname, - struct amount_msat msat) -{ - json_add_string(result, msatfieldname, - type_to_string(tmpctx, struct amount_msat, &msat)); -} - -void json_add_amount_sat_compat(struct json_stream *result, - struct amount_sat sat, - const char *rawfieldname, - const char *msatfieldname) -{ - json_add_u64(result, rawfieldname, sat.satoshis); /* Raw: low-level helper */ - json_add_amount_sat_only(result, msatfieldname, sat); -} - -void json_add_amount_sat_only(struct json_stream *result, - const char *msatfieldname, - struct amount_sat sat) -{ - struct amount_msat msat; - if (amount_sat_to_msat(&msat, sat)) - json_add_string(result, msatfieldname, - type_to_string(tmpctx, struct amount_msat, &msat)); -} - void json_add_timeabs(struct json_stream *result, const char *fieldname, struct timeabs t) { @@ -735,24 +564,6 @@ void json_add_time(struct json_stream *result, const char *fieldname, json_add_string(result, fieldname, timebuf); } -void json_add_secret(struct json_stream *response, const char *fieldname, - const struct secret *secret) -{ - json_add_hex(response, fieldname, secret, sizeof(struct secret)); -} - -void json_add_sha256(struct json_stream *result, const char *fieldname, - const struct sha256 *hash) -{ - json_add_hex(result, fieldname, hash, sizeof(*hash)); -} - -void json_add_preimage(struct json_stream *result, const char *fieldname, - const struct preimage *preimage) -{ - json_add_hex(result, fieldname, preimage, sizeof(*preimage)); -} - void json_add_tok(struct json_stream *result, const char *fieldname, const jsmntok_t *tok, const char *buffer) { diff --git a/common/json.h b/common/json.h index f92a634d9..51feffc63 100644 --- a/common/json.h +++ b/common/json.h @@ -11,26 +11,10 @@ #define JSMN_STRICT 1 # include -struct amount_sat; -struct amount_msat; -struct bitcoin_tx; -struct bitcoin_txid; -struct channel_id; struct json_escape; struct json_stream; -struct pubkey; -struct node_id; -struct sha256; -struct preimage; -struct secret; -struct short_channel_id; struct timeabs; struct timespec; -struct wallet_payment; -struct wallet_tx; -struct wireaddr; -struct wireaddr_internal; - /* Include " if it's a string. */ const char *json_tok_full(const char *buffer, const jsmntok_t *t); @@ -47,9 +31,6 @@ char *json_strdup(const tal_t *ctx, const char *buffer, const jsmntok_t *tok); /* Decode a hex-encoded binary */ u8 *json_tok_bin_from_hex(const tal_t *ctx, const char *buffer, const jsmntok_t *tok); -/* Decode a hex-encoded payment preimage */ -bool json_to_preimage(const char *buffer, const jsmntok_t *tok, struct preimage *preimage); - /* Extract number from this (may be a string, or a number literal) */ bool json_to_number(const char *buffer, const jsmntok_t *tok, unsigned int *num); @@ -86,9 +67,6 @@ bool json_to_errcode(const char *buffer, const jsmntok_t *tok, errcode_t *errcod /* Extract boolean from this */ bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b); -/* Extract a secret from this. */ -bool json_to_secret(const char *buffer, const jsmntok_t *tok, struct secret *dest); - /* Is this a number? [0..9]+ */ bool json_tok_is_num(const char *buffer, const jsmntok_t *tok); @@ -140,41 +118,6 @@ const jsmntok_t *json_delve(const char *buffer, for (i = 0, t = (obj) + 1; i < (obj)->size; t = json_next(t+1), i++) -/* Helpers for outputting JSON results */ - -/* '"fieldname" : "0289abcdef..."' or "0289abcdef..." if fieldname is NULL */ -void json_add_pubkey(struct json_stream *response, - const char *fieldname, - const struct pubkey *key); - -/* '"fieldname" : "89abcdef..."' or "89abcdef..." if fieldname is NULL */ -void json_add_secret(struct json_stream *response, - const char *fieldname, - const struct secret *secret); - -/* '"fieldname" : "0289abcdef..."' or "0289abcdef..." if fieldname is NULL */ -void json_add_node_id(struct json_stream *response, - const char *fieldname, - const struct node_id *id); - -/* '"fieldname" : ' or "" if fieldname is NULL */ -void json_add_txid(struct json_stream *result, const char *fieldname, - const struct bitcoin_txid *txid); - -/* '"fieldname" : "1234:5:6"' */ -void json_add_short_channel_id(struct json_stream *response, - const char *fieldname, - const struct short_channel_id *id); - -/* JSON serialize a network address for a node */ -void json_add_address(struct json_stream *response, const char *fieldname, - const struct wireaddr *addr); - -/* JSON serialize a network address for a node. */ -void json_add_address_internal(struct json_stream *response, - const char *fieldname, - const struct wireaddr_internal *addr); - /* '"fieldname" : "value"' or '"value"' if fieldname is NULL. Turns * any non-printable chars into JSON escapes, but leaves existing escapes alone. */ @@ -218,36 +161,6 @@ void json_add_hex(struct json_stream *result, const char *fieldname, void json_add_hex_talarr(struct json_stream *result, const char *fieldname, const tal_t *data); -/* '"fieldname" : "010000000001..."' or "010000000001..." if fieldname is NULL */ -void json_add_tx(struct json_stream *result, - const char *fieldname, - const struct bitcoin_tx *tx); - -/* Adds both a 'raw' number field and an 'amount_msat' field */ -void json_add_amount_msat_compat(struct json_stream *result, - struct amount_msat msat, - const char *rawfieldname, - const char *msatfieldname) - NO_NULL_ARGS; - -/* Adds both a 'raw' number field and an 'amount_msat' field */ -void json_add_amount_sat_compat(struct json_stream *result, - struct amount_sat sat, - const char *rawfieldname, - const char *msatfieldname) - NO_NULL_ARGS; - -/* Adds an 'msat' field */ -void json_add_amount_msat_only(struct json_stream *result, - const char *msatfieldname, - struct amount_msat msat) - NO_NULL_ARGS; - -/* Adds an 'msat' field */ -void json_add_amount_sat_only(struct json_stream *result, - const char *msatfieldname, - struct amount_sat sat) - NO_NULL_ARGS; void json_add_timeabs(struct json_stream *result, const char *fieldname, struct timeabs t); @@ -256,12 +169,6 @@ void json_add_timeabs(struct json_stream *result, const char *fieldname, void json_add_time(struct json_stream *result, const char *fieldname, struct timespec ts); -void json_add_sha256(struct json_stream *result, const char *fieldname, - const struct sha256 *hash); - -void json_add_preimage(struct json_stream *result, const char *fieldname, - const struct preimage *preimage); - /* Add any json token */ void json_add_tok(struct json_stream *result, const char *fieldname, const jsmntok_t *tok, const char *buffer); diff --git a/common/json_helpers.c b/common/json_helpers.c index 2d04d4924..44a0eb4d7 100644 --- a/common/json_helpers.c +++ b/common/json_helpers.c @@ -1,10 +1,15 @@ +#include +#include #include #include #include #include #include #include +#include #include +#include +#include #include bool json_to_bitcoin_amount(const char *buffer, const jsmntok_t *tok, @@ -108,3 +113,183 @@ bool split_tok(const char *buffer, const jsmntok_t *tok, return true; } + +bool json_to_secret(const char *buffer, const jsmntok_t *tok, struct secret *dest) +{ + return hex_decode(buffer + tok->start, tok->end - tok->start, + dest->data, sizeof(struct secret)); +} + +bool json_to_preimage(const char *buffer, const jsmntok_t *tok, struct preimage *preimage) +{ + size_t hexlen = tok->end - tok->start; + return hex_decode(buffer + tok->start, hexlen, preimage->r, sizeof(preimage->r)); +} + +void json_add_node_id(struct json_stream *response, + const char *fieldname, + const struct node_id *id) +{ + json_add_hex(response, fieldname, id->k, sizeof(id->k)); +} + +void json_add_pubkey(struct json_stream *response, + const char *fieldname, + const struct pubkey *key) +{ + u8 der[PUBKEY_CMPR_LEN]; + + pubkey_to_der(der, key); + json_add_hex(response, fieldname, der, sizeof(der)); +} + +void json_add_txid(struct json_stream *result, const char *fieldname, + const struct bitcoin_txid *txid) +{ + char hex[hex_str_size(sizeof(*txid))]; + + bitcoin_txid_to_hex(txid, hex, sizeof(hex)); + json_add_string(result, fieldname, hex); +} + +void json_add_short_channel_id(struct json_stream *response, + const char *fieldname, + const struct short_channel_id *scid) +{ + json_add_member(response, fieldname, true, "%dx%dx%d", + short_channel_id_blocknum(scid), + short_channel_id_txnum(scid), + short_channel_id_outnum(scid)); +} + +void json_add_address(struct json_stream *response, const char *fieldname, + const struct wireaddr *addr) +{ + json_object_start(response, fieldname); + char *addrstr = tal_arr(response, char, INET6_ADDRSTRLEN); + if (addr->type == ADDR_TYPE_IPV4) { + inet_ntop(AF_INET, addr->addr, addrstr, INET_ADDRSTRLEN); + json_add_string(response, "type", "ipv4"); + json_add_string(response, "address", addrstr); + json_add_num(response, "port", addr->port); + } else if (addr->type == ADDR_TYPE_IPV6) { + inet_ntop(AF_INET6, addr->addr, addrstr, INET6_ADDRSTRLEN); + json_add_string(response, "type", "ipv6"); + json_add_string(response, "address", addrstr); + json_add_num(response, "port", addr->port); + } else if (addr->type == ADDR_TYPE_TOR_V2) { + json_add_string(response, "type", "torv2"); + json_add_string(response, "address", fmt_wireaddr_without_port(tmpctx, addr)); + json_add_num(response, "port", addr->port); + } else if (addr->type == ADDR_TYPE_TOR_V3) { + json_add_string(response, "type", "torv3"); + json_add_string(response, "address", fmt_wireaddr_without_port(tmpctx, addr)); + json_add_num(response, "port", addr->port); + } + json_object_end(response); +} + +void json_add_address_internal(struct json_stream *response, + const char *fieldname, + const struct wireaddr_internal *addr) +{ + switch (addr->itype) { + case ADDR_INTERNAL_SOCKNAME: + json_object_start(response, fieldname); + json_add_string(response, "type", "local socket"); + json_add_string(response, "socket", addr->u.sockname); + json_object_end(response); + return; + case ADDR_INTERNAL_ALLPROTO: + json_object_start(response, fieldname); + json_add_string(response, "type", "any protocol"); + json_add_num(response, "port", addr->u.port); + json_object_end(response); + return; + case ADDR_INTERNAL_AUTOTOR: + json_object_start(response, fieldname); + json_add_string(response, "type", "Tor generated address"); + json_add_address(response, "service", &addr->u.torservice.address); + json_object_end(response); + return; + case ADDR_INTERNAL_STATICTOR: + json_object_start(response, fieldname); + json_add_string(response, "type", "Tor from blob generated static address"); + json_add_address(response, "service", &addr->u.torservice.address); + json_object_end(response); + return; + case ADDR_INTERNAL_FORPROXY: + json_object_start(response, fieldname); + json_add_string(response, "type", "unresolved"); + json_add_string(response, "name", addr->u.unresolved.name); + json_add_num(response, "port", addr->u.unresolved.port); + json_object_end(response); + return; + case ADDR_INTERNAL_WIREADDR: + json_add_address(response, fieldname, &addr->u.wireaddr); + return; + } + abort(); +} + +void json_add_tx(struct json_stream *result, + const char *fieldname, + const struct bitcoin_tx *tx) +{ + json_add_hex_talarr(result, fieldname, linearize_tx(tmpctx, tx)); +} + +void json_add_amount_msat_compat(struct json_stream *result, + struct amount_msat msat, + const char *rawfieldname, + const char *msatfieldname) +{ + json_add_u64(result, rawfieldname, msat.millisatoshis); /* Raw: low-level helper */ + json_add_amount_msat_only(result, msatfieldname, msat); +} + +void json_add_amount_msat_only(struct json_stream *result, + const char *msatfieldname, + struct amount_msat msat) +{ + json_add_string(result, msatfieldname, + type_to_string(tmpctx, struct amount_msat, &msat)); +} + +void json_add_amount_sat_compat(struct json_stream *result, + struct amount_sat sat, + const char *rawfieldname, + const char *msatfieldname) +{ + json_add_u64(result, rawfieldname, sat.satoshis); /* Raw: low-level helper */ + json_add_amount_sat_only(result, msatfieldname, sat); +} + +void json_add_amount_sat_only(struct json_stream *result, + const char *msatfieldname, + struct amount_sat sat) +{ + struct amount_msat msat; + if (amount_sat_to_msat(&msat, sat)) + json_add_string(result, msatfieldname, + type_to_string(tmpctx, struct amount_msat, &msat)); +} + +void json_add_secret(struct json_stream *response, const char *fieldname, + const struct secret *secret) +{ + json_add_hex(response, fieldname, secret, sizeof(struct secret)); +} + +void json_add_sha256(struct json_stream *result, const char *fieldname, + const struct sha256 *hash) +{ + json_add_hex(result, fieldname, hash, sizeof(*hash)); +} + +void json_add_preimage(struct json_stream *result, const char *fieldname, + const struct preimage *preimage) +{ + json_add_hex(result, fieldname, preimage, sizeof(*preimage)); +} + diff --git a/common/json_helpers.h b/common/json_helpers.h index 9e2499045..2d422bbf0 100644 --- a/common/json_helpers.h +++ b/common/json_helpers.h @@ -8,9 +8,18 @@ struct amount_msat; struct amount_sat; +struct channel_id; struct pubkey; struct node_id; struct short_channel_id; +struct wireaddr; +struct wireaddr_internal; + +/* Decode a hex-encoded payment preimage */ +bool json_to_preimage(const char *buffer, const jsmntok_t *tok, struct preimage *preimage); + +/* Extract a secret from this. */ +bool json_to_secret(const char *buffer, const jsmntok_t *tok, struct secret *dest); /* Extract a pubkey from this */ bool json_to_pubkey(const char *buffer, const jsmntok_t *tok, @@ -54,4 +63,76 @@ bool split_tok(const char *buffer, const jsmntok_t *tok, char split, jsmntok_t *a, jsmntok_t *b); + +/* Helpers for outputting JSON results */ + +/* '"fieldname" : "0289abcdef..."' or "0289abcdef..." if fieldname is NULL */ +void json_add_pubkey(struct json_stream *response, + const char *fieldname, + const struct pubkey *key); + +/* '"fieldname" : "89abcdef..."' or "89abcdef..." if fieldname is NULL */ +void json_add_secret(struct json_stream *response, + const char *fieldname, + const struct secret *secret); + +/* '"fieldname" : "0289abcdef..."' or "0289abcdef..." if fieldname is NULL */ +void json_add_node_id(struct json_stream *response, + const char *fieldname, + const struct node_id *id); + +/* '"fieldname" : ' or "" if fieldname is NULL */ +void json_add_txid(struct json_stream *result, const char *fieldname, + const struct bitcoin_txid *txid); + +/* '"fieldname" : "1234:5:6"' */ +void json_add_short_channel_id(struct json_stream *response, + const char *fieldname, + const struct short_channel_id *id); + +/* JSON serialize a network address for a node */ +void json_add_address(struct json_stream *response, const char *fieldname, + const struct wireaddr *addr); + +/* JSON serialize a network address for a node. */ +void json_add_address_internal(struct json_stream *response, + const char *fieldname, + const struct wireaddr_internal *addr); + +/* Adds both a 'raw' number field and an 'amount_msat' field */ +void json_add_amount_msat_compat(struct json_stream *result, + struct amount_msat msat, + const char *rawfieldname, + const char *msatfieldname) + NO_NULL_ARGS; + +/* Adds both a 'raw' number field and an 'amount_msat' field */ +void json_add_amount_sat_compat(struct json_stream *result, + struct amount_sat sat, + const char *rawfieldname, + const char *msatfieldname) + NO_NULL_ARGS; + +/* Adds an 'msat' field */ +void json_add_amount_msat_only(struct json_stream *result, + const char *msatfieldname, + struct amount_msat msat) + NO_NULL_ARGS; + +/* Adds an 'msat' field */ +void json_add_amount_sat_only(struct json_stream *result, + const char *msatfieldname, + struct amount_sat sat) + NO_NULL_ARGS; + +void json_add_sha256(struct json_stream *result, const char *fieldname, + const struct sha256 *hash); + +void json_add_preimage(struct json_stream *result, const char *fieldname, + const struct preimage *preimage); + +/* '"fieldname" : "010000000001..."' or "010000000001..." if fieldname is NULL */ +void json_add_tx(struct json_stream *result, + const char *fieldname, + const struct bitcoin_tx *tx); #endif /* LIGHTNING_COMMON_JSON_HELPERS_H */ diff --git a/common/json_tok.h b/common/json_tok.h index 038015a8e..3a428bc2e 100644 --- a/common/json_tok.h +++ b/common/json_tok.h @@ -10,6 +10,7 @@ struct amount_msat; struct amount_sat; +struct channel_id; struct command; struct command_result; struct json_escape; diff --git a/common/test/run-bigsize.c b/common/test/run-bigsize.c index e833e3a96..b933bc073 100644 --- a/common/test/run-bigsize.c +++ b/common/test/run-bigsize.c @@ -31,13 +31,6 @@ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_sub called!\n"); abort(); } -/* Generated stub for amount_sat_to_msat */ - bool amount_sat_to_msat(struct amount_msat *msat UNNEEDED, - struct amount_sat sat UNNEEDED) -{ fprintf(stderr, "amount_sat_to_msat called!\n"); abort(); } -/* 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 fromwire_fail */ const void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) { fprintf(stderr, "fromwire_fail called!\n"); abort(); } @@ -63,10 +56,6 @@ void json_object_end(struct json_stream *js UNNEEDED) /* Generated stub for json_object_start */ void json_object_start(struct json_stream *ks UNNEEDED, const char *fieldname UNNEEDED) { fprintf(stderr, "json_object_start called!\n"); abort(); } -/* Generated stub for type_to_string_ */ -const char *type_to_string_(const tal_t *ctx UNNEEDED, const char *typename UNNEEDED, - union printable_types u UNNEEDED) -{ fprintf(stderr, "type_to_string_ called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ /* BOLT #1: diff --git a/common/test/run-json_remove.c b/common/test/run-json_remove.c index 7d2816e43..86a0fafa6 100644 --- a/common/test/run-json_remove.c +++ b/common/test/run-json_remove.c @@ -23,13 +23,6 @@ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_sub called!\n"); abort(); } -/* Generated stub for amount_sat_to_msat */ - bool amount_sat_to_msat(struct amount_msat *msat UNNEEDED, - struct amount_sat sat UNNEEDED) -{ fprintf(stderr, "amount_sat_to_msat called!\n"); abort(); } -/* 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 fromwire_fail */ const void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) { fprintf(stderr, "fromwire_fail called!\n"); abort(); } @@ -55,10 +48,6 @@ void json_object_end(struct json_stream *js UNNEEDED) /* Generated stub for json_object_start */ void json_object_start(struct json_stream *ks UNNEEDED, const char *fieldname UNNEEDED) { fprintf(stderr, "json_object_start called!\n"); abort(); } -/* Generated stub for type_to_string_ */ -const char *type_to_string_(const tal_t *ctx UNNEEDED, const char *typename UNNEEDED, - union printable_types u UNNEEDED) -{ fprintf(stderr, "type_to_string_ called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ struct json { diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index 32faa3e72..23a6ed0d5 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include diff --git a/lightningd/hsm_control.c b/lightningd/hsm_control.c index 9f14aa02b..22b6b715e 100644 --- a/lightningd/hsm_control.c +++ b/lightningd/hsm_control.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 4961b2dc4..7e9352e30 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include diff --git a/lightningd/log.c b/lightningd/log.c index ee7acc452..04de821df 100644 --- a/lightningd/log.c +++ b/lightningd/log.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include diff --git a/lightningd/notification.c b/lightningd/notification.c index cb398048c..99d794440 100644 --- a/lightningd/notification.c +++ b/lightningd/notification.c @@ -1,4 +1,5 @@ #include +#include #include #include #include diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 40a62bcda..636c8988e 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 731da6e1a..641a325b2 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index 130c66eaa..4ee749be3 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -108,9 +108,6 @@ bool feature_is_set(const u8 *features UNNEEDED, size_t bit UNNEEDED) /* Generated stub for fixup_htlcs_out */ void fixup_htlcs_out(struct lightningd *ld UNNEEDED) { fprintf(stderr, "fixup_htlcs_out called!\n"); abort(); } -/* 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 fromwire_channel_dev_memleak_reply */ bool fromwire_channel_dev_memleak_reply(const void *p UNNEEDED, bool *leak UNNEEDED) { fprintf(stderr, "fromwire_channel_dev_memleak_reply called!\n"); abort(); } @@ -157,6 +154,29 @@ void htlc_set_fail(struct htlc_set *set UNNEEDED, const u8 *failmsg TAKES UNNEED /* Generated stub for htlc_set_fulfill */ void htlc_set_fulfill(struct htlc_set *set UNNEEDED, const struct preimage *preimage UNNEEDED) { fprintf(stderr, "htlc_set_fulfill called!\n"); abort(); } +/* Generated stub for json_add_address */ +void json_add_address(struct json_stream *response UNNEEDED, const char *fieldname UNNEEDED, + const struct wireaddr *addr UNNEEDED) +{ fprintf(stderr, "json_add_address called!\n"); abort(); } +/* Generated stub for json_add_address_internal */ +void json_add_address_internal(struct json_stream *response UNNEEDED, + const char *fieldname UNNEEDED, + const struct wireaddr_internal *addr UNNEEDED) +{ fprintf(stderr, "json_add_address_internal called!\n"); abort(); } +/* Generated stub for json_add_amount_msat_compat */ +void json_add_amount_msat_compat(struct json_stream *result UNNEEDED, + struct amount_msat msat UNNEEDED, + const char *rawfieldname UNNEEDED, + const char *msatfieldname) + +{ fprintf(stderr, "json_add_amount_msat_compat called!\n"); abort(); } +/* Generated stub for json_add_amount_sat_compat */ +void json_add_amount_sat_compat(struct json_stream *result UNNEEDED, + struct amount_sat sat UNNEEDED, + const char *rawfieldname UNNEEDED, + const char *msatfieldname) + +{ fprintf(stderr, "json_add_amount_sat_compat called!\n"); abort(); } /* Generated stub for json_add_log */ void json_add_log(struct json_stream *result UNNEEDED, const struct log_book *lr UNNEEDED, @@ -169,6 +189,38 @@ void json_add_member(struct json_stream *js UNNEEDED, bool quote UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "json_add_member called!\n"); abort(); } +/* Generated stub for json_add_node_id */ +void json_add_node_id(struct json_stream *response UNNEEDED, + const char *fieldname UNNEEDED, + const struct node_id *id UNNEEDED) +{ fprintf(stderr, "json_add_node_id called!\n"); abort(); } +/* Generated stub for json_add_preimage */ +void json_add_preimage(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, + const struct preimage *preimage UNNEEDED) +{ fprintf(stderr, "json_add_preimage called!\n"); abort(); } +/* Generated stub for json_add_secret */ +void json_add_secret(struct json_stream *response UNNEEDED, + const char *fieldname UNNEEDED, + const struct secret *secret UNNEEDED) +{ fprintf(stderr, "json_add_secret called!\n"); abort(); } +/* Generated stub for json_add_sha256 */ +void json_add_sha256(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, + const struct sha256 *hash UNNEEDED) +{ fprintf(stderr, "json_add_sha256 called!\n"); abort(); } +/* Generated stub for json_add_short_channel_id */ +void json_add_short_channel_id(struct json_stream *response UNNEEDED, + const char *fieldname UNNEEDED, + const struct short_channel_id *id UNNEEDED) +{ fprintf(stderr, "json_add_short_channel_id called!\n"); abort(); } +/* Generated stub for json_add_tx */ +void json_add_tx(struct json_stream *result UNNEEDED, + const char *fieldname UNNEEDED, + const struct bitcoin_tx *tx UNNEEDED) +{ fprintf(stderr, "json_add_tx called!\n"); abort(); } +/* Generated stub for json_add_txid */ +void json_add_txid(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, + const struct bitcoin_txid *txid UNNEEDED) +{ fprintf(stderr, "json_add_txid called!\n"); abort(); } /* Generated stub for json_add_uncommitted_channel */ void json_add_uncommitted_channel(struct json_stream *response UNNEEDED, const struct uncommitted_channel *uc UNNEEDED) diff --git a/lightningd/test/run-jsonrpc.c b/lightningd/test/run-jsonrpc.c index ed283b350..2532cdb85 100644 --- a/lightningd/test/run-jsonrpc.c +++ b/lightningd/test/run-jsonrpc.c @@ -24,13 +24,14 @@ u32 feerate_from_style(u32 feerate UNNEEDED, enum feerate_style style UNNEEDED) /* Generated stub for feerate_name */ const char *feerate_name(enum feerate feerate UNNEEDED) { fprintf(stderr, "feerate_name called!\n"); abort(); } -/* 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 fromwire_channel_id */ void fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct channel_id *channel_id UNNEEDED) { fprintf(stderr, "fromwire_channel_id called!\n"); abort(); } +/* Generated stub for json_add_sha256 */ +void json_add_sha256(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, + const struct sha256 *hash UNNEEDED) +{ fprintf(stderr, "json_add_sha256 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) diff --git a/lightningd/test/run-log-pruning.c b/lightningd/test/run-log-pruning.c index 2ee49dfe1..a95d2ee5b 100644 --- a/lightningd/test/run-log-pruning.c +++ b/lightningd/test/run-log-pruning.c @@ -22,9 +22,6 @@ struct command_result *command_success(struct command *cmd UNNEEDED, struct json_stream *response) { fprintf(stderr, "command_success called!\n"); abort(); } -/* 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 fromwire_channel_id */ void fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct channel_id *channel_id UNNEEDED) @@ -35,6 +32,11 @@ void json_add_member(struct json_stream *js UNNEEDED, bool quote UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "json_add_member called!\n"); abort(); } +/* Generated stub for json_add_node_id */ +void json_add_node_id(struct json_stream *response UNNEEDED, + const char *fieldname UNNEEDED, + const struct node_id *id UNNEEDED) +{ fprintf(stderr, "json_add_node_id called!\n"); abort(); } /* Generated stub for json_array_end */ void json_array_end(struct json_stream *js UNNEEDED) { fprintf(stderr, "json_array_end called!\n"); abort(); } diff --git a/wire/test/run-peer-wire.c b/wire/test/run-peer-wire.c index bec4ecd85..c12a3478f 100644 --- a/wire/test/run-peer-wire.c +++ b/wire/test/run-peer-wire.c @@ -1,6 +1,7 @@ #include "../towire.c" #include "../fromwire.c" #include "../peer_wire.c" +#include "common/channel_id.c" #include #include @@ -34,13 +35,6 @@ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) { fprintf(stderr, "amount_sat_sub called!\n"); abort(); } -/* Generated stub for fromwire_channel_id */ -void fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, - struct channel_id *channel_id UNNEEDED) -{ fprintf(stderr, "fromwire_channel_id called!\n"); abort(); } -/* Generated stub for towire_channel_id */ -void towire_channel_id(u8 **pptr UNNEEDED, const struct channel_id *channel_id UNNEEDED) -{ fprintf(stderr, "towire_channel_id called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ /* memsetting pubkeys doesn't work */