2020-04-28 15:03:16 +02:00
|
|
|
'use strict'
|
|
|
|
/* eslint-env mocha */
|
|
|
|
|
2021-04-27 17:13:34 +02:00
|
|
|
const { expect } = require('aegir/utils/chai')
|
2020-04-28 15:03:16 +02:00
|
|
|
const sinon = require('sinon')
|
2022-01-20 12:03:35 +00:00
|
|
|
const { MemoryDatastore } = require('datastore-core/memory')
|
2020-04-28 15:03:16 +02:00
|
|
|
const PeerStore = require('../../src/peer-store')
|
2022-01-20 12:03:35 +00:00
|
|
|
const pDefer = require('p-defer')
|
2020-04-28 15:03:16 +02:00
|
|
|
const peerUtils = require('../utils/creators/peer')
|
|
|
|
const {
|
|
|
|
codes: { ERR_INVALID_PARAMETERS }
|
|
|
|
} = require('../../src/errors')
|
|
|
|
|
2022-01-20 12:03:35 +00:00
|
|
|
/**
|
|
|
|
* @typedef {import('../../src/peer-store/types').PeerStore} PeerStore
|
|
|
|
* @typedef {import('../../src/peer-store/types').KeyBook} KeyBook
|
|
|
|
* @typedef {import('peer-id')} PeerId
|
|
|
|
*/
|
|
|
|
|
2020-04-28 15:03:16 +02:00
|
|
|
describe('keyBook', () => {
|
2022-01-20 12:03:35 +00:00
|
|
|
/** @type {PeerId} */
|
|
|
|
let peerId
|
|
|
|
/** @type {PeerStore} */
|
|
|
|
let peerStore
|
|
|
|
/** @type {KeyBook} */
|
|
|
|
let kb
|
|
|
|
/** @type {MemoryDatastore} */
|
|
|
|
let datastore
|
2020-04-28 15:03:16 +02:00
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
[peerId] = await peerUtils.createPeerId()
|
2022-01-20 12:03:35 +00:00
|
|
|
datastore = new MemoryDatastore()
|
|
|
|
peerStore = new PeerStore({
|
|
|
|
peerId,
|
|
|
|
datastore
|
|
|
|
})
|
2020-04-28 15:03:16 +02:00
|
|
|
kb = peerStore.keyBook
|
|
|
|
})
|
|
|
|
|
2022-01-20 12:03:35 +00:00
|
|
|
it('throws invalid parameters error if invalid PeerId is provided in set', async () => {
|
2020-04-28 15:03:16 +02:00
|
|
|
try {
|
2022-01-20 12:03:35 +00:00
|
|
|
await kb.set('invalid peerId')
|
2021-11-19 08:02:24 +00:00
|
|
|
} catch (/** @type {any} */ err) {
|
2020-04-28 15:03:16 +02:00
|
|
|
expect(err.code).to.equal(ERR_INVALID_PARAMETERS)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
throw new Error('invalid peerId should throw error')
|
|
|
|
})
|
|
|
|
|
2022-01-20 12:03:35 +00:00
|
|
|
it('throws invalid parameters error if invalid PeerId is provided in get', async () => {
|
2020-05-06 16:55:43 +02:00
|
|
|
try {
|
2022-01-20 12:03:35 +00:00
|
|
|
await kb.get('invalid peerId')
|
2021-11-19 08:02:24 +00:00
|
|
|
} catch (/** @type {any} */ err) {
|
2020-05-06 16:55:43 +02:00
|
|
|
expect(err.code).to.equal(ERR_INVALID_PARAMETERS)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
throw new Error('invalid peerId should throw error')
|
|
|
|
})
|
|
|
|
|
2022-01-20 12:03:35 +00:00
|
|
|
it('stores the peerId in the book and returns the public key', async () => {
|
2020-04-28 15:03:16 +02:00
|
|
|
// Set PeerId
|
2022-01-20 12:03:35 +00:00
|
|
|
await kb.set(peerId, peerId.pubKey)
|
2020-04-28 15:03:16 +02:00
|
|
|
|
|
|
|
// Get public key
|
2022-01-20 12:03:35 +00:00
|
|
|
const pubKey = await kb.get(peerId)
|
2020-04-28 15:03:16 +02:00
|
|
|
expect(peerId.pubKey.bytes).to.equalBytes(pubKey.bytes)
|
|
|
|
})
|
|
|
|
|
2022-01-20 12:03:35 +00:00
|
|
|
it('should not store if already stored', async () => {
|
|
|
|
const spy = sinon.spy(datastore, 'put')
|
2020-04-28 15:03:16 +02:00
|
|
|
|
|
|
|
// Set PeerId
|
2022-01-20 12:03:35 +00:00
|
|
|
await kb.set(peerId, peerId.pubKey)
|
|
|
|
await kb.set(peerId, peerId.pubKey)
|
2020-04-28 15:03:16 +02:00
|
|
|
|
|
|
|
expect(spy).to.have.property('callCount', 1)
|
|
|
|
})
|
2022-01-20 12:03:35 +00:00
|
|
|
|
|
|
|
it('should emit an event when setting a key', async () => {
|
|
|
|
const defer = pDefer()
|
|
|
|
|
|
|
|
peerStore.on('change:pubkey', ({ peerId: id, pubKey }) => {
|
|
|
|
expect(id.toB58String()).to.equal(peerId.toB58String())
|
|
|
|
expect(pubKey.bytes).to.equalBytes(peerId.pubKey.bytes)
|
|
|
|
defer.resolve()
|
|
|
|
})
|
|
|
|
|
|
|
|
// Set PeerId
|
|
|
|
await kb.set(peerId, peerId.pubKey)
|
|
|
|
await defer.promise
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not set when key does not match', async () => {
|
|
|
|
const [edKey] = await peerUtils.createPeerId({ fixture: false, opts: { keyType: 'Ed25519' } })
|
|
|
|
|
|
|
|
// Set PeerId
|
|
|
|
await expect(kb.set(edKey, peerId.pubKey)).to.eventually.be.rejectedWith(/bytes do not match/)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should emit an event when deleting a key', async () => {
|
|
|
|
const defer = pDefer()
|
|
|
|
|
|
|
|
await kb.set(peerId, peerId.pubKey)
|
|
|
|
|
|
|
|
peerStore.on('change:pubkey', ({ peerId: id, pubKey }) => {
|
|
|
|
expect(id.toB58String()).to.equal(peerId.toB58String())
|
|
|
|
expect(pubKey).to.be.undefined()
|
|
|
|
defer.resolve()
|
|
|
|
})
|
|
|
|
|
|
|
|
await kb.delete(peerId)
|
|
|
|
await defer.promise
|
|
|
|
})
|
2020-04-28 15:03:16 +02:00
|
|
|
})
|