24 lines
1.2 KiB
TypeScript
24 lines
1.2 KiB
TypeScript
import type { Event } from 'nostr-tools'
|
|
import { generatePurchaseHashId } from '../hashIdGenerator'
|
|
import type { ExtractedPurchase } from './types'
|
|
import { readAmountSats, readPaymentHash, readPayerPubkeyFromZapReceipt, readTagValue, readZapReceiptKind } from './utils'
|
|
|
|
export async function extractPurchaseFromEvent(event: Event): Promise<ExtractedPurchase | null> {
|
|
if (event.kind !== 9735 || readZapReceiptKind(event) !== 'purchase') {
|
|
return null
|
|
}
|
|
|
|
const payerPubkey = readPayerPubkeyFromZapReceipt(event)
|
|
const authorPubkey = readTagValue(event, 'author') ?? readTagValue(event, 'p')
|
|
const articleId = readTagValue(event, 'article') ?? readTagValue(event, 'e')
|
|
const amount = readAmountSats(event)
|
|
if (!payerPubkey || !authorPubkey || !articleId || amount === undefined) {
|
|
console.error('[metadataExtractor] Invalid purchase zap receipt: missing required fields', { eventId: event.id })
|
|
return null
|
|
}
|
|
|
|
const paymentHash = readPaymentHash(event)
|
|
const id = await generatePurchaseHashId({ payerPubkey, articleId, authorPubkey, amount, paymentHash })
|
|
return { type: 'purchase', id, payerPubkey, articleId, authorPubkey, amount, paymentHash, eventId: event.id }
|
|
}
|