// Capture a self-custody peer's coords (serverKey/repoKey/firehoseKey) by DID, while it is ONLINE —
// mirrors PeerRouter._identify (corestore.replicate + rpc/v1 get-coords). Run with node:
// node scripts/capture-coords.mjs did:plc:...
import Hyperswarm from 'hyperswarm'
import c from 'compact-encoding'
import ProtomuxRPC from 'protomux-rpc'
import { mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { openCorestore } from 'atproto-store/lib/corestore.js'
import { discoveryTopic } from 'atproto-identity/lib/discovery.js'
const did = process.argv[2]
if (!did) { console.log('usage: node capture-coords.mjs <did>'); process.exit(1) }
const dir = mkdtempSync(join(tmpdir(), 'cc-'))
const store = openCorestore(dir)
const swarm = new Hyperswarm()
const done = new Promise((res, rej) => {
const to = setTimeout(() => rej(new Error('timeout — peer offline or unreachable')), 25000)
swarm.on('connection', (conn) => {
try { store.replicate(conn) } catch {}
const rpc = new ProtomuxRPC(conn, { protocol: 'hyper-proto-peer/rpc/v1', valueEncoding: c.json })
rpc.request('get-coords', {}).then((coords) => { if (coords && coords.did === did) { clearTimeout(to); res(coords) } }).catch(() => {})
})
})
swarm.join(discoveryTopic(did), { server: true, client: true })
await swarm.flush()
try { const coords = await done; console.log('COORDS ' + JSON.stringify(coords)) } catch (e) { console.log('FAIL ' + e.message) }
await swarm.destroy(); await store.close(); rmSync(dir, { recursive: true, force: true }); process.exit(0)