75 lines
2.4 KiB
TypeScript
75 lines
2.4 KiB
TypeScript
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<string | null> {
|
|
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<string | null> {
|
|
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<Event | null> {
|
|
return getCachedEventById(eventId)
|
|
}
|