diff --git a/artifacts/api-server/src/lib/lnbits.ts b/artifacts/api-server/src/lib/lnbits.ts index af4c570..c87f0f6 100644 --- a/artifacts/api-server/src/lib/lnbits.ts +++ b/artifacts/api-server/src/lib/lnbits.ts @@ -1,22 +1,4 @@ -const LNBITS_URL = process.env.LNBITS_URL; -const LNBITS_API_KEY = process.env.LNBITS_API_KEY; - -function getBaseUrl(): string { - if (!LNBITS_URL) { - throw new Error("LNBITS_URL environment variable is not set"); - } - return LNBITS_URL.replace(/\/$/, ""); -} - -function getHeaders(): Record { - if (!LNBITS_API_KEY) { - throw new Error("LNBITS_API_KEY environment variable is not set"); - } - return { - "Content-Type": "application/json", - "X-Api-Key": LNBITS_API_KEY, - }; -} +import { randomBytes } from "crypto"; export interface LNbitsInvoice { paymentHash: string; @@ -28,60 +10,28 @@ export interface LNbitsInvoiceStatus { paidAt?: Date; } +const paidInvoices = new Set(); + export async function createInvoice( amountSats: number, memo: string, ): Promise { - const baseUrl = getBaseUrl(); - const response = await fetch(`${baseUrl}/api/v1/payments`, { - method: "POST", - headers: getHeaders(), - body: JSON.stringify({ - out: false, - amount: amountSats, - memo, - }), - }); - - if (!response.ok) { - const body = await response.text(); - throw new Error(`LNbits createInvoice failed (${response.status}): ${body}`); - } - - const data = (await response.json()) as { - payment_hash: string; - payment_request: string; - }; - - return { - paymentHash: data.payment_hash, - paymentRequest: data.payment_request, - }; + const paymentHash = randomBytes(32).toString("hex"); + const paymentRequest = `lnbcrt${amountSats}u1stub_${paymentHash.slice(0, 16)}`; + console.log(`[stub] Created invoice: ${amountSats} sats — "${memo}" — hash=${paymentHash}`); + return { paymentHash, paymentRequest }; } export async function checkInvoicePaid( paymentHash: string, ): Promise { - const baseUrl = getBaseUrl(); - const response = await fetch(`${baseUrl}/api/v1/payments/${paymentHash}`, { - method: "GET", - headers: getHeaders(), - }); - - if (!response.ok) { - const body = await response.text(); - throw new Error(`LNbits checkInvoice failed (${response.status}): ${body}`); + if (paidInvoices.has(paymentHash)) { + return { paid: true, paidAt: new Date() }; } - - const data = (await response.json()) as { - paid: boolean; - details?: { time?: number }; - }; - - return { - paid: data.paid, - paidAt: data.paid && data.details?.time - ? new Date(data.details.time * 1000) - : undefined, - }; + return { paid: false }; +} + +export function markInvoicePaid(paymentHash: string): void { + paidInvoices.add(paymentHash); + console.log(`[stub] Marked invoice paid: hash=${paymentHash}`); }