diff --git a/common/json_tok.c b/common/json_tok.c index 0c9aec460..4621d4ccc 100644 --- a/common/json_tok.c +++ b/common/json_tok.c @@ -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); +} diff --git a/common/json_tok.h b/common/json_tok.h index 23b53d53f..3a8cf9417 100644 --- a/common/json_tok.h +++ b/common/json_tok.h @@ -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 */ diff --git a/lightningd/pay.c b/lightningd/pay.c index 74884e290..6a08dbcd9 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -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,