import { Event, EventTemplate, finalizeEvent } from 'nostr-tools' import { hexToBytes } from 'nostr-tools/utils' import type { ContentDeliveryTracking } from './platformTrackingTypes' const TRACKING_KIND = 30078 // Custom kind for platform tracking export function buildTrackingTags(tracking: ContentDeliveryTracking, platformPubkey: string): string[][] { return [ ['p', platformPubkey], ['article', tracking.articleId], ['author', tracking.articlePubkey], ['recipient', tracking.recipientPubkey], ['message', tracking.messageEventId], ['amount', tracking.amount.toString()], ...(tracking.authorAmount ? [['author_amount', tracking.authorAmount.toString()]] : []), ...(tracking.platformCommission ? [['platform_commission', tracking.platformCommission.toString()]] : []), ['verified', tracking.verified ? 'true' : 'false'], ['timestamp', tracking.timestamp.toString()], ...(tracking.zapReceiptId ? [['zap_receipt', tracking.zapReceiptId]] : []), ] } export function buildTrackingEvent( tracking: ContentDeliveryTracking, _authorPubkey: string, authorPrivateKey: string, platformPubkey: string ): Event { const eventTemplate: EventTemplate = { kind: TRACKING_KIND, created_at: Math.floor(Date.now() / 1000), tags: buildTrackingTags(tracking, platformPubkey), content: JSON.stringify({ articleId: tracking.articleId, articlePubkey: tracking.articlePubkey, recipientPubkey: tracking.recipientPubkey, messageEventId: tracking.messageEventId, amount: tracking.amount, authorAmount: tracking.authorAmount, platformCommission: tracking.platformCommission, verified: tracking.verified, timestamp: tracking.timestamp, zapReceiptId: tracking.zapReceiptId, }), } const secretKey = hexToBytes(authorPrivateKey) return finalizeEvent(eventTemplate, secretKey) } export function getTrackingKind(): number { return TRACKING_KIND }