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.
90 lines (89 loc) • 3.7 kB
JavaScript
;
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.NIP04 = void 0;
const secp256k1 = __importStar(require("@noble/secp256k1"));
const utils_1 = require("@noble/hashes/utils");
const sha256_1 = require("@noble/hashes/sha256");
const event_1 = require("../core/event");
class NIP04 {
static async encrypt(privateKey, publicKey, text) {
const sharedPoint = secp256k1.getSharedSecret((0, utils_1.hexToBytes)(privateKey), (0, utils_1.hexToBytes)(publicKey));
const sharedX = sharedPoint.slice(1, 33);
// In a real implementation, we'd use this shared secret with
// proper encryption. This is just a placeholder.
const key = (0, sha256_1.sha256)(sharedX);
// TODO: Implement actual encryption using AES-256-CBC
return (0, utils_1.bytesToHex)(key);
}
static async decrypt(privateKey, publicKey, encryptedText) {
const sharedPoint = secp256k1.getSharedSecret((0, utils_1.hexToBytes)(privateKey), (0, utils_1.hexToBytes)(publicKey));
const sharedX = sharedPoint.slice(1, 33);
// TODO: Implement actual decryption using AES-256-CBC
return "decrypted text";
}
static async createEncryptedEvent(recipientPubkey, content, privateKey) {
const encryptedContent = await this.encrypt(privateKey, recipientPubkey, content);
const tags = [['p', recipientPubkey]];
return await event_1.EventBuilder.createEvent(4, // NIP-04 Direct Message kind
encryptedContent, privateKey, tags);
}
static async decryptEvent(event, privateKey) {
if (event.kind !== 4)
throw new Error('Not a DM event');
const recipientTag = event.tags.find(tag => tag[0] === 'p');
if (!recipientTag)
throw new Error('No recipient tag found');
return await this.decrypt(privateKey, event.pubkey, event.content);
}
static getDMFilter(pubkey, otherPubkey) {
const filter = {
kinds: [4],
limit: 100
};
if (otherPubkey) {
filter['#p'] = [otherPubkey];
filter.authors = [pubkey];
}
else {
filter.$or = [
{ authors: [pubkey] },
{ '#p': [pubkey] }
];
}
return filter;
}
}
exports.NIP04 = NIP04;