UNPKG

nostr-dm-magiclink-utils

Version:

A comprehensive Nostr utility library for magic link authentication via direct messages, supporting both ESM and CommonJS. Features NIP-01/04 compliant message encryption, multi-relay support, internationalization (i18n) with RTL support, and TypeScript-f

65 lines 2.31 kB
import { getPublicKeySync, finalizeEvent, verifySignature } from 'nostr-crypto-utils'; import crypto from 'crypto'; import { NostrError, NostrErrorCode } from '../../types/errors.js'; import { logger } from '../../utils/logger.js'; /** * Create a signed Nostr event * Uses finalizeEvent for one-step create+sign and getPublicKeySync for sync pubkey derivation. * @param content Event content * @param kind Event kind * @param privateKey Private key to sign the event * @param tags Optional event tags * @returns Signed Nostr event */ export const createEvent = async (content, kind, privateKey, tags = []) => { try { const pubkey = getPublicKeySync(privateKey); const nonce = crypto.randomBytes(4).readUInt32BE(0) % 1000000; // Use finalizeEvent for one-step create + hash + sign const signed = await finalizeEvent({ pubkey, kind, tags, content: `${content}:${nonce}`, }, privateKey); return { pubkey: signed.pubkey, created_at: signed.created_at, kind: signed.kind, tags: signed.tags, content: signed.content, id: signed.id, sig: signed.sig, }; } catch (error) { logger.error({ error }, 'Error creating event'); throw new NostrError('Failed to create event', NostrErrorCode.EVENT_CREATION_FAILED, error instanceof Error ? error : new Error(String(error))); } }; /** * Verify a Nostr event's signature and structure * @param event Event to verify * @returns True if event is valid, false otherwise */ export const verifyEvent = async (event) => { try { const now = Math.floor(Date.now() / 1000); // Check timestamp if (event.created_at > now + 60) { // Allow 1 minute clock skew logger.warn('Event from future'); return false; } if (event.created_at < now - 60 * 60 * 24 * 365) { // Reject events older than 1 year logger.warn('Event too old'); return false; } // Verify signature return await verifySignature(event); } catch (error) { logger.error({ error }, 'Error verifying event'); return false; } }; //# sourceMappingURL=nip01.js.map