diff --git a/lightningd/invoice.c b/lightningd/invoice.c index c8ffcb78a..0f10421f1 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -151,7 +151,7 @@ static void json_invoice(struct command *cmd, { struct invoice invoice; const struct invoice_details *details; - const jsmntok_t *msatoshi, *desctok, *fallbacks; + const jsmntok_t *desctok, *fallbacks; const jsmntok_t *preimagetok; u64 *msatoshi_val; struct json_escaped *label_val, *desc; @@ -165,7 +165,7 @@ static void json_invoice(struct command *cmd, bool result; if (!param(cmd, buffer, params, - p_req("msatoshi", json_tok_tok, &msatoshi), + p_req("msatoshi", json_tok_msat, &msatoshi_val), p_req("label", json_tok_label, &label_val), p_req("description", json_tok_tok, &desctok), p_opt_def("expiry", json_tok_u64, &expiry, 3600), @@ -174,21 +174,6 @@ static void json_invoice(struct command *cmd, NULL)) return; - /* Get arguments. */ - /* msatoshi */ - if (json_tok_streq(buffer, msatoshi, "any")) - msatoshi_val = NULL; - else { - msatoshi_val = tal(cmd, u64); - if (!json_to_u64(buffer, msatoshi, msatoshi_val) - || *msatoshi_val == 0) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%.*s' is not a valid positive number", - msatoshi->end - msatoshi->start, - buffer + msatoshi->start); - return; - } - } if (wallet_invoice_find_by_label(wallet, &invoice, label_val)) { command_fail(cmd, INVOICE_LABEL_ALREADY_EXISTS, "Duplicate label '%s'", label_val->s); diff --git a/lightningd/json.c b/lightningd/json.c index 70ca770bb..2717f6355 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -159,6 +159,27 @@ bool json_tok_sha256(struct command *cmd, const char *name, return false; } +bool json_tok_msat(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t * tok, + u64 **msatoshi_val) +{ + if (json_tok_streq(buffer, tok, "any")) { + *msatoshi_val = NULL; + return true; + } + *msatoshi_val = tal(cmd, u64); + + if (json_to_u64(buffer, tok, *msatoshi_val) && *msatoshi_val != 0) + return true; + + command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a positive number or 'any', not '%.*s'", + name, + tok->end - tok->start, + buffer + tok->start); + return false; +} + bool json_tok_percent(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, double **num) diff --git a/lightningd/json.h b/lightningd/json.h index 25f85a21e..9358110eb 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -63,6 +63,11 @@ bool json_tok_sha256(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, struct sha256 **hash); +/* Extract positive integer, or NULL if tok is 'any'. */ +bool json_tok_msat(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t * tok, + u64 **msatoshi_val); + /* Extract double in range [0.0, 100.0] */ bool json_tok_percent(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, diff --git a/lightningd/test/run-param.c b/lightningd/test/run-param.c index 79419eb3c..66f489c03 100644 --- a/lightningd/test/run-param.c +++ b/lightningd/test/run-param.c @@ -413,29 +413,6 @@ static void sendpay_nulltok(void) assert(msatoshi == NULL); } -static bool json_tok_msat(struct command *cmd, - const char *name, - const char *buffer, - const jsmntok_t * tok, - u64 **msatoshi_val) -{ - if (json_tok_streq(buffer, tok, "any")) { - *msatoshi_val = NULL; - return true; - } - *msatoshi_val = tal(cmd, u64); - - if (json_to_u64(buffer, tok, *msatoshi_val) && *msatoshi_val != 0) - return true; - - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be a positive number or 'any', not '%.*s'", - name, - tok->end - tok->start, - buffer + tok->start); - return false; -} - /* * New version of json_tok_label conforming to advanced style. This can eventually * replace the existing json_tok_label.