#!/usr/bin/env node /** * BTC L1 settlement E2E (regtest or simulated confirmed deposit). * * Modes: * --simulate Skip bitcoind; post a synthetic confirmed deposit to local settlement * (default) Prefer bitcoind RPC if BITCOIND_RPC_URL reachable * * Prerequisites: settlement-middleware on SETTLEMENT_MIDDLEWARE_URL (default :3011) * live BTC/USD via TOKEN_AGGREGATION_URL */ import { randomUUID } from 'crypto'; const settleBase = (process.env.SETTLEMENT_MIDDLEWARE_URL || 'http://127.0.0.1:3011').replace(/\/$/, ''); const apiKey = process.env.OMNL_API_KEY || ''; const simulate = process.argv.includes('--simulate'); const amountSats = Number(process.env.BTC_E2E_AMOUNT_SATS || 100_000); // 0.001 BTC const recipient = process.env.BTC_E2E_RECIPIENT || process.env.ALI_GENESIS_ADDRESS || '0xa55A4B57A91561e9df5a883D4883Bd4b1a7C4882'; const headers = { 'Content-Type': 'application/json', Accept: 'application/json', ...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}), }; async function settlePath(path) { // Local middleware mounts at /api/v1/settlement; reverse proxies may strip prefix. const candidates = [ `${settleBase}/api/v1/settlement${path}`, `${settleBase}${path}`, ]; let lastErr; for (const url of candidates) { try { const res = await fetch(url, { headers }); if (res.status !== 404) return { url, res }; } catch (e) { lastErr = e; } } throw lastErr || new Error(`No settlement route for ${path}`); } async function postSettle(body) { const candidates = [ `${settleBase}/api/v1/settlement/btc/l1-settle`, `${settleBase}/btc/l1-settle`, ]; let last; for (const url of candidates) { const res = await fetch(url, { method: 'POST', headers, body: JSON.stringify(body) }); const text = await res.text(); let json; try { json = JSON.parse(text); } catch { json = { raw: text }; } last = { url, status: res.status, json }; if (res.status !== 404) return last; } return last; } async function main() { const health = await settlePath('/health'); const healthBody = await health.res.json(); console.log('health', health.url, healthBody.status || healthBody); const instructionId = randomUUID(); const txId = simulate ? `sim-${randomUUID().replace(/-/g, '').slice(0, 32)}` : process.env.BTC_E2E_TXID || `regtest-${Date.now().toString(16)}`; const depositAddress = process.env.BTC_E2E_DEPOSIT_ADDRESS || `bcrt1q${instructionId.replace(/-/g, '').slice(0, 38)}`; const body = { idempotencyKey: `btc-e2e-${txId}`, instructionId, txId, depositAddress, amountSats, confirmations: Number(process.env.BTC_CONFIRMATIONS_REQUIRED || 6), recipientAddress: recipient, officeId: 24, }; console.log('posting', { amountSats, recipient, simulate, txId }); const result = await postSettle(body); console.log(JSON.stringify(result, null, 2)); const ledger = await settlePath('/btc/ledger'); if (ledger.res.ok) { console.log('ledger', await ledger.res.json()); } else { console.log('ledger_status', ledger.res.status); } if (result.status >= 400 && result.json?.phase !== 'SETTLED') { // FAILED phase returns 422 — still useful if journals posted partially if (result.json?.phase === 'FAILED') { console.error('Settlement FAILED:', result.json.errors); process.exit(2); } process.exit(1); } if (result.json?.phase && result.json.phase !== 'SETTLED') { console.error('Unexpected phase', result.json.phase); process.exit(2); } console.log('E2E_OK', result.json?.settlementId, result.json?.phase); } main().catch((e) => { console.error(e); process.exit(1); });