Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

5.1 KB
Raw Blame
/**
 * Regenerates src/lib/e2ee/deviceAck.vectors.json — the fixture every implementation of the device
 * acknowledgement (deviceAck.ts: CLI, backend, desktop and mobile Dart) is held to. Deterministic: fixed
 * seeds, Ed25519 signs deterministically. Run: `npx tsx scripts/gen-devack-vectors.ts`, then copy the
 * file to backend/src/lib/ and to desktop/test/viewer/ and mobile/test/viewer/.
 */
import { writeFileSync } from 'fs'
import { join } from 'path'
import { ed25519 } from '@noble/curves/ed25519'
import { lvCat } from '../src/lib/e2ee/core.js'
import { checkDevAck, devAckMessage, signDevAck, type DevAck, type UnsignedDevAck } from '../src/lib/e2ee/deviceAck.js'
import { applyDevLogEntries, emptyDevLogState, nextDevLogEntry, signDevLogEntry, type DevLogEntry, type DevLogState } from '../src/lib/e2ee/deviceLog.js'

const b64 = (u: Uint8Array): string => Buffer.from(u).toString('base64')
const seed = (n: number): Uint8Array => new Uint8Array(32).fill(n)
const keys = { box1: seed(1), box2: seed(2), browser: seed(3), stranger: seed(4) }
type Name = keyof typeof keys
const pub = (k: Name): string => b64(ed25519.getPublicKey(keys[k]))
const acct = 'acct-vectors-1'
const MID1 = 'a'.repeat(32)
const MID2 = 'b'.repeat(32)

// The log the acknowledgements are checked against: two machines and a browser, then box2 removed.
let state: DevLogState = emptyDevLogState(acct)
const log: DevLogEntry[] = []
function add(k: Name, kind: 'machine' | 'viewer', machineId: string, label: string, at: number): void {
  const e = signDevLogEntry(nextDevLogEntry(state, { op: 'add', pub: pub(k), kind, machineId, label, signer: pub(k) }, at), keys[k])
  state = applyDevLogEntries(state, [e]).state
  log.push(e)
}
add('box1', 'machine', MID1, 'box1', 1_000)
add('box2', 'machine', MID2, 'box2', 2_000)
add('browser', 'viewer', '', 'browser', 3_000)
{
  const m = state.active[pub('box2')]
  const e = signDevLogEntry(nextDevLogEntry(state, { op: 'remove', pub: m.pub, kind: m.kind, machineId: m.machineId, label: m.label, signer: pub('box1') }, 4_000), keys.box1)
  state = applyDevLogEntries(state, [e]).state
  log.push(e)
}

const unsigned = (by: Name, of: Name, addSeq: number, at = 5_000, account = acct): UnsignedDevAck =>
  ({ v: 1, acct: account, pub: pub(of), addSeq, signer: pub(by), at })
const ack = (by: Name, of: Name, addSeq: number, at?: number, account?: string): DevAck => signDevAck(unsigned(by, of, addSeq, at, account), keys[by])

const valid = [ack('box1', 'browser', 3), ack('browser', 'box1', 1, 6_000)]
for (const a of valid) if (!checkDevAck(state, a).ok) throw new Error('a valid acknowledgement was refused')

/** Each invalid case must be refused with `code`. */
const invalid: Array<{ name: string; ack: unknown; code: string }> = []
function bad(name: string, raw: unknown, code: string): void {
  const r = checkDevAck(state, raw)
  if (r.ok || r.code !== code) throw new Error(`${name}: expected ${code}, got ${r.ok ? 'ok' : r.code}`)
  invalid.push({ name, ack: raw, code })
}
bad('tampered time', { ...ack('box1', 'browser', 3), at: 5_001 }, 'BAD_SIGNATURE')
// The same fields signed under the device log's tag: a signature is only ever good for the one kind of
// record it was made for.
{
  const u = unsigned('box1', 'browser', 3)
  const wrongTag = lvCat('harness-devlog-v1', String(u.v), u.acct, Buffer.from(u.pub, 'base64'), String(u.addSeq), Buffer.from(u.signer, 'base64'), String(u.at))
  bad('signed under the device log tag', { ...u, sig: b64(ed25519.sign(wrongTag, keys.box1)) }, 'BAD_SIGNATURE')
}
bad('signed by another key than the signer', { ...unsigned('box1', 'browser', 3), sig: b64(ed25519.sign(devAckMessage(unsigned('box1', 'browser', 3)), keys.stranger)) }, 'BAD_SIGNATURE')
bad('a key acknowledging itself', ack('browser', 'browser', 3), 'SELF_ACK')
bad('signer not in the log', ack('stranger', 'browser', 3), 'SIGNER_NOT_ACTIVE')
bad('signer removed from the log', ack('box2', 'browser', 3), 'SIGNER_NOT_ACTIVE')
bad('wrong add entry', ack('box1', 'browser', 2), 'NOT_ADDED_THERE')
bad('key removed from the log', ack('box1', 'box2', 2), 'NOT_ADDED_THERE')
bad('key never in the log', ack('box1', 'stranger', 3), 'NOT_ADDED_THERE')
bad('another account', ack('box1', 'browser', 3, 5_000, 'acct-other'), 'WRONG_ACCOUNT')
bad('unknown version', { ...ack('box1', 'browser', 3), v: 2 }, 'BAD_ACK')
bad('add entry zero', ack('box1', 'browser', 0), 'BAD_ACK')
bad('key of the wrong length', { ...ack('box1', 'browser', 3), pub: b64(new Uint8Array(31)) }, 'BAD_ACK')
bad('not an object', 'ack', 'BAD_ACK')

const out = {
  comment: 'Generated by cli/scripts/gen-devack-vectors.ts. Keys are Ed25519 seeds of 32 identical bytes (box1=1, box2=2, browser=3, stranger=4). `message` is the hex of devAckMessage(valid[0]).',
  acct,
  seeds: Object.fromEntries(Object.entries(keys).map(([k, v]) => [k, b64(v)])),
  pubs: Object.fromEntries(Object.keys(keys).map((k) => [k, pub(k as Name)])),
  log,
  message: Buffer.from(devAckMessage(valid[0])).toString('hex'),
  valid,
  invalid,
}
writeFileSync(join(import.meta.dirname, '../src/lib/e2ee/deviceAck.vectors.json'), JSON.stringify(out, null, 2) + '\n')
console.log(`wrote ${valid.length} valid acknowledgements, ${invalid.length} invalid cases`)