|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Persona address rotation watcher. |
| 4 | + * |
| 5 | + * Personas in `src/demo/personas.ts` point at real public wallets that |
| 6 | + * may drift over time — a "stable-saver" cell could exit all USDC |
| 7 | + * positions tomorrow, leaving the persona description false. This |
| 8 | + * script does cheap liveness checks against public RPC / public chain |
| 9 | + * APIs and prints a markdown report flagging cells that look dead or |
| 10 | + * drifted. Designed for `workflow_dispatch` (manual + weekly cron) so |
| 11 | + * a human reviews the report and refreshes the matrix when needed. |
| 12 | + * |
| 13 | + * Scope is intentionally minimal: we verify the address still exists |
| 14 | + * and has a non-zero native balance for the chain. We do NOT try to |
| 15 | + * verify per-flow rehearsability (e.g. "does this wallet actually |
| 16 | + * have an Aave V3 supply position?") — that would require per-protocol |
| 17 | + * SDK setup with API keys, which is out of scope for an out-of-tree |
| 18 | + * weekly watcher. Liveness is the cheap proxy: a wallet that's gone |
| 19 | + * from "exchange hot wallet" to zero balance has clearly rotated. |
| 20 | + * |
| 21 | + * Usage: |
| 22 | + * npm run build # produce dist/demo/personas.js |
| 23 | + * node scripts/verify-personas.mjs # prints report, exits 0 / 1 |
| 24 | + */ |
| 25 | +import { DEMO_WALLETS } from "../dist/demo/personas.js"; |
| 26 | + |
| 27 | +const TIMEOUT_MS = 15_000; |
| 28 | + |
| 29 | +async function fetchWithTimeout(url, init) { |
| 30 | + const controller = new AbortController(); |
| 31 | + const timer = setTimeout(() => controller.abort(), TIMEOUT_MS); |
| 32 | + try { |
| 33 | + return await fetch(url, { ...init, signal: controller.signal }); |
| 34 | + } finally { |
| 35 | + clearTimeout(timer); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +async function evmNativeBalance(address) { |
| 40 | + const res = await fetchWithTimeout("https://ethereum-rpc.publicnode.com", { |
| 41 | + method: "POST", |
| 42 | + headers: { "content-type": "application/json" }, |
| 43 | + body: JSON.stringify({ |
| 44 | + jsonrpc: "2.0", |
| 45 | + id: 1, |
| 46 | + method: "eth_getBalance", |
| 47 | + params: [address, "latest"], |
| 48 | + }), |
| 49 | + }); |
| 50 | + if (!res.ok) throw new Error(`HTTP ${res.status}`); |
| 51 | + const json = await res.json(); |
| 52 | + if (json.error) throw new Error(json.error.message); |
| 53 | + return BigInt(json.result); |
| 54 | +} |
| 55 | + |
| 56 | +async function solanaNativeBalance(address) { |
| 57 | + const res = await fetchWithTimeout("https://api.mainnet-beta.solana.com", { |
| 58 | + method: "POST", |
| 59 | + headers: { "content-type": "application/json" }, |
| 60 | + body: JSON.stringify({ |
| 61 | + jsonrpc: "2.0", |
| 62 | + id: 1, |
| 63 | + method: "getBalance", |
| 64 | + params: [address], |
| 65 | + }), |
| 66 | + }); |
| 67 | + if (!res.ok) throw new Error(`HTTP ${res.status}`); |
| 68 | + const json = await res.json(); |
| 69 | + if (json.error) throw new Error(json.error.message); |
| 70 | + return BigInt(json.result.value); |
| 71 | +} |
| 72 | + |
| 73 | +async function tronNativeBalance(address) { |
| 74 | + const res = await fetchWithTimeout( |
| 75 | + `https://api.trongrid.io/v1/accounts/${address}`, |
| 76 | + ); |
| 77 | + if (!res.ok) throw new Error(`HTTP ${res.status}`); |
| 78 | + const json = await res.json(); |
| 79 | + const account = json.data?.[0]; |
| 80 | + if (!account) return 0n; |
| 81 | + return BigInt(account.balance ?? 0); |
| 82 | +} |
| 83 | + |
| 84 | +async function btcAddressTxCount(address) { |
| 85 | + const res = await fetchWithTimeout( |
| 86 | + `https://mempool.space/api/address/${address}`, |
| 87 | + ); |
| 88 | + if (!res.ok) throw new Error(`HTTP ${res.status}`); |
| 89 | + const json = await res.json(); |
| 90 | + return (json.chain_stats?.tx_count ?? 0) + (json.mempool_stats?.tx_count ?? 0); |
| 91 | +} |
| 92 | + |
| 93 | +function isTransientErr(err) { |
| 94 | + const msg = err instanceof Error ? err.message : String(err); |
| 95 | + // Rate-limit / abort / network reset: not drift, just public-API noise. |
| 96 | + return /HTTP 429|HTTP 5\d\d|aborted|fetch failed|ECONN/i.test(msg); |
| 97 | +} |
| 98 | + |
| 99 | +async function checkCell(chain, type, cell) { |
| 100 | + try { |
| 101 | + if (chain === "evm") { |
| 102 | + const wei = await evmNativeBalance(cell.address); |
| 103 | + return { |
| 104 | + status: wei > 0n ? "alive" : "drifted", |
| 105 | + detail: `${(Number(wei) / 1e18).toFixed(4)} ETH`, |
| 106 | + }; |
| 107 | + } |
| 108 | + if (chain === "solana") { |
| 109 | + const lamports = await solanaNativeBalance(cell.address); |
| 110 | + return { |
| 111 | + status: lamports > 0n ? "alive" : "drifted", |
| 112 | + detail: `${(Number(lamports) / 1e9).toFixed(4)} SOL`, |
| 113 | + }; |
| 114 | + } |
| 115 | + if (chain === "tron") { |
| 116 | + const sun = await tronNativeBalance(cell.address); |
| 117 | + return { |
| 118 | + status: sun > 0n ? "alive" : "drifted", |
| 119 | + detail: `${(Number(sun) / 1e6).toFixed(4)} TRX`, |
| 120 | + }; |
| 121 | + } |
| 122 | + if (chain === "bitcoin") { |
| 123 | + const txCount = await btcAddressTxCount(cell.address); |
| 124 | + return { |
| 125 | + status: txCount > 0 ? "alive" : "drifted", |
| 126 | + detail: `${txCount} txs (lifetime)`, |
| 127 | + }; |
| 128 | + } |
| 129 | + return { status: "drifted", detail: `unknown chain: ${chain}` }; |
| 130 | + } catch (err) { |
| 131 | + const msg = err instanceof Error ? err.message : String(err); |
| 132 | + return { |
| 133 | + status: isTransientErr(err) ? "inconclusive" : "drifted", |
| 134 | + detail: `error: ${msg}`, |
| 135 | + }; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +async function main() { |
| 140 | + const lines = []; |
| 141 | + lines.push("# Persona address rotation report"); |
| 142 | + lines.push(""); |
| 143 | + lines.push(`Generated: ${new Date().toISOString()}`); |
| 144 | + lines.push(""); |
| 145 | + lines.push("| Chain | Type | Address | Status | Detail |"); |
| 146 | + lines.push("|-------|------|---------|--------|--------|"); |
| 147 | + |
| 148 | + let drifted = 0; |
| 149 | + let inconclusive = 0; |
| 150 | + let total = 0; |
| 151 | + for (const [chain, byType] of Object.entries(DEMO_WALLETS)) { |
| 152 | + for (const [type, cell] of Object.entries(byType)) { |
| 153 | + if (!cell) continue; |
| 154 | + total++; |
| 155 | + const { status, detail } = await checkCell(chain, type, cell); |
| 156 | + const label = |
| 157 | + status === "alive" ? "✓ alive" : status === "inconclusive" ? "⚠ inconclusive" : "✗ drifted"; |
| 158 | + if (status === "drifted") drifted++; |
| 159 | + else if (status === "inconclusive") inconclusive++; |
| 160 | + const shortAddr = cell.address.slice(0, 8) + "…" + cell.address.slice(-4); |
| 161 | + lines.push(`| ${chain} | ${type} | \`${shortAddr}\` | ${label} | ${detail} |`); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + lines.push(""); |
| 166 | + const alive = total - drifted - inconclusive; |
| 167 | + lines.push( |
| 168 | + `**Summary:** ${alive} / ${total} cells alive, ${drifted} drifted, ${inconclusive} inconclusive (transient API errors — re-run later).`, |
| 169 | + ); |
| 170 | + if (drifted > 0) { |
| 171 | + lines.push(""); |
| 172 | + lines.push("Drifted cells need attention — refresh the wallet in `src/demo/personas.ts` and bump `verifiedAt`."); |
| 173 | + } |
| 174 | + |
| 175 | + const report = lines.join("\n"); |
| 176 | + console.log(report); |
| 177 | + // Inconclusive results don't fail the workflow — they're rate-limit |
| 178 | + // noise, not real drift. A genuine drift requires a confirmed |
| 179 | + // empty-balance / no-activity response. |
| 180 | + process.exit(drifted > 0 ? 1 : 0); |
| 181 | +} |
| 182 | + |
| 183 | +main().catch((err) => { |
| 184 | + console.error("verify-personas failed:", err); |
| 185 | + process.exit(2); |
| 186 | +}); |
0 commit comments