#!/usr/bin/env node
// Check on-chain owner / admin for every contract in agent/src/config.ts.

import { createPublicClient, http } from 'viem';
import { base } from 'viem/chains';

const client = createPublicClient({ chain: base, transport: http('https://mainnet.base.org') });

const ALL = {
  ERC8004IdentityRegistry: '0xa7BD20bf5De63df949cA5Be2F20835978eCba81A',
  AIPartnershipBondsV2: '0x01C479F0c039fEC40c0Cf1c5C921bab457d57441',
  AIAccountabilityBondsV2: '0x6750D28865434344e04e1D0a6044394b726C3dfE',
  FlourishingMetricsOracle: '0xf7B2dC73Ec69528F171F4a0ded62da557866BaA5',
  ProductionBeliefAttestationVerifier: '0x2a46B5932e9559176241056A04a608B8fEbCc5A7',
  ERC8004ReputationRegistry: '0x98afd1440B2238D73c1394720277a6d031fCbbD0',
  ERC8004ValidationRegistry: '0x8D3495772EAcAdB7dC09F1784eF06F2D109fb8a5',
  VaultfireERC8004Adapter: '0x8bEFe7411AecddA6E1Aea8F031fedD293Ca7372E',
  MultisigGovernance: '0x34a19C7298aCA59e185F1ecfB352AC566bB4B4A9',
  PrivacyGuarantees: '0x2711AA5f1934Fe97825F02e51cB5Da58d1759625',
  MissionEnforcement: '0xBc4208A7A59af3B7E438d3308C3dc0d299d739a7',
  AntiSurveillance: '0xaaC44F1396f3F5b3C06048a1F819DC9b0f86D90c',
  BeliefAttestationVerifier: '0x32211773C8266774A704aeD471f8eF073f34bA79',
  DilithiumAttestor: '0xef703bBe9b21960ede3e96abBB0Bbd0c525Fe050',
  VaultfireTeleporterBridge: '0x62edf0cBc8Fa3d98B6A07197DFE9cb1c128a7eC7',
};

const COMPROMISED = '0xA054f831B562e729F8D268291EBde1B2EDcFb84F';
const GHOSTKEY = '0xfA15Ee28939B222B0448261A22156070f0A7813C';
const sleep = (ms) => new Promise(r => setTimeout(r, ms));

const ownerAbi = [{ name: 'owner', type: 'function', stateMutability: 'view', inputs: [], outputs: [{ type: 'address' }] }];

let safe = 0, compromised = 0, immutable_ = 0;

for (const [name, addr] of Object.entries(ALL)) {
  let result = '';
  try {
    const owner = await client.readContract({ address: addr, abi: ownerAbi, functionName: 'owner' });
    if (owner.toLowerCase() === COMPROMISED.toLowerCase()) { result = '⚠️  COMPROMISED'; compromised++; }
    else if (owner.toLowerCase() === GHOSTKEY.toLowerCase()) { result = '✅ ghostkey'; safe++; }
    else { result = `?  unknown owner ${owner}`; }
  } catch (e) {
    result = '   immutable (no owner)';
    immutable_++;
  }
  console.log(`${name.padEnd(40)} ${addr}  ${result}`);
  await sleep(300);
}

console.log(`\n${safe} owned by ghostkey, ${compromised} owned by compromised, ${immutable_} immutable`);
