ssb-keyring
Version:
A persistence store for encryption keys for scuttlebutt. It's purpose is to make easy to answer box2 encryption/ decryption questions.
74 lines (57 loc) • 2.7 kB
JavaScript
/* eslint-disable camelcase */
const test = require('tape')
const BFE = require('ssb-bfe')
const na = require('sodium-universal')
const { promisify: p } = require('util')
const Keyring = require('../')
const { tmpPath, POBox } = require('./helpers')
test('keyring.poBox', async t => {
const path = tmpPath()
let keyring = await Keyring(path)
let DESCRIPTION
const { id: poBoxId, key } = POBox()
DESCRIPTION = 'poBoxIdBFE must be 2 + na.crypto_scalarmult_SCALARBYTES long'
const poBoxIdBFE = BFE.encode(poBoxId)
t.equals(poBoxIdBFE.length, 2 + na.crypto_scalarmult_SCALARBYTES, DESCRIPTION)
DESCRIPTION = 'poBox key must be na.crypto_scalarmult_SCALARBYTES long'
t.equals(key.length, na.crypto_scalarmult_SCALARBYTES, DESCRIPTION)
/* keys.poBox.add(poBoxId, keyInfo, cb) */
DESCRIPTION = 'poBox.add, bad poBoxId => error'
await p(keyring.poBox.add)('junk', { key: POBox() })
.then(res => t.fail(DESCRIPTION))
.catch(err => t.match(err && err.message, /expected a poBoxId/, DESCRIPTION)) // ✓
DESCRIPTION = 'poBox.add, bad info.key => error'
await p(keyring.poBox.add)(poBoxId, { key: 'junk' })
.then(res => t.fail(DESCRIPTION))
.catch(err => t.match(err && err.message, /expected buffer of length 32/, DESCRIPTION)) // ✓
// TODO associate with groupId?
// DESCRIPTION = 'poBox.add, bad info.root => error'
// await keyring.poBox.add(Group().id, { key: GroupKey(), root: 'dog' })
// .then(res => t.fail(DESCRIPTION))
// .catch(err => t.match(err && err.message, /expected info.root to be MsgId/, DESCRIPTION)) // ✓
DESCRIPTION = 'poBox.add, works! => true'
const info = { key }
const res = keyring.poBox.add(poBoxId, info, (err) => err && t.fail(err, DESCRIPTION))
t.equals(res, true, DESCRIPTION) // ✓
DESCRIPTION = 'poBox.add, same key again => false'
const res2 = keyring.poBox.add(poBoxId, info, (err) => err && t.fail(err, DESCRIPTION))
t.equals(res2, false, DESCRIPTION) // ✓
/* keys.poBox.get(poBoxId) */
DESCRIPTION = 'poBox.get'
const expected = info
t.deepEqual(keyring.poBox.get(poBoxId), expected, DESCRIPTION)
t.deepEqual(keyring.poBox.get(POBox().id), undefined, DESCRIPTION + ' (unknown poBoxId)')
DESCRIPTION = '...persists'
await keyring.close()
keyring = await Keyring(path)
t.deepEqual(keyring.poBox.get(poBoxId), expected, DESCRIPTION)
/* keys.poBox.has(poBoxId) */
DESCRIPTION = 'poBox.has'
t.true(keyring.poBox.has(poBoxId), DESCRIPTION)
t.false(keyring.poBox.has(POBox().id), DESCRIPTION + ' (unknown poBoxId)')
/* keys.poBox.list() */
DESCRIPTION = 'poBox.list'
t.deepEqual(keyring.poBox.list(), [poBoxId], DESCRIPTION)
keyring.close()
t.end()
})