json: Add two param parsers for secrets and hex-encoded binary data

These are useful for the `createonion` JSON-RPC we're going to build next. The
secret is used for the optional `session_key` while the hex-encoded binary is
used for the `assocdata` field to which the onion commits. The latter does not
have a constant size, hence the raw binary conversion.
This commit is contained in:
Christian Decker 2019-11-07 17:56:06 +01:00
parent d712f732d8
commit de6bf3e421
3 changed files with 38 additions and 17 deletions

View File

@ -228,3 +228,31 @@ struct command_result *param_channel_id(struct command *cmd, const char *name,
name, json_tok_full_len(tok),
json_tok_full(buffer, tok));
}
struct command_result *param_secret(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
struct secret **secret)
{
*secret = tal(cmd, struct secret);
if (hex_decode(buffer + tok->start,
tok->end - tok->start,
*secret, sizeof(**secret)))
return NULL;
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%s' should be a 32 byte hex value, not '%.*s'",
name, tok->end - tok->start, buffer + tok->start);
}
struct command_result *param_bin_from_hex(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
u8 **bin)
{
*bin = json_tok_bin_from_hex(cmd, buffer, tok);
if (bin != NULL)
return NULL;
else
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%s' should be a hex value, not '%.*s'",
name, tok->end - tok->start, buffer + tok->start);
}

View File

@ -111,4 +111,14 @@ struct command_result *param_ignore(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
const void *unused);
/* Extract a secret from this string */
struct command_result *param_secret(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
struct secret **secret);
/* Extract a binary value from the param and unhexlify it. */
struct command_result *param_bin_from_hex(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
u8 **bin);
#endif /* LIGHTNING_COMMON_JSON_TOK_H */

View File

@ -842,23 +842,6 @@ static struct command_result *param_route_hop_style(struct command *cmd,
json_tok_full(buffer, tok));
}
static struct command_result *param_secret(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct secret **secret)
{
*secret = tal(cmd, struct secret);
if (hex_decode(buffer + tok->start,
tok->end - tok->start,
*secret, sizeof(**secret)))
return NULL;
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%s' should be a 32 byte hex value, not '%.*s'",
name, tok->end - tok->start, buffer + tok->start);
}
static struct command_result *json_sendpay(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,