2026-01-13 14:49:19 +01:00

39 lines
1.6 KiB
TypeScript

import { nostrService } from '../nostr'
import type { Event, EventTemplate } from 'nostr-tools'
import { finalizeEvent } from 'nostr-tools'
import { hexToBytes } from 'nostr-tools/utils'
import { writeOrchestrator } from '../writeOrchestrator'
import { getPublishRelays } from '../relaySelection'
import type { Purchase, ReviewTip, Sponsoring } from '@/types/nostr'
export type PaymentCategoryTag = 'sciencefiction' | 'research'
export function mapPaymentCategory(category: 'science-fiction' | 'scientific-research' | undefined): PaymentCategoryTag {
return category === 'scientific-research' ? 'research' : 'sciencefiction'
}
export function buildPaymentNoteTemplate(tags: string[][], content: string): EventTemplate {
return { kind: 1, created_at: Math.floor(Date.now() / 1000), tags, content }
}
export async function publishPaymentNoteToRelays(params: {
payerPrivateKey: string
objectType: 'purchase' | 'review_tip' | 'sponsoring'
hash: string
eventTemplate: EventTemplate
parsed: Purchase | ReviewTip | Sponsoring
version: number
hidden: boolean
index: number
}): Promise<Event | null> {
nostrService.setPrivateKey(params.payerPrivateKey)
writeOrchestrator.setPrivateKey(params.payerPrivateKey)
const event = finalizeEvent(params.eventTemplate, hexToBytes(params.payerPrivateKey))
const relays = await getPublishRelays()
const result = await writeOrchestrator.writeAndPublish(
{ objectType: params.objectType, hash: params.hash, event, parsed: params.parsed, version: params.version, hidden: params.hidden, index: params.index },
relays
)
return result.success ? event : null
}