diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index c3db528c7..ade73a96e 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -383,7 +383,7 @@ static void json_listchannels(struct command *cmd, const char *buffer, u8 *req; struct short_channel_id *id; if (!param(cmd, buffer, params, - p_opt("short_channel_id", json_tok_short_channel_id, &id), + p_opt_tal("short_channel_id", json_tok_short_channel_id, &id), NULL)) return; @@ -452,7 +452,7 @@ static void json_dev_query_scids(struct command *cmd, 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++) { - if (!json_tok_short_channel_id(buffer, t, &scids[i])) { + if (!json_to_short_channel_id(buffer, t, &scids[i])) { command_fail(cmd, JSONRPC2_INVALID_PARAMS, "scid %zu '%.*s' is not an scid", i, t->end - t->start, diff --git a/lightningd/json.c b/lightningd/json.c index fc92e45bf..baac19d0b 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -201,12 +201,26 @@ void json_add_short_channel_id(struct json_result *response, type_to_string(response, struct short_channel_id, id)); } -bool json_tok_short_channel_id(const char *buffer, const jsmntok_t *tok, - struct short_channel_id *scid) +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); + return (short_channel_id_from_str(buffer + tok->start, + tok->end - tok->start, scid)); +} + +bool json_tok_short_channel_id(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + struct short_channel_id **scid) +{ + *scid = tal(cmd, struct short_channel_id); + if (short_channel_id_from_str(buffer + tok->start, + tok->end - tok->start, *scid)) + return true; + + command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a short channel id, not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); + return false; } bool diff --git a/lightningd/json.h b/lightningd/json.h index 7e9c76441..cf664ce10 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -71,8 +71,12 @@ bool json_tok_pubkey(struct command *cmd, const char *name, struct pubkey **pubkey); /* Extract a short_channel_id from this */ -bool json_tok_short_channel_id(const char *buffer, const jsmntok_t *tok, - struct short_channel_id *scid); +bool json_to_short_channel_id(const char *buffer, const jsmntok_t *tok, + struct short_channel_id *scid); + +bool json_tok_short_channel_id(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + struct short_channel_id **scid); /* Extract number from this (may be a string, or a number literal) */ bool json_tok_u64(struct command *cmd, const char *name, diff --git a/lightningd/pay.c b/lightningd/pay.c index 3988c342f..6cb19991e 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -1007,8 +1007,8 @@ static void json_sendpay(struct command *cmd, return; } - if (!json_tok_short_channel_id(buffer, chantok, - &route[n_hops].channel_id)) { + if (!json_to_short_channel_id(buffer, chantok, + &route[n_hops].channel_id)) { command_fail(cmd, JSONRPC2_INVALID_PARAMS, "Route %zu invalid channel_id", n_hops); return; diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 39f55b309..add0e06d3 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -803,7 +803,7 @@ command_find_channel(struct command *cmd, tok->end - tok->start, buffer + tok->start); return NULL; - } else if (json_tok_short_channel_id(buffer, tok, &scid)) { + } else if (json_to_short_channel_id(buffer, tok, &scid)) { list_for_each(&ld->peers, peer, list) { channel = peer_active_channel(peer); if (!channel) @@ -1172,7 +1172,7 @@ static void json_dev_forget_channel(struct command *cmd, const char *buffer, bool *force; if (!param(cmd, buffer, params, p_req_tal("id", json_tok_pubkey, &peerid), - p_opt("short_channel_id", json_tok_short_channel_id, &scid), + p_opt_tal("short_channel_id", json_tok_short_channel_id, &scid), p_opt_def_tal("force", json_tok_bool, &force, false), NULL)) return; diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 9a6dd9605..aef8add53 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -241,8 +241,9 @@ bool json_tok_pubkey(struct command *cmd UNNEEDED, const char *name UNNEEDED, struct pubkey **pubkey UNNEEDED) { fprintf(stderr, "json_tok_pubkey called!\n"); abort(); } /* Generated stub for json_tok_short_channel_id */ -bool json_tok_short_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - struct short_channel_id *scid UNNEEDED) +bool json_tok_short_channel_id(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + struct short_channel_id **scid UNNEEDED) { fprintf(stderr, "json_tok_short_channel_id called!\n"); abort(); } /* Generated stub for json_tok_tok */ bool json_tok_tok(struct command *cmd UNNEEDED, const char *name UNNEEDED, @@ -253,6 +254,10 @@ bool json_tok_tok(struct command *cmd UNNEEDED, const char *name UNNEEDED, 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 kill_uncommitted_channel */ void kill_uncommitted_channel(struct uncommitted_channel *uc UNNEEDED, const char *why UNNEEDED)