115 lines
3.6 KiB
TypeScript
115 lines
3.6 KiB
TypeScript
import type { Event } from 'nostr-tools'
|
|
import type { Purchase } from '@/types/nostr'
|
|
import { buildTags } from '../nostrTagSystem'
|
|
import { PLATFORM_SERVICE } from '../platformConfig'
|
|
import { generatePurchaseHashId } from '../hashIdGenerator'
|
|
import { buildObjectId } from '../urlGenerator'
|
|
import { buildPaymentNoteTemplate, mapPaymentCategory, type PaymentCategoryTag, publishPaymentNoteToRelays } from './shared'
|
|
|
|
export async function publishPurchaseNote(params: {
|
|
articleId: string
|
|
authorPubkey: string
|
|
payerPubkey: string
|
|
amount: number
|
|
paymentHash: string
|
|
zapReceiptId?: string
|
|
category?: 'science-fiction' | 'scientific-research'
|
|
seriesId?: string
|
|
payerPrivateKey: string
|
|
}): Promise<Event | null> {
|
|
const category = mapPaymentCategory(params.category)
|
|
const payload = await buildPurchaseNotePayload({ ...params, category })
|
|
return publishPaymentNoteToRelays({
|
|
payerPrivateKey: params.payerPrivateKey,
|
|
objectType: 'purchase',
|
|
hash: payload.hashId,
|
|
eventTemplate: payload.eventTemplate,
|
|
parsed: payload.parsedPurchase,
|
|
version: 0,
|
|
hidden: false,
|
|
index: 0,
|
|
})
|
|
}
|
|
|
|
async function buildPurchaseNotePayload(params: {
|
|
articleId: string
|
|
authorPubkey: string
|
|
payerPubkey: string
|
|
amount: number
|
|
paymentHash: string
|
|
zapReceiptId?: string
|
|
category: PaymentCategoryTag
|
|
seriesId?: string
|
|
}): Promise<{ hashId: string; eventTemplate: import('nostr-tools').EventTemplate; parsedPurchase: Purchase }> {
|
|
const purchaseData = {
|
|
payerPubkey: params.payerPubkey,
|
|
articleId: params.articleId,
|
|
authorPubkey: params.authorPubkey,
|
|
amount: params.amount,
|
|
paymentHash: params.paymentHash,
|
|
...(params.seriesId ? { seriesId: params.seriesId } : {}),
|
|
}
|
|
const hashId = await generatePurchaseHashId(purchaseData)
|
|
const id = buildObjectId(hashId, 0, 0)
|
|
const tags = buildPurchaseNoteTags({ ...params, hashId })
|
|
tags.push(['json', JSON.stringify({ type: 'purchase', id, hash: hashId, version: 0, index: 0, ...purchaseData })])
|
|
const parsedPurchase = buildParsedPurchase({ ...params, id, hashId })
|
|
return { hashId, eventTemplate: buildPaymentNoteTemplate(tags, `Purchase confirmed: ${params.amount} sats for article ${params.articleId}`), parsedPurchase }
|
|
}
|
|
|
|
function buildPurchaseNoteTags(params: {
|
|
articleId: string
|
|
authorPubkey: string
|
|
payerPubkey: string
|
|
amount: number
|
|
paymentHash: string
|
|
zapReceiptId?: string
|
|
category: PaymentCategoryTag
|
|
seriesId?: string
|
|
hashId: string
|
|
}): string[][] {
|
|
return buildTags({
|
|
type: 'payment',
|
|
category: params.category,
|
|
id: params.hashId,
|
|
service: PLATFORM_SERVICE,
|
|
version: 0,
|
|
hidden: false,
|
|
payment: true,
|
|
paymentType: 'purchase',
|
|
amount: params.amount,
|
|
payerPubkey: params.payerPubkey,
|
|
recipientPubkey: params.authorPubkey,
|
|
paymentHash: params.paymentHash,
|
|
articleId: params.articleId,
|
|
...(params.zapReceiptId ? { zapReceiptId: params.zapReceiptId } : {}),
|
|
...(params.seriesId ? { seriesId: params.seriesId } : {}),
|
|
})
|
|
}
|
|
|
|
function buildParsedPurchase(params: {
|
|
articleId: string
|
|
authorPubkey: string
|
|
payerPubkey: string
|
|
amount: number
|
|
paymentHash: string
|
|
id: string
|
|
hashId: string
|
|
seriesId?: string
|
|
}): Purchase {
|
|
return {
|
|
id: params.id,
|
|
hash: params.hashId,
|
|
version: 0,
|
|
index: 0,
|
|
payerPubkey: params.payerPubkey,
|
|
articleId: params.articleId,
|
|
authorPubkey: params.authorPubkey,
|
|
amount: params.amount,
|
|
paymentHash: params.paymentHash,
|
|
createdAt: Math.floor(Date.now() / 1000),
|
|
...(params.seriesId ? { seriesId: params.seriesId } : {}),
|
|
kindType: 'purchase',
|
|
}
|
|
}
|