diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c index b31e06464..9f0aef981 100644 --- a/lightningd/connect_control.c +++ b/lightningd/connect_control.c @@ -105,7 +105,7 @@ static void json_connect(struct command *cmd, 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, "id %.*s not valid", idtok->end - idtok->start, diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 586952d22..c3db528c7 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -233,7 +233,7 @@ static void json_listnodes(struct command *cmd, const char *buffer, struct pubkey *id; if (!param(cmd, buffer, params, - p_opt("id", json_tok_pubkey, &id), + p_opt_tal("id", json_tok_pubkey, &id), NULL)) 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) { struct lightningd *ld = cmd->ld; - struct pubkey destination; - struct pubkey source; + struct pubkey *destination; + struct pubkey *source; const jsmntok_t *seedtok; u64 *msatoshi; unsigned *cltv; @@ -287,11 +287,11 @@ static void json_getroute(struct command *cmd, const char *buffer, const jsmntok struct siphash_seed seed; 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("riskfactor", json_tok_double, &riskfactor), 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_def_tal("fuzzpercent", json_tok_double, &fuzz, 75.0), NULL)) @@ -317,7 +317,7 @@ static void json_getroute(struct command *cmd, const char *buffer, const jsmntok } else 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, *cltv, fuzz, &seed); 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; const jsmntok_t *scidstok; const jsmntok_t *t, *end; - struct pubkey id; + struct pubkey *id; struct short_channel_id *scids; size_t i; 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), NULL)) return; @@ -462,7 +462,7 @@ static void json_dev_query_scids(struct command *cmd, } /* 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, take(msg), -1, 0, json_scids_reply, cmd); command_still_pending(cmd); @@ -480,11 +480,11 @@ static void json_dev_send_timestamp_filter(struct command *cmd, const jsmntok_t *params) { u8 *msg; - struct pubkey id; + struct pubkey *id; u32 *first, *range; 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("range", json_tok_number, &range), 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); /* 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)); command_success(cmd, null_response(cmd)); @@ -548,18 +548,18 @@ static void json_dev_query_channel_range(struct command *cmd, const jsmntok_t *params) { u8 *msg; - struct pubkey id; + struct pubkey *id; u32 *first, *num; 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("num", json_tok_number, &num), NULL)) return; /* 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, take(msg), -1, 0, json_channel_range_reply, cmd); command_still_pending(cmd); diff --git a/lightningd/json.c b/lightningd/json.c index 5cd46fff5..fc92e45bf 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -172,13 +172,27 @@ bool json_tok_u64(struct command *cmd, const char *name, return true; } -bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok, - struct pubkey *pubkey) +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_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, const char *fieldname, const struct short_channel_id *id) diff --git a/lightningd/json.h b/lightningd/json.h index 23f5a5e81..7e9c76441 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -63,8 +63,12 @@ bool json_tok_sha256(struct command *cmd, const char *name, struct sha256 **hash); /* Extract a pubkey from this */ -bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok, - struct pubkey *pubkey); +bool json_to_pubkey(const char *buffer, const jsmntok_t *tok, + 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 */ bool json_tok_short_channel_id(const char *buffer, const jsmntok_t *tok, diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 5f4e5becd..063c36268 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -759,7 +759,7 @@ static void json_fund_channel(struct command *cmd, { const jsmntok_t *sattok; struct funding_channel * fc = tal(cmd, struct funding_channel); - struct pubkey id; + struct pubkey *id; struct peer *peer; struct channel *channel; 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; wtx_init(cmd, &fc->wtx); 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), NULL)) return; @@ -777,7 +777,7 @@ static void json_fund_channel(struct command *cmd, if (!json_tok_wtx(&fc->wtx, buffer, sattok, MAX_FUNDING_SATOSHI)) return; - peer = peer_by_id(cmd->ld, &id); + peer = peer_by_id(cmd->ld, id); if (!peer) { command_fail(cmd, LIGHTNINGD, "Unknown peer"); return; diff --git a/lightningd/pay.c b/lightningd/pay.c index fd41c271e..3988c342f 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -1013,7 +1013,7 @@ static void json_sendpay(struct command *cmd, "Route %zu invalid channel_id", n_hops); 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, "Route %zu invalid id", n_hops); return; diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index ec7bcae01..39f55b309 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -163,7 +163,7 @@ struct peer *peer_from_json(struct lightningd *ld, { struct pubkey peerid; - if (!json_tok_pubkey(buffer, peeridtok, &peerid)) + if (!json_to_pubkey(buffer, peeridtok, &peerid)) return NULL; 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); 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), NULL)) return; @@ -941,16 +941,16 @@ void activate_peers(struct lightningd *ld) static void json_disconnect(struct command *cmd, const char *buffer, const jsmntok_t *params) { - struct pubkey id; + struct pubkey *id; struct peer *peer; struct channel *channel; if (!param(cmd, buffer, params, - p_req("id", json_tok_pubkey, &id), + p_req_tal("id", json_tok_pubkey, &id), NULL)) return; - peer = peer_by_id(cmd->ld, &id); + peer = peer_by_id(cmd->ld, id); if (!peer) { command_fail(cmd, LIGHTNINGD, "Peer not connected"); return; @@ -981,18 +981,18 @@ AUTODATA(json_command, &disconnect_command); static void json_sign_last_tx(struct command *cmd, const char *buffer, const jsmntok_t *params) { - struct pubkey peerid; + struct pubkey *peerid; struct peer *peer; struct json_result *response = new_json_result(cmd); u8 *linear; struct channel *channel; if (!param(cmd, buffer, params, - p_req("id", json_tok_pubkey, &peerid), + p_req_tal("id", json_tok_pubkey, &peerid), NULL)) return; - peer = peer_by_id(cmd->ld, &peerid); + peer = peer_by_id(cmd->ld, peerid); if (!peer) { command_fail(cmd, LIGHTNINGD, "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, const char *buffer, const jsmntok_t *params) { - struct pubkey peerid; + struct pubkey *peerid; struct peer *peer; struct channel *channel; if (!param(cmd, buffer, params, - p_req("id", json_tok_pubkey, &peerid), + p_req_tal("id", json_tok_pubkey, &peerid), NULL)) return; - peer = peer_by_id(cmd->ld, &peerid); + peer = peer_by_id(cmd->ld, peerid); if (!peer) { command_fail(cmd, LIGHTNINGD, "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, const char *buffer, const jsmntok_t *params) { - struct pubkey peerid; + struct pubkey *peerid; struct peer *peer; u8 *msg; struct channel *channel; if (!param(cmd, buffer, params, - p_req("id", json_tok_pubkey, &peerid), + p_req_tal("id", json_tok_pubkey, &peerid), NULL)) return; - peer = peer_by_id(cmd->ld, &peerid); + peer = peer_by_id(cmd->ld, peerid); if (!peer) { command_fail(cmd, LIGHTNINGD, "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, const jsmntok_t *params) { - struct pubkey peerid; + struct pubkey *peerid; struct peer *peer; struct channel *channel; struct short_channel_id *scid; @@ -1171,14 +1171,14 @@ static void json_dev_forget_channel(struct command *cmd, const char *buffer, bool *force; 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_def_tal("force", json_tok_bool, &force, false), NULL)) return; forget->force = *force; - peer = peer_by_id(cmd->ld, &peerid); + peer = peer_by_id(cmd->ld, peerid); if (!peer) { command_fail(cmd, LIGHTNINGD, "Could not find channel with that peer"); diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 52635efe4..1fbb6c913 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -1693,17 +1693,17 @@ void notify_feerate_change(struct lightningd *ld) static void json_dev_ignore_htlcs(struct command *cmd, const char *buffer, const jsmntok_t *params) { - struct pubkey peerid; + struct pubkey *peerid; struct peer *peer; bool *ignore; 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), NULL)) return; - peer = peer_by_id(cmd->ld, &peerid); + peer = peer_by_id(cmd->ld, peerid); if (!peer) { command_fail(cmd, LIGHTNINGD, "Could not find channel with that peer"); diff --git a/lightningd/ping.c b/lightningd/ping.c index b973e0deb..4a822d3c2 100644 --- a/lightningd/ping.c +++ b/lightningd/ping.c @@ -40,10 +40,10 @@ static void json_ping(struct command *cmd, { u8 *msg; unsigned int *len, *pongbytes; - struct pubkey id; + struct pubkey *id; 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("pongbytes", json_tok_number, &pongbytes, 128), NULL)) @@ -77,7 +77,7 @@ static void json_ping(struct command *cmd, } /* 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, take(msg), -1, 0, ping_reply, cmd); command_still_pending(cmd); diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 70dcfacc9..9a6dd9605 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -236,8 +236,9 @@ bool json_tok_number(struct command *cmd UNNEEDED, const char *name UNNEEDED, unsigned int **num UNNEEDED) { fprintf(stderr, "json_tok_number called!\n"); abort(); } /* Generated stub for json_tok_pubkey */ -bool json_tok_pubkey(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - struct pubkey *pubkey UNNEEDED) +bool json_tok_pubkey(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t *tok 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, @@ -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 jsmntok_t **out UNNEEDED) { 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 */ void kill_uncommitted_channel(struct uncommitted_channel *uc UNNEEDED, const char *why UNNEEDED)