param: added json_tok_msat

This could have been a local static but its used by the run-param test,
so putting it in json.c made things easier.

Signed-off-by: Mark Beckwith <wythe@intrig.com>
This commit is contained in:
Mark Beckwith 2018-08-27 15:58:50 -05:00 committed by Rusty Russell
parent c553bba7a8
commit 4f81cd3852
4 changed files with 28 additions and 40 deletions

View File

@ -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);

View File

@ -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)

View File

@ -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,

View File

@ -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.