OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-proto-peer / test / peer-authors.test.js

51 lines · javascript ref:
// Model B proof: a peer HOLDS its own key and authors + SIGNS its own atproto repo,
// with no server custody. The commit verifies against the peer's own did:key — the
// litmus for the peer/bridge split (custody = key possession).

import test from 'brittle'
import { mkdtempSync, rmSync } from 'bare-fs'
import { tmpdir } from 'bare-os'
import { join } from 'bare-path'
import { Peer } from '../lib/peer.js'
import { verifyCommit } from 'atproto-repo/lib/commit.js'
import { readCar, loadCommit, listRecords } from 'atproto-repo/lib/repo.js'
import { decodeDidKey } from 'atproto-identity/lib/did-key.js'

const MNEMONIC = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'

test('a peer authors + signs its own repo with a self-held key (Model B custody)', async (t) => {
  const dir = mkdtempSync(join(tmpdir(), 'hyper-proto-peer-'))
  try {
    const peer = await Peer.fromMnemonic({ storage: dir, mnemonic: MNEMONIC, handle: 'alice.7rnx.net', pdsUrl: 'https://pds.7rnx.net' })

    // custody: the peer holds its OWN 32-byte signing key + a publishable did:plc genesis
    t.is(peer.privateKey.length, 32, 'peer holds its own 32-byte signing key')
    t.ok(peer.did.startsWith('did:plc:'), 'peer minted a did:plc from its own key')
    t.ok(peer.signedOp && peer.signedOp.sig, 'peer holds a signed genesis op to publish to PLC')

    const w = await peer.write({
      collection: 'app.bsky.feed.post',
      rkey: '3aaa',
      record: { $type: 'app.bsky.feed.post', text: 'hello from a self-custody peer', createdAt: '2026-07-03T00:00:00.000Z' }
    })

    // the commit is signed by the peer's OWN key and verifies against its did:key
    const { pubkey, curve } = decodeDidKey(peer.didKey)
    t.is(curve, 'secp256k1', 'did:key is secp256k1')
    t.ok(verifyCommit(w.signedCommit, pubkey), "commit verifies against the peer's own did:key")
    t.is(w.signedCommit.did, peer.did, "commit authored under the peer's DID")

    // the peer serves its repo as a byte-exact atproto CAR (what a bridge would fetch + relay)
    const { carBytes } = await peer.getRepo()
    const car = await readCar(carBytes)
    const commitInCar = loadCommit(car).value
    t.ok(verifyCommit(commitInCar, pubkey), 'CAR commit verifies against the same self-held key')
    const recs = await listRecords(car)
    t.ok(recs.some(r => r.value.text === 'hello from a self-custody peer'), 'authored record is present in the served CAR')

    await peer.close()
  } finally {
    rmSync(dir, { recursive: true, force: true })
  }
})