148 lines
7.6 KiB
JavaScript
148 lines
7.6 KiB
JavaScript
import { randomUUID, randomBytes } from 'node:crypto'
|
|
|
|
const METHODS = new Set([
|
|
'content.payment-status', 'content.purchase', 'content.cancel-purchase',
|
|
...['attempt', 'create', 'pay', 'recover', 'cancel', 'download', 'retry-native'].map(s => `content.invoice-${s}`),
|
|
...['attempt', 'create', 'prepare', 'pay', 'expose', 'recover', 'cancel', 'download'].map(s => `content.onchain-${s}`),
|
|
])
|
|
const hex = () => randomBytes(32).toString('hex')
|
|
|
|
/** Mock-only payment journal. All state and balances belong to one visitor.
|
|
* Never contacts a wallet, mint, seller, or Bitcoin network. */
|
|
export function contentPaymentRpc(method, params, { state, wallet, entry, owned, now = Date.now }) {
|
|
if (!METHODS.has(method)) return undefined
|
|
const p = params || {}
|
|
// The real cancellation RPC identifies the saved operation, not a file.
|
|
if (method === 'content.cancel-purchase') {
|
|
const saved = Object.entries(state.contentPayments || {}).find(([key, account]) =>
|
|
JSON.parse(key)[0] === p.onion && account.cashu?.operation_id === p.operation_id)
|
|
if (!saved) throw Error('Demo purchase operation not found')
|
|
const op = saved[1].cashu
|
|
if (op.state === 'confirmation_required') op.state = 'cancelled_unspent'
|
|
return { state: op.state, operation_id: op.operation_id }
|
|
}
|
|
if (!entry || !p.onion) throw Error('Demo file not found')
|
|
const key = JSON.stringify([p.onion, entry.id])
|
|
const journal = state.contentPayments ||= {}
|
|
const account = journal[key] ||= { cashu: null, invoice: null, onchain: null }
|
|
const price = entry.access?.paid?.price_sats || 0
|
|
const purchase = () => owned.find(o => o.onion === p.onion && o.content_id === entry.id)
|
|
const delivered = () => {
|
|
if (!purchase()) owned.push({ onion: p.onion, content_id: entry.id, filename: entry.filename,
|
|
mime_type: entry.mime_type, size_bytes: entry.size_bytes, paid_sats: price,
|
|
purchased_at: new Date(now()).toISOString() })
|
|
return { state: 'delivered', owned: true, owned_content_id: entry.id, mime_type: entry.mime_type }
|
|
}
|
|
const debit = (rail, amount, operation) => {
|
|
if (operation.debited || purchase()) return
|
|
if (wallet[rail] < amount) throw Error('Insufficient demo balance')
|
|
wallet[rail] -= amount
|
|
operation.debited = true
|
|
state.contentPaymentHistory ||= []
|
|
state.contentPaymentHistory.push({ operation_id: operation.operation_id, rail, amount_sats: amount,
|
|
content_id: entry.id, filename: entry.filename, timestamp: new Date(now()).toISOString() })
|
|
}
|
|
const original = (operation) => {
|
|
if (!operation || p.operation_id && p.operation_id !== operation.operation_id) throw Error('Demo purchase operation not found')
|
|
return operation
|
|
}
|
|
|
|
if (method === 'content.payment-status') return { attempts: account.cashu ? [account.cashu] : [] }
|
|
if (method === 'content.purchase') {
|
|
if (purchase()) return delivered()
|
|
let op = account.cashu
|
|
if (!op || op.state === 'cancelled_unspent') {
|
|
if (p.consent) throw Error('Demo quote is no longer active')
|
|
op = account.cashu = { operation_id: randomUUID(), state: 'confirmation_required', network: 'testnet',
|
|
mint_url: 'https://mint.demo.archipelago.invalid', envelope_sha256: hex(), gross_token_sats: price,
|
|
seller_net_sats: price, wallet_debit_sats: price, expires_at: Math.floor(now() / 1000) + 3600 }
|
|
}
|
|
if (!p.consent) return op
|
|
const c = p.consent
|
|
if (c.operation_id !== op.operation_id || c.envelope_sha256 !== op.envelope_sha256 || c.wallet_debit_sats !== op.wallet_debit_sats || p.max_wallet_debit < price) throw Error('Demo quote confirmation does not match')
|
|
debit('ecash_sats', price, op)
|
|
op.state = 'delivered'
|
|
return { ...op, ...delivered() }
|
|
}
|
|
|
|
if (method.startsWith('content.invoice-')) {
|
|
let op = account.invoice
|
|
const lifecycle = () => ({ ...op, state: op.state === 'canceled_unpaid' ? 'canceled' : op.state === 'issued' ? 'open' : op.state })
|
|
const settle = () => {
|
|
if (op && op.state === 'issued' && op.external_exposure && now() - op.exposed_at >= 2000) {
|
|
op.state = 'settled'; op.paid = true
|
|
}
|
|
}
|
|
settle()
|
|
if (method === 'content.invoice-attempt') return { attempt: !op || op.state === 'canceled_unpaid' ? null : {
|
|
...op, status: { payment_hash: op.payment_hash, bolt11: op.bolt11, state: op.state, can_switch_method: false },
|
|
native_succeeded: op.paid && !op.external_exposure,
|
|
} }
|
|
if (method === 'content.invoice-create' || method === 'content.invoice-retry-native') {
|
|
if (!op || op.state === 'canceled_unpaid') {
|
|
op = account.invoice = { operation_id: randomUUID(), price_sats: price, payment_hash: hex(),
|
|
bolt11: `lntb${price}n1demo${hex()}`, external_exposure: p.external_exposure === true,
|
|
exposed_at: now(), state: 'issued', paid: false, can_switch_method: false }
|
|
}
|
|
return op
|
|
}
|
|
original(op)
|
|
if (p.external_exposure && !op.external_exposure) { op.external_exposure = true; op.exposed_at = now() }
|
|
if (method === 'content.invoice-cancel') {
|
|
if (!op.paid) { op.state = 'canceled_unpaid'; op.can_switch_method = true }
|
|
return lifecycle()
|
|
}
|
|
if (method === 'content.invoice-pay') {
|
|
if (op.state === 'canceled_unpaid') throw Error('Demo invoice was cancelled')
|
|
debit('channel_sats', price, op)
|
|
op.state = 'settled'; op.paid = true
|
|
return { status: 'succeeded', payment_hash: op.payment_hash }
|
|
}
|
|
if (method === 'content.invoice-download') {
|
|
if (!op.paid) throw Error('Demo invoice is not settled')
|
|
return delivered()
|
|
}
|
|
return lifecycle()
|
|
}
|
|
|
|
let op = account.onchain
|
|
if (op?.external_exposure && !op.paid && now() - op.exposed_at >= 2000) { op.paid = true; op.phase = 'published'; op.txid = hex() }
|
|
if (method === 'content.onchain-attempt') return { attempt: op?.retired_unallocated ? null : op }
|
|
if (method === 'content.onchain-create' || method === 'content.onchain-expose') {
|
|
if (price < 546) throw Error('This demo file accepts Lightning and Cashu')
|
|
if (!op || op.retired_unallocated) op = account.onchain = {
|
|
operation_id: randomUUID(), price_sats: price, phase: 'offer_prepared', network: 'signet',
|
|
external_exposure: false, address: null, fee_sats: null, max_fee_sats: null,
|
|
template_sha256: null, plan_sha256: null, txid: null, paid: false,
|
|
change_allocation_ambiguous: false, can_switch_method: false, retired_unallocated: false,
|
|
}
|
|
if (method === 'content.onchain-expose' && !op.external_exposure) {
|
|
op.external_exposure = true; op.exposed_at = now(); op.phase = 'quoted'
|
|
// Display-only demo destination; never sent to a network.
|
|
op.address = 'tb1qdemopayment0000000000000000000000000000000'
|
|
}
|
|
return op
|
|
}
|
|
original(op)
|
|
if (method === 'content.onchain-cancel') {
|
|
if (!op.external_exposure && !op.paid) { op.retired_unallocated = true; op.can_switch_method = true }
|
|
return op
|
|
}
|
|
if (method === 'content.onchain-prepare') {
|
|
if (!Number.isSafeInteger(p.max_fee_sats) || p.max_fee_sats <= 0) throw Error('Enter a positive demo fee limit')
|
|
if (!op.plan_sha256) Object.assign(op, { phase: 'plan_prepared', plan_sha256: hex(), fee_sats: Math.min(25, p.max_fee_sats), max_fee_sats: p.max_fee_sats })
|
|
return op
|
|
}
|
|
if (method === 'content.onchain-pay') {
|
|
if (op.retired_unallocated || !op.plan_sha256 || p.plan_sha256 !== op.plan_sha256) throw Error('Review the demo funding plan first')
|
|
debit('onchain_sats', price + op.fee_sats, op)
|
|
op.paid = true; op.phase = 'published'; op.txid ||= hex()
|
|
return op
|
|
}
|
|
if (method === 'content.onchain-download') {
|
|
if (!op.paid) throw Error('Demo Bitcoin payment is not settled')
|
|
return delivered()
|
|
}
|
|
return op
|
|
}
|