From 65342b17abd25cf01c20823d8bf0bf0b261c3899 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 25 Nov 2019 13:42:23 +0100 Subject: [PATCH] json-rpc: Add helper for an array of secrets Suggested-by: Rusty Russell <@rustyrussell> --- common/json_tok.c | 31 +++++++++++++++++++++++++++++++ common/json_tok.h | 5 +++++ lightningd/pay.c | 18 +----------------- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/common/json_tok.c b/common/json_tok.c index db94f84b13d3..89b23b0ae3a2 100644 --- a/common/json_tok.c +++ b/common/json_tok.c @@ -324,3 +324,34 @@ struct command_result *param_hops_array(struct command *cmd, const char *name, return NULL; } + +struct command_result *param_secrets_array(struct command *cmd, + const char *name, const char *buffer, + const jsmntok_t *tok, + struct secret **secrets) +{ + size_t i; + const jsmntok_t *s; + struct secret secret; + + if (tok->type != JSMN_ARRAY) { + return command_fail( + cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be an array of secrets, got '%.*s'", name, + tok->end - tok->start, buffer + tok->start); + } + + *secrets = tal_arr(cmd, struct secret, 0); + json_for_each_arr(i, s, tok) { + if (!hex_decode(buffer + s->start, s->end - s->start, &secret, + sizeof(secret))) + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s[%zu]' should be a 32 byte hex " + "value, not '%.*s'", + name, i, s->end - s->start, + buffer + s->start); + + tal_arr_expand(secrets, secret); + } + return NULL; +} diff --git a/common/json_tok.h b/common/json_tok.h index beba94303941..8e5800b9adaa 100644 --- a/common/json_tok.h +++ b/common/json_tok.h @@ -126,4 +126,9 @@ struct command_result *param_hops_array(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, struct sphinx_hop **hops); +struct command_result *param_secrets_array(struct command *cmd, + const char *name, const char *buffer, + const jsmntok_t *tok, + struct secret **secrets); + #endif /* LIGHTNING_COMMON_JSON_TOK_H */ diff --git a/lightningd/pay.c b/lightningd/pay.c index faa9d7049034..38e325fe5264 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -967,16 +967,14 @@ static struct command_result *json_sendonion(struct command *cmd, struct lightningd *ld = cmd->ld; struct wallet_payment *payment; const char *label; - const jsmntok_t *secretstok, *cur; struct secret *path_secrets; - size_t i; if (!param(cmd, buffer, params, p_req("onion", param_bin_from_hex, &onion), p_req("first_hop", param_route_hop, &first_hop), p_req("payment_hash", param_sha256, &payment_hash), p_opt("label", param_escaped_string, &label), - p_opt("shared_secrets", param_array, &secretstok), + p_opt("shared_secrets", param_secrets_array, &path_secrets), NULL)) return command_param_failed(); @@ -988,20 +986,6 @@ static struct command_result *json_sendonion(struct command *cmd, "with failcode=%d", failcode); - if (secretstok) { - path_secrets = tal_arr(cmd, struct secret, secretstok->size); - json_for_each_arr(i, cur, secretstok) { - if (!json_to_secret(buffer, cur, &path_secrets[i])) - return command_fail( - cmd, JSONRPC2_INVALID_PARAMS, - "shared_secret[%zu] isn't a valid " - "hex-encoded 32 byte secret", - i); - } - } else { - path_secrets = NULL; - } - /* Now, do we already have a payment? */ payment = wallet_payment_by_hash(tmpctx, ld->wallet, payment_hash); if (payment) {