| … | … | @@ -0,0 +1,61 @@ |
| 1 | import test from 'brittle' |
| 2 | import fs from 'bare-fs' |
| 3 | import path from 'bare-path' |
| 4 | import os from 'bare-os' |
| 5 | import b4a from 'b4a' |
| 6 | import Corestore from 'corestore' |
| 7 | import crypto from 'hypercore-crypto' |
| 8 | import { SharedRegistry } from '../../lib/multiwriter/shared-registry.js' |
| 9 | |
| 10 | function tmpStore () { |
| 11 | const dir = path.join(os.tmpdir(), 'hn-srkp-' + Date.now() + '-' + Math.random().toString(36).slice(2)) |
| 12 | fs.mkdirSync(dir, { recursive: true }) |
| 13 | return { store: new Corestore(dir), dir } |
| 14 | } |
| 15 | |
| 16 | function rmrf (dir) { |
| 17 | try { |
| 18 | for (const f of fs.readdirSync(dir)) { |
| 19 | const fp = path.join(dir, f) |
| 20 | if (fs.statSync(fp).isDirectory()) rmrf(fp) |
| 21 | else fs.unlinkSync(fp) |
| 22 | } |
| 23 | fs.rmdirSync(dir) |
| 24 | } catch {} |
| 25 | } |
| 26 | |
| 27 | test('SharedRegistry - stable keyPair makes localWriterKey deterministic + bootstrap-independent', async (t) => { |
| 28 | const keyPair = crypto.keyPair() |
| 29 | |
| 30 | // Same keyPair, DIFFERENT store, DIFFERENT bootstrap → SAME writer key. |
| 31 | const a = tmpStore() |
| 32 | const reg1 = new SharedRegistry(a.store, { keyPair }) |
| 33 | await reg1.ready() |
| 34 | const k1 = reg1.localWriterKey |
| 35 | |
| 36 | const b = tmpStore() |
| 37 | const reg2 = new SharedRegistry(b.store, { keyPair, bootstrapKey: crypto.keyPair().publicKey }) |
| 38 | await reg2.ready() |
| 39 | const k2 = reg2.localWriterKey |
| 40 | |
| 41 | t.ok(b4a.equals(k1, k2), 'writer key is identical across store + bootstrap — knowable before pairing') |
| 42 | |
| 43 | await reg1.close(); await reg2.close() |
| 44 | rmrf(a.dir); rmrf(b.dir) |
| 45 | }) |
| 46 | |
| 47 | test('SharedRegistry - no keyPair preserves the old random-per-namespace behaviour', async (t) => { |
| 48 | const keyPair = crypto.keyPair() |
| 49 | const c = tmpStore() |
| 50 | const stable = new SharedRegistry(c.store, { keyPair }) |
| 51 | await stable.ready() |
| 52 | |
| 53 | const d = tmpStore() |
| 54 | const random = new SharedRegistry(d.store, {}) |
| 55 | await random.ready() |
| 56 | |
| 57 | t.absent(b4a.equals(stable.localWriterKey, random.localWriterKey), 'unkeyed registry keeps a distinct (default) writer key') |
| 58 | |
| 59 | await stable.close(); await random.close() |
| 60 | rmrf(c.dir); rmrf(d.dir) |
| 61 | }) |