From a4a193840fdc15683e4e168874666dbffb62dc1b Mon Sep 17 00:00:00 2001 From: lisa neigut Date: Wed, 5 Jun 2019 14:58:53 +0930 Subject: [PATCH] json: add a param parser for a txid --- common/json_helpers.c | 7 +++++++ common/json_helpers.h | 4 ++++ lightningd/json.c | 15 +++++++++++++++ lightningd/json.h | 3 +++ lightningd/test/run-jsonrpc.c | 4 ++++ 5 files changed, 33 insertions(+) diff --git a/common/json_helpers.c b/common/json_helpers.c index 7d2001788..20cd345ae 100644 --- a/common/json_helpers.c +++ b/common/json_helpers.c @@ -68,3 +68,10 @@ bool json_to_short_channel_id(const char *buffer, const jsmntok_t *tok, tok->end - tok->start, scid, may_be_deprecated_form)); } + +bool json_to_txid(const char *buffer, const jsmntok_t *tok, + struct bitcoin_txid *txid) +{ + return bitcoin_txid_from_hex(buffer + tok->start, + tok->end - tok->start, txid); +} diff --git a/common/json_helpers.h b/common/json_helpers.h index de64d8668..03286469d 100644 --- a/common/json_helpers.h +++ b/common/json_helpers.h @@ -2,6 +2,7 @@ #ifndef LIGHTNING_COMMON_JSON_HELPERS_H #define LIGHTNING_COMMON_JSON_HELPERS_H #include "config.h" +#include #include struct amount_msat; @@ -35,4 +36,7 @@ bool json_to_sat(const char *buffer, const jsmntok_t *tok, bool json_to_msat(const char *buffer, const jsmntok_t *tok, struct amount_msat *msat); +/* Extract a bitcoin txid from this */ +bool json_to_txid(const char *buffer, const jsmntok_t *tok, + struct bitcoin_txid *txid); #endif /* LIGHTNING_COMMON_JSON_HELPERS_H */ diff --git a/lightningd/json.c b/lightningd/json.c index 6a3cc7d76..13379661c 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -108,6 +108,21 @@ struct command_result *param_pubkey(struct command *cmd, const char *name, json_tok_full(buffer, tok)); } +struct command_result *param_txid(struct command *cmd, + const char *name, + const char *buffer, + const jsmntok_t *tok, + struct bitcoin_txid **txid) +{ + *txid = tal(cmd, struct bitcoin_txid); + if (json_to_txid(buffer, tok, *txid)) + return NULL; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be txid, not '%.*s'", + name, json_tok_full_len(tok), + json_tok_full(buffer, tok)); +} + void json_add_short_channel_id(struct json_stream *response, const char *fieldname, const struct short_channel_id *scid) diff --git a/lightningd/json.h b/lightningd/json.h index c30cfd9f6..ea3a0eb2c 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -54,6 +54,9 @@ struct command_result *param_pubkey(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, struct pubkey **pubkey); +struct command_result *param_txid(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + struct bitcoin_txid **txid); /* Makes sure *id is valid. */ struct command_result *param_node_id(struct command *cmd, const char *name, diff --git a/lightningd/test/run-jsonrpc.c b/lightningd/test/run-jsonrpc.c index 6d881c075..1808f6d3c 100644 --- a/lightningd/test/run-jsonrpc.c +++ b/lightningd/test/run-jsonrpc.c @@ -34,6 +34,10 @@ bool json_to_short_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok struct short_channel_id *scid UNNEEDED, bool may_be_deprecated_form UNNEEDED) { fprintf(stderr, "json_to_short_channel_id called!\n"); abort(); } +/* Generated stub for json_to_txid */ +bool json_to_txid(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + struct bitcoin_txid *txid UNNEEDED) +{ fprintf(stderr, "json_to_txid called!\n"); abort(); } /* Generated stub for log_ */ void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED, const char *fmt UNNEEDED, ...)