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.
125 lines (124 loc) • 4.82 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.KeyManager = exports.DEFAULT_READER_KEYS = void 0;
exports.generateKeyPair = generateKeyPair;
exports.hexToNpub = hexToNpub;
exports.npubToHex = npubToHex;
exports.getPublicKey = getPublicKey;
const bech32_1 = require("bech32");
const utils_1 = require("@noble/hashes/utils");
const sha256_1 = require("@noble/hashes/sha256");
// Default read-only key pair for basic operations
// This is a dedicated key for node-red-contrib-nostr that's only used for reading events
exports.DEFAULT_READER_KEYS = {
privateKey: '5acf32b3374c8c0aa6e483e0f7c6ba8c4b2d2f35d1d5854f08c8c9555d81903b',
publicKey: '04dbc5c6c357e5f33e19c89a2c0b2c1c41f7b15cc3e8f6a63f5e0e8c5d5c5c5c',
};
let secp256k1;
async function initSecp256k1() {
if (!secp256k1) {
secp256k1 = await Promise.resolve().then(() => __importStar(require('@noble/secp256k1')));
}
return secp256k1;
}
/**
* Generate a new Nostr key pair
* @returns {Promise<Object>} Object containing private and public keys
*/
async function generateKeyPair() {
const secp = await initSecp256k1();
const privateKey = secp.utils.randomPrivateKey();
const publicKey = secp.getPublicKey(privateKey, true);
return {
privateKey: Buffer.from(privateKey).toString('hex'),
publicKey: Buffer.from(publicKey).toString('hex')
};
}
/**
* Convert a hex public key to npub format
* @param {string} publicKeyHex - Public key in hex format
* @returns {string} npub formatted public key
*/
function hexToNpub(publicKeyHex) {
const words = bech32_1.bech32.toWords(Buffer.from(publicKeyHex, 'hex'));
return bech32_1.bech32.encode('npub', words);
}
/**
* Convert an npub to hex format
* @param {string} npub - npub formatted public key
* @returns {string} Public key in hex format
*/
function npubToHex(npub) {
const { words } = bech32_1.bech32.decode(npub);
return Buffer.from(bech32_1.bech32.fromWords(words)).toString('hex');
}
/**
* Get public key from private key
* @param {string} privateKeyHex - Private key in hex format
* @returns {Promise<string>} Public key in hex format
*/
async function getPublicKey(privateKeyHex) {
const secp = await initSecp256k1();
const publicKey = secp.getPublicKey(privateKeyHex, true);
return Buffer.from(publicKey).toString('hex');
}
class KeyManager {
constructor() {
this.secp256k1Promise = initSecp256k1();
}
async generatePrivateKey() {
const secp = await this.secp256k1Promise;
const privateKey = secp.utils.randomPrivateKey();
return Buffer.from(privateKey).toString('hex');
}
async getPublicKey(privateKey) {
const secp = await this.secp256k1Promise;
const publicKey = secp.getPublicKey(privateKey, true);
return Buffer.from(publicKey).toString('hex');
}
async sign(privateKey, message) {
const secp = await this.secp256k1Promise;
const messageHash = (0, sha256_1.sha256)(Buffer.from(message));
const signature = await secp.sign(messageHash, privateKey);
return (0, utils_1.bytesToHex)(signature.toCompactRawBytes());
}
async verify(publicKey, message, signature) {
const secp = await this.secp256k1Promise;
const messageHash = (0, sha256_1.sha256)(Buffer.from(message));
return secp.verify(signature, messageHash, publicKey);
}
}
exports.KeyManager = KeyManager;