diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 559b6c534..1a9a01ecc 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -431,18 +431,10 @@ static void json_dev_query_scids(struct command *cmd, if (!param(cmd, buffer, params, p_req("id", json_tok_pubkey, &id), - p_req("scids", json_tok_tok, &scidstok), + p_req("scids", json_tok_array, &scidstok), NULL)) return; - if (scidstok->type != JSMN_ARRAY) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%.*s' is not an array", - scidstok->end - scidstok->start, - buffer + scidstok->start); - return; - } - scids = tal_arr(cmd, struct short_channel_id, scidstok->size); end = json_next(scidstok); for (i = 0, t = scidstok + 1; t < end; t = json_next(t), i++) { diff --git a/lightningd/invoice.c b/lightningd/invoice.c index 9163f860d..375169aaa 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -149,7 +149,7 @@ static void json_invoice(struct command *cmd, p_req("label", json_tok_label, &label_val), p_req("description", json_tok_escaped_string, &desc_val), p_opt_def("expiry", json_tok_u64, &expiry, 3600), - p_opt("fallbacks", json_tok_tok, &fallbacks), + p_opt("fallbacks", json_tok_array, &fallbacks), p_opt("preimage", json_tok_tok, &preimagetok), NULL)) return; @@ -180,11 +180,6 @@ static void json_invoice(struct command *cmd, const jsmntok_t *i, *end = json_next(fallbacks); size_t n = 0; - if (fallbacks->type != JSMN_ARRAY) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "fallback must be an array"); - return; - } fallback_scripts = tal_arr(cmd, const u8 *, n); for (i = fallbacks + 1; i < end; i = json_next(i)) { tal_resize(&fallback_scripts, n+1); diff --git a/lightningd/json.c b/lightningd/json.c index 75f49dcdf..e821d34e5 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -92,6 +92,19 @@ void json_add_txid(struct json_result *result, const char *fieldname, json_add_string(result, fieldname, hex); } +bool json_tok_array(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + const jsmntok_t **arr) +{ + if (tok->type == JSMN_ARRAY) + return (*arr = tok); + + command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be an array, not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); + return false; +} + bool json_tok_bool(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, bool **b) diff --git a/lightningd/json.h b/lightningd/json.h index 531eef896..232271243 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -44,6 +44,11 @@ void json_add_pubkey(struct json_result *response, void json_add_txid(struct json_result *result, const char *fieldname, const struct bitcoin_txid *txid); +/* 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); + /* 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, diff --git a/lightningd/pay.c b/lightningd/pay.c index 64ac0cc63..040e5a68b 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -954,21 +954,13 @@ static void json_sendpay(struct command *cmd, const char *description; if (!param(cmd, buffer, params, - p_req("route", json_tok_tok, &routetok), + p_req("route", json_tok_array, &routetok), p_req("payment_hash", json_tok_sha256, &rhash), p_opt("description", json_tok_escaped_string, &description), p_opt("msatoshi", json_tok_u64, &msatoshi), NULL)) return; - if (routetok->type != JSMN_ARRAY) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%.*s' is not an array", - routetok->end - routetok->start, - buffer + routetok->start); - return; - } - end = json_next(routetok); n_hops = 0; route = tal_arr(cmd, struct route_hop, n_hops);