#!/usr/bin/env node // Canary watcher: anyone can run it. Polls the canary address and its token accounts; if the canary ever SIGNS a // transaction (only possible for someone who broke Ed25519), it posts to a Discord or Slack webhook and prints the signature. // // node watch.mjs [--rpc https://api.mainnet-beta.solana.com] [--webhook https://discord.com/api/webhooks/...] [--every 30] // // Node 18+, no packages. Remembers what it already checked in ./canary-watch.json. import fs from 'node:fs'; const args = process.argv.slice(2); const opt = (k, d) => { const i = args.indexOf('--' + k); return i >= 0 ? args[i + 1] : d; }; const ADDR = args.find((a) => !a.startsWith('--') && !args[args.indexOf(a) - 1]?.startsWith('--')); if (!ADDR) { console.error('usage: node watch.mjs [--rpc URL] [--webhook URL] [--every SECONDS]'); process.exit(1); } const RPC = opt('rpc', 'https://api.mainnet-beta.solana.com'), HOOK = opt('webhook', ''), EVERY = Number(opt('every', 30)) * 1000; const STATE = './canary-watch.json'; let seen = {}; try { seen = JSON.parse(fs.readFileSync(STATE, 'utf8')); } catch { } async function rpc(method, params) { const r = await fetch(RPC, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ jsonrpc: '2.0', id: 1, method, params }) }); const j = await r.json(); if (j.error) throw new Error(j.error.message); return j.result; } async function alert(text) { console.log(new Date().toISOString(), 'ALERT', text); if (!HOOK) return; const body = HOOK.includes('slack') ? { text } : { content: text }; await fetch(HOOK, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) }).catch((e) => console.error('webhook failed', e.message)); } async function round() { const accts = [ADDR]; for (const prog of ['TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA', 'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb']) { const r = await rpc('getTokenAccountsByOwner', [ADDR, { programId: prog }, { encoding: 'jsonParsed' }]); for (const a of r.value) accts.push(a.pubkey); } for (const a of accts) { for (const s of await rpc('getSignaturesForAddress', [a, { limit: 25 }])) { if (s.signature in seen) continue; const tx = await rpc('getTransaction', [s.signature, { encoding: 'jsonParsed', maxSupportedTransactionVersion: 0 }]); const signed = !!tx?.transaction?.message?.accountKeys?.some((k) => k.pubkey === ADDR && k.signer); seen[s.signature] = signed; if (signed) await alert(`The canary ${ADDR} signed a transaction: ${s.signature}. Ed25519 may be broken.`); } } fs.writeFileSync(STATE, JSON.stringify(seen)); console.log(new Date().toISOString(), `checked ${accts.length} account(s), ${Object.keys(seen).length} transactions seen, canary has never signed: ${!Object.values(seen).some(Boolean)}`); } for (; ;) { await round().catch((e) => console.error('round failed:', e.message)); await new Promise((r) => setTimeout(r, EVERY)); }