Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check whether the recipient supports keysend #4236

Merged
merged 2 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions plugins/keysend.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <bitcoin/preimage.h>
#include <ccan/array_size/array_size.h>
#include <ccan/tal/str/str.h>
#include <common/gossmap.h>
#include <plugins/libplugin-pay.h>
#include <plugins/libplugin.h>
#include <wire/onion_wire.h>
Expand Down Expand Up @@ -54,6 +55,24 @@ static struct keysend_data *keysend_init(struct payment *p)
static void keysend_cb(struct keysend_data *d, struct payment *p) {
struct createonion_hop *last_payload;
size_t hopcount;
struct gossmap_node *node;
bool enabled;
const struct gossmap *gossmap = get_gossmap(p->plugin);

/* On the root payment we perform the featurebit check. */
if (p->parent == NULL && p->step == PAYMENT_STEP_INITIALIZED) {
node = gossmap_find_node(gossmap, p->destination);

enabled = gossmap_node_get_feature(gossmap, node,
KEYSEND_FEATUREBIT) != -1;
if (!enabled)
return payment_fail(
p,
"Recipient %s does not support keysend payments "
"(feature bit %d missing in node announcement)",
node_id_to_hexstr(tmpctx, p->destination),
KEYSEND_FEATUREBIT);
}

if (p->step != PAYMENT_STEP_ONION_PAYLOAD)
return payment_continue(p);
Expand Down
18 changes: 16 additions & 2 deletions tests/test_pay.py
Original file line number Diff line number Diff line change
Expand Up @@ -2990,13 +2990,22 @@ def test_excluded_adjacent_routehint(node_factory, bitcoind, compat):

def test_keysend(node_factory):
amt = 10000
l1, l2, l3 = node_factory.line_graph(3, wait_for_announce=True)
l1, l2, l3, l4 = node_factory.line_graph(
4,
wait_for_announce=True,
opts=[{}, {}, {}, {'disable-plugin': 'keysend'}]
)

# The keysend featurebit must be set in the announcement, i.e., l1 should
# learn that l3 supports keysends.
features = l1.rpc.listnodes(l3.info['id'])['nodes'][0]['features']
assert(int(features, 16) >> 55 & 0x01 == 1)

# If we disable keysend, then the featurebit must not be set,
# i.e., l4 doesn't support it.
features = l1.rpc.listnodes(l4.info['id'])['nodes'][0]['features']
assert(int(features, 16) >> 55 & 0x01 == 0)

# Send an indirect one from l1 to l3
l1.rpc.keysend(l3.info['id'], amt)
invs = l3.rpc.listinvoices()['invoices']
Expand All @@ -3014,6 +3023,11 @@ def test_keysend(node_factory):
inv = invs[0]
assert(inv['msatoshi_received'] >= amt)

# And finally try to send a keysend payment to l4, which doesn't
# support it. It MUST fail.
with pytest.raises(RpcError, match=r"Recipient [0-9a-f]{66} does not support keysend payments"):
l3.rpc.keysend(l4.info['id'], amt)


def test_invalid_onion_channel_update(node_factory):
'''
Expand Down Expand Up @@ -3373,7 +3387,7 @@ def test_listpay_result_with_paymod(node_factory, bitcoind):

amount_sat = 10 ** 6

l1, l2, l3 = node_factory.line_graph(3)
l1, l2, l3 = node_factory.line_graph(3, wait_for_announce=True)

invl2 = l2.rpc.invoice(amount_sat * 2, "inv_l2", "inv_l2")
l1.rpc.pay(invl2['bolt11'])
Expand Down