Skip to content

Commit

Permalink
invoice: don't allow zero-value invoices.
Browse files Browse the repository at this point in the history
You can't pay them anyway, and at least one person used 0 instead of "any".

Closes: #3808
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `invoice` no longer accepts zero amounts (did you mean "any"?)
  • Loading branch information
rustyrussell committed Aug 25, 2020
1 parent 2e51f23 commit ef3fbab
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion doc/lightning-invoice.7

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion doc/lightning-invoice.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ lightning daemon can use to pay this invoice. This token includes a
invoice, if any exists.

The *msatoshi* parameter can be the string "any", which creates an
invoice that can be paid with any amount. Otherwise it is in
invoice that can be paid with any amount. Otherwise it is a positive value in
millisatoshi precision; it can be a whole number, or a whole number
ending in *msat* or *sat*, or a number with three decimal places ending
in *sat*, or a number with 1 to 11 decimal places ending in *btc*.
Expand Down
18 changes: 10 additions & 8 deletions lightningd/invoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -826,22 +826,24 @@ static struct route_info **unpack_routes(const tal_t *ctx,
}
#endif /* DEVELOPER */

static struct command_result *param_msat_or_any(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct amount_msat **msat)
static struct command_result *param_positive_msat_or_any(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct amount_msat **msat)
{
if (json_tok_streq(buffer, tok, "any")) {
*msat = NULL;
return NULL;
}
*msat = tal(cmd, struct amount_msat);
if (parse_amount_msat(*msat, buffer + tok->start, tok->end - tok->start))
if (parse_amount_msat(*msat, buffer + tok->start, tok->end - tok->start)
&& !amount_msat_eq(**msat, AMOUNT_MSAT(0)))
return NULL;

return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%s' should be millisatoshis or 'any', not '%.*s'",
"'%s' should be positive millisatoshis or 'any',"
" not '%.*s'",
name,
tok->end - tok->start,
buffer + tok->start);
Expand Down Expand Up @@ -963,7 +965,7 @@ static struct command_result *json_invoice(struct command *cmd,
info->cmd = cmd;

if (!param(cmd, buffer, params,
p_req("msatoshi", param_msat_or_any, &msatoshi_val),
p_req("msatoshi", param_positive_msat_or_any, &msatoshi_val),
p_req("label", param_label, &info->label),
p_req("description", param_escaped_string, &desc_val),
p_opt_def("expiry", param_time, &expiry, 3600*24*7),
Expand Down
20 changes: 20 additions & 0 deletions tests/test_invoices.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ def test_invoice(node_factory, chainparams):
l2.rpc.invoice(4294967295, 'inv3', '?')


def test_invoice_zeroval(node_factory):
"""A zero value invoice is unpayable, did you mean 'any'?"""
l1 = node_factory.get_node()

with pytest.raises(RpcError, match=r"positive .* not '0'"):
l1.rpc.invoice(0, 'inv', '?')

with pytest.raises(RpcError, match=r"positive .* not '0msat'"):
l1.rpc.invoice('0msat', 'inv', '?')

with pytest.raises(RpcError, match=r"positive .* not '0sat'"):
l1.rpc.invoice('0sat', 'inv', '?')

with pytest.raises(RpcError, match=r"positive .* not '0.00000000btc'"):
l1.rpc.invoice('0.00000000btc', 'inv', '?')

with pytest.raises(RpcError, match=r"positive .* not '0.00000000000btc'"):
l1.rpc.invoice('0.00000000000btc', 'inv', '?')


def test_invoice_weirdstring(node_factory):
l1 = node_factory.get_node()

Expand Down

0 comments on commit ef3fbab

Please sign in to comment.