story-research-zapwall/lib/articleInvoice.ts
2025-12-23 02:20:57 +01:00

72 lines
1.7 KiB
TypeScript

import { getAlbyService } from './alby'
import type { AlbyInvoice } from '@/types/alby'
import type { ArticleDraft } from './articlePublisher'
/**
* Create Lightning invoice for article
* Requires Alby/WebLN to be available and enabled
*/
export async function createArticleInvoice(draft: ArticleDraft): Promise<AlbyInvoice> {
const alby = getAlbyService()
await alby.enable() // Request permission
const invoice = await alby.createInvoice({
amount: draft.zapAmount,
description: `Payment for article: ${draft.title}`,
expiry: 86400, // 24 hours
})
return invoice
}
/**
* Create preview event with invoice tags
*/
export function createPreviewEvent(
draft: ArticleDraft,
invoice: AlbyInvoice,
authorPresentationId?: string,
extraTags: string[][] = []
): {
kind: 1
created_at: number
tags: string[][]
content: string
} {
const tags = buildPreviewTags(draft, invoice, authorPresentationId, extraTags)
return {
kind: 1 as const,
created_at: Math.floor(Date.now() / 1000),
tags,
content: draft.preview,
}
}
function buildPreviewTags(
draft: ArticleDraft,
invoice: AlbyInvoice,
authorPresentationId?: string,
extraTags: string[][] = []
): string[][] {
const base: string[][] = [
['title', draft.title],
['preview', draft.preview],
['zap', draft.zapAmount.toString()],
['content-type', 'article'],
['invoice', invoice.invoice],
['payment_hash', invoice.paymentHash],
]
if (draft.category) {
base.push(['category', draft.category])
}
if (authorPresentationId) {
base.push(['author_presentation_id', authorPresentationId])
}
// Preserve any kind_type tags in extraTags if provided by caller
if (extraTags.length > 0) {
base.push(...extraTags)
}
return base
}