mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-17 19:03:42 +01:00
json-rpc: Add helper for an array of secrets
Suggested-by: Rusty Russell <@rustyrussell>
This commit is contained in:
parent
21b5b59c8c
commit
82255e2401
@ -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;
|
||||
}
|
||||
|
@ -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 */
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user