Browse Source

Plugins: Add a notification for invoice payment

Similarly to the 'invoice_payment' hook
pull/2938/head
darosior 6 years ago
committed by ZmnSCPxj, ZmnSCPxj jxPCSmnZ
parent
commit
b5bb7f191f
  1. 11
      lightningd/invoice.c
  2. 22
      lightningd/notification.c
  3. 5
      lightningd/notification.h
  4. 4
      lightningd/test/run-invoice-select-inchan.c

11
lightningd/invoice.c

@ -1,7 +1,4 @@
#include "invoice.h"
#include "json.h"
#include "jsonrpc.h"
#include "lightningd.h"
#include <bitcoin/address.h>
#include <bitcoin/base58.h>
#include <bitcoin/script.h>
@ -25,7 +22,11 @@
#include <inttypes.h>
#include <lightningd/channel.h>
#include <lightningd/hsm_control.h>
#include <lightningd/json.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
#include <lightningd/notification.h>
#include <lightningd/options.h>
#include <lightningd/peer_control.h>
#include <lightningd/peer_htlcs.h>
@ -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);

22
lightningd/notification.c

@ -1,11 +1,12 @@
#include "lightningd/notification.h"
#include <ccan/array_size/array_size.h>
#include <lightningd/json.h>
#include <lightningd/notification.h>
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));
}
}
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));
}

5
lightningd/notification.h

@ -1,6 +1,8 @@
#ifndef LIGHTNING_LIGHTNINGD_NOTIFICATION_H
#define LIGHTNING_LIGHTNINGD_NOTIFICATION_H
#include "config.h"
#include <ccan/json_escape/json_escape.h>
#include <common/amount.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
@ -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 */

4
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,

Loading…
Cancel
Save