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 <wythe@intrig.com>
This commit is contained in:
Mark Beckwith
2018-07-30 23:11:01 -05:00
committed by Rusty Russell
parent d3edfc8028
commit 1fca7ab562
5 changed files with 17 additions and 36 deletions

View File

@@ -158,6 +158,14 @@ bool json_tok_bool(const char *buffer, const jsmntok_t *tok, bool *b)
return false; 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, bool json_tok_tok(const char *buffer, const jsmntok_t * tok,
const jsmntok_t **out) const jsmntok_t **out)
{ {

View File

@@ -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) */ /* Extract boolean this (must be a true or false) */
bool json_tok_bool(const char *buffer, const jsmntok_t *tok, bool *b); 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 * Set the address of @out to @tok. Used as a param_table callback by handlers that
* want to unmarshal @tok themselves. * want to unmarshal @tok themselves.

View File

@@ -96,24 +96,13 @@ static void json_rhash(struct command *cmd,
const char *buffer, const jsmntok_t *params) const char *buffer, const jsmntok_t *params)
{ {
struct json_result *response = new_json_result(cmd); struct json_result *response = new_json_result(cmd);
const jsmntok_t *secrettok;
struct sha256 secret; struct sha256 secret;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("secret", json_tok_tok, &secrettok), p_req("secret", json_tok_sha256, &secret),
NULL)) NULL))
return; 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. */ /* Hash in place. */
sha256(&secret, &secret, sizeof(secret)); sha256(&secret, &secret, sizeof(secret));
json_object_start(response, NULL); json_object_start(response, NULL);

View File

@@ -57,6 +57,7 @@ static struct fail_format fail_formats[] = {
{json_tok_wtx, {json_tok_wtx,
"'%s' should be 'all' or a positive integer greater than " "'%s' should be 'all' or a positive integer greater than "
"545, not '%.*s'"}, "545, not '%.*s'"},
{json_tok_sha256, "'%s' should be a 32 byte hex value, not '%.*s'"},
{NULL, "'%s' of '%.*s' is invalid'"} {NULL, "'%s' of '%.*s' is invalid'"}
}; };

View File

@@ -945,7 +945,7 @@ static void json_sendpay_on_resolve(const struct sendpay_result* r,
static void json_sendpay(struct command *cmd, static void json_sendpay(struct command *cmd,
const char *buffer, const jsmntok_t *params) const char *buffer, const jsmntok_t *params)
{ {
const jsmntok_t *routetok, *rhashtok, *desctok; const jsmntok_t *routetok, *desctok;
const jsmntok_t *t, *end; const jsmntok_t *t, *end;
size_t n_hops; size_t n_hops;
struct sha256 rhash; struct sha256 rhash;
@@ -956,22 +956,12 @@ static void json_sendpay(struct command *cmd,
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("route", json_tok_tok, &routetok), 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("msatoshi", json_tok_u64, &msatoshi),
p_opt_tok("description", &desctok), p_opt_tok("description", &desctok),
NULL)) NULL))
return; 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) { if (routetok->type != JSMN_ARRAY) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS, command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%.*s' is not an array", "'%.*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, static void json_waitsendpay(struct command *cmd, const char *buffer,
const jsmntok_t *params) const jsmntok_t *params)
{ {
const jsmntok_t *rhashtok;
struct sha256 rhash; struct sha256 rhash;
unsigned int *timeout; unsigned int *timeout;
if (!param(cmd, buffer, params, 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), p_opt("timeout", json_tok_number, &timeout),
NULL)) NULL))
return; 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)) if (!wait_payment(cmd, cmd->ld, &rhash, &json_waitsendpay_on_resolve, cmd))
return; return;