diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index a8aa897a9..a4d23805e 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -262,8 +262,7 @@ static void json_getroute(struct command *cmd, const char *buffer, const jsmntok return; } - if (!pubkey_from_hexstr(buffer + idtok->start, - idtok->end - idtok->start, &id)) { + if (!json_tok_pubkey(buffer, idtok, &id)) { command_fail(cmd, "Invalid id"); return; } diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 7de4f2032..465d15f1b 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -352,6 +352,13 @@ void json_add_pubkey(struct json_result *response, 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, const char *fieldname, const struct short_channel_id *id) diff --git a/lightningd/jsonrpc.h b/lightningd/jsonrpc.h index d3fa25897..22507d4be 100644 --- a/lightningd/jsonrpc.h +++ b/lightningd/jsonrpc.h @@ -59,6 +59,10 @@ void json_add_pubkey(struct json_result *response, const char *fieldname, 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"' */ void json_add_short_channel_id(struct json_result *response, const char *fieldname, diff --git a/lightningd/pay.c b/lightningd/pay.c index 3c4039794..ecdc00ab8 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -232,9 +232,7 @@ static void json_sendpay(struct command *cmd, command_fail(cmd, "route %zu invalid channel_id", n_hops); return; } - if (!pubkey_from_hexstr(buffer + idtok->start, - idtok->end - idtok->start, - &ids[n_hops])) { + if (!json_tok_pubkey(buffer, idtok, &ids[n_hops])) { command_fail(cmd, "route %zu invalid id", n_hops); return; } diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 72bc922e1..e8c37c45e 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -808,8 +808,7 @@ static void json_connect(struct command *cmd, return; } - if (!pubkey_from_hexstr(buffer + idtok->start, - idtok->end - idtok->start, &id)) { + if (!json_tok_pubkey(buffer, idtok, &id)) { command_fail(cmd, "id %.*s not valid", idtok->end - idtok->start, buffer + idtok->start); @@ -1015,8 +1014,7 @@ struct peer *peer_from_json(struct lightningd *ld, { struct pubkey peerid; - if (!pubkey_from_hexstr(buffer + peeridtok->start, - peeridtok->end - peeridtok->start, &peerid)) + if (!json_tok_pubkey(buffer, peeridtok, &peerid)) return NULL; return peer_by_id(ld, &peerid);