Skip to content

Commit

Permalink
json-rpc: Add helper for an array of secrets
Browse files Browse the repository at this point in the history
Suggested-by: Rusty Russell <@rustyrussell>
  • Loading branch information
cdecker committed Nov 28, 2019
1 parent 0109fd7 commit 65342b1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
31 changes: 31 additions & 0 deletions common/json_tok.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions common/json_tok.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
18 changes: 1 addition & 17 deletions lightningd/pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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) {
Expand Down

0 comments on commit 65342b1

Please sign in to comment.