keysend: Add error when trying to send to self

There is little point in faking a self-payment, but we should also not
crash :-)

Fixes #4438

Changelog-Fixed: keysend: Keysend returns an error when a self-payment is requested
This commit is contained in:
Christian Decker
2021-03-17 12:30:06 +01:00
committed by Rusty Russell
parent 3a031bf0e3
commit 6cfb72ea1b
2 changed files with 11 additions and 1 deletions

View File

@@ -171,13 +171,19 @@ static struct command_result *json_keysend(struct command *cmd, const char *buf,
p->deadline = timeabs_add(time_now(), time_from_sec(*retryfor));
p->getroute->riskfactorppm = 10000000;
if (node_id_eq(&my_id, p->destination)) {
return command_fail(
cmd, JSONRPC2_INVALID_PARAMS,
"We are the destination. Keysend cannot be used to send funds to yourself");
}
if (!amount_msat_fee(&p->constraints.fee_budget, p->amount, 0,
*maxfee_pct_millionths / 100)) {
tal_free(p);
return command_fail(
cmd, JSONRPC2_INVALID_PARAMS,
"Overflow when computing fee budget, fee rate too high.");
}
p->constraints.cltv_budget = *maxdelay;
payment_mod_exemptfee_get_data(p)->amount = *exemptfee;

View File

@@ -3030,6 +3030,10 @@ def test_keysend(node_factory):
features = l1.rpc.listnodes(l4.info['id'])['nodes'][0]['features']
assert(int(features, 16) >> 55 & 0x01 == 0)
# Self-sends are not allowed (see #4438)
with pytest.raises(RpcError, match=r'We are the destination.'):
l1.rpc.keysend(l1.info['id'], amt)
# Send an indirect one from l1 to l3
l1.rpc.keysend(l3.info['id'], amt)
invs = l3.rpc.listinvoices()['invoices']