OverviewFilesCommitsIssuesDocsReleasesPeers

keet-daemon / test / probe-v3-devicekeypair.mjs

27 lines · javascript ref:
import { startDaemon } from '../index.js'
import Pipe from 'bare-pipe'
import fs from 'bare-fs'
const STORAGE = '/tmp/v3-devkp'; const SOCK = '/tmp/v3-devkp.sock'
try { fs.rmSync(STORAGE, { recursive: true, force: true }) } catch {}
const sleep = (ms) => new Promise(r => setTimeout(r, ms))
function rpc (method, params) { return new Promise((res) => { const p = new Pipe(SOCK); let b = Buffer.alloc(0); const id = Math.floor(Math.random() * 1e9); const f = Buffer.from(JSON.stringify({ id, method, params })); const l = Buffer.alloc(4); l.writeUInt32LE(f.length, 0); p.on('connect', () => p.write(Buffer.concat([l, f]))); p.on('data', d => { b = Buffer.concat([b, d]); while (b.length >= 4) { const n = b.readUInt32LE(0); if (b.length < 4 + n) break; const m = JSON.parse(b.subarray(4, 4 + n)); b = b.subarray(4 + n); if (m.id === id) { p.destroy(); res(m) } } }); p.on('error', e => res({ ok: false, error: 'pipe:' + e.message })); setTimeout(() => { try { p.destroy() } catch {} ; res({ ok: false, error: 'timeout' }) }, 25000) }) }
let pass = 0; let fail = 0; const ok = (c, m) => { if (c) { pass++; console.log('  ok  ' + m) } else { fail++; console.log('  FAIL ' + m) } }
async function main () {
  await startDaemon({ storage: STORAGE, socketPath: SOCK, debug: false })
  await sleep(12000)
  const id = await rpc('identity', {})
  const r = await rpc('identity.deviceKeypair', {})
  ok(r.ok, 'identity.deviceKeypair ok (' + (r.ok ? '' : r.error) + ')')
  if (r.ok) {
    const d = r.data
    ok(d.memberId && d.memberId === (id.ok && id.data.memberId), 'returns memberId matching identity')
    ok(d.deviceKeyPair && /^[0-9a-f]{64}$/.test(d.deviceKeyPair.publicKey), 'device publicKey = 32 bytes hex')
    ok(d.deviceKeyPair && /^[0-9a-f]{128}$/.test(d.deviceKeyPair.secretKey), 'device secretKey = 64 bytes hex')
    ok(typeof d.deviceProof === 'string' && d.deviceProof.length > 0, 'deviceProof present (' + (d.deviceProof ? d.deviceProof.length / 2 : 0) + ' bytes)')
    ok(d.deviceKeyPair.publicKey !== (id.ok && id.data.memberId), 'device key != memberId (device is per-instance; memberId is the member key)')
  }
  console.log('\n[probe] pass=' + pass + ' fail=' + fail)
  Bare.exit(fail ? 1 : 0)
}
main().catch((e) => { console.error('[probe] THREW:', e && e.stack ? e.stack : e); Bare.exit(2) })