export async function sendDm(recipientPubkey: string, message: string): Promise { const privkeyHex = process.env.NOSTR_PRIVATE_KEY if (!privkeyHex || !recipientPubkey) return try { const { finalizeEvent } = await import('nostr-tools/pure') const { encrypt } = await import('nostr-tools/nip04') const privkey = Uint8Array.from(Buffer.from(privkeyHex, 'hex')) const ciphertext = await encrypt(privkey, recipientPubkey, message) const event = finalizeEvent({ kind: 4, created_at: Math.floor(Date.now() / 1000), tags: [['p', recipientPubkey]], content: ciphertext }, privkey) privkey.fill(0) const { Relay } = await import('nostr-tools/relay') const relays = ['wss://relay.damus.io', 'wss://nos.lol', 'wss://relay.nostr.band'] for (const url of relays) { try { const relay = await Relay.connect(url); await relay.publish(event); relay.close() } catch {} } } catch { console.error('Failed to send Nostr DM') } } export async function notifyOrderConfirmed(pubkey: string, orderId: string, totalSats: number): Promise { await sendDm(pubkey, `Your Antonym order ${orderId} for ${totalSats.toLocaleString()} sats has been confirmed. Track it at: ${process.env.SITE_URL || 'http://localhost:3333'}/order/${orderId}`) } export async function notifyStatusUpdate(pubkey: string, orderId: string, status: string, note?: string): Promise { const noteText = note ? ` — ${note}` : '' await sendDm(pubkey, `Your Antonym order ${orderId} has been updated to: ${status}${noteText}`) }