OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-note / test / unit / multiwriter-admission.test.js

48 lines · javascript ref:
import test from 'brittle'
import fs from 'bare-fs'
import path from 'bare-path'
import os from 'bare-os'
import b4a from 'b4a'
import Corestore from 'corestore'
import crypto from 'hypercore-crypto'
import { MultiwriterManager } from '../../lib/multiwriter/manager.js'
import { SharedRegistry } from '../../lib/multiwriter/shared-registry.js'

function tmpStore () {
  const dir = path.join(os.tmpdir(), 'hn-mwa-' + Date.now() + '-' + Math.random().toString(36).slice(2))
  fs.mkdirSync(dir, { recursive: true })
  return { store: new Corestore(dir), dir }
}
function rmrf (dir) {
  try {
    for (const f of fs.readdirSync(dir)) {
      const fp = path.join(dir, f)
      if (fs.statSync(fp).isDirectory()) rmrf(fp)
      else fs.unlinkSync(fp)
    }
    fs.rmdirSync(dir)
  } catch {}
}

test('MultiwriterManager - threads a stable, bootstrap-independent writer key', async (t) => {
  const a = tmpStore()
  const m = new MultiwriterManager(a.store)
  await m.ready()
  const kManager = m.registry.localWriterKey

  // The manager derives its writer keypair from the corestore primary key.
  // Re-deriving it and using it under a DIFFERENT bootstrap must yield the SAME
  // writer key — i.e. our key is knowable before pairing (the whole basis for
  // admitting over the authenticated handshake instead of an HTTP token).
  const kp = await a.store.createKeyPair('multiwriter-writer')
  const b = tmpStore()
  const ref = new SharedRegistry(b.store, { keyPair: kp, bootstrapKey: crypto.keyPair().publicKey })
  await ref.ready()

  t.ok(b4a.equals(kManager, ref.localWriterKey), 'manager writer key is the stable key, independent of bootstrap')

  if (m.close) await m.close()
  await ref.close()
  rmrf(a.dir); rmrf(b.dir)
})