param: upgraded json_tok_pubkey

Also add json_to_pubkey as utility function.

Signed-off-by: Mark Beckwith <wythe@intrig.com>
This commit is contained in:
Mark Beckwith 2018-08-14 16:19:31 -05:00 committed by Rusty Russell
parent 6254d15efd
commit 9b28ecf8fc
10 changed files with 72 additions and 49 deletions

View File

@ -105,7 +105,7 @@ static void json_connect(struct command *cmd,
idtok->end = idtok->start + atidx; idtok->end = idtok->start + atidx;
} }
if (!json_tok_pubkey(buffer, idtok, &id)) { if (!json_to_pubkey(buffer, idtok, &id)) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS, command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"id %.*s not valid", "id %.*s not valid",
idtok->end - idtok->start, idtok->end - idtok->start,

View File

@ -233,7 +233,7 @@ static void json_listnodes(struct command *cmd, const char *buffer,
struct pubkey *id; struct pubkey *id;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_opt("id", json_tok_pubkey, &id), p_opt_tal("id", json_tok_pubkey, &id),
NULL)) NULL))
return; return;
@ -272,8 +272,8 @@ static void json_getroute_reply(struct subd *gossip UNUSED, const u8 *reply, con
static void json_getroute(struct command *cmd, const char *buffer, const jsmntok_t *params) static void json_getroute(struct command *cmd, const char *buffer, const jsmntok_t *params)
{ {
struct lightningd *ld = cmd->ld; struct lightningd *ld = cmd->ld;
struct pubkey destination; struct pubkey *destination;
struct pubkey source; struct pubkey *source;
const jsmntok_t *seedtok; const jsmntok_t *seedtok;
u64 *msatoshi; u64 *msatoshi;
unsigned *cltv; unsigned *cltv;
@ -287,11 +287,11 @@ static void json_getroute(struct command *cmd, const char *buffer, const jsmntok
struct siphash_seed seed; struct siphash_seed seed;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("id", json_tok_pubkey, &destination), p_req_tal("id", json_tok_pubkey, &destination),
p_req_tal("msatoshi", json_tok_u64, &msatoshi), p_req_tal("msatoshi", json_tok_u64, &msatoshi),
p_req_tal("riskfactor", json_tok_double, &riskfactor), p_req_tal("riskfactor", json_tok_double, &riskfactor),
p_opt_def_tal("cltv", json_tok_number, &cltv, 9), p_opt_def_tal("cltv", json_tok_number, &cltv, 9),
p_opt_def("fromid", json_tok_pubkey, &source, ld->id), p_opt_def_tal("fromid", json_tok_pubkey, &source, ld->id),
p_opt_tal("seed", json_tok_tok, &seedtok), p_opt_tal("seed", json_tok_tok, &seedtok),
p_opt_def_tal("fuzzpercent", json_tok_double, &fuzz, 75.0), p_opt_def_tal("fuzzpercent", json_tok_double, &fuzz, 75.0),
NULL)) NULL))
@ -317,7 +317,7 @@ static void json_getroute(struct command *cmd, const char *buffer, const jsmntok
} else } else
randombytes_buf(&seed, sizeof(seed)); randombytes_buf(&seed, sizeof(seed));
u8 *req = towire_gossip_getroute_request(cmd, &source, &destination, u8 *req = towire_gossip_getroute_request(cmd, source, destination,
*msatoshi, *riskfactor * 1000, *msatoshi, *riskfactor * 1000,
*cltv, fuzz, &seed); *cltv, fuzz, &seed);
subd_req(ld->gossip, ld->gossip, req, -1, 0, json_getroute_reply, cmd); subd_req(ld->gossip, ld->gossip, req, -1, 0, json_getroute_reply, cmd);
@ -431,12 +431,12 @@ static void json_dev_query_scids(struct command *cmd,
u8 *msg; u8 *msg;
const jsmntok_t *scidstok; const jsmntok_t *scidstok;
const jsmntok_t *t, *end; const jsmntok_t *t, *end;
struct pubkey id; struct pubkey *id;
struct short_channel_id *scids; struct short_channel_id *scids;
size_t i; size_t i;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("id", json_tok_pubkey, &id), p_req_tal("id", json_tok_pubkey, &id),
p_req_tal("scids", json_tok_tok, &scidstok), p_req_tal("scids", json_tok_tok, &scidstok),
NULL)) NULL))
return; return;
@ -462,7 +462,7 @@ static void json_dev_query_scids(struct command *cmd,
} }
/* Tell gossipd, since this is a gossip query. */ /* Tell gossipd, since this is a gossip query. */
msg = towire_gossip_query_scids(cmd, &id, scids); msg = towire_gossip_query_scids(cmd, id, scids);
subd_req(cmd->ld->gossip, cmd->ld->gossip, subd_req(cmd->ld->gossip, cmd->ld->gossip,
take(msg), -1, 0, json_scids_reply, cmd); take(msg), -1, 0, json_scids_reply, cmd);
command_still_pending(cmd); command_still_pending(cmd);
@ -480,11 +480,11 @@ static void json_dev_send_timestamp_filter(struct command *cmd,
const jsmntok_t *params) const jsmntok_t *params)
{ {
u8 *msg; u8 *msg;
struct pubkey id; struct pubkey *id;
u32 *first, *range; u32 *first, *range;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("id", json_tok_pubkey, &id), p_req_tal("id", json_tok_pubkey, &id),
p_req_tal("first", json_tok_number, &first), p_req_tal("first", json_tok_number, &first),
p_req_tal("range", json_tok_number, &range), p_req_tal("range", json_tok_number, &range),
NULL)) NULL))
@ -492,7 +492,7 @@ static void json_dev_send_timestamp_filter(struct command *cmd,
log_debug(cmd->ld->log, "Setting timestamp range %u+%u", *first, *range); log_debug(cmd->ld->log, "Setting timestamp range %u+%u", *first, *range);
/* Tell gossipd, since this is a gossip query. */ /* Tell gossipd, since this is a gossip query. */
msg = towire_gossip_send_timestamp_filter(NULL, &id, *first, *range); msg = towire_gossip_send_timestamp_filter(NULL, id, *first, *range);
subd_send_msg(cmd->ld->gossip, take(msg)); subd_send_msg(cmd->ld->gossip, take(msg));
command_success(cmd, null_response(cmd)); command_success(cmd, null_response(cmd));
@ -548,18 +548,18 @@ static void json_dev_query_channel_range(struct command *cmd,
const jsmntok_t *params) const jsmntok_t *params)
{ {
u8 *msg; u8 *msg;
struct pubkey id; struct pubkey *id;
u32 *first, *num; u32 *first, *num;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("id", json_tok_pubkey, &id), p_req_tal("id", json_tok_pubkey, &id),
p_req_tal("first", json_tok_number, &first), p_req_tal("first", json_tok_number, &first),
p_req_tal("num", json_tok_number, &num), p_req_tal("num", json_tok_number, &num),
NULL)) NULL))
return; return;
/* Tell gossipd, since this is a gossip query. */ /* Tell gossipd, since this is a gossip query. */
msg = towire_gossip_query_channel_range(cmd, &id, *first, *num); msg = towire_gossip_query_channel_range(cmd, id, *first, *num);
subd_req(cmd->ld->gossip, cmd->ld->gossip, subd_req(cmd->ld->gossip, cmd->ld->gossip,
take(msg), -1, 0, json_channel_range_reply, cmd); take(msg), -1, 0, json_channel_range_reply, cmd);
command_still_pending(cmd); command_still_pending(cmd);

View File

@ -172,13 +172,27 @@ bool json_tok_u64(struct command *cmd, const char *name,
return true; return true;
} }
bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok, bool json_to_pubkey(const char *buffer, const jsmntok_t *tok,
struct pubkey *pubkey) struct pubkey *pubkey)
{ {
return pubkey_from_hexstr(buffer + tok->start, return pubkey_from_hexstr(buffer + tok->start,
tok->end - tok->start, pubkey); tok->end - tok->start, pubkey);
} }
bool json_tok_pubkey(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
struct pubkey **pubkey)
{
*pubkey = tal(cmd, struct pubkey);
if (json_to_pubkey(buffer, tok, *pubkey))
return true;
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%s' should be a pubkey, not '%.*s'",
name, tok->end - tok->start, buffer + tok->start);
return false;
}
void json_add_short_channel_id(struct json_result *response, void json_add_short_channel_id(struct json_result *response,
const char *fieldname, const char *fieldname,
const struct short_channel_id *id) const struct short_channel_id *id)

View File

@ -63,8 +63,12 @@ bool json_tok_sha256(struct command *cmd, const char *name,
struct sha256 **hash); struct sha256 **hash);
/* Extract a pubkey from this */ /* Extract a pubkey from this */
bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok, bool json_to_pubkey(const char *buffer, const jsmntok_t *tok,
struct pubkey *pubkey); struct pubkey *pubkey);
bool json_tok_pubkey(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
struct pubkey **pubkey);
/* Extract a short_channel_id from this */ /* Extract a short_channel_id from this */
bool json_tok_short_channel_id(const char *buffer, const jsmntok_t *tok, bool json_tok_short_channel_id(const char *buffer, const jsmntok_t *tok,

View File

@ -759,7 +759,7 @@ static void json_fund_channel(struct command *cmd,
{ {
const jsmntok_t *sattok; const jsmntok_t *sattok;
struct funding_channel * fc = tal(cmd, struct funding_channel); struct funding_channel * fc = tal(cmd, struct funding_channel);
struct pubkey id; struct pubkey *id;
struct peer *peer; struct peer *peer;
struct channel *channel; struct channel *channel;
u32 feerate_per_kw = get_feerate(cmd->ld->topology, FEERATE_NORMAL); u32 feerate_per_kw = get_feerate(cmd->ld->topology, FEERATE_NORMAL);
@ -769,7 +769,7 @@ static void json_fund_channel(struct command *cmd,
fc->uc = NULL; fc->uc = NULL;
wtx_init(cmd, &fc->wtx); wtx_init(cmd, &fc->wtx);
if (!param(fc->cmd, buffer, params, if (!param(fc->cmd, buffer, params,
p_req("id", json_tok_pubkey, &id), p_req_tal("id", json_tok_pubkey, &id),
p_req_tal("satoshi", json_tok_tok, &sattok), p_req_tal("satoshi", json_tok_tok, &sattok),
NULL)) NULL))
return; return;
@ -777,7 +777,7 @@ static void json_fund_channel(struct command *cmd,
if (!json_tok_wtx(&fc->wtx, buffer, sattok, MAX_FUNDING_SATOSHI)) if (!json_tok_wtx(&fc->wtx, buffer, sattok, MAX_FUNDING_SATOSHI))
return; return;
peer = peer_by_id(cmd->ld, &id); peer = peer_by_id(cmd->ld, id);
if (!peer) { if (!peer) {
command_fail(cmd, LIGHTNINGD, "Unknown peer"); command_fail(cmd, LIGHTNINGD, "Unknown peer");
return; return;

View File

@ -1013,7 +1013,7 @@ static void json_sendpay(struct command *cmd,
"Route %zu invalid channel_id", n_hops); "Route %zu invalid channel_id", n_hops);
return; return;
} }
if (!json_tok_pubkey(buffer, idtok, &route[n_hops].nodeid)) { if (!json_to_pubkey(buffer, idtok, &route[n_hops].nodeid)) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS, command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Route %zu invalid id", n_hops); "Route %zu invalid id", n_hops);
return; return;

View File

@ -163,7 +163,7 @@ struct peer *peer_from_json(struct lightningd *ld,
{ {
struct pubkey peerid; struct pubkey peerid;
if (!json_tok_pubkey(buffer, peeridtok, &peerid)) if (!json_to_pubkey(buffer, peeridtok, &peerid))
return NULL; return NULL;
return peer_by_id(ld, &peerid); return peer_by_id(ld, &peerid);
@ -749,7 +749,7 @@ static void json_listpeers(struct command *cmd,
struct json_result *response = new_json_result(cmd); struct json_result *response = new_json_result(cmd);
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_opt("id", json_tok_pubkey, &specific_id), p_opt_tal("id", json_tok_pubkey, &specific_id),
p_opt("level", json_tok_loglevel, &ll), p_opt("level", json_tok_loglevel, &ll),
NULL)) NULL))
return; return;
@ -941,16 +941,16 @@ void activate_peers(struct lightningd *ld)
static void json_disconnect(struct command *cmd, static void json_disconnect(struct command *cmd,
const char *buffer, const jsmntok_t *params) const char *buffer, const jsmntok_t *params)
{ {
struct pubkey id; struct pubkey *id;
struct peer *peer; struct peer *peer;
struct channel *channel; struct channel *channel;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("id", json_tok_pubkey, &id), p_req_tal("id", json_tok_pubkey, &id),
NULL)) NULL))
return; return;
peer = peer_by_id(cmd->ld, &id); peer = peer_by_id(cmd->ld, id);
if (!peer) { if (!peer) {
command_fail(cmd, LIGHTNINGD, "Peer not connected"); command_fail(cmd, LIGHTNINGD, "Peer not connected");
return; return;
@ -981,18 +981,18 @@ AUTODATA(json_command, &disconnect_command);
static void json_sign_last_tx(struct command *cmd, static void json_sign_last_tx(struct command *cmd,
const char *buffer, const jsmntok_t *params) const char *buffer, const jsmntok_t *params)
{ {
struct pubkey peerid; struct pubkey *peerid;
struct peer *peer; struct peer *peer;
struct json_result *response = new_json_result(cmd); struct json_result *response = new_json_result(cmd);
u8 *linear; u8 *linear;
struct channel *channel; struct channel *channel;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("id", json_tok_pubkey, &peerid), p_req_tal("id", json_tok_pubkey, &peerid),
NULL)) NULL))
return; return;
peer = peer_by_id(cmd->ld, &peerid); peer = peer_by_id(cmd->ld, peerid);
if (!peer) { if (!peer) {
command_fail(cmd, LIGHTNINGD, command_fail(cmd, LIGHTNINGD,
"Could not find peer with that id"); "Could not find peer with that id");
@ -1027,16 +1027,16 @@ AUTODATA(json_command, &dev_sign_last_tx);
static void json_dev_fail(struct command *cmd, static void json_dev_fail(struct command *cmd,
const char *buffer, const jsmntok_t *params) const char *buffer, const jsmntok_t *params)
{ {
struct pubkey peerid; struct pubkey *peerid;
struct peer *peer; struct peer *peer;
struct channel *channel; struct channel *channel;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("id", json_tok_pubkey, &peerid), p_req_tal("id", json_tok_pubkey, &peerid),
NULL)) NULL))
return; return;
peer = peer_by_id(cmd->ld, &peerid); peer = peer_by_id(cmd->ld, peerid);
if (!peer) { if (!peer) {
command_fail(cmd, LIGHTNINGD, command_fail(cmd, LIGHTNINGD,
"Could not find peer with that id"); "Could not find peer with that id");
@ -1072,17 +1072,17 @@ static void dev_reenable_commit_finished(struct subd *channeld UNUSED,
static void json_dev_reenable_commit(struct command *cmd, static void json_dev_reenable_commit(struct command *cmd,
const char *buffer, const jsmntok_t *params) const char *buffer, const jsmntok_t *params)
{ {
struct pubkey peerid; struct pubkey *peerid;
struct peer *peer; struct peer *peer;
u8 *msg; u8 *msg;
struct channel *channel; struct channel *channel;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("id", json_tok_pubkey, &peerid), p_req_tal("id", json_tok_pubkey, &peerid),
NULL)) NULL))
return; return;
peer = peer_by_id(cmd->ld, &peerid); peer = peer_by_id(cmd->ld, peerid);
if (!peer) { if (!peer) {
command_fail(cmd, LIGHTNINGD, command_fail(cmd, LIGHTNINGD,
"Could not find peer with that id"); "Could not find peer with that id");
@ -1162,7 +1162,7 @@ static void process_dev_forget_channel(struct bitcoind *bitcoind UNUSED,
static void json_dev_forget_channel(struct command *cmd, const char *buffer, static void json_dev_forget_channel(struct command *cmd, const char *buffer,
const jsmntok_t *params) const jsmntok_t *params)
{ {
struct pubkey peerid; struct pubkey *peerid;
struct peer *peer; struct peer *peer;
struct channel *channel; struct channel *channel;
struct short_channel_id *scid; struct short_channel_id *scid;
@ -1171,14 +1171,14 @@ static void json_dev_forget_channel(struct command *cmd, const char *buffer,
bool *force; bool *force;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("id", json_tok_pubkey, &peerid), p_req_tal("id", json_tok_pubkey, &peerid),
p_opt("short_channel_id", json_tok_short_channel_id, &scid), p_opt("short_channel_id", json_tok_short_channel_id, &scid),
p_opt_def_tal("force", json_tok_bool, &force, false), p_opt_def_tal("force", json_tok_bool, &force, false),
NULL)) NULL))
return; return;
forget->force = *force; forget->force = *force;
peer = peer_by_id(cmd->ld, &peerid); peer = peer_by_id(cmd->ld, peerid);
if (!peer) { if (!peer) {
command_fail(cmd, LIGHTNINGD, command_fail(cmd, LIGHTNINGD,
"Could not find channel with that peer"); "Could not find channel with that peer");

View File

@ -1693,17 +1693,17 @@ void notify_feerate_change(struct lightningd *ld)
static void json_dev_ignore_htlcs(struct command *cmd, const char *buffer, static void json_dev_ignore_htlcs(struct command *cmd, const char *buffer,
const jsmntok_t *params) const jsmntok_t *params)
{ {
struct pubkey peerid; struct pubkey *peerid;
struct peer *peer; struct peer *peer;
bool *ignore; bool *ignore;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("id", json_tok_pubkey, &peerid), p_req_tal("id", json_tok_pubkey, &peerid),
p_req_tal("ignore", json_tok_bool, &ignore), p_req_tal("ignore", json_tok_bool, &ignore),
NULL)) NULL))
return; return;
peer = peer_by_id(cmd->ld, &peerid); peer = peer_by_id(cmd->ld, peerid);
if (!peer) { if (!peer) {
command_fail(cmd, LIGHTNINGD, command_fail(cmd, LIGHTNINGD,
"Could not find channel with that peer"); "Could not find channel with that peer");

View File

@ -40,10 +40,10 @@ static void json_ping(struct command *cmd,
{ {
u8 *msg; u8 *msg;
unsigned int *len, *pongbytes; unsigned int *len, *pongbytes;
struct pubkey id; struct pubkey *id;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("id", json_tok_pubkey, &id), p_req_tal("id", json_tok_pubkey, &id),
p_opt_def_tal("len", json_tok_number, &len, 128), p_opt_def_tal("len", json_tok_number, &len, 128),
p_opt_def_tal("pongbytes", json_tok_number, &pongbytes, 128), p_opt_def_tal("pongbytes", json_tok_number, &pongbytes, 128),
NULL)) NULL))
@ -77,7 +77,7 @@ static void json_ping(struct command *cmd,
} }
/* gossipd handles all pinging, even if it's in another daemon. */ /* gossipd handles all pinging, even if it's in another daemon. */
msg = towire_gossip_ping(NULL, &id, *pongbytes, *len); msg = towire_gossip_ping(NULL, id, *pongbytes, *len);
subd_req(cmd->ld->gossip, cmd->ld->gossip, subd_req(cmd->ld->gossip, cmd->ld->gossip,
take(msg), -1, 0, ping_reply, cmd); take(msg), -1, 0, ping_reply, cmd);
command_still_pending(cmd); command_still_pending(cmd);

View File

@ -236,8 +236,9 @@ bool json_tok_number(struct command *cmd UNNEEDED, const char *name UNNEEDED,
unsigned int **num UNNEEDED) unsigned int **num UNNEEDED)
{ fprintf(stderr, "json_tok_number called!\n"); abort(); } { fprintf(stderr, "json_tok_number called!\n"); abort(); }
/* Generated stub for json_tok_pubkey */ /* Generated stub for json_tok_pubkey */
bool json_tok_pubkey(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, bool json_tok_pubkey(struct command *cmd UNNEEDED, const char *name UNNEEDED,
struct pubkey *pubkey UNNEEDED) const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
struct pubkey **pubkey UNNEEDED)
{ fprintf(stderr, "json_tok_pubkey called!\n"); abort(); } { fprintf(stderr, "json_tok_pubkey called!\n"); abort(); }
/* Generated stub for json_tok_short_channel_id */ /* Generated stub for json_tok_short_channel_id */
bool json_tok_short_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, bool json_tok_short_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
@ -248,6 +249,10 @@ bool json_tok_tok(struct command *cmd UNNEEDED, const char *name UNNEEDED,
const char *buffer UNNEEDED, const jsmntok_t * tok UNNEEDED, const char *buffer UNNEEDED, const jsmntok_t * tok UNNEEDED,
const jsmntok_t **out UNNEEDED) const jsmntok_t **out UNNEEDED)
{ fprintf(stderr, "json_tok_tok called!\n"); abort(); } { fprintf(stderr, "json_tok_tok 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 kill_uncommitted_channel */ /* Generated stub for kill_uncommitted_channel */
void kill_uncommitted_channel(struct uncommitted_channel *uc UNNEEDED, void kill_uncommitted_channel(struct uncommitted_channel *uc UNNEEDED,
const char *why UNNEEDED) const char *why UNNEEDED)