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.
57 lines (56 loc) • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NIP04 = void 0;
const secp256k1_js_1 = require("@noble/curves/secp256k1.js");
const utils_js_1 = require("@noble/hashes/utils.js");
const sha2_js_1 = require("@noble/hashes/sha2.js");
const event_1 = require("../core/event");
class NIP04 {
static async encrypt(privateKey, publicKey, _text) {
const sharedPoint = secp256k1_js_1.secp256k1.getSharedSecret((0, utils_js_1.hexToBytes)(privateKey), (0, utils_js_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, sha2_js_1.sha256)(sharedX);
// TODO: Implement actual encryption using AES-256-CBC
return (0, utils_js_1.bytesToHex)(key);
}
static async decrypt(privateKey, publicKey, _encryptedText) {
const sharedPoint = secp256k1_js_1.secp256k1.getSharedSecret((0, utils_js_1.hexToBytes)(privateKey), (0, utils_js_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;