OverviewFilesCommitsIssuesDocsReleasesPeers

keet-daemon / test / probe-attest-live.js

83 lines · javascript ref:
// Probe (room-state P3 — LIVE): verify the keet-identity attestation accessor
// the router wires (router._stateBaseProviders.attest) works against a real
// keet-core: boot a fresh identity, open the private-profile room, attest a
// buffer via core.internalAttestData(data, room.identity), and verify it with
// IdentityKey.verify. Proves the live accessor — the one thing the in-process
// attestation test (probe-state-attestation.js) couldn't cover.
//
// Run: bare test/probe-attest-live.js   (slow — full keet-core identity boot)
// PASS = proof produced by the live core verifies against the identity pubkey.

import fs from 'bare-fs'
import path from 'bare-path'
import os from 'bare-os'
import z32 from 'z32'
import IdentityKey from 'keet-identity-key'
import { startDaemon } from '../index.js'

const baseDir = path.join(os.tmpdir(), 'probe-attest-live-' + Date.now())
fs.mkdirSync(baseDir, { recursive: true })
const socketPath = path.join(baseDir, 'daemon.sock')

let exitCode = 1
let daemon = null

try {
  console.log('[live] startDaemon (fresh identity)…')
  daemon = await startDaemon({ storage: baseDir, socketPath })
  const core = daemon.core
  const router = daemon.router

  console.log('[live] vault.setup…')
  const res = await router._vaultSetup({ password: 'probe-attest-pw' })
  console.log('[live] vault.setup ->', JSON.stringify(res))

  // identity record (privateProfileKey + identity publicKey)
  const localId = await core.local.get('@local/identity')
  console.log('[live] @local/identity keys:', localId ? Object.keys(localId).join(',') : null)
  if (!localId || !localId.privateProfileKey) throw new Error('no @local/identity / privateProfileKey')

  // @local/identity holds deviceKeyPair + deviceProof directly (the router's
  // attest path). Sign a test buffer with keet's own primitive.
  console.log('[live] deviceKeyPair?', !!localId.deviceKeyPair, ' deviceProof?', !!localId.deviceProof)
  if (!localId.deviceKeyPair || !localId.deviceProof) throw new Error('@local/identity lacks deviceKeyPair/deviceProof')

  const testData = Buffer.concat([Buffer.alloc(32, 7), Buffer.alloc(32, 9)])
  const proof = IdentityKey.attestData(testData, localId.deviceKeyPair, localId.deviceProof)
  console.log('[live] attestData -> proof bytes:', proof && proof.length)
  if (!proof) throw new Error('attestData returned null')

  // verify exactly how a peer would: expectedIdentity = identity pubkey
  const expectedFromLocal = localId.publicKey
  const ok1 = IdentityKey.verify(proof, testData, { expectedIdentity: expectedFromLocal }) !== null
  console.log('[live] verify vs @local/identity.publicKey:', ok1)

  // and cross-check the router's representation: z32.decode(getIdentity().memberId)
  const id = await core.backendAPI.api.core.getIdentity().catch(() => null)
  let ok2 = null
  if (id && id.memberId) {
    const expectedFromMember = z32.decode(id.memberId)
    ok2 = IdentityKey.verify(proof, testData, { expectedIdentity: expectedFromMember }) !== null
    console.log('[live] verify vs z32.decode(memberId):', ok2, ' (memberId=' + id.memberId.slice(0, 12) + '…)')
  }

  if (ok1 && (ok2 === null || ok2 === true)) {
    console.log('\n✓ LIVE attestation accessor works: attestData(data, @local/identity.deviceKeyPair, deviceProof) verifies vs identity pubkey AND z32.decode(memberId)')
    exitCode = 0
  } else {
    console.error('\n✗ verify failed (ok1=' + ok1 + ' ok2=' + ok2 + ')')
  }
} catch (err) {
  console.error('\n✗ FAIL:', err.message)
  if (err.stack) console.error(err.stack)
} finally {
  await Promise.race([
    (async () => { try { await daemon?.core?.close() } catch {}; try { await daemon?.uds?.stop?.() } catch {} })(),
    new Promise(r => setTimeout(r, 4000))
  ])
  try { fs.rmSync(baseDir, { recursive: true, force: true }) } catch {}
}

console.log(exitCode === 0 ? '\nPASS' : '\nFAIL')
Bare.exit(exitCode)