UNPKG

node-red-contrib-nostr

Version:

Node-RED nodes for seamless Nostr protocol integration. Features robust WebSocket handling, event filtering, and NPUB-based routing. Built with TypeScript for type safety and extensive testing. Perfect for Nostr automation flows.

105 lines (104 loc) 4.33 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = default_1; function default_1(RED) { // Create a function to initialize the node async function initializeNode(config) { RED.nodes.createNode(this, config); // Get the relay configuration node this.relay = RED.nodes.getNode(config.relay); if (!this.relay) { this.error("No relay configuration found"); this.status({ fill: "red", shape: "ring", text: "Missing relay config" }); return; } this.npubValue = config.npubValue; this.eventKinds = config.eventKinds || [0, 1]; // Default to metadata and text notes try { // Dynamically import ESM dependencies const { nip19 } = await Promise.resolve().then(() => __importStar(require('nostr-tools'))); // Convert npub to hex if needed if (this.npubValue) { try { const decoded = nip19.decode(this.npubValue); if (decoded.type === 'npub') { this.hexPubkey = decoded.data; this.status({ fill: "green", shape: "dot", text: "Ready" }); } else { throw new Error("Invalid npub format"); } } catch (err) { this.error("Invalid npub: " + err.message); this.status({ fill: "red", shape: "dot", text: "Invalid npub" }); return; } } // Set up message handler on the config node's event emitter this.relay.on('message', (msg) => { if (msg.type === 'EVENT' && msg.event) { const event = msg.event; // Check if event matches our filter criteria if (this.hexPubkey && event.pubkey === this.hexPubkey && this.eventKinds.includes(event.kind)) { this.send({ payload: event }); } } }); // Subscribe to events based on filter if (this.hexPubkey) { const filter = { authors: [this.hexPubkey], kinds: this.eventKinds }; await this.relay._ws?.sendMessage(['REQ', 'sub', filter]); } } catch (err) { this.error("Failed to initialize node: " + err.message); this.status({ fill: "red", shape: "dot", text: "Error" }); } } // Register the node RED.nodes.registerType("nostr-npub-filter", function (config) { // Initialize asynchronously initializeNode.call(this, config).catch((err) => { this.error("Failed to initialize node: " + err.message); }); }); }