// Live integration: read wj62's repo over the REAL swarm through the DidPeer module — a genuine
// P2P read (not the keystone spike's HTTP stand-in). Proves the extracted module works standalone.
// The CONSUMER owns swarm + discovery + verifyKey (as favela will); the module is the reader.
import { mkdtempSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import Hyperswarm from 'hyperswarm'
import { DidPeer } from '../index.js'
// Create the Corestore via atproto-store's OWN copy (openCorestore) so RepoStore's instanceof-Corestore
// check passes — a plain `new Corestore` from a separately-installed corestore is a different class.
import { openCorestore } from 'atproto-store/lib/corestore.js'
import { discoveryTopic } from 'atproto-identity/lib/discovery.js'
import { decodeDidKey } from 'atproto-identity/lib/did-key.js'
const DID = 'did:plc:wj62ebbusln6gxo5azo6ogzo'
const KNOWN_RKEY = '3mqkrr6bnh222' // a real wj62 post from this session
async function resolveVerifyKey (did) {
const res = await fetch(`https://plc.directory/${did}`)
if (!res.ok) throw new Error(`PLC ${res.status}`)
const doc = await res.json()
const vm = (doc.verificationMethod || []).find((m) => m.id && m.id.endsWith('#atproto')) || (doc.verificationMethod || [])[0]
return decodeDidKey(`did:key:${vm.publicKeyMultibase}`).pubkey
}
const verifyKey = await resolveVerifyKey(DID)
console.log('resolved wj62 verifyKey from PLC (mandatory auth) ✓')
const store = openCorestore(mkdtempSync(join(tmpdir(), 'didpeer-live-')))
const swarm = new Hyperswarm()
const peer = new DidPeer(store, { did: DID, verifyKey, onError: (e) => console.error(' [err]', e.stack || e.message) })
swarm.on('connection', (conn) => peer.attach(conn))
swarm.join(discoveryTopic(DID), { client: true, server: true })
await swarm.flush()
console.log('joined discoveryTopic(wj62); replicating over the swarm...')
const head = await peer.ready({ timeout: 45000 })
if (!head) { console.log('FAIL: no verified head (no peer reachable / replication stalled)'); await swarm.destroy(); process.exit(1) }
console.log('VERIFIED HEAD:', { rev: head.rev, cid: head.cid.slice(0, 24) + '…' })
console.log('collections:', await peer.collections())
const rec = await peer.getRecord('app.bsky.feed.post', KNOWN_RKEY)
console.log('getRecord ->', JSON.stringify(rec?.value?.text || rec).slice(0, 66))
const list = (await peer.listRecords('app.bsky.feed.post')).records
console.log('listRecords ->', list.length, 'posts')
const { carBytes } = await peer.getRepo()
console.log('getRepo ->', carBytes.length, 'CAR bytes (keyless, DID-key-verified)')
console.log('\nMODULE OK: atproto-did-peer read wj62 over genuine P2P (announce + replicate + read-view).')
await swarm.destroy(); await peer.close(); await store.close()