Skip to content

Commit 1fca7ab

Browse files
wytherustyrussell
authored andcommitted
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>
1 parent d3edfc8 commit 1fca7ab

File tree

5 files changed

+17
-36
lines changed

5 files changed

+17
-36
lines changed

common/json.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ bool json_tok_bool(const char *buffer, const jsmntok_t *tok, bool *b)
158158
return false;
159159
}
160160

161+
bool json_tok_sha256(const char *buffer, const jsmntok_t * tok,
162+
struct sha256 *hash)
163+
{
164+
return hex_decode(buffer + tok->start,
165+
tok->end - tok->start,
166+
hash, sizeof(*hash));
167+
}
168+
161169
bool json_tok_tok(const char *buffer, const jsmntok_t * tok,
162170
const jsmntok_t **out)
163171
{

common/json.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ bool json_tok_percent(const char *buffer, const jsmntok_t *tok, double *num);
4545
/* Extract boolean this (must be a true or false) */
4646
bool json_tok_bool(const char *buffer, const jsmntok_t *tok, bool *b);
4747

48+
/* Extract sha256 hash */
49+
bool json_tok_sha256(const char *buffer, const jsmntok_t * tok,
50+
struct sha256 *hash);
51+
4852
/*
4953
* Set the address of @out to @tok. Used as a param_table callback by handlers that
5054
* want to unmarshal @tok themselves.

lightningd/jsonrpc.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,13 @@ static void json_rhash(struct command *cmd,
9696
const char *buffer, const jsmntok_t *params)
9797
{
9898
struct json_result *response = new_json_result(cmd);
99-
const jsmntok_t *secrettok;
10099
struct sha256 secret;
101100

102101
if (!param(cmd, buffer, params,
103-
p_req("secret", json_tok_tok, &secrettok),
102+
p_req("secret", json_tok_sha256, &secret),
104103
NULL))
105104
return;
106105

107-
if (!hex_decode(buffer + secrettok->start,
108-
secrettok->end - secrettok->start,
109-
&secret, sizeof(secret))) {
110-
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
111-
"'%.*s' is not a valid 32-byte hex value",
112-
secrettok->end - secrettok->start,
113-
buffer + secrettok->start);
114-
return;
115-
}
116-
117106
/* Hash in place. */
118107
sha256(&secret, &secret, sizeof(secret));
119108
json_object_start(response, NULL);

lightningd/param.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ static struct fail_format fail_formats[] = {
5757
{json_tok_wtx,
5858
"'%s' should be 'all' or a positive integer greater than "
5959
"545, not '%.*s'"},
60+
{json_tok_sha256, "'%s' should be a 32 byte hex value, not '%.*s'"},
6061
{NULL, "'%s' of '%.*s' is invalid'"}
6162
};
6263

lightningd/pay.c

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ static void json_sendpay_on_resolve(const struct sendpay_result* r,
945945
static void json_sendpay(struct command *cmd,
946946
const char *buffer, const jsmntok_t *params)
947947
{
948-
const jsmntok_t *routetok, *rhashtok, *desctok;
948+
const jsmntok_t *routetok, *desctok;
949949
const jsmntok_t *t, *end;
950950
size_t n_hops;
951951
struct sha256 rhash;
@@ -956,22 +956,12 @@ static void json_sendpay(struct command *cmd,
956956

957957
if (!param(cmd, buffer, params,
958958
p_req("route", json_tok_tok, &routetok),
959-
p_req("payment_hash", json_tok_tok, &rhashtok),
959+
p_req("payment_hash", json_tok_sha256, &rhash),
960960
p_opt("msatoshi", json_tok_u64, &msatoshi),
961961
p_opt_tok("description", &desctok),
962962
NULL))
963963
return;
964964

965-
if (!hex_decode(buffer + rhashtok->start,
966-
rhashtok->end - rhashtok->start,
967-
&rhash, sizeof(rhash))) {
968-
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
969-
"'%.*s' is not a valid sha256 hash",
970-
rhashtok->end - rhashtok->start,
971-
buffer + rhashtok->start);
972-
return;
973-
}
974-
975965
if (routetok->type != JSMN_ARRAY) {
976966
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
977967
"'%.*s' is not an array",
@@ -1099,26 +1089,15 @@ static void waitsendpay_timeout(struct command *cmd)
10991089
static void json_waitsendpay(struct command *cmd, const char *buffer,
11001090
const jsmntok_t *params)
11011091
{
1102-
const jsmntok_t *rhashtok;
11031092
struct sha256 rhash;
11041093
unsigned int *timeout;
11051094

11061095
if (!param(cmd, buffer, params,
1107-
p_req("payment_hash", json_tok_tok, &rhashtok),
1096+
p_req("payment_hash", json_tok_sha256, &rhash),
11081097
p_opt("timeout", json_tok_number, &timeout),
11091098
NULL))
11101099
return;
11111100

1112-
if (!hex_decode(buffer + rhashtok->start,
1113-
rhashtok->end - rhashtok->start,
1114-
&rhash, sizeof(rhash))) {
1115-
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
1116-
"'%.*s' is not a valid sha256 hash",
1117-
rhashtok->end - rhashtok->start,
1118-
buffer + rhashtok->start);
1119-
return;
1120-
}
1121-
11221101
if (!wait_payment(cmd, cmd->ld, &rhash, &json_waitsendpay_on_resolve, cmd))
11231102
return;
11241103

0 commit comments

Comments
 (0)