From 1fca7ab562f3bd6a95ee8dbbf551cd26fb8f6ca9 Mon Sep 17 00:00:00 2001 From: Mark Beckwith Date: Mon, 30 Jul 2018 23:11:01 -0500 Subject: [PATCH] Added json_tok_sha256 (#1779) Added json_tok_sha256 Converted json_tok_tok over a few places. [ Folded: fixed spacing ] Signed-off-by: Mark Beckwith --- common/json.c | 8 ++++++++ common/json.h | 4 ++++ lightningd/jsonrpc.c | 13 +------------ lightningd/param.c | 1 + lightningd/pay.c | 27 +++------------------------ 5 files changed, 17 insertions(+), 36 deletions(-) diff --git a/common/json.c b/common/json.c index a853a6b92..607a8f7bd 100644 --- a/common/json.c +++ b/common/json.c @@ -158,6 +158,14 @@ bool json_tok_bool(const char *buffer, const jsmntok_t *tok, bool *b) return false; } +bool json_tok_sha256(const char *buffer, const jsmntok_t * tok, + struct sha256 *hash) +{ + return hex_decode(buffer + tok->start, + tok->end - tok->start, + hash, sizeof(*hash)); +} + bool json_tok_tok(const char *buffer, const jsmntok_t * tok, const jsmntok_t **out) { diff --git a/common/json.h b/common/json.h index 15a00dd1a..bd5bb2696 100644 --- a/common/json.h +++ b/common/json.h @@ -45,6 +45,10 @@ bool json_tok_percent(const char *buffer, const jsmntok_t *tok, double *num); /* Extract boolean this (must be a true or false) */ bool json_tok_bool(const char *buffer, const jsmntok_t *tok, bool *b); +/* Extract sha256 hash */ +bool json_tok_sha256(const char *buffer, const jsmntok_t * tok, + struct sha256 *hash); + /* * Set the address of @out to @tok. Used as a param_table callback by handlers that * want to unmarshal @tok themselves. diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index cce2eb79b..c82862523 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -96,24 +96,13 @@ static void json_rhash(struct command *cmd, const char *buffer, const jsmntok_t *params) { struct json_result *response = new_json_result(cmd); - const jsmntok_t *secrettok; struct sha256 secret; if (!param(cmd, buffer, params, - p_req("secret", json_tok_tok, &secrettok), + p_req("secret", json_tok_sha256, &secret), NULL)) return; - if (!hex_decode(buffer + secrettok->start, - secrettok->end - secrettok->start, - &secret, sizeof(secret))) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%.*s' is not a valid 32-byte hex value", - secrettok->end - secrettok->start, - buffer + secrettok->start); - return; - } - /* Hash in place. */ sha256(&secret, &secret, sizeof(secret)); json_object_start(response, NULL); diff --git a/lightningd/param.c b/lightningd/param.c index 5569ec7ed..95db6042a 100644 --- a/lightningd/param.c +++ b/lightningd/param.c @@ -57,6 +57,7 @@ static struct fail_format fail_formats[] = { {json_tok_wtx, "'%s' should be 'all' or a positive integer greater than " "545, not '%.*s'"}, + {json_tok_sha256, "'%s' should be a 32 byte hex value, not '%.*s'"}, {NULL, "'%s' of '%.*s' is invalid'"} }; diff --git a/lightningd/pay.c b/lightningd/pay.c index 07a73ad15..cdf6c933f 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -945,7 +945,7 @@ static void json_sendpay_on_resolve(const struct sendpay_result* r, static void json_sendpay(struct command *cmd, const char *buffer, const jsmntok_t *params) { - const jsmntok_t *routetok, *rhashtok, *desctok; + const jsmntok_t *routetok, *desctok; const jsmntok_t *t, *end; size_t n_hops; struct sha256 rhash; @@ -956,22 +956,12 @@ static void json_sendpay(struct command *cmd, if (!param(cmd, buffer, params, p_req("route", json_tok_tok, &routetok), - p_req("payment_hash", json_tok_tok, &rhashtok), + p_req("payment_hash", json_tok_sha256, &rhash), p_opt("msatoshi", json_tok_u64, &msatoshi), p_opt_tok("description", &desctok), NULL)) return; - if (!hex_decode(buffer + rhashtok->start, - rhashtok->end - rhashtok->start, - &rhash, sizeof(rhash))) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%.*s' is not a valid sha256 hash", - rhashtok->end - rhashtok->start, - buffer + rhashtok->start); - return; - } - if (routetok->type != JSMN_ARRAY) { command_fail(cmd, JSONRPC2_INVALID_PARAMS, "'%.*s' is not an array", @@ -1099,26 +1089,15 @@ static void waitsendpay_timeout(struct command *cmd) static void json_waitsendpay(struct command *cmd, const char *buffer, const jsmntok_t *params) { - const jsmntok_t *rhashtok; struct sha256 rhash; unsigned int *timeout; if (!param(cmd, buffer, params, - p_req("payment_hash", json_tok_tok, &rhashtok), + p_req("payment_hash", json_tok_sha256, &rhash), p_opt("timeout", json_tok_number, &timeout), NULL)) return; - if (!hex_decode(buffer + rhashtok->start, - rhashtok->end - rhashtok->start, - &rhash, sizeof(rhash))) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%.*s' is not a valid sha256 hash", - rhashtok->end - rhashtok->start, - buffer + rhashtok->start); - return; - } - if (!wait_payment(cmd, cmd->ld, &rhash, &json_waitsendpay_on_resolve, cmd)) return;