import type { Event } from 'nostr-tools' import { generateSponsoringHashId } from '../hashIdGenerator' import type { ExtractedSponsoring } from './types' import { readAmountSats, readPaymentHash, readPayerPubkeyFromZapReceipt, readTagValue, readZapReceiptKind, resolveSponsoringArticleId, } from './utils' export async function extractSponsoringFromEvent(event: Event): Promise { if (event.kind !== 9735 || readZapReceiptKind(event) !== 'sponsoring') { return null } const extracted = readSponsoringFields(event) if (!extracted) { return null } const id = await generateSponsoringHashId(extracted) return { type: 'sponsoring', id, ...extracted, eventId: event.id } } function readSponsoringFields(event: Event): Omit | null { const payerPubkey = readPayerPubkeyFromZapReceipt(event) const authorPubkey = readTagValue(event, 'author') ?? readTagValue(event, 'p') const amount = readAmountSats(event) if (!payerPubkey || !authorPubkey || amount === undefined) { console.error('[metadataExtractor] Invalid sponsoring zap receipt: missing required fields', { eventId: event.id }) return null } const paymentHash = readPaymentHash(event) const seriesId = readTagValue(event, 'series') const articleId = resolveSponsoringArticleId({ articleTag: readTagValue(event, 'article'), eTag: readTagValue(event, 'e') }) return { payerPubkey, authorPubkey, amount, paymentHash, ...(seriesId ? { seriesId } : {}), ...(articleId ? { articleId } : {}), } }