61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { Event, EventTemplate, getEventHash, signEvent } from 'nostr-tools'
|
|
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 unsignedEvent = {
|
|
pubkey: authorPubkey,
|
|
...eventTemplate,
|
|
}
|
|
|
|
return {
|
|
...unsignedEvent,
|
|
id: getEventHash(unsignedEvent),
|
|
sig: signEvent(unsignedEvent, authorPrivateKey),
|
|
} as Event
|
|
}
|
|
|
|
export function getTrackingKind(): number {
|
|
return TRACKING_KIND
|
|
}
|