diff --git a/common/json.c b/common/json.c index 162d7a625..23aab5b8b 100644 --- a/common/json.c +++ b/common/json.c @@ -126,14 +126,6 @@ bool json_tok_is_null(const char *buffer, const jsmntok_t *tok) return buffer[tok->start] == 'n'; } -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)); -} - const jsmntok_t *json_next(const jsmntok_t *tok) { const jsmntok_t *t; diff --git a/common/json.h b/common/json.h index aa56d188b..02ef93058 100644 --- a/common/json.h +++ b/common/json.h @@ -39,10 +39,6 @@ bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num); bool json_tok_bitcoin_amount(const char *buffer, const jsmntok_t *tok, uint64_t *satoshi); -/* Extract sha256 hash */ -bool json_tok_sha256(const char *buffer, const jsmntok_t * tok, - struct sha256 *hash); - /* Is this the null primitive? */ bool json_tok_is_null(const char *buffer, const jsmntok_t *tok); diff --git a/lightningd/json.c b/lightningd/json.c index 1d399da03..5cd46fff5 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -142,6 +142,22 @@ bool json_tok_number(struct command *cmd, const char *name, return true; } +bool json_tok_sha256(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + struct sha256 **hash) +{ + *hash = tal(cmd, struct sha256); + if (hex_decode(buffer + tok->start, + tok->end - tok->start, + *hash, sizeof(**hash))) + return true; + + command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a 32 byte hex value, not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); + return false; +} + bool json_tok_u64(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, uint64_t **num) diff --git a/lightningd/json.h b/lightningd/json.h index f0d526e85..23f5a5e81 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -18,6 +18,7 @@ struct command; struct json_result; struct pubkey; struct route_hop; +struct sha256; struct short_channel_id; struct wallet_payment; struct wireaddr; @@ -56,6 +57,11 @@ bool json_tok_number(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, unsigned int **num); +/* Extract sha256 hash */ +bool json_tok_sha256(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + struct sha256 **hash); + /* Extract a pubkey from this */ bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok, struct pubkey *pubkey); diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 5d31d8f03..d5b6dec65 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -96,17 +96,17 @@ static void json_rhash(struct command *cmd, const char *buffer, const jsmntok_t *params) { struct json_result *response = new_json_result(cmd); - struct sha256 secret; + struct sha256 *secret; if (!param(cmd, buffer, params, - p_req("secret", json_tok_sha256, &secret), + p_req_tal("secret", json_tok_sha256, &secret), NULL)) return; /* Hash in place. */ - sha256(&secret, &secret, sizeof(secret)); + sha256(secret, secret, sizeof(*secret)); json_object_start(response, NULL); - json_add_hex(response, "rhash", &secret, sizeof(secret)); + json_add_hex(response, "rhash", secret, sizeof(*secret)); json_object_end(response); command_success(cmd, response); } diff --git a/lightningd/param.c b/lightningd/param.c index 73852bf9d..b371bc0ad 100644 --- a/lightningd/param.c +++ b/lightningd/param.c @@ -53,7 +53,6 @@ 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 57e3fc0c3..fd41c271e 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -948,7 +948,7 @@ static void json_sendpay(struct command *cmd, const jsmntok_t *routetok, *desctok; const jsmntok_t *t, *end; size_t n_hops; - struct sha256 rhash; + struct sha256 *rhash; struct route_hop *route; u64 *msatoshi; const struct json_escaped *desc; @@ -956,7 +956,7 @@ static void json_sendpay(struct command *cmd, if (!param(cmd, buffer, params, p_req_tal("route", json_tok_tok, &routetok), - p_req("payment_hash", json_tok_sha256, &rhash), + p_req_tal("payment_hash", json_tok_sha256, &rhash), p_opt_tal("description", json_tok_tok, &desctok), p_opt_tal("msatoshi", json_tok_u64, &msatoshi), NULL)) @@ -1068,7 +1068,7 @@ static void json_sendpay(struct command *cmd, description = NULL; } - if (send_payment(cmd, cmd->ld, &rhash, route, + if (send_payment(cmd, cmd->ld, rhash, route, msatoshi ? *msatoshi : route[n_hops-1].amount, description, &json_sendpay_on_resolve, cmd)) @@ -1090,16 +1090,16 @@ static void waitsendpay_timeout(struct command *cmd) static void json_waitsendpay(struct command *cmd, const char *buffer, const jsmntok_t *params) { - struct sha256 rhash; + struct sha256 *rhash; unsigned int *timeout; if (!param(cmd, buffer, params, - p_req("payment_hash", json_tok_sha256, &rhash), + p_req_tal("payment_hash", json_tok_sha256, &rhash), p_opt_tal("timeout", json_tok_number, &timeout), NULL)) 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; if (timeout)