json: helper to parse pubkeys.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-10-11 20:33:50 +10:30 committed by Christian Decker
parent 8430e33f3b
commit 79ebb9dfd0
5 changed files with 15 additions and 9 deletions

View file

@ -262,8 +262,7 @@ static void json_getroute(struct command *cmd, const char *buffer, const jsmntok
return; return;
} }
if (!pubkey_from_hexstr(buffer + idtok->start, if (!json_tok_pubkey(buffer, idtok, &id)) {
idtok->end - idtok->start, &id)) {
command_fail(cmd, "Invalid id"); command_fail(cmd, "Invalid id");
return; return;
} }

View file

@ -352,6 +352,13 @@ void json_add_pubkey(struct json_result *response,
json_add_hex(response, fieldname, der, sizeof(der)); json_add_hex(response, fieldname, der, sizeof(der));
} }
bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok,
struct pubkey *pubkey)
{
return pubkey_from_hexstr(buffer + tok->start,
tok->end - tok->start, pubkey);
}
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

@ -59,6 +59,10 @@ void json_add_pubkey(struct json_result *response,
const char *fieldname, const char *fieldname,
const struct pubkey *key); const struct pubkey *key);
/* Extract a pubkey from this */
bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok,
struct pubkey *pubkey);
/* '"fieldname" : "1234:5:6"' */ /* '"fieldname" : "1234:5:6"' */
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,

View file

@ -232,9 +232,7 @@ static void json_sendpay(struct command *cmd,
command_fail(cmd, "route %zu invalid channel_id", n_hops); command_fail(cmd, "route %zu invalid channel_id", n_hops);
return; return;
} }
if (!pubkey_from_hexstr(buffer + idtok->start, if (!json_tok_pubkey(buffer, idtok, &ids[n_hops])) {
idtok->end - idtok->start,
&ids[n_hops])) {
command_fail(cmd, "route %zu invalid id", n_hops); command_fail(cmd, "route %zu invalid id", n_hops);
return; return;
} }

View file

@ -808,8 +808,7 @@ static void json_connect(struct command *cmd,
return; return;
} }
if (!pubkey_from_hexstr(buffer + idtok->start, if (!json_tok_pubkey(buffer, idtok, &id)) {
idtok->end - idtok->start, &id)) {
command_fail(cmd, "id %.*s not valid", command_fail(cmd, "id %.*s not valid",
idtok->end - idtok->start, idtok->end - idtok->start,
buffer + idtok->start); buffer + idtok->start);
@ -1015,8 +1014,7 @@ struct peer *peer_from_json(struct lightningd *ld,
{ {
struct pubkey peerid; struct pubkey peerid;
if (!pubkey_from_hexstr(buffer + peeridtok->start, if (!json_tok_pubkey(buffer, peeridtok, &peerid))
peeridtok->end - peeridtok->start, &peerid))
return NULL; return NULL;
return peer_by_id(ld, &peerid); return peer_by_id(ld, &peerid);