import type { Event } from 'nostr-tools' import { decryptArticleContentWithKey, getDecryptionKey, getPrivateContent as getPrivateContentFromPool } from '../nostrPrivateMessages' import { getCachedEventById } from './cache' export async function getPrivateContent(params: { pool: import('nostr-tools').SimplePool eventId: string authorPubkey: string privateKey: string publicKey: string }): Promise { return getPrivateContentFromPool({ pool: params.pool, eventId: params.eventId, authorPubkey: params.authorPubkey, privateKey: params.privateKey, publicKey: params.publicKey, }) } export async function getDecryptedArticleContent(params: { pool: import('nostr-tools').SimplePool eventId: string authorPubkey: string privateKey: string publicKey: string }): Promise { try { const event = await getEventById(params.eventId) if (!event) { console.error('[NostrService] Event not found', { eventId: params.eventId, authorPubkey: params.authorPubkey }) return null } const decryptionKey = await retrieveDecryptionKey({ pool: params.pool, eventId: params.eventId, authorPubkey: params.authorPubkey, recipientPrivateKey: params.privateKey, recipientPublicKey: params.publicKey, }) if (!decryptionKey) { console.warn('[NostrService] Decryption key not found in private messages', { eventId: params.eventId, authorPubkey: params.authorPubkey }) return null } return decryptArticleContentWithKey(event.content, decryptionKey) } catch (decryptError) { console.error('[NostrService] Error decrypting article content', { eventId: params.eventId, authorPubkey: params.authorPubkey, error: decryptError instanceof Error ? decryptError.message : 'Unknown error', }) return null } } async function retrieveDecryptionKey(params: { pool: import('nostr-tools').SimplePool eventId: string authorPubkey: string recipientPrivateKey: string recipientPublicKey: string }): Promise<{ key: string; iv: string } | null> { return getDecryptionKey({ pool: params.pool, eventId: params.eventId, authorPubkey: params.authorPubkey, recipientPrivateKey: params.recipientPrivateKey, recipientPublicKey: params.recipientPublicKey, }) } async function getEventById(eventId: string): Promise { return getCachedEventById(eventId) }