| … | … | @@ -0,0 +1,42 @@ |
| 1 | // getRepoFromCommit: assemble a full repo CAR KEYLESSLY from the store's records + a PROVIDED |
| 2 | // signed commit (as a bridge does with a commit read from a peer's replicated firehose). The |
| 3 | // MST/records rebuild without the key; the signature travels in the commit. Includes the |
| 4 | // integrity guard (a commit that doesn't match the records is refused). |
| 5 | |
| 6 | import test from 'brittle' |
| 7 | import { mkdtempSync, rmSync } from 'node:fs' |
| 8 | import { tmpdir } from 'node:os' |
| 9 | import { join } from 'node:path' |
| 10 | import { openCorestore } from '../lib/corestore.js' |
| 11 | import { RepoStore } from '../lib/repo-store.js' |
| 12 | import { RepoManager } from '../lib/repo-manager.js' |
| 13 | import { getRepoFromCommit } from '../lib/read-view.js' |
| 14 | import { readCar, loadCommit, listRecords } from 'atproto-repo/lib/repo.js' |
| 15 | import { cidForBytesSync } from 'atproto-repo/lib/cid.js' |
| 16 | |
| 17 | const PRIV = new Uint8Array(32).fill(7) |
| 18 | const NOW = '2026-07-03T00:00:00.000Z' |
| 19 | |
| 20 | test('getRepoFromCommit assembles a byte-exact CAR keylessly from a provided signed commit', async (t) => { |
| 21 | const dir = mkdtempSync(join(tmpdir(), 'store-keyless-')) |
| 22 | const store = openCorestore(dir) |
| 23 | const repo = await new RepoStore(store, { name: 'atproto-repo' }).ready() |
| 24 | let micros = 1_700_000_000_000_000n |
| 25 | const mgr = await new RepoManager(repo, { did: 'did:plc:x', privateKey: PRIV, clock: () => (micros += 1_000_000n) }).init() |
| 26 | const w = await mgr.applyWrite({ action: 'create', collection: 'app.bsky.feed.post', rkey: '3a', record: { $type: 'app.bsky.feed.post', text: 'hi', createdAt: NOW } }) |
| 27 | |
| 28 | t.teardown(async () => { await repo.close(); await store.close(); rmSync(dir, { recursive: true, force: true }) }) |
| 29 | |
| 30 | // KEYLESS: no privateKey — the signed commit is provided (as read from a firehose). |
| 31 | const { carBytes, commitCid: cc } = await getRepoFromCommit(repo, w.signedCommit) |
| 32 | t.ok(cc.equals(w.commitCid), 'keyless CAR is rooted at the provided signed commit') |
| 33 | const car = await readCar(carBytes) |
| 34 | t.ok(loadCommit(car).value.sig, 'CAR carries the signed commit (signature present, not re-derived)') |
| 35 | const recs = await listRecords(car) |
| 36 | t.ok(recs.some(r => r.value.text === 'hi'), 'records present in the keyless CAR') |
| 37 | |
| 38 | // integrity guard: a commit that does not commit to these records is refused |
| 39 | const bogus = cidForBytesSync(new Uint8Array([1, 2, 3])) |
| 40 | await t.exception(getRepoFromCommit(repo, { ...w.signedCommit, data: bogus }), |
| 41 | 'refuses a signed commit whose data CID != the rebuilt MST root') |
| 42 | }) |