From b5bb7f191ff4b8fc3dd29874c3d16c34f0715049 Mon Sep 17 00:00:00 2001 From: darosior Date: Sun, 21 Jul 2019 13:39:15 +0200 Subject: [PATCH] Plugins: Add a notification for invoice payment Similarly to the 'invoice_payment' hook --- lightningd/invoice.c | 11 ++++++++--- lightningd/notification.c | 22 ++++++++++++++++++--- lightningd/notification.h | 5 +++++ lightningd/test/run-invoice-select-inchan.c | 4 ++++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lightningd/invoice.c b/lightningd/invoice.c index 577480eee..66ccb7e7f 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -1,7 +1,4 @@ #include "invoice.h" -#include "json.h" -#include "jsonrpc.h" -#include "lightningd.h" #include #include #include @@ -25,7 +22,11 @@ #include #include #include +#include +#include +#include #include +#include #include #include #include @@ -173,6 +174,10 @@ invoice_payment_hook_cb(struct invoice_payment_hook_payload *payload, struct invoice invoice; enum onion_type failcode; + /* We notify here to benefit from the payload and because the hook callback is + * called even if the hook is not registered. */ + notify_invoice_payment(ld, payload->msat, payload->preimage, payload->label); + tal_del_destructor2(payload->hin, invoice_payload_remove_hin, payload); /* We want to free this, whatever happens. */ tal_steal(tmpctx, payload); diff --git a/lightningd/notification.c b/lightningd/notification.c index d133b9083..369d8ec7f 100644 --- a/lightningd/notification.c +++ b/lightningd/notification.c @@ -1,11 +1,12 @@ -#include "lightningd/notification.h" #include #include +#include const char *notification_topics[] = { "connect", "disconnect", - "warning" + "warning", + "invoice_payment" }; bool notifications_have_topic(const char *topic) @@ -59,4 +60,19 @@ void notify_warning(struct lightningd *ld, struct log_entry *l) json_object_end(n->stream); /* .warning */ jsonrpc_notification_end(n); plugins_notify(ld->plugins, take(n)); -} \ No newline at end of file +} + +void notify_invoice_payment(struct lightningd *ld, struct amount_msat amount, + struct preimage preimage, const struct json_escape *label) +{ + struct jsonrpc_notification *n = + jsonrpc_notification_start(NULL, notification_topics[3]); + json_object_start(n->stream, notification_topics[3]); + json_add_string(n->stream, "msat", + type_to_string(tmpctx, struct amount_msat, &amount)); + json_add_hex(n->stream, "preimage", &preimage, sizeof(preimage)); + json_add_escaped_string(n->stream, "label", label); + json_object_end(n->stream); + jsonrpc_notification_end(n); + plugins_notify(ld->plugins, take(n)); +} diff --git a/lightningd/notification.h b/lightningd/notification.h index dcc8d261a..9d1c46304 100644 --- a/lightningd/notification.h +++ b/lightningd/notification.h @@ -1,6 +1,8 @@ #ifndef LIGHTNING_LIGHTNINGD_NOTIFICATION_H #define LIGHTNING_LIGHTNINGD_NOTIFICATION_H #include "config.h" +#include +#include #include #include #include @@ -14,4 +16,7 @@ void notify_disconnect(struct lightningd *ld, struct node_id *nodeid); 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); + #endif /* LIGHTNING_LIGHTNINGD_NOTIFICATION_H */ diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index 1ddf8dd26..3373aca7b 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -287,6 +287,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_payment */ +void notify_invoice_payment(struct lightningd *ld UNNEEDED, struct amount_msat amount UNNEEDED, + struct preimage preimage UNNEEDED, const struct json_escape *label UNNEEDED) +{ fprintf(stderr, "notify_invoice_payment called!\n"); abort(); } /* Generated stub for onchaind_funding_spent */ enum watch_result onchaind_funding_spent(struct channel *channel UNNEEDED, const struct bitcoin_tx *tx UNNEEDED,