Repository: louislam/uptime-kuma
File: server/notification-providers/nostr.js (JavaScript)

Code before:
const NotificationProvider = require("./notification-provider");
global.crypto = require("crypto");

const {
    relayInit,
    getPublicKey,
    getEventHash,
    signEvent,
    nip04,
    nip19
} = require("nostr-tools");
require("websocket-polyfill");

class Nostr extends NotificationProvider {

    name = "nostr";

    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {

        // All DMs should have same timestamp
        const createdAt = Math.floor(Date.now() / 1000);

        const senderPrivateKey = await this.getPrivateKey(notification.sender);
        const senderPublicKey = getPublicKey(senderPrivateKey);
        const recipientsPublicKeys = await this.getPublicKeys(notification.recipients);

        if (heartbeatJSON == null) {
            msg = `🤙💜 Test Notification for [${notification.name}]`;
        }

        // Create NIP-04 encrypted direct message event for each recipient
        const events = [];
        for (const recipientPublicKey of recipientsPublicKeys) {
            const ciphertext = await nip04.encrypt(senderPrivateKey, recipientPublicKey, msg);
            let event = {
                kind: 4,
                pubkey: senderPublicKey,
                created_at: createdAt,
                tags: [[ "p", recipientPublicKey ]],
                content: ciphertext,
            };
            event.id = getEventHash(event);
            event.sig = signEvent(event, senderPrivateKey);
            events.push(event);

Review comment (suggestion):
```suggestion
    name = "nostr";

    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
```

Code after:
const NotificationProvider = require("./notification-provider");
// polyfill for node <= 18
global.crypto = require("crypto");
const {
    relayInit,
    getPublicKey,
    getEventHash,
    signEvent,
    nip04,
    nip19
} = require("nostr-tools");
// polyfill for node <= 18
require("websocket-polyfill");

class Nostr extends NotificationProvider {
    name = "nostr";

    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        // All DMs should have same timestamp
        const createdAt = Math.floor(Date.now() / 1000);

        const senderPrivateKey = await this.getPrivateKey(notification.sender);
        const senderPublicKey = getPublicKey(senderPrivateKey);
        const recipientsPublicKeys = await this.getPublicKeys(notification.recipients);

        // Create NIP-04 encrypted direct message event for each recipient
        const events = [];
        for (const recipientPublicKey of recipientsPublicKeys) {
            const ciphertext = await nip04.encrypt(senderPrivateKey, recipientPublicKey, msg);
            let event = {
                kind: 4,
                pubkey: senderPublicKey,
                created_at: createdAt,
                tags: [[ "p", recipientPublicKey ]],
                content: ciphertext,
            };
            event.id = getEventHash(event);
            event.sig = signEvent(event, senderPrivateKey);
            events.push(event);
        }

        // Publish events to each relay
        const relays = notification.relays.split("\n");
        let successfulRelays = 0;