Skip to content

Commit

Permalink
plugin:added invoice creation event
Browse files Browse the repository at this point in the history
New invoice_creation event triggered when an new invoice is created

Changelog-Added: plugin: New invoice_creation plugin event
  • Loading branch information
rbndg authored and cdecker committed Apr 28, 2020
1 parent fc86720 commit df014c6
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 0 deletions.
8 changes: 8 additions & 0 deletions contrib/plugins/helloworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ def on_payment(plugin, invoice_payment, **kwargs):
invoice_payment.get("msat")))


@plugin.subscribe("invoice_creation")
def on_invoice_creation(plugin, invoice_creation, **kwargs):
plugin.log("Received invoice_creation event for label {}, preimage {},"
" and amount of {}".format(invoice_creation.get("label"),
invoice_creation.get("preimage"),
invoice_creation.get("msat")))


@plugin.hook("htlc_accepted")
def on_htlc_accepted(onion, htlc, plugin, **kwargs):
plugin.log('on_htlc_accepted called')
Expand Down
14 changes: 14 additions & 0 deletions doc/PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,20 @@ A notification for topic `invoice_payment` is sent every time an invoie is paid.
"msat": "10000msat"
}
}

```
### `invoice_creation`

A notification for topic `invoice_creation` is sent every time an invoie is paid.

```json
{
"invoice_creation": {
"label": "unique-label-for-invoice",
"preimage": "0000000000000000000000000000000000000000000000000000000000000000",
"msat": "10000msat"
}
}
```

### `warning`
Expand Down
2 changes: 2 additions & 0 deletions lightningd/invoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,8 @@ static void gossipd_incoming_channels_reply(struct subd *gossipd,
json_add_u64(response, "expires_at", details->expiry_time);
json_add_string(response, "bolt11", details->bolt11);

notify_invoice_creation(info->cmd->ld, *info->b11->msat, info->payment_preimage, info->label);

/* Warn if there's not sufficient incoming capacity. */
if (tal_count(info->b11->routes) == 0) {
log_unusual(info->cmd->ld->log,
Expand Down
31 changes: 31 additions & 0 deletions lightningd/notification.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,37 @@ void notify_invoice_payment(struct lightningd *ld, struct amount_msat amount,
plugins_notify(ld->plugins, take(n));
}

static void invoice_creation_notification_serialize(struct json_stream *stream,
struct amount_msat amount,
struct preimage preimage,
const struct json_escape *label)
{
json_object_start(stream, "invoice_creation");
json_add_string(stream, "msat",
type_to_string(tmpctx, struct amount_msat, &amount));
json_add_hex(stream, "preimage", &preimage, sizeof(preimage));
json_add_escaped_string(stream, "label", label);
json_object_end(stream);
}

REGISTER_NOTIFICATION(invoice_creation,
invoice_creation_notification_serialize)

void notify_invoice_creation(struct lightningd *ld, struct amount_msat amount,
struct preimage preimage, const struct json_escape *label)
{
void (*serialize)(struct json_stream *,
struct amount_msat,
struct preimage,
const struct json_escape *) = invoice_creation_notification_gen.serialize;

struct jsonrpc_notification *n
= jsonrpc_notification_start(NULL, invoice_creation_notification_gen.topic);
serialize(n->stream, amount, preimage, label);
jsonrpc_notification_end(n);
plugins_notify(ld->plugins, take(n));
}

static void channel_opened_notification_serialize(struct json_stream *stream,
struct node_id *node_id,
struct amount_sat *funding_sat,
Expand Down
3 changes: 3 additions & 0 deletions lightningd/notification.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ void notify_warning(struct lightningd *ld, struct log_entry *l);
void notify_invoice_payment(struct lightningd *ld, struct amount_msat amount,
struct preimage preimage, const struct json_escape *label);

void notify_invoice_creation(struct lightningd *ld, struct amount_msat amount,
struct preimage preimage, const struct json_escape *label);

void notify_channel_opened(struct lightningd *ld, struct node_id *node_id,
struct amount_sat *funding_sat, struct bitcoin_txid *funding_txid,
bool *funding_locked);
Expand Down
4 changes: 4 additions & 0 deletions lightningd/test/run-invoice-select-inchan.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ void notify_connect(struct lightningd *ld UNNEEDED, struct node_id *nodeid UNNEE
/* Generated stub for notify_disconnect */
void notify_disconnect(struct lightningd *ld UNNEEDED, struct node_id *nodeid UNNEEDED)
{ fprintf(stderr, "notify_disconnect called!\n"); abort(); }
/* Generated stub for notify_invoice_creation */
void notify_invoice_creation(struct lightningd *ld UNNEEDED, struct amount_msat amount UNNEEDED,
struct preimage preimage UNNEEDED, const struct json_escape *label UNNEEDED)
{ fprintf(stderr, "notify_invoice_creation called!\n"); abort(); }
/* Generated stub for notify_invoice_payment */
void notify_invoice_payment(struct lightningd *ld UNNEEDED, struct amount_msat amount UNNEEDED,
struct preimage preimage UNNEEDED, const struct json_escape *label UNNEEDED)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,24 @@ def test_invoice_payment_notification(node_factory):
.format(label, preimage, msats))


@unittest.skipIf(not DEVELOPER, "needs to deactivate shadow routing")
def test_invoice_creation_notification(node_factory):
"""
Test the 'invoice_creation' notification
"""
opts = [{}, {"plugin": os.path.join(os.getcwd(), "contrib/plugins/helloworld.py")}]
l1, l2 = node_factory.line_graph(2, opts=opts)

msats = 12345
preimage = '1' * 64
label = "a_descriptive_label"
l2.rpc.invoice(msats, label, 'description', preimage=preimage)

l2.daemon.wait_for_log(r"Received invoice_creation event for label {},"
" preimage {}, and amount of {}msat"
.format(label, preimage, msats))


def test_channel_opened_notification(node_factory):
"""
Test the 'channel_opened' notification sent at channel funding success.
Expand Down

0 comments on commit df014c6

Please sign in to comment.