From de6bf3e4210ae7bdc6f2fec63f98174231af5436 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 7 Nov 2019 17:56:06 +0100 Subject: [PATCH] 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. --- common/json_tok.c | 28 ++++++++++++++++++++++++++++ common/json_tok.h | 10 ++++++++++ lightningd/pay.c | 17 ----------------- 3 files changed, 38 insertions(+), 17 deletions(-) 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,