OverviewFilesCommitsIssuesDocsReleasesPeers

keet-daemon / test / room-writer-creds.test.js

53 lines · javascript ref:
// keet-core >= 4.22 rooms expose `room.engine` (the autobee RoomEngine), not
// `room.base`. _stateBaseProviders read room.base.local behind a guard, so on 4.22
// roomWriterKeyPair / roomWriterManifest silently became null and StateBaseV3 could
// never sign the keet-writer admission proof -- a 2nd writer could not be admitted.
//
// This asserts the creds the proof needs, against a REAL 4.22 room on fresh
// throwaway storage, exactly as state-base-v3.js verifies them (lines 648-655):
//   device = Hypercore.key(manifest) must be the room's device key, and
//   manifest.signers[0].publicKey must be the keyPair's public key.
//
//   bare test/room-writer-creds.test.js
import { startDaemon } from '../index.js'
import Hypercore from 'hypercore'
import Verifier from 'hypercore/lib/verifier.js'
import b4a from 'b4a'
import os from 'bare-os'
import path from 'bare-path'
import fs from 'bare-fs'
import idEnc from 'hypercore-id-encoding'

let failed = 0
const ok = (c, m) => { console.log((c ? 'ok   ' : 'FAIL ') + m); if (!c) failed++ }

const dir = path.join(os.tmpdir(), 'kd-writer-creds-' + Date.now())
fs.mkdirSync(dir, { recursive: true })
const sock = '/tmp/kd-wc-' + Bare.pid + '.sock'

const { router, core } = await startDaemon({ storage: dir, socketPath: sock })
const roomId = await router._roomCreate({ title: 'writer-creds' })   // the room.create RPC path
ok(typeof roomId === 'string' && roomId.length > 0, 'room created: ' + roomId)

const p = await router._stateBaseProviders(roomId)
ok(!!p.roomWriterKeyPair, 'roomWriterKeyPair is present')
ok(!!p.roomWriterManifest, 'roomWriterManifest is present')

if (p.roomWriterKeyPair && p.roomWriterManifest) {
  const room = core.internalOpenExistingRoom(idEnc.decode(roomId))
  await room.ready()
  const device = room.deviceKey
  core.internalReleaseRoom(room)

  const enc = Verifier.encodeManifest(p.roomWriterManifest)
  const dec = Verifier.decodeManifest(enc)
  const derived = Hypercore.key(dec)
  ok(b4a.equals(derived, device), 'Hypercore.key(manifest) is the room device key')
  const signer = dec.signers && dec.signers[0] && dec.signers[0].publicKey
  ok(!!signer && b4a.equals(signer, p.roomWriterKeyPair.publicKey), "manifest signer is the keyPair's public key")
  ok(!!p.roomWriterKeyPair.secretKey, 'keyPair carries a secret key (can sign the proof)')
}

console.log(failed === 0 ? 'PASS room-writer-creds' : 'FAIL room-writer-creds (' + failed + ')')
Bare.exit(failed === 0 ? 0 : 1)