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;
}
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)
{

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) */
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.

View file

@ -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);

View file

@ -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'"}
};

View file

@ -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;