From be2d5900ded779e7c1075e76a497079d8132b42a Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Fri, 24 Feb 2023 19:57:16 +0000 Subject: [PATCH 01/12] refactor: wip. --- src/key-store.js | 160 +++++++++++++++++++---------------------- test/key-store.test.js | 32 +++++++++ 2 files changed, 104 insertions(+), 88 deletions(-) create mode 100644 test/key-store.test.js diff --git a/src/key-store.js b/src/key-store.js index d8029e5..6546a93 100644 --- a/src/key-store.js +++ b/src/key-store.js @@ -1,9 +1,6 @@ -import { Level } from 'level' import * as crypto from '@libp2p/crypto' import secp256k1 from 'secp256k1' -import LRU from 'lru' import { Buffer } from 'safe-buffer' -import fs from 'fs' import pkg from 'elliptic' const { ec: EC } = pkg @@ -39,53 +36,66 @@ const verifySignature = async (signature, publicKey, data) => { return Promise.resolve(res) } -function createStore (path = './keystore') { - if (fs && fs.mkdirSync) { - fs.mkdirSync(path, { recursive: true }) +const sign = async (key, data) => { + if (!key) { + throw new Error('No signing key given') } - return new Level(path, {}) + if (!data) { + throw new Error('Given input data was undefined') + } + + if (!Buffer.isBuffer(data)) { + data = Buffer.from(data) + } + + return Buffer.from(await key.sign(data)).toString('hex') +} + +const verify = async (signature, publicKey, data) => { + // const cached = verifiedCache.get(signature) + const cached = null + let res = false + if (!cached) { + const verified = await verifySignature(signature, publicKey, data) + res = verified + // if (verified) { + // verifiedCache.set(signature, { publicKey, data }) + // } + } else { + const compare = (cached, data) => { + // let match + // if (v === 'v0') { + // match = Buffer.compare(Buffer.alloc(30, cached), Buffer.alloc(30, data)) === 0 + // } else { + const match = Buffer.isBuffer(data) ? Buffer.compare(cached, data) === 0 : cached === data + // } + return match + } + res = cached.publicKey === publicKey && compare(cached.data, data) + } + return res } // const verifiedCache = new LRU(1000) -export default class KeyStore { - constructor (input = {}) { - if (typeof input === 'string') { - this._store = createStore(input) - } else if (typeof input.open === 'function') { - this._store = input - } else if (typeof input.store === 'string') { - this._store = createStore(input.store) - } else { - this._store = input.store || createStore() - } - this._cache = input.cache || new LRU(100) +const KeyStore = async ({ storage, cache }) => { + const close = async () => { + if (!storage) return + await storage.close() } - async open () { - if (!this._store) { - throw new Error('KeyStore: No store found to open') - } - await this._store.open() - } - - async close () { - if (!this._store) return - await this._store.close() - } - - async hasKey (id) { + const hasKey = async (id) => { if (!id) { throw new Error('id needed to check a key') } - if (this._store.status && this._store.status !== 'open') { + if (storage.status && storage.status !== 'open') { return null } let hasKey = false try { - const storedKey = this._cache.get(id) || await this._store.get(id) + const storedKey = await cache.get(id) || await storage.get(id) hasKey = storedKey !== undefined && storedKey !== null } catch (e) { // Catches 'Error: ENOENT: no such file or directory, open ' @@ -95,20 +105,20 @@ export default class KeyStore { return hasKey } - async addKey (id, key) { + const addKey = async (id, key) => { try { - await this._store.put(id, JSON.stringify(key)) + await storage.put(id, JSON.stringify(key)) } catch (e) { console.log(e) } - this._cache.set(id, key) + cache.put(id, key) } - async createKey (id, { entropy } = {}) { + const createKey = async (id, { entropy } = {}) => { if (!id) { throw new Error('id needed to create a key') } - // if (this._store.status && this._store.status !== 'open') { + // if (storage.status && storage.status !== 'open') { // console.log("22::", id) // return null // } @@ -132,30 +142,30 @@ export default class KeyStore { } try { - await this._store.put(id, JSON.stringify(key)) + await storage.put(id, JSON.stringify(key)) } catch (e) { console.log(e) } - this._cache.set(id, key) + cache.put(id, key) return keys } - async getKey (id) { + const getKey = async (id) => { if (!id) { throw new Error('id needed to get a key') } - if (!this._store) { - await this.open() + if (!storage) { + await open() } - if (this._store.status && this._store.status !== 'open') { + if (storage.status && storage.status !== 'open') { return null } - const cachedKey = this._cache.get(id) + const cachedKey = await cache.get(id) let storedKey try { - storedKey = cachedKey || await this._store.get(id) + storedKey = cachedKey || await storage.get(id) } catch (e) { // ignore ENOENT error } @@ -170,13 +180,13 @@ export default class KeyStore { } if (!cachedKey) { - this._cache.set(id, deserializedKey) + cache.put(id, deserializedKey) } return unmarshal(Buffer.from(deserializedKey.privateKey, 'hex')) } - getPublic (keys, options = {}) { + const getPublic = (keys, options = {}) => { const formats = ['hex', 'buffer'] const decompress = typeof options.decompress === 'undefined' ? true : options.decompress const format = options.format || 'hex' @@ -191,44 +201,18 @@ export default class KeyStore { return format === 'buffer' ? pubKey : pubKey.toString('hex') } - static async sign (key, data) { - if (!key) { - throw new Error('No signing key given') - } - - if (!data) { - throw new Error('Given input data was undefined') - } - - if (!Buffer.isBuffer(data)) { - data = Buffer.from(data) - } - - return Buffer.from(await key.sign(data)).toString('hex') - } - - static async verify (signature, publicKey, data) { - // const cached = verifiedCache.get(signature) - const cached = null - let res = false - if (!cached) { - const verified = await verifySignature(signature, publicKey, data) - res = verified - // if (verified) { - // verifiedCache.set(signature, { publicKey, data }) - // } - } else { - const compare = (cached, data) => { - // let match - // if (v === 'v0') { - // match = Buffer.compare(Buffer.alloc(30, cached), Buffer.alloc(30, data)) === 0 - // } else { - const match = Buffer.isBuffer(data) ? Buffer.compare(cached, data) === 0 : cached === data - // } - return match - } - res = cached.publicKey === publicKey && compare(cached.data, data) - } - return res + return { + close, + hasKey, + addKey, + createKey, + getKey, + getPublic } } + +export { + KeyStore as default, + verify, + sign +} diff --git a/test/key-store.test.js b/test/key-store.test.js new file mode 100644 index 0000000..9de42c3 --- /dev/null +++ b/test/key-store.test.js @@ -0,0 +1,32 @@ +import { deepStrictEqual } from 'assert' +import LevelStorage from '../src/storage/level.js' +import LRUStorage from '../src/storage/lru.js' +import KeyStore from '../src/key-store.js' +import { config, testAPIs } from 'orbit-db-test-utils' +// import { userA } from '../fixtures/orbit-db-identity-keys.js' + +Object.keys(testAPIs).forEach((IPFS) => { + describe('KeyStore (' + IPFS + ')', () => { + let keystore + beforeEach(async () => { + const storage = await LevelStorage('./keys_1') + const cache = await LRUStorage(100) + keystore = await KeyStore({ storage, cache }) + }) + + it('creates a key', async () => { + const id = 'key1' + console.log(await keystore.createKey(id)) + }) + + it('gets a key', async () => { + const id = 'key1' + const keys = await keystore.createKey(id) + deepStrictEqual(await keystore.getKey(id), keys) + }) + + afterEach(async () => { + keystore.close() + }) + }) +}) From 13f02ba5801d9b8aa861cb9ecaf651f89b997d59 Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Sun, 26 Feb 2023 18:16:06 +0000 Subject: [PATCH 02/12] refactor: Defaults. --- src/key-store.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/key-store.js b/src/key-store.js index 6546a93..a5e15c7 100644 --- a/src/key-store.js +++ b/src/key-store.js @@ -1,6 +1,8 @@ import * as crypto from '@libp2p/crypto' import secp256k1 from 'secp256k1' import { Buffer } from 'safe-buffer' +import LevelStorage from './storage/level.js' +import LRUStorage from './storage/lru.js' import pkg from 'elliptic' const { ec: EC } = pkg @@ -80,11 +82,20 @@ const verify = async (signature, publicKey, data) => { // const verifiedCache = new LRU(1000) const KeyStore = async ({ storage, cache }) => { + storage = storage || await LevelStorage() + cache = cache || await LRUStorage() + const close = async () => { if (!storage) return await storage.close() } + const clear = async () => { + if (!storage) return + await storage.clear() + await cache.clear() + } + const hasKey = async (id) => { if (!id) { throw new Error('id needed to check a key') @@ -202,6 +213,7 @@ const KeyStore = async ({ storage, cache }) => { } return { + clear, close, hasKey, addKey, From 0dac15ba7ff8a5dcd76bb754842b7163363fd892 Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Sun, 26 Feb 2023 18:16:28 +0000 Subject: [PATCH 03/12] test: Signing and verifying. --- test/fixtures/orbit-db-identity-keys.js | 2 +- test/key-store.test.js | 273 +++++++++++++++++++++++- 2 files changed, 264 insertions(+), 11 deletions(-) diff --git a/test/fixtures/orbit-db-identity-keys.js b/test/fixtures/orbit-db-identity-keys.js index f0a5a36..b961b78 100644 --- a/test/fixtures/orbit-db-identity-keys.js +++ b/test/fixtures/orbit-db-identity-keys.js @@ -31,7 +31,7 @@ const signingKeys = { const createTestIdentities = async (ipfs1, ipfs2) => { rmrf('./keys_1') - const keystore = new KeyStore('./keys_1') + const keystore = await KeyStore() await keystore.open() for (const [key, value] of Object.entries(identityKeys)) { await keystore.addKey(key, value) diff --git a/test/key-store.test.js b/test/key-store.test.js index 9de42c3..4a90173 100644 --- a/test/key-store.test.js +++ b/test/key-store.test.js @@ -1,22 +1,108 @@ -import { deepStrictEqual } from 'assert' +import { strictEqual, deepStrictEqual } from 'assert' import LevelStorage from '../src/storage/level.js' import LRUStorage from '../src/storage/lru.js' -import KeyStore from '../src/key-store.js' -import { config, testAPIs } from 'orbit-db-test-utils' -// import { userA } from '../fixtures/orbit-db-identity-keys.js' +import KeyStore, { sign, verify } from '../src/key-store.js' +import { testAPIs } from 'orbit-db-test-utils' +import path from 'path' +import fs from 'fs-extra' +import rmrf from 'rimraf' +import { signingKeys } from './fixtures/orbit-db-identity-keys.js' Object.keys(testAPIs).forEach((IPFS) => { describe('KeyStore (' + IPFS + ')', () => { + const fixturePath = path.join('test', 'fixtures', 'keys', 'signing-keys') + const storagePath = path.join('test', 'keys', 'signing-keys') let keystore + beforeEach(async () => { - const storage = await LevelStorage('./keys_1') - const cache = await LRUStorage(100) - keystore = await KeyStore({ storage, cache }) + await fs.copy(fixturePath, storagePath) + + const storage = await LevelStorage({ path: storagePath }) + + keystore = await KeyStore({ storage }) + }) + + afterEach(async () => { + await keystore.clear() + await keystore.close() + + rmrf.sync(path.join('test', 'keys')) }) it('creates a key', async () => { const id = 'key1' - console.log(await keystore.createKey(id)) + await keystore.createKey(id) + const hasKey = await keystore.hasKey(id) + strictEqual(hasKey, true) + }) + + it('creates a new key using provided entropy', async () => { + const id = 'key1' + + await keystore.createKey(id, { + entropy: 'jANfduGRj4HU9Pk6nJzujANfduGRj4HU9Pk6nJzu' + }) + + const hasKey = await keystore.hasKey(id) + + strictEqual(hasKey, true) + + // Deterministic public key + const keyContent = await keystore.getKey(id) + const publicKey = keyContent._publicKey + + strictEqual( + Buffer.from(publicKey).toString('hex'), + '0328401cd1b561040b87cd66563be722ba429b42d6abfeca9cb4c34e9845c86d2e' + ) + }) + + it('throws an error when creating a key without an id', async () => { + let err + + try { + await keystore.createKey() + } catch (e) { + err = e.toString() + } + + strictEqual(err, 'Error: id needed to create a key') + }) + + it('throws an error when creating a key with a null id', async () => { + let err + + try { + await keystore.createKey(null) + } catch (e) { + err = e.toString() + } + + strictEqual(err, 'Error: id needed to create a key') + }) + + it('returns true if key exists', async () => { + const id = 'key1' + + await keystore.createKey(id) + const hasKey = await keystore.hasKey(id) + strictEqual(hasKey, true) + }) + + it('returns false if key does not exist', async () => { + const id = 'key1' + const hasKey = await keystore.hasKey(id) + strictEqual(hasKey, false) + }) + + it('throws an error when checking if key exists when no id is specified', async () => { + let err + try { + await keystore.hasKey() + } catch (e) { + err = e.toString() + } + strictEqual(err, 'Error: id needed to check a key') }) it('gets a key', async () => { @@ -25,8 +111,175 @@ Object.keys(testAPIs).forEach((IPFS) => { deepStrictEqual(await keystore.getKey(id), keys) }) - afterEach(async () => { - keystore.close() + it('throws an error when getting a key without an id', async () => { + const id = 'key1' + let err + + await keystore.createKey(id) + + try { + await keystore.getKey() + } catch (e) { + err = e.toString() + } + + strictEqual(err, 'Error: id needed to get a key') }) + + it('throws an error when getting a key with a null id', async () => { + const id = 'key1' + let err + + await keystore.createKey(id) + + try { + await keystore.getKey(null) + } catch (e) { + err = e.toString() + } + + strictEqual(err, 'Error: id needed to get a key') + }) + + it('gets a non-existent key', async () => { + const expected = undefined + const id = 'key1' + + const actual = await keystore.getKey(id) + + strictEqual(actual, expected) + }) + + describe('signing', () => { + it('signs data', async () => { + const expected = '304402207eb6e4f4b2c56665c505696c41ec0831c6c2998620589d4b6f405d49134dea5102207e71ba37d94b7a70e3d9fb3bea7c8d8b7082c3c880b6831e9613a0a3e7aabd9f' + + const key = await keystore.getKey('userA') + const actual = await sign(key, 'data data data') + strictEqual(actual, expected) + }) + + it('throws an error if no key is passed', async () => { + let err + try { + await sign(null, 'data data data') + } catch (e) { + err = e.toString() + } + + strictEqual(err, 'Error: No signing key given') + }) + + it('throws an error if no data is passed', async () => { + const key = 'key_1' + let err + try { + await sign(key) + } catch (e) { + err = e.toString() + } + + strictEqual(err, 'Error: Given input data was undefined') + }) + }) + + describe('Getting the public key', async () => { + let key + + beforeEach(async () => { + key = await keystore.getKey('userA') + }) + + it('gets the public key', async () => { + const expected = '04e0480538c2a39951d054e17ff31fde487cb1031d0044a037b53ad2e028a3e77c34e864b8579e7c7b24542959e7325361a96f1efb41ed5d3c08f0ea1e5dd0c8ed' + const publicKey = await keystore.getPublic(key) + strictEqual(publicKey, expected) + }) + + it('gets the public key buffer', async () => { + const expected = { + type: 'Buffer', + data: [4, 224, 72, 5, 56, 194, 163, 153, 81, 208, 84, + 225, 127, 243, 31, 222, 72, 124, 177, 3, 29, 0, + 68, 160, 55, 181, 58, 210, 224, 40, 163, 231, 124, + 52, 232, 100, 184, 87, 158, 124, 123, 36, 84, 41, + 89, 231, 50, 83, 97, 169, 111, 30, 251, 65, 237, + 93, 60, 8, 240, 234, 30, 93, 208, 200, 237] + } + const publicKey = await keystore.getPublic(key, { format: 'buffer' }) + + deepStrictEqual(publicKey.toJSON(), expected) + }) + + it('gets the public key when decompress is false', async () => { + const expectedCompressedKey = signingKeys.userA.publicKey + const publicKey = await keystore.getPublic(key, { decompress: false }) + strictEqual(publicKey, expectedCompressedKey) + }) + + it('gets the public key buffer when decompressed is false', async () => { + const expected = { + type: 'Buffer', + data: [3, 224, 72, 5, 56, 194, 163, 153, + 81, 208, 84, 225, 127, 243, 31, 222, + 72, 124, 177, 3, 29, 0, 68, 160, + 55, 181, 58, 210, 224, 40, 163, 231, + 124] + } + + const publicKey = await keystore.getPublic(key, { format: 'buffer', decompress: false }) + + deepStrictEqual(publicKey.toJSON(), expected) + }) + + it('throws an error if no keys are passed', async () => { + try { + await keystore.getPublic() + } catch (e) { + strictEqual(true, true) + } + }) + + it('throws an error if a bad format is passed', async () => { + try { + await keystore.getPublic(key, { format: 'foo' }) + } catch (e) { + strictEqual(true, true) + } + }) + }) + + describe('Verifying', async function () { + let key, publicKey + + beforeEach(async () => { + key = await keystore.getKey('userA') + publicKey = await keystore.getPublic(key) + }) + + it('verifies content', async () => { + const signature = '304402207eb6e4f4b2c56665c505696c41ec0831c6c2998620589d4b6f405d49134dea5102207e71ba37d94b7a70e3d9fb3bea7c8d8b7082c3c880b6831e9613a0a3e7aabd9f' + const verified = await verify(signature, publicKey, 'data data data') + strictEqual(verified, true) + }) + + it('verifies content with cache', async () => { + const data = 'data'.repeat(1024 * 1024) + const signature = await sign(key, data) + const startTime = new Date().getTime() + await verify(signature, publicKey, data) + const first = new Date().getTime() + await verify(signature, publicKey, data) + const after = new Date().getTime() + console.log('First pass:', first - startTime, 'ms', 'Cached:', after - first, 'ms') + strictEqual(first - startTime > after - first, true) + }) + + it('does not verify content with bad signature', async () => { + const signature = 'xxxxxx' + const verified = await verify(signature, publicKey, 'data data data') + strictEqual(verified, false) + }) + }) }) }) From 2e8530e7b19bf4e8e845a646a373c9406f21aee1 Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Sun, 26 Feb 2023 23:23:27 +0000 Subject: [PATCH 04/12] test: Persisted keyvalue with KeyStore. --- src/db/keyvalue-persisted.js | 6 +++--- src/identities/identities.js | 6 +++--- src/identities/providers/orbitdb.js | 2 +- src/key-store.js | 7 ++----- test/db/keyvalue-persisted.js | 3 +-- test/fixtures/orbit-db-identity-keys.js | 1 - test/key-store.test.js | 7 +++---- 7 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/db/keyvalue-persisted.js b/src/db/keyvalue-persisted.js index ac173b0..f84fd79 100644 --- a/src/db/keyvalue-persisted.js +++ b/src/db/keyvalue-persisted.js @@ -1,4 +1,4 @@ -import { Level } from 'level' +import LevelStorage from '../storage/level.js' import PQueue from 'p-queue' const valueEncoding = 'json' @@ -10,8 +10,8 @@ const KeyValuePersisted = async ({ KeyValue, OpLog, Database, ipfs, identity, da const queue = new PQueue({ concurrency: 1 }) const path = `./${identity.id}/${databaseId}/_index` - const index = new Level(path, { valueEncoding }) - await index.open() + const index = await LevelStorage({ path, valueEncoding: 'json' }) + // await index.open() let latestOplogHash diff --git a/src/identities/identities.js b/src/identities/identities.js index 5a07718..c815125 100644 --- a/src/identities/identities.js +++ b/src/identities/identities.js @@ -2,7 +2,7 @@ import Identity, { isIdentity, isEqual, decodeIdentity } from './identity.js' import OrbitDBIdentityProvider from './providers/orbitdb.js' // import DIDIdentityProvider from './identity-providers/did.js' // import EthIdentityProvider from './identity-providers/ethereum.js' -import KeyStore from '../key-store.js' +import * as KeyStore from '../key-store.js' import { LRUStorage, IPFSBlockStorage, MemoryStorage } from '../storage/index.js' import path from 'path' @@ -59,7 +59,7 @@ const Identities = async ({ keystore, identityKeysPath, storage, ipfs } = {}) => const { id, publicKey, signatures } = identity - const idSignatureVerified = await KeyStore.verify(signatures.id, publicKey, id) + const idSignatureVerified = await verify(signatures.id, publicKey, id) if (!idSignatureVerified) { return false } @@ -90,7 +90,7 @@ const Identities = async ({ keystore, identityKeysPath, storage, ipfs } = {}) => } const verify = async (signature, publicKey, data) => { - return KeyStore.verify(signature, publicKey, data) + return verify(signature, publicKey, data) } return { diff --git a/src/identities/providers/orbitdb.js b/src/identities/providers/orbitdb.js index 3664638..11f4a40 100644 --- a/src/identities/providers/orbitdb.js +++ b/src/identities/providers/orbitdb.js @@ -1,5 +1,5 @@ import IdentityProvider from './interface.js' -import KeyStore from '../../key-store.js' +import * as KeyStore from '../../key-store.js' const type = 'orbitdb' diff --git a/src/key-store.js b/src/key-store.js index a5e15c7..f181bd8 100644 --- a/src/key-store.js +++ b/src/key-store.js @@ -81,10 +81,10 @@ const verify = async (signature, publicKey, data) => { // const verifiedCache = new LRU(1000) -const KeyStore = async ({ storage, cache }) => { +const KeyStore = async ({ storage, cache } = {}) => { storage = storage || await LevelStorage() cache = cache || await LRUStorage() - + const close = async () => { if (!storage) return await storage.close() @@ -166,9 +166,6 @@ const KeyStore = async ({ storage, cache }) => { if (!id) { throw new Error('id needed to get a key') } - if (!storage) { - await open() - } if (storage.status && storage.status !== 'open') { return null } diff --git a/test/db/keyvalue-persisted.js b/test/db/keyvalue-persisted.js index 3ea0bf6..3ce2874 100644 --- a/test/db/keyvalue-persisted.js +++ b/test/db/keyvalue-persisted.js @@ -1,5 +1,4 @@ import { deepStrictEqual, strictEqual } from 'assert' -import mapSeries from 'p-map-series' import rimraf from 'rimraf' import { Log, Entry } from '../../src/oplog/index.js' import { KeyValuePersisted, KeyValue, Database } from '../../src/db/index.js' @@ -97,7 +96,7 @@ Object.keys(testAPIs).forEach((IPFS) => { const key = 'key1' const expected = 'value1' - const hash = await db.put(key, expected) + await db.put(key, expected) const actual = await db.get(key) strictEqual(actual, expected) }) diff --git a/test/fixtures/orbit-db-identity-keys.js b/test/fixtures/orbit-db-identity-keys.js index b961b78..48421b2 100644 --- a/test/fixtures/orbit-db-identity-keys.js +++ b/test/fixtures/orbit-db-identity-keys.js @@ -32,7 +32,6 @@ const createTestIdentities = async (ipfs1, ipfs2) => { rmrf('./keys_1') const keystore = await KeyStore() - await keystore.open() for (const [key, value] of Object.entries(identityKeys)) { await keystore.addKey(key, value) } diff --git a/test/key-store.test.js b/test/key-store.test.js index 4a90173..9d00281 100644 --- a/test/key-store.test.js +++ b/test/key-store.test.js @@ -1,6 +1,5 @@ import { strictEqual, deepStrictEqual } from 'assert' import LevelStorage from '../src/storage/level.js' -import LRUStorage from '../src/storage/lru.js' import KeyStore, { sign, verify } from '../src/key-store.js' import { testAPIs } from 'orbit-db-test-utils' import path from 'path' @@ -248,10 +247,10 @@ Object.keys(testAPIs).forEach((IPFS) => { } }) }) - + describe('Verifying', async function () { let key, publicKey - + beforeEach(async () => { key = await keystore.getKey('userA') publicKey = await keystore.getPublic(key) @@ -280,6 +279,6 @@ Object.keys(testAPIs).forEach((IPFS) => { const verified = await verify(signature, publicKey, 'data data data') strictEqual(verified, false) }) - }) + }) }) }) From 7cd2831a8274f389ed28762ccdb28b03c1cad4e9 Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Sun, 26 Feb 2023 23:25:43 +0000 Subject: [PATCH 05/12] fix: Missing KeyValue verify. --- src/identities/identities.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/identities/identities.js b/src/identities/identities.js index c815125..e19b0fd 100644 --- a/src/identities/identities.js +++ b/src/identities/identities.js @@ -90,7 +90,7 @@ const Identities = async ({ keystore, identityKeysPath, storage, ipfs } = {}) => } const verify = async (signature, publicKey, data) => { - return verify(signature, publicKey, data) + return KeyStore.verify(signature, publicKey, data) } return { From bf86156f9f5b51fac2db2c3d795b583b2644a5d7 Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Mon, 27 Feb 2023 02:42:18 +0000 Subject: [PATCH 06/12] refactor: Use KeyStore as function. Export signMessage and verifyMessage to avoid clashes with similarly named functions. --- src/identities/identities.js | 10 ++--- src/identities/providers/orbitdb.js | 6 +-- src/key-store.js | 8 ++-- test/identities/did-identity-provider.test.js | 16 +++---- .../ethereum-identity-provider.test.js | 17 +++---- test/identities/identities.test.js | 44 +++++++++---------- test/key-store.test.js | 18 ++++---- test/oplog/append.test.js | 2 +- test/oplog/crdt.test.js | 2 +- test/oplog/entry.test.js | 2 +- test/oplog/heads.test.js | 2 +- test/oplog/iterator.test.js | 4 +- test/oplog/join-concurrent.test.js | 2 +- test/oplog/join.test.js | 4 +- test/oplog/load.test.js | 2 +- test/oplog/log.test.js | 2 +- test/oplog/references.test.js | 2 +- test/storage.spec.js | 2 +- 18 files changed, 72 insertions(+), 73 deletions(-) diff --git a/src/identities/identities.js b/src/identities/identities.js index e19b0fd..59c86e1 100644 --- a/src/identities/identities.js +++ b/src/identities/identities.js @@ -2,7 +2,7 @@ import Identity, { isIdentity, isEqual, decodeIdentity } from './identity.js' import OrbitDBIdentityProvider from './providers/orbitdb.js' // import DIDIdentityProvider from './identity-providers/did.js' // import EthIdentityProvider from './identity-providers/ethereum.js' -import * as KeyStore from '../key-store.js' +import KeyStore, { signMessage, verifyMessage } from '../key-store.js' import { LRUStorage, IPFSBlockStorage, MemoryStorage } from '../storage/index.js' import path from 'path' @@ -16,7 +16,7 @@ const supportedTypes = { } const Identities = async ({ keystore, identityKeysPath, storage, ipfs } = {}) => { - keystore = keystore || new KeyStore(identityKeysPath || DefaultIdentityKeysPath) + keystore = keystore || await KeyStore(identityKeysPath || DefaultIdentityKeysPath) storage = storage || (ipfs ? await IPFSBlockStorage({ ipfs, pin: true }) : await MemoryStorage()) const verifiedIdentitiesCache = await LRUStorage({ size: 1000 }) @@ -38,7 +38,7 @@ const Identities = async ({ keystore, identityKeysPath, storage, ipfs } = {}) => const privateKey = await keystore.getKey(id) || await keystore.createKey(id) const publicKey = keystore.getPublic(privateKey) - const idSignature = await KeyStore.sign(privateKey, id) + const idSignature = await signMessage(privateKey, id) const publicKeyAndIdSignature = await identityProvider.signIdentity(publicKey + idSignature, options) const signatures = { id: idSignature, @@ -86,11 +86,11 @@ const Identities = async ({ keystore, identityKeysPath, storage, ipfs } = {}) => throw new Error('Private signing key not found from KeyStore') } - return KeyStore.sign(signingKey, data) + return await signMessage(signingKey, data) } const verify = async (signature, publicKey, data) => { - return KeyStore.verify(signature, publicKey, data) + return await verifyMessage(signature, publicKey, data) } return { diff --git a/src/identities/providers/orbitdb.js b/src/identities/providers/orbitdb.js index 11f4a40..a438150 100644 --- a/src/identities/providers/orbitdb.js +++ b/src/identities/providers/orbitdb.js @@ -1,5 +1,5 @@ import IdentityProvider from './interface.js' -import * as KeyStore from '../../key-store.js' +import KeyStore, { signMessage, verifyMessage } from '../../key-store.js' const type = 'orbitdb' @@ -35,13 +35,13 @@ class OrbitDBIdentityProvider extends IdentityProvider { throw new Error(`Signing key for '${id}' not found`) } - return KeyStore.sign(key, data) + return signMessage(key, data) } static async verifyIdentity (identity) { const { id, publicKey, signatures } = identity // Verify that identity was signed by the ID - return KeyStore.verify(signatures.publicKey, id, publicKey + signatures.id) + return verifyMessage(signatures.publicKey, id, publicKey + signatures.id) } } diff --git a/src/key-store.js b/src/key-store.js index f181bd8..26aae39 100644 --- a/src/key-store.js +++ b/src/key-store.js @@ -38,7 +38,7 @@ const verifySignature = async (signature, publicKey, data) => { return Promise.resolve(res) } -const sign = async (key, data) => { +const signMessage = async (key, data) => { if (!key) { throw new Error('No signing key given') } @@ -54,7 +54,7 @@ const sign = async (key, data) => { return Buffer.from(await key.sign(data)).toString('hex') } -const verify = async (signature, publicKey, data) => { +const verifyMessage = async (signature, publicKey, data) => { // const cached = verifiedCache.get(signature) const cached = null let res = false @@ -222,6 +222,6 @@ const KeyStore = async ({ storage, cache } = {}) => { export { KeyStore as default, - verify, - sign + verifyMessage, + signMessage } diff --git a/test/identities/did-identity-provider.test.js b/test/identities/did-identity-provider.test.js index c7d842d..8a7d222 100644 --- a/test/identities/did-identity-provider.test.js +++ b/test/identities/did-identity-provider.test.js @@ -1,8 +1,9 @@ import assert from 'assert' import path from 'path' import rmrf from 'rimraf' -import { KeyStore, Identities } from '../../src/index.js' -import { Identity, addIdentityProvider } from '../../src/identities/index.js' +import KeyStore, { signMessage, verifyMessage } from '../../src/key-store.js' +import Identities, { addIdentityProvider } from '../../src/identities/identities.js' +import Identity from '../../src/identities/identity.js' import { Ed25519Provider } from 'key-did-provider-ed25519' import KeyDidResolver from 'key-did-resolver' import DIDIdentityProvider from '../../src/identities/providers/did.js' @@ -16,8 +17,7 @@ describe('DID Identity Provider', function () { let identities before(async () => { - keystore = new KeyStore() - await keystore.open() + keystore = await KeyStore() DIDIdentityProvider.setDIDResolver(KeyDidResolver.getResolver()) addIdentityProvider(DIDIdentityProvider) identities = await Identities({ keystore }) @@ -56,15 +56,15 @@ describe('DID Identity Provider', function () { it('has a signature for the id', async () => { const signingKey = await keystore.getKey(didStr) - const idSignature = await KeyStore.sign(signingKey, didStr) - const verifies = await KeyStore.verify(idSignature, identity.publicKey, didStr) + const idSignature = await signMessage(signingKey, didStr) + const verifies = await verifyMessage(idSignature, identity.publicKey, didStr) assert.strictEqual(verifies, true) assert.strictEqual(identity.signatures.id, idSignature) }) it('has a signature for the publicKey', async () => { const signingKey = await keystore.getKey(didStr) - const idSignature = await KeyStore.sign(signingKey, didStr) + const idSignature = await signMessage(signingKey, didStr) assert.notStrictEqual(idSignature, undefined) }) }) @@ -106,7 +106,7 @@ describe('DID Identity Provider', function () { it('sign data', async () => { const signingKey = await keystore.getKey(identity.id) - const expectedSignature = await KeyStore.sign(signingKey, data) + const expectedSignature = await signMessage(signingKey, data) const signature = await identities.sign(identity, data, keystore) assert.strictEqual(signature, expectedSignature) }) diff --git a/test/identities/ethereum-identity-provider.test.js b/test/identities/ethereum-identity-provider.test.js index 7af290a..39228e2 100644 --- a/test/identities/ethereum-identity-provider.test.js +++ b/test/identities/ethereum-identity-provider.test.js @@ -1,8 +1,9 @@ import assert from 'assert' import path from 'path' import rmrf from 'rimraf' -import { KeyStore, Identities } from '../../src/index.js' -import { Identity, addIdentityProvider } from '../../src/identities/index.js' +import KeyStore, { signMessage, verifyMessage } from '../../src/key-store.js' +import Identities, { addIdentityProvider } from '../../src/identities/identities.js' +import Identity from '../../src/identities/identity.js' import EthIdentityProvider from '../../src/identities/providers/ethereum.js' const type = EthIdentityProvider.type @@ -12,8 +13,8 @@ describe('Ethereum Identity Provider', function () { let identities before(async () => { - keystore = new KeyStore() - await keystore.open() + keystore = await KeyStore() + addIdentityProvider(EthIdentityProvider) identities = await Identities({ keystore }) }) @@ -53,15 +54,15 @@ describe('Ethereum Identity Provider', function () { it('has a signature for the id', async () => { const signingKey = await keystore.getKey(wallet.address) - const idSignature = await KeyStore.sign(signingKey, wallet.address) - const verifies = await KeyStore.verify(idSignature, Buffer.from(signingKey.public.marshal()).toString('hex'), wallet.address) + const idSignature = await signMessage(signingKey, wallet.address) + const verifies = await verifyMessage(idSignature, Buffer.from(signingKey.public.marshal()).toString('hex'), wallet.address) assert.strictEqual(verifies, true) assert.strictEqual(identity.signatures.id, idSignature) }) it('has a signature for the publicKey', async () => { const signingKey = await keystore.getKey(wallet.address) - const idSignature = await KeyStore.sign(signingKey, wallet.address) + const idSignature = await signMessage(signingKey, wallet.address) const publicKeyAndIdSignature = await wallet.signMessage(identity.publicKey + idSignature) assert.strictEqual(identity.signatures.publicKey, publicKeyAndIdSignature) }) @@ -102,7 +103,7 @@ describe('Ethereum Identity Provider', function () { it('sign data', async () => { const signingKey = await keystore.getKey(identity.id) - const expectedSignature = await KeyStore.sign(signingKey, data) + const expectedSignature = await signMessage(signingKey, data) const signature = await identities.sign(identity, data, keystore) assert.strictEqual(signature, expectedSignature) }) diff --git a/test/identities/identities.test.js b/test/identities/identities.test.js index 289df24..2bd21a2 100644 --- a/test/identities/identities.test.js +++ b/test/identities/identities.test.js @@ -1,8 +1,9 @@ import assert from 'assert' import path from 'path' import rmrf from 'rimraf' -import { KeyStore, Identities } from '../../src/index.js' -import { Identity, addIdentityProvider } from '../../src/identities/index.js' +import KeyStore, { signMessage, verifyMessage } from '../../src/key-store.js' +import Identities, { addIdentityProvider } from '../../src/identities/identities.js' +import Identity from '../../src/identities/identity.js' import fs from 'fs-extra' const fixturesPath = path.resolve('./test/identities/fixtures/keys') const savedKeysPath = path.resolve('./test/identities/fixtures/savedKeys') @@ -73,8 +74,8 @@ describe('Identities', function () { let keystore before(async () => { - keystore = new KeyStore(identityKeysPath) - await keystore.open() + keystore = await KeyStore(identityKeysPath) + identities = await Identities({ keystore }) }) @@ -110,9 +111,9 @@ describe('Identities', function () { const key = await keystore.getKey(id) const externalId = Buffer.from(key.public.marshal()).toString('hex') const signingKey = await keystore.getKey(externalId) - const idSignature = await KeyStore.sign(signingKey, externalId) + const idSignature = await signMessage(signingKey, externalId) const publicKey = Buffer.from(signingKey.public.marshal()).toString('hex') - const verifies = await KeyStore.verify(idSignature, publicKey, externalId) + const verifies = await verifyMessage(idSignature, publicKey, externalId) assert.strictEqual(verifies, true) assert.strictEqual(identity.signatures.id, idSignature) }) @@ -121,9 +122,9 @@ describe('Identities', function () { const key = await keystore.getKey(id) const externalId = Buffer.from(key.public.marshal()).toString('hex') const signingKey = await keystore.getKey(externalId) - const idSignature = await KeyStore.sign(signingKey, externalId) + const idSignature = await signMessage(signingKey, externalId) const externalKey = await keystore.getKey(id) - const publicKeyAndIdSignature = await KeyStore.sign(externalKey, identity.publicKey + idSignature) + const publicKeyAndIdSignature = await signMessage(externalKey, identity.publicKey + idSignature) assert.strictEqual(identity.signatures.publicKey, publicKeyAndIdSignature) }) }) @@ -142,8 +143,7 @@ describe('Identities', function () { before(async () => { await fs.copy(fixturesPath, savedKeysPath) - savedKeysKeyStore = new KeyStore(savedKeysPath) - await savedKeysKeyStore.open() + savedKeysKeyStore = await KeyStore(savedKeysPath) identities = await Identities({ keystore: savedKeysKeyStore }) identity = await identities.createIdentity({ id }) @@ -181,8 +181,8 @@ describe('Identities', function () { it('has the correct signatures', async () => { const internalSigningKey = await savedKeysKeyStore.getKey(identity.id) const externalSigningKey = await savedKeysKeyStore.getKey(id) - const idSignature = await KeyStore.sign(internalSigningKey, identity.id) - const publicKeyAndIdSignature = await KeyStore.sign(externalSigningKey, identity.publicKey + idSignature) + const idSignature = await signMessage(internalSigningKey, identity.id) + const publicKeyAndIdSignature = await signMessage(externalSigningKey, identity.publicKey + idSignature) const expectedSignature = { id: idSignature, publicKey: publicKeyAndIdSignature } assert.deepStrictEqual(identity.signatures, expectedSignature) }) @@ -196,8 +196,7 @@ describe('Identities', function () { let keystore before(async () => { - keystore = new KeyStore(identityKeysPath) - await keystore.open() + keystore = await KeyStore(identityKeysPath) }) after(async () => { @@ -209,14 +208,14 @@ describe('Identities', function () { it('identity pkSignature verifies', async () => { identities = await Identities({ keystore }) identity = await identities.createIdentity({ id, type }) - const verified = await KeyStore.verify(identity.signatures.id, identity.publicKey, identity.id) + const verified = await verifyMessage(identity.signatures.id, identity.publicKey, identity.id) assert.strictEqual(verified, true) }) it('identity signature verifies', async () => { identities = await Identities({ keystore }) identity = await identities.createIdentity({ id, type }) - const verified = await KeyStore.verify(identity.signatures.publicKey, identity.id, identity.publicKey + identity.signatures.id) + const verified = await verifyMessage(identity.signatures.publicKey, identity.id, identity.publicKey + identity.signatures.id) assert.strictEqual(verified, true) }) @@ -246,8 +245,8 @@ describe('Identities', function () { let keystore before(async () => { - keystore = new KeyStore(identityKeysPath) - await keystore.open() + keystore = await KeyStore(identityKeysPath) + identities = await Identities({ keystore }) }) @@ -273,8 +272,8 @@ describe('Identities', function () { let keystore before(async () => { - keystore = new KeyStore(identityKeysPath) - await keystore.open() + keystore = await KeyStore(identityKeysPath) + identities = await Identities({ keystore }) identity = await identities.createIdentity({ id }) }) @@ -287,7 +286,7 @@ describe('Identities', function () { it('sign data', async () => { const signingKey = await keystore.getKey(identity.id) - const expectedSignature = await KeyStore.sign(signingKey, data) + const expectedSignature = await signMessage(signingKey, data) const signature = await identities.sign(identity, data, keystore) assert.strictEqual(signature, expectedSignature) }) @@ -318,8 +317,7 @@ describe('Identities', function () { let signature before(async () => { - keystore = new KeyStore(identityKeysPath) - await keystore.open() + keystore = await KeyStore(identityKeysPath) }) after(async () => { diff --git a/test/key-store.test.js b/test/key-store.test.js index 9d00281..2edf2db 100644 --- a/test/key-store.test.js +++ b/test/key-store.test.js @@ -1,6 +1,6 @@ import { strictEqual, deepStrictEqual } from 'assert' import LevelStorage from '../src/storage/level.js' -import KeyStore, { sign, verify } from '../src/key-store.js' +import KeyStore, { signMessage, verifyMessage } from '../src/key-store.js' import { testAPIs } from 'orbit-db-test-utils' import path from 'path' import fs from 'fs-extra' @@ -154,14 +154,14 @@ Object.keys(testAPIs).forEach((IPFS) => { const expected = '304402207eb6e4f4b2c56665c505696c41ec0831c6c2998620589d4b6f405d49134dea5102207e71ba37d94b7a70e3d9fb3bea7c8d8b7082c3c880b6831e9613a0a3e7aabd9f' const key = await keystore.getKey('userA') - const actual = await sign(key, 'data data data') + const actual = await signMessage(key, 'data data data') strictEqual(actual, expected) }) it('throws an error if no key is passed', async () => { let err try { - await sign(null, 'data data data') + await signMessage(null, 'data data data') } catch (e) { err = e.toString() } @@ -173,7 +173,7 @@ Object.keys(testAPIs).forEach((IPFS) => { const key = 'key_1' let err try { - await sign(key) + await signMessage(key) } catch (e) { err = e.toString() } @@ -258,17 +258,17 @@ Object.keys(testAPIs).forEach((IPFS) => { it('verifies content', async () => { const signature = '304402207eb6e4f4b2c56665c505696c41ec0831c6c2998620589d4b6f405d49134dea5102207e71ba37d94b7a70e3d9fb3bea7c8d8b7082c3c880b6831e9613a0a3e7aabd9f' - const verified = await verify(signature, publicKey, 'data data data') + const verified = await verifyMessage(signature, publicKey, 'data data data') strictEqual(verified, true) }) it('verifies content with cache', async () => { const data = 'data'.repeat(1024 * 1024) - const signature = await sign(key, data) + const signature = await signMessage(key, data) const startTime = new Date().getTime() - await verify(signature, publicKey, data) + await verifyMessage(signature, publicKey, data) const first = new Date().getTime() - await verify(signature, publicKey, data) + await verifyMessage(signature, publicKey, data) const after = new Date().getTime() console.log('First pass:', first - startTime, 'ms', 'Cached:', after - first, 'ms') strictEqual(first - startTime > after - first, true) @@ -276,7 +276,7 @@ Object.keys(testAPIs).forEach((IPFS) => { it('does not verify content with bad signature', async () => { const signature = 'xxxxxx' - const verified = await verify(signature, publicKey, 'data data data') + const verified = await verifyMessage(signature, publicKey, 'data data data') strictEqual(verified, false) }) }) diff --git a/test/oplog/append.test.js b/test/oplog/append.test.js index e221cf7..21aaf83 100644 --- a/test/oplog/append.test.js +++ b/test/oplog/append.test.js @@ -28,7 +28,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = new KeyStore(identityKeysPath) + keystore = await KeyStore(identityKeysPath) const storage = await MemoryStorage() diff --git a/test/oplog/crdt.test.js b/test/oplog/crdt.test.js index 3337243..0365b40 100644 --- a/test/oplog/crdt.test.js +++ b/test/oplog/crdt.test.js @@ -28,7 +28,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = new KeyStore(identityKeysPath) + keystore = await KeyStore(identityKeysPath) const storage = await MemoryStorage() diff --git a/test/oplog/entry.test.js b/test/oplog/entry.test.js index 99354ee..2e4ab38 100644 --- a/test/oplog/entry.test.js +++ b/test/oplog/entry.test.js @@ -30,7 +30,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = new KeyStore(identityKeysPath) + keystore = await KeyStore(identityKeysPath) identities = await Identities({ keystore, ipfs }) testIdentity = await identities.createIdentity({ id: 'userA' }) diff --git a/test/oplog/heads.test.js b/test/oplog/heads.test.js index 516784b..c52fe89 100644 --- a/test/oplog/heads.test.js +++ b/test/oplog/heads.test.js @@ -33,7 +33,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = new KeyStore(identityKeysPath) + keystore = await KeyStore(identityKeysPath) const storage = await MemoryStorage() identities = await Identities({ keystore, storage }) diff --git a/test/oplog/iterator.test.js b/test/oplog/iterator.test.js index 6d5279b..9539210 100644 --- a/test/oplog/iterator.test.js +++ b/test/oplog/iterator.test.js @@ -26,8 +26,8 @@ Object.keys(testAPIs).forEach((IPFS) => { let testIdentity, testIdentity2, testIdentity3 before(async () => { - keystore = new KeyStore('./keys_1') - await keystore.open() + keystore = await KeyStore('./keys_1') + for (const [key, value] of Object.entries(identityKeys)) { await keystore.addKey(key, value) } diff --git a/test/oplog/join-concurrent.test.js b/test/oplog/join-concurrent.test.js index ae9da17..963709b 100644 --- a/test/oplog/join-concurrent.test.js +++ b/test/oplog/join-concurrent.test.js @@ -29,7 +29,7 @@ Object.keys(testAPIs).forEach(IPFS => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = new KeyStore(identityKeysPath) + keystore = await KeyStore(identityKeysPath) const storage = await MemoryStorage() diff --git a/test/oplog/join.test.js b/test/oplog/join.test.js index 6b86aea..332916e 100644 --- a/test/oplog/join.test.js +++ b/test/oplog/join.test.js @@ -26,8 +26,8 @@ Object.keys(testAPIs).forEach((IPFS) => { let testIdentity, testIdentity2, testIdentity3, testIdentity4 before(async () => { - keystore = new KeyStore('./keys_1') - await keystore.open() + keystore = await KeyStore('./keys_1') + for (const [key, value] of Object.entries(identityKeys)) { await keystore.addKey(key, value) } diff --git a/test/oplog/load.test.js b/test/oplog/load.test.js index 80efc98..01f402e 100644 --- a/test/oplog/load.test.js +++ b/test/oplog/load.test.js @@ -49,7 +49,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = new KeyStore(identityKeysPath) + keystore = await KeyStore(identityKeysPath) testIdentity = await createIdentity({ id: 'userC', keystore }) testIdentity2 = await createIdentity({ id: 'userB', keystore }) diff --git a/test/oplog/log.test.js b/test/oplog/log.test.js index f469a6b..5df1834 100644 --- a/test/oplog/log.test.js +++ b/test/oplog/log.test.js @@ -28,7 +28,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = new KeyStore(identityKeysPath) + keystore = await KeyStore(identityKeysPath) const storage = await MemoryStorage() diff --git a/test/oplog/references.test.js b/test/oplog/references.test.js index a12416d..cffe9b1 100644 --- a/test/oplog/references.test.js +++ b/test/oplog/references.test.js @@ -29,7 +29,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = new KeyStore(identityKeysPath) + keystore = await KeyStore(identityKeysPath) const storage = await MemoryStorage() diff --git a/test/storage.spec.js b/test/storage.spec.js index b41d9aa..f60f31c 100644 --- a/test/storage.spec.js +++ b/test/storage.spec.js @@ -34,7 +34,7 @@ Object.keys(testAPIs).forEach((_) => { // Start an IPFS instance ipfs1 = await IPFS.create({ ...config.daemon1, repo: './ipfs1' }) - keystore = new KeyStore(identityKeysPath) + keystore = await KeyStore(identityKeysPath) const storage = await MemoryStorage() const identities = await Identities({ keystore, storage }) From 45770b05ac5cef8f2ae5047c2a1cb0da2e430c09 Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Mon, 27 Feb 2023 16:10:50 +0000 Subject: [PATCH 07/12] feat: Default keystore location. --- src/key-store.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/key-store.js b/src/key-store.js index 26aae39..d44a3c0 100644 --- a/src/key-store.js +++ b/src/key-store.js @@ -82,7 +82,7 @@ const verifyMessage = async (signature, publicKey, data) => { // const verifiedCache = new LRU(1000) const KeyStore = async ({ storage, cache } = {}) => { - storage = storage || await LevelStorage() + storage = storage || await LevelStorage('./keystore') cache = cache || await LRUStorage() const close = async () => { From 8b16332bd63a891a8fd260e230a2f4165d44b63f Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Mon, 27 Feb 2023 16:12:08 +0000 Subject: [PATCH 08/12] feat: Default cache size. --- src/key-store.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/key-store.js b/src/key-store.js index d44a3c0..2da6084 100644 --- a/src/key-store.js +++ b/src/key-store.js @@ -83,7 +83,7 @@ const verifyMessage = async (signature, publicKey, data) => { const KeyStore = async ({ storage, cache } = {}) => { storage = storage || await LevelStorage('./keystore') - cache = cache || await LRUStorage() + cache = cache || await LRUStorage({ size: 1000 }) const close = async () => { if (!storage) return From 0c5c44e429b953997c86c94e447f01aea9061ce3 Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Tue, 28 Feb 2023 03:34:55 +0000 Subject: [PATCH 09/12] refactor: Generate keys using crypto. --- src/key-store.js | 17 ++++------------- test/key-store.test.js | 21 --------------------- 2 files changed, 4 insertions(+), 34 deletions(-) diff --git a/src/key-store.js b/src/key-store.js index 2da6084..67321cf 100644 --- a/src/key-store.js +++ b/src/key-store.js @@ -3,10 +3,7 @@ import secp256k1 from 'secp256k1' import { Buffer } from 'safe-buffer' import LevelStorage from './storage/level.js' import LRUStorage from './storage/lru.js' -import pkg from 'elliptic' -const { ec: EC } = pkg -const ec = new EC('secp256k1') const unmarshal = crypto.keys.supportedKeys.secp256k1.unmarshalSecp256k1PrivateKey const verifySignature = async (signature, publicKey, data) => { @@ -135,18 +132,11 @@ const KeyStore = async ({ storage, cache } = {}) => { // } // Generate a private key - const privKey = ec.genKeyPair({ entropy }).getPrivate().toArrayLike(Buffer) - // Left pad the key to 32 bytes. The module used in libp2p crypto (noble-secp256k1) - // verifies the length and will throw an error if key is not 32 bytes. - // For more details on why the generated key is not always 32 bytes, see: - // https://stackoverflow.com/questions/62938091/why-are-secp256k1-privatekeys-not-always-32-bytes-in-nodejs - const buf = Buffer.alloc(32) - // Copy the private key buffer to the padded buffer - privKey.copy(buf, buf.length - privKey.length) - - const keys = await unmarshal(buf) + const pair = await crypto.keys.generateKeyPair('secp256k1') + const keys = await crypto.keys.unmarshalPrivateKey(pair.bytes) const pubKey = keys.public.marshal() const decompressedKey = secp256k1.publicKeyConvert(Buffer.from(pubKey), false) + const key = { publicKey: Buffer.from(decompressedKey).toString('hex'), privateKey: Buffer.from(keys.marshal()).toString('hex') @@ -157,6 +147,7 @@ const KeyStore = async ({ storage, cache } = {}) => { } catch (e) { console.log(e) } + cache.put(id, key) return keys diff --git a/test/key-store.test.js b/test/key-store.test.js index 2edf2db..7e224c5 100644 --- a/test/key-store.test.js +++ b/test/key-store.test.js @@ -35,27 +35,6 @@ Object.keys(testAPIs).forEach((IPFS) => { strictEqual(hasKey, true) }) - it('creates a new key using provided entropy', async () => { - const id = 'key1' - - await keystore.createKey(id, { - entropy: 'jANfduGRj4HU9Pk6nJzujANfduGRj4HU9Pk6nJzu' - }) - - const hasKey = await keystore.hasKey(id) - - strictEqual(hasKey, true) - - // Deterministic public key - const keyContent = await keystore.getKey(id) - const publicKey = keyContent._publicKey - - strictEqual( - Buffer.from(publicKey).toString('hex'), - '0328401cd1b561040b87cd66563be722ba429b42d6abfeca9cb4c34e9845c86d2e' - ) - }) - it('throws an error when creating a key without an id', async () => { let err From ba71a7985edb8aa55de99374791322543205f837 Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Tue, 28 Feb 2023 16:30:36 +0000 Subject: [PATCH 10/12] feat: Use composite storage for key store. --- src/key-store.js | 43 ++--- test/key-store.test.js | 424 +++++++++++++++++++++-------------------- 2 files changed, 240 insertions(+), 227 deletions(-) diff --git a/src/key-store.js b/src/key-store.js index 67321cf..c928a5d 100644 --- a/src/key-store.js +++ b/src/key-store.js @@ -1,6 +1,7 @@ import * as crypto from '@libp2p/crypto' import secp256k1 from 'secp256k1' import { Buffer } from 'safe-buffer' +import ComposedStorage from './storage/composed.js' import LevelStorage from './storage/level.js' import LRUStorage from './storage/lru.js' @@ -51,22 +52,25 @@ const signMessage = async (key, data) => { return Buffer.from(await key.sign(data)).toString('hex') } +const verifiedCache = await LRUStorage({ size: 1000 }) + const verifyMessage = async (signature, publicKey, data) => { - // const cached = verifiedCache.get(signature) - const cached = null + const cached = await verifiedCache.get(signature) + let res = false + if (!cached) { const verified = await verifySignature(signature, publicKey, data) res = verified - // if (verified) { - // verifiedCache.set(signature, { publicKey, data }) - // } + if (verified) { + await verifiedCache.put(signature, { publicKey, data }) + } } else { const compare = (cached, data) => { - // let match - // if (v === 'v0') { - // match = Buffer.compare(Buffer.alloc(30, cached), Buffer.alloc(30, data)) === 0 - // } else { + /* let match + if (v === 'v0') { + match = Buffer.compare(Buffer.alloc(30, cached), Buffer.alloc(30, data)) === 0 + } else { */ const match = Buffer.isBuffer(data) ? Buffer.compare(cached, data) === 0 : cached === data // } return match @@ -78,9 +82,8 @@ const verifyMessage = async (signature, publicKey, data) => { // const verifiedCache = new LRU(1000) -const KeyStore = async ({ storage, cache } = {}) => { - storage = storage || await LevelStorage('./keystore') - cache = cache || await LRUStorage({ size: 1000 }) +const KeyStore = async ({ storage } = {}) => { + storage = storage || await ComposedStorage(LevelStorage('./keystore'), LRUStorage({ size: 1000 })) const close = async () => { if (!storage) return @@ -90,7 +93,6 @@ const KeyStore = async ({ storage, cache } = {}) => { const clear = async () => { if (!storage) return await storage.clear() - await cache.clear() } const hasKey = async (id) => { @@ -103,7 +105,7 @@ const KeyStore = async ({ storage, cache } = {}) => { let hasKey = false try { - const storedKey = await cache.get(id) || await storage.get(id) + const storedKey = await storage.get(id) hasKey = storedKey !== undefined && storedKey !== null } catch (e) { // Catches 'Error: ENOENT: no such file or directory, open ' @@ -119,7 +121,6 @@ const KeyStore = async ({ storage, cache } = {}) => { } catch (e) { console.log(e) } - cache.put(id, key) } const createKey = async (id, { entropy } = {}) => { @@ -148,8 +149,6 @@ const KeyStore = async ({ storage, cache } = {}) => { console.log(e) } - cache.put(id, key) - return keys } @@ -157,14 +156,14 @@ const KeyStore = async ({ storage, cache } = {}) => { if (!id) { throw new Error('id needed to get a key') } + if (storage.status && storage.status !== 'open') { return null } - const cachedKey = await cache.get(id) let storedKey try { - storedKey = cachedKey || await storage.get(id) + storedKey = await storage.get(id) } catch (e) { // ignore ENOENT error } @@ -173,15 +172,11 @@ const KeyStore = async ({ storage, cache } = {}) => { return } - const deserializedKey = cachedKey || JSON.parse(storedKey) + const deserializedKey = JSON.parse(storedKey) if (!deserializedKey) { return } - if (!cachedKey) { - cache.put(id, deserializedKey) - } - return unmarshal(Buffer.from(deserializedKey.privateKey, 'hex')) } diff --git a/test/key-store.test.js b/test/key-store.test.js index 7e224c5..0e14cda 100644 --- a/test/key-store.test.js +++ b/test/key-store.test.js @@ -1,5 +1,7 @@ import { strictEqual, deepStrictEqual } from 'assert' import LevelStorage from '../src/storage/level.js' +import LRUStorage from '../src/storage/lru.js' +import ComposedStorage from '../src/storage/composed.js' import KeyStore, { signMessage, verifyMessage } from '../src/key-store.js' import { testAPIs } from 'orbit-db-test-utils' import path from 'path' @@ -9,254 +11,270 @@ import { signingKeys } from './fixtures/orbit-db-identity-keys.js' Object.keys(testAPIs).forEach((IPFS) => { describe('KeyStore (' + IPFS + ')', () => { - const fixturePath = path.join('test', 'fixtures', 'keys', 'signing-keys') - const storagePath = path.join('test', 'keys', 'signing-keys') let keystore - beforeEach(async () => { - await fs.copy(fixturePath, storagePath) + describe('Creating and retrieving keys', () => { + beforeEach(async () => { + keystore = await KeyStore() + }) - const storage = await LevelStorage({ path: storagePath }) + afterEach(async () => { + await keystore.clear() + await keystore.close() + }) - keystore = await KeyStore({ storage }) - }) + it('creates a key', async () => { + const id = 'key1' + await keystore.createKey(id) + const hasKey = await keystore.hasKey(id) + strictEqual(hasKey, true) + }) - afterEach(async () => { - await keystore.clear() - await keystore.close() + it('throws an error when creating a key without an id', async () => { + let err - rmrf.sync(path.join('test', 'keys')) - }) + try { + await keystore.createKey() + } catch (e) { + err = e.toString() + } - it('creates a key', async () => { - const id = 'key1' - await keystore.createKey(id) - const hasKey = await keystore.hasKey(id) - strictEqual(hasKey, true) - }) + strictEqual(err, 'Error: id needed to create a key') + }) - it('throws an error when creating a key without an id', async () => { - let err + it('throws an error when creating a key with a null id', async () => { + let err - try { - await keystore.createKey() - } catch (e) { - err = e.toString() - } + try { + await keystore.createKey(null) + } catch (e) { + err = e.toString() + } - strictEqual(err, 'Error: id needed to create a key') - }) + strictEqual(err, 'Error: id needed to create a key') + }) - it('throws an error when creating a key with a null id', async () => { - let err + it('returns true if key exists', async () => { + const id = 'key1' - try { - await keystore.createKey(null) - } catch (e) { - err = e.toString() - } + await keystore.createKey(id) + const hasKey = await keystore.hasKey(id) + strictEqual(hasKey, true) + }) - strictEqual(err, 'Error: id needed to create a key') - }) + it('returns false if key does not exist', async () => { + const id = 'key1' + const hasKey = await keystore.hasKey(id) + strictEqual(hasKey, false) + }) - it('returns true if key exists', async () => { - const id = 'key1' + it('throws an error when checking if key exists when no id is specified', async () => { + let err + try { + await keystore.hasKey() + } catch (e) { + err = e.toString() + } + strictEqual(err, 'Error: id needed to check a key') + }) - await keystore.createKey(id) - const hasKey = await keystore.hasKey(id) - strictEqual(hasKey, true) - }) + it('gets a key', async () => { + const id = 'key1' + const keys = await keystore.createKey(id) + deepStrictEqual(await keystore.getKey(id), keys) + }) - it('returns false if key does not exist', async () => { - const id = 'key1' - const hasKey = await keystore.hasKey(id) - strictEqual(hasKey, false) - }) + it('throws an error when getting a key without an id', async () => { + const id = 'key1' + let err - it('throws an error when checking if key exists when no id is specified', async () => { - let err - try { - await keystore.hasKey() - } catch (e) { - err = e.toString() - } - strictEqual(err, 'Error: id needed to check a key') - }) + await keystore.createKey(id) - it('gets a key', async () => { - const id = 'key1' - const keys = await keystore.createKey(id) - deepStrictEqual(await keystore.getKey(id), keys) - }) + try { + await keystore.getKey() + } catch (e) { + err = e.toString() + } - it('throws an error when getting a key without an id', async () => { - const id = 'key1' - let err + strictEqual(err, 'Error: id needed to get a key') + }) - await keystore.createKey(id) + it('throws an error when getting a key with a null id', async () => { + const id = 'key1' + let err - try { - await keystore.getKey() - } catch (e) { - err = e.toString() - } + await keystore.createKey(id) - strictEqual(err, 'Error: id needed to get a key') - }) + try { + await keystore.getKey(null) + } catch (e) { + err = e.toString() + } - it('throws an error when getting a key with a null id', async () => { - const id = 'key1' - let err + strictEqual(err, 'Error: id needed to get a key') + }) - await keystore.createKey(id) + it('gets a non-existent key', async () => { + const expected = undefined + const id = 'key1' - try { - await keystore.getKey(null) - } catch (e) { - err = e.toString() - } + const actual = await keystore.getKey(id) - strictEqual(err, 'Error: id needed to get a key') - }) - - it('gets a non-existent key', async () => { - const expected = undefined - const id = 'key1' - - const actual = await keystore.getKey(id) - - strictEqual(actual, expected) - }) - - describe('signing', () => { - it('signs data', async () => { - const expected = '304402207eb6e4f4b2c56665c505696c41ec0831c6c2998620589d4b6f405d49134dea5102207e71ba37d94b7a70e3d9fb3bea7c8d8b7082c3c880b6831e9613a0a3e7aabd9f' - - const key = await keystore.getKey('userA') - const actual = await signMessage(key, 'data data data') strictEqual(actual, expected) }) - - it('throws an error if no key is passed', async () => { - let err - try { - await signMessage(null, 'data data data') - } catch (e) { - err = e.toString() - } - - strictEqual(err, 'Error: No signing key given') - }) - - it('throws an error if no data is passed', async () => { - const key = 'key_1' - let err - try { - await signMessage(key) - } catch (e) { - err = e.toString() - } - - strictEqual(err, 'Error: Given input data was undefined') - }) }) - describe('Getting the public key', async () => { - let key + describe('Using keys for signing and verifying', () => { + const fixturePath = path.join('test', 'fixtures', 'keys', 'signing-keys') + const storagePath = path.join('test', 'keys', 'signing-keys') beforeEach(async () => { - key = await keystore.getKey('userA') + await fs.copy(fixturePath, storagePath) + + // load existing keystore + const storage = await LevelStorage({ path: storagePath }) + const cache = await LRUStorage({ size: 1000 }) + const composedStorage = await ComposedStorage(storage, cache) + keystore = await KeyStore({ storage: composedStorage }) }) - it('gets the public key', async () => { - const expected = '04e0480538c2a39951d054e17ff31fde487cb1031d0044a037b53ad2e028a3e77c34e864b8579e7c7b24542959e7325361a96f1efb41ed5d3c08f0ea1e5dd0c8ed' - const publicKey = await keystore.getPublic(key) - strictEqual(publicKey, expected) + afterEach(async () => { + await keystore.clear() + await keystore.close() + + rmrf.sync(path.join('test', 'keys')) }) - it('gets the public key buffer', async () => { - const expected = { - type: 'Buffer', - data: [4, 224, 72, 5, 56, 194, 163, 153, 81, 208, 84, - 225, 127, 243, 31, 222, 72, 124, 177, 3, 29, 0, - 68, 160, 55, 181, 58, 210, 224, 40, 163, 231, 124, - 52, 232, 100, 184, 87, 158, 124, 123, 36, 84, 41, - 89, 231, 50, 83, 97, 169, 111, 30, 251, 65, 237, - 93, 60, 8, 240, 234, 30, 93, 208, 200, 237] - } - const publicKey = await keystore.getPublic(key, { format: 'buffer' }) + describe('Signing', () => { + it('signs data', async () => { + const expected = '304402207eb6e4f4b2c56665c505696c41ec0831c6c2998620589d4b6f405d49134dea5102207e71ba37d94b7a70e3d9fb3bea7c8d8b7082c3c880b6831e9613a0a3e7aabd9f' - deepStrictEqual(publicKey.toJSON(), expected) + const key = await keystore.getKey('userA') + const actual = await signMessage(key, 'data data data') + strictEqual(actual, expected) + }) + + it('throws an error if no key is passed', async () => { + let err + try { + await signMessage(null, 'data data data') + } catch (e) { + err = e.toString() + } + + strictEqual(err, 'Error: No signing key given') + }) + + it('throws an error if no data is passed', async () => { + const key = 'key_1' + let err + try { + await signMessage(key) + } catch (e) { + err = e.toString() + } + + strictEqual(err, 'Error: Given input data was undefined') + }) }) - it('gets the public key when decompress is false', async () => { - const expectedCompressedKey = signingKeys.userA.publicKey - const publicKey = await keystore.getPublic(key, { decompress: false }) - strictEqual(publicKey, expectedCompressedKey) + describe('Getting the public key', async () => { + let key + + beforeEach(async () => { + key = await keystore.getKey('userA') + }) + + it('gets the public key', async () => { + const expected = '04e0480538c2a39951d054e17ff31fde487cb1031d0044a037b53ad2e028a3e77c34e864b8579e7c7b24542959e7325361a96f1efb41ed5d3c08f0ea1e5dd0c8ed' + const publicKey = await keystore.getPublic(key) + strictEqual(publicKey, expected) + }) + + it('gets the public key buffer', async () => { + const expected = { + type: 'Buffer', + data: [4, 224, 72, 5, 56, 194, 163, 153, 81, 208, 84, + 225, 127, 243, 31, 222, 72, 124, 177, 3, 29, 0, + 68, 160, 55, 181, 58, 210, 224, 40, 163, 231, 124, + 52, 232, 100, 184, 87, 158, 124, 123, 36, 84, 41, + 89, 231, 50, 83, 97, 169, 111, 30, 251, 65, 237, + 93, 60, 8, 240, 234, 30, 93, 208, 200, 237] + } + const publicKey = await keystore.getPublic(key, { format: 'buffer' }) + + deepStrictEqual(publicKey.toJSON(), expected) + }) + + it('gets the public key when decompress is false', async () => { + const expectedCompressedKey = signingKeys.userA.publicKey + const publicKey = await keystore.getPublic(key, { decompress: false }) + strictEqual(publicKey, expectedCompressedKey) + }) + + it('gets the public key buffer when decompressed is false', async () => { + const expected = { + type: 'Buffer', + data: [3, 224, 72, 5, 56, 194, 163, 153, + 81, 208, 84, 225, 127, 243, 31, 222, + 72, 124, 177, 3, 29, 0, 68, 160, + 55, 181, 58, 210, 224, 40, 163, 231, + 124] + } + + const publicKey = await keystore.getPublic(key, { format: 'buffer', decompress: false }) + + deepStrictEqual(publicKey.toJSON(), expected) + }) + + it('throws an error if no keys are passed', async () => { + try { + await keystore.getPublic() + } catch (e) { + strictEqual(true, true) + } + }) + + it('throws an error if a bad format is passed', async () => { + try { + await keystore.getPublic(key, { format: 'foo' }) + } catch (e) { + strictEqual(true, true) + } + }) }) - it('gets the public key buffer when decompressed is false', async () => { - const expected = { - type: 'Buffer', - data: [3, 224, 72, 5, 56, 194, 163, 153, - 81, 208, 84, 225, 127, 243, 31, 222, - 72, 124, 177, 3, 29, 0, 68, 160, - 55, 181, 58, 210, 224, 40, 163, 231, - 124] - } + describe('Verifying', async function () { + let key, publicKey - const publicKey = await keystore.getPublic(key, { format: 'buffer', decompress: false }) + beforeEach(async () => { + key = await keystore.getKey('userA') + publicKey = await keystore.getPublic(key) + }) - deepStrictEqual(publicKey.toJSON(), expected) - }) + it('verifies content', async () => { + const signature = '304402207eb6e4f4b2c56665c505696c41ec0831c6c2998620589d4b6f405d49134dea5102207e71ba37d94b7a70e3d9fb3bea7c8d8b7082c3c880b6831e9613a0a3e7aabd9f' + const verified = await verifyMessage(signature, publicKey, 'data data data') + strictEqual(verified, true) + }) - it('throws an error if no keys are passed', async () => { - try { - await keystore.getPublic() - } catch (e) { - strictEqual(true, true) - } - }) + it('verifies content with cache', async () => { + const data = 'data'.repeat(1024 * 1024) + const signature = await signMessage(key, data) + const startTime = new Date().getTime() + await verifyMessage(signature, publicKey, data) + const first = new Date().getTime() + await verifyMessage(signature, publicKey, data) + const after = new Date().getTime() + console.log('First pass:', first - startTime, 'ms', 'Cached:', after - first, 'ms') + strictEqual(first - startTime > after - first, true) + }) - it('throws an error if a bad format is passed', async () => { - try { - await keystore.getPublic(key, { format: 'foo' }) - } catch (e) { - strictEqual(true, true) - } - }) - }) - - describe('Verifying', async function () { - let key, publicKey - - beforeEach(async () => { - key = await keystore.getKey('userA') - publicKey = await keystore.getPublic(key) - }) - - it('verifies content', async () => { - const signature = '304402207eb6e4f4b2c56665c505696c41ec0831c6c2998620589d4b6f405d49134dea5102207e71ba37d94b7a70e3d9fb3bea7c8d8b7082c3c880b6831e9613a0a3e7aabd9f' - const verified = await verifyMessage(signature, publicKey, 'data data data') - strictEqual(verified, true) - }) - - it('verifies content with cache', async () => { - const data = 'data'.repeat(1024 * 1024) - const signature = await signMessage(key, data) - const startTime = new Date().getTime() - await verifyMessage(signature, publicKey, data) - const first = new Date().getTime() - await verifyMessage(signature, publicKey, data) - const after = new Date().getTime() - console.log('First pass:', first - startTime, 'ms', 'Cached:', after - first, 'ms') - strictEqual(first - startTime > after - first, true) - }) - - it('does not verify content with bad signature', async () => { - const signature = 'xxxxxx' - const verified = await verifyMessage(signature, publicKey, 'data data data') - strictEqual(verified, false) + it('does not verify content with bad signature', async () => { + const signature = 'xxxxxx' + const verified = await verifyMessage(signature, publicKey, 'data data data') + strictEqual(verified, false) + }) }) }) }) From f6368c26452ab00a5a18c9f9f60159f1c800419c Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Wed, 1 Mar 2023 10:01:17 +0000 Subject: [PATCH 11/12] feat: Implement new keystore across tests. --- src/identities/identities.js | 5 ++--- src/key-store.js | 13 +++++-------- test/identities/identities.test.js | 20 +++++++++++++------- test/key-store.test.js | 2 +- test/oplog/append.test.js | 3 ++- test/oplog/crdt.test.js | 2 +- test/oplog/entry.test.js | 6 ++++-- test/oplog/heads.test.js | 2 +- test/oplog/iterator.test.js | 3 ++- test/oplog/join-concurrent.test.js | 3 ++- test/oplog/join.test.js | 3 ++- test/oplog/load.test.js | 2 +- test/oplog/log.test.js | 3 ++- test/oplog/references.test.js | 3 ++- test/storage.spec.js | 4 ++-- 15 files changed, 42 insertions(+), 32 deletions(-) diff --git a/src/identities/identities.js b/src/identities/identities.js index 59c86e1..6371548 100644 --- a/src/identities/identities.js +++ b/src/identities/identities.js @@ -3,7 +3,7 @@ import OrbitDBIdentityProvider from './providers/orbitdb.js' // import DIDIdentityProvider from './identity-providers/did.js' // import EthIdentityProvider from './identity-providers/ethereum.js' import KeyStore, { signMessage, verifyMessage } from '../key-store.js' -import { LRUStorage, IPFSBlockStorage, MemoryStorage } from '../storage/index.js' +import { LRUStorage, IPFSBlockStorage, MemoryStorage, LevelStorage } from '../storage/index.js' import path from 'path' const DefaultProviderType = 'orbitdb' @@ -16,7 +16,7 @@ const supportedTypes = { } const Identities = async ({ keystore, identityKeysPath, storage, ipfs } = {}) => { - keystore = keystore || await KeyStore(identityKeysPath || DefaultIdentityKeysPath) + keystore = keystore || await KeyStore({ storage: LevelStorage(identityKeysPath || DefaultIdentityKeysPath), valueEncoding: 'json' }) storage = storage || (ipfs ? await IPFSBlockStorage({ ipfs, pin: true }) : await MemoryStorage()) const verifiedIdentitiesCache = await LRUStorage({ size: 1000 }) @@ -35,7 +35,6 @@ const Identities = async ({ keystore, identityKeysPath, storage, ipfs } = {}) => const Provider = getProviderFor(type) const identityProvider = new Provider(options) const id = await identityProvider.getId(options) - const privateKey = await keystore.getKey(id) || await keystore.createKey(id) const publicKey = keystore.getPublic(privateKey) const idSignature = await signMessage(privateKey, id) diff --git a/src/key-store.js b/src/key-store.js index c928a5d..8ec3b95 100644 --- a/src/key-store.js +++ b/src/key-store.js @@ -83,7 +83,7 @@ const verifyMessage = async (signature, publicKey, data) => { // const verifiedCache = new LRU(1000) const KeyStore = async ({ storage } = {}) => { - storage = storage || await ComposedStorage(LevelStorage('./keystore'), LRUStorage({ size: 1000 })) + storage = storage || await ComposedStorage(await LevelStorage({ path: './keystore', valueEncoding: 'json' }), await LRUStorage({ size: 1000 })) const close = async () => { if (!storage) return @@ -143,11 +143,7 @@ const KeyStore = async ({ storage } = {}) => { privateKey: Buffer.from(keys.marshal()).toString('hex') } - try { - await storage.put(id, JSON.stringify(key)) - } catch (e) { - console.log(e) - } + await addKey(id, key) return keys } @@ -171,11 +167,12 @@ const KeyStore = async ({ storage } = {}) => { if (!storedKey) { return } - + const deserializedKey = JSON.parse(storedKey) + if (!deserializedKey) { return - } + } return unmarshal(Buffer.from(deserializedKey.privateKey, 'hex')) } diff --git a/test/identities/identities.test.js b/test/identities/identities.test.js index 2bd21a2..763fa94 100644 --- a/test/identities/identities.test.js +++ b/test/identities/identities.test.js @@ -1,6 +1,7 @@ import assert from 'assert' import path from 'path' import rmrf from 'rimraf' +import LevelStorage from '../../src/storage/level.js' import KeyStore, { signMessage, verifyMessage } from '../../src/key-store.js' import Identities, { addIdentityProvider } from '../../src/identities/identities.js' import Identity from '../../src/identities/identity.js' @@ -74,7 +75,8 @@ describe('Identities', function () { let keystore before(async () => { - keystore = await KeyStore(identityKeysPath) + const storage = await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) + keystore = await KeyStore({ storage }) identities = await Identities({ keystore }) }) @@ -142,8 +144,8 @@ describe('Identities', function () { before(async () => { await fs.copy(fixturesPath, savedKeysPath) - - savedKeysKeyStore = await KeyStore(savedKeysPath) + const storage = await LevelStorage({ path: savedKeysPath, valueEncoding: 'json' }) + savedKeysKeyStore = await KeyStore({ storage }) identities = await Identities({ keystore: savedKeysKeyStore }) identity = await identities.createIdentity({ id }) @@ -196,7 +198,8 @@ describe('Identities', function () { let keystore before(async () => { - keystore = await KeyStore(identityKeysPath) + const storage = await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) + keystore = await KeyStore({ storage }) }) after(async () => { @@ -245,7 +248,8 @@ describe('Identities', function () { let keystore before(async () => { - keystore = await KeyStore(identityKeysPath) + const storage = await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) + keystore = await KeyStore({ storage }) identities = await Identities({ keystore }) }) @@ -272,7 +276,8 @@ describe('Identities', function () { let keystore before(async () => { - keystore = await KeyStore(identityKeysPath) + const storage = await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) + keystore = await KeyStore({ storage }) identities = await Identities({ keystore }) identity = await identities.createIdentity({ id }) @@ -317,7 +322,8 @@ describe('Identities', function () { let signature before(async () => { - keystore = await KeyStore(identityKeysPath) + const storage = await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) + keystore = await KeyStore({ storage }) }) after(async () => { diff --git a/test/key-store.test.js b/test/key-store.test.js index 0e14cda..2e6ce8f 100644 --- a/test/key-store.test.js +++ b/test/key-store.test.js @@ -132,7 +132,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await fs.copy(fixturePath, storagePath) // load existing keystore - const storage = await LevelStorage({ path: storagePath }) + const storage = await LevelStorage({ path: storagePath, valueEncoding: 'json' }) const cache = await LRUStorage({ size: 1000 }) const composedStorage = await ComposedStorage(storage, cache) keystore = await KeyStore({ storage: composedStorage }) diff --git a/test/oplog/append.test.js b/test/oplog/append.test.js index 21aaf83..d501efb 100644 --- a/test/oplog/append.test.js +++ b/test/oplog/append.test.js @@ -3,6 +3,7 @@ import rimraf from 'rimraf' import { copy } from 'fs-extra' import { Log } from '../../src/oplog/index.js' import MemoryStorage from '../../src/storage/memory.js' +import LevelStorage from '../../src/storage/level.js' import { Identities } from '../../src/identities/index.js' import KeyStore from '../../src/key-store.js' @@ -28,7 +29,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = await KeyStore(identityKeysPath) + keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath }) }) const storage = await MemoryStorage() diff --git a/test/oplog/crdt.test.js b/test/oplog/crdt.test.js index 0365b40..cac935c 100644 --- a/test/oplog/crdt.test.js +++ b/test/oplog/crdt.test.js @@ -28,7 +28,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = await KeyStore(identityKeysPath) + keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath }) }) const storage = await MemoryStorage() diff --git a/test/oplog/entry.test.js b/test/oplog/entry.test.js index 2e4ab38..a204737 100644 --- a/test/oplog/entry.test.js +++ b/test/oplog/entry.test.js @@ -4,6 +4,7 @@ import { copy } from 'fs-extra' import { Entry } from '../../src/oplog/index.js' import { Identities } from '../../src/identities/index.js' import KeyStore from '../../src/key-store.js' +import LevelStorage from '../../src/storage/level.js' import { config, testAPIs, startIpfs, stopIpfs } from 'orbit-db-test-utils' // import IdentityStorage from '../src/identity-storage.js' // import IPFSBlockStorage from '../src/ipfs-block-storage.js' @@ -29,8 +30,9 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - - keystore = await KeyStore(identityKeysPath) + + const storage = await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) + keystore = await KeyStore({ storage }) identities = await Identities({ keystore, ipfs }) testIdentity = await identities.createIdentity({ id: 'userA' }) diff --git a/test/oplog/heads.test.js b/test/oplog/heads.test.js index c52fe89..e1222f6 100644 --- a/test/oplog/heads.test.js +++ b/test/oplog/heads.test.js @@ -33,7 +33,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = await KeyStore(identityKeysPath) + keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath }) }) const storage = await MemoryStorage() identities = await Identities({ keystore, storage }) diff --git a/test/oplog/iterator.test.js b/test/oplog/iterator.test.js index 9539210..abefc46 100644 --- a/test/oplog/iterator.test.js +++ b/test/oplog/iterator.test.js @@ -6,6 +6,7 @@ import KeyStore from '../../src/key-store.js' import LogCreator from './utils/log-creator.js' import all from 'it-all' import MemoryStorage from '../../src/storage/memory.js' +import LevelStorage from '../../src/storage/level.js' // Test utils import { config, testAPIs, startIpfs, stopIpfs } from 'orbit-db-test-utils' @@ -26,7 +27,7 @@ Object.keys(testAPIs).forEach((IPFS) => { let testIdentity, testIdentity2, testIdentity3 before(async () => { - keystore = await KeyStore('./keys_1') + keystore = await KeyStore({ storage: await LevelStorage({ path: './keys_1', valueEncoding: 'json' })}) for (const [key, value] of Object.entries(identityKeys)) { await keystore.addKey(key, value) diff --git a/test/oplog/join-concurrent.test.js b/test/oplog/join-concurrent.test.js index 963709b..e95997c 100644 --- a/test/oplog/join-concurrent.test.js +++ b/test/oplog/join-concurrent.test.js @@ -4,6 +4,7 @@ import { copy } from 'fs-extra' import { Log } from '../../src/oplog/index.js' import { Identities } from '../../src/identities/index.js' import KeyStore from '../../src/key-store.js' +import LevelStorage from '../../src/storage/level.js' import MemoryStorage from '../../src/storage/memory.js' // Test utils @@ -29,7 +30,7 @@ Object.keys(testAPIs).forEach(IPFS => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = await KeyStore(identityKeysPath) + keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) }) const storage = await MemoryStorage() diff --git a/test/oplog/join.test.js b/test/oplog/join.test.js index 332916e..a42cd01 100644 --- a/test/oplog/join.test.js +++ b/test/oplog/join.test.js @@ -3,6 +3,7 @@ import rimraf from 'rimraf' import { Log, Clock } from '../../src/oplog/index.js' import { Identities } from '../../src/identities/index.js' import KeyStore from '../../src/key-store.js' +import LevelStorage from '../../src/storage/level.js' import MemoryStorage from '../../src/storage/memory.js' // Test utils @@ -26,7 +27,7 @@ Object.keys(testAPIs).forEach((IPFS) => { let testIdentity, testIdentity2, testIdentity3, testIdentity4 before(async () => { - keystore = await KeyStore('./keys_1') + keystore = await KeyStore({ storage: await LevelStorage({ path: './keys_1', valueEncoding: 'json' }) }) for (const [key, value] of Object.entries(identityKeys)) { await keystore.addKey(key, value) diff --git a/test/oplog/load.test.js b/test/oplog/load.test.js index 01f402e..8ae4f0b 100644 --- a/test/oplog/load.test.js +++ b/test/oplog/load.test.js @@ -49,7 +49,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = await KeyStore(identityKeysPath) + keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) }) testIdentity = await createIdentity({ id: 'userC', keystore }) testIdentity2 = await createIdentity({ id: 'userB', keystore }) diff --git a/test/oplog/log.test.js b/test/oplog/log.test.js index 5df1834..80fe57b 100644 --- a/test/oplog/log.test.js +++ b/test/oplog/log.test.js @@ -4,6 +4,7 @@ import { Log, Entry } from '../../src/oplog/index.js' import { Identities } from '../../src/identities/index.js' import KeyStore from '../../src/key-store.js' import { copy } from 'fs-extra' +import LevelStorage from '../../src/storage/level.js' import MemoryStorage from '../../src/storage/memory.js' // Test utils @@ -28,7 +29,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = await KeyStore(identityKeysPath) + keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) }) const storage = await MemoryStorage() diff --git a/test/oplog/references.test.js b/test/oplog/references.test.js index cffe9b1..ca2734f 100644 --- a/test/oplog/references.test.js +++ b/test/oplog/references.test.js @@ -4,6 +4,7 @@ import { copy } from 'fs-extra' import { Log } from '../../src/oplog/index.js' import { Identities } from '../../src/identities/index.js' import KeyStore from '../../src/key-store.js' +import LevelStorage from '../../src/storage/level.js' import MemoryStorage from '../../src/storage/memory.js' // Test utils @@ -29,7 +30,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = await KeyStore(identityKeysPath) + keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) }) const storage = await MemoryStorage() diff --git a/test/storage.spec.js b/test/storage.spec.js index f60f31c..691b0b5 100644 --- a/test/storage.spec.js +++ b/test/storage.spec.js @@ -4,7 +4,7 @@ import rimraf from 'rimraf' import { Log } from '../src/oplog/index.js' import { Identities } from '../src/identities/index.js' import KeyStore from '../src/key-store.js' -import { IPFSBlockStorage, MemoryStorage, LRUStorage, ComposedStorage } from '../src/storage/index.js' +import { IPFSBlockStorage, MemoryStorage, LRUStorage, ComposedStorage, LevelStorage } from '../src/storage/index.js' import { copy } from 'fs-extra' // Test utils @@ -34,7 +34,7 @@ Object.keys(testAPIs).forEach((_) => { // Start an IPFS instance ipfs1 = await IPFS.create({ ...config.daemon1, repo: './ipfs1' }) - keystore = await KeyStore(identityKeysPath) + keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) }) const storage = await MemoryStorage() const identities = await Identities({ keystore, storage }) From 1575f00912d28db382f8e777aa2e8161576220e3 Mon Sep 17 00:00:00 2001 From: haad Date: Wed, 1 Mar 2023 14:42:58 +0200 Subject: [PATCH 12/12] Add new test keys fixtures database --- src/identities/identities.js | 2 +- src/key-store.js | 47 ++++---------- test/db/keyvalue-persisted.js | 4 +- test/db/keyvalue.test.js | 4 +- test/fixtures/newtestkeys2/001232.ldb | Bin 0 -> 3272 bytes test/fixtures/newtestkeys2/001274.log | 0 test/fixtures/newtestkeys2/CURRENT | 1 + test/fixtures/newtestkeys2/LOCK | 0 test/fixtures/newtestkeys2/LOG | 3 + test/fixtures/newtestkeys2/LOG.old | 3 + test/fixtures/newtestkeys2/MANIFEST-001273 | Bin 0 -> 185 bytes test/fixtures/orbit-db-identity-keys.js | 18 +++--- test/fixtures/test-keys-path.js | 1 + test/identities/identities.test.js | 39 +++++------ test/key-store.test.js | 71 +++++++++++---------- test/oplog/crdt.test.js | 18 ++---- test/oplog/entry.test.js | 8 +-- test/oplog/heads.test.js | 5 +- test/oplog/join-concurrent.test.js | 19 ++---- test/oplog/log.test.js | 5 +- test/oplog/references.test.js | 5 +- test/storage.spec.js | 7 +- 22 files changed, 108 insertions(+), 152 deletions(-) create mode 100644 test/fixtures/newtestkeys2/001232.ldb create mode 100644 test/fixtures/newtestkeys2/001274.log create mode 100644 test/fixtures/newtestkeys2/CURRENT create mode 100644 test/fixtures/newtestkeys2/LOCK create mode 100644 test/fixtures/newtestkeys2/LOG create mode 100644 test/fixtures/newtestkeys2/LOG.old create mode 100644 test/fixtures/newtestkeys2/MANIFEST-001273 create mode 100644 test/fixtures/test-keys-path.js diff --git a/src/identities/identities.js b/src/identities/identities.js index 6371548..cc08e3d 100644 --- a/src/identities/identities.js +++ b/src/identities/identities.js @@ -16,7 +16,7 @@ const supportedTypes = { } const Identities = async ({ keystore, identityKeysPath, storage, ipfs } = {}) => { - keystore = keystore || await KeyStore({ storage: LevelStorage(identityKeysPath || DefaultIdentityKeysPath), valueEncoding: 'json' }) + keystore = keystore || await KeyStore({ storage: await LevelStorage(identityKeysPath || DefaultIdentityKeysPath), valueEncoding: 'json' }) storage = storage || (ipfs ? await IPFSBlockStorage({ ipfs, pin: true }) : await MemoryStorage()) const verifiedIdentitiesCache = await LRUStorage({ size: 1000 }) diff --git a/src/key-store.js b/src/key-store.js index 8ec3b95..becac4d 100644 --- a/src/key-store.js +++ b/src/key-store.js @@ -33,6 +33,7 @@ const verifySignature = async (signature, publicKey, data) => { } catch (e) { // Catch error: sig length wrong } + return Promise.resolve(res) } @@ -67,12 +68,7 @@ const verifyMessage = async (signature, publicKey, data) => { } } else { const compare = (cached, data) => { - /* let match - if (v === 'v0') { - match = Buffer.compare(Buffer.alloc(30, cached), Buffer.alloc(30, data)) === 0 - } else { */ - const match = Buffer.isBuffer(data) ? Buffer.compare(cached, data) === 0 : cached === data - // } + const match = Buffer.isBuffer(data) ? Buffer.compare(cached, data) === 0 : cached.toString() === data.toString() return match } res = cached.publicKey === publicKey && compare(cached.data, data) @@ -80,18 +76,14 @@ const verifyMessage = async (signature, publicKey, data) => { return res } -// const verifiedCache = new LRU(1000) - -const KeyStore = async ({ storage } = {}) => { - storage = storage || await ComposedStorage(await LevelStorage({ path: './keystore', valueEncoding: 'json' }), await LRUStorage({ size: 1000 })) +const KeyStore = async ({ storage, path } = {}) => { + storage = storage || await ComposedStorage(await LevelStorage({ path: path || './keystore' }), await LRUStorage({ size: 1000 })) const close = async () => { - if (!storage) return await storage.close() } const clear = async () => { - if (!storage) return await storage.clear() } @@ -99,13 +91,10 @@ const KeyStore = async ({ storage } = {}) => { if (!id) { throw new Error('id needed to check a key') } - if (storage.status && storage.status !== 'open') { - return null - } let hasKey = false try { - const storedKey = await storage.get(id) + const storedKey = await storage.get('private_' + id) hasKey = storedKey !== undefined && storedKey !== null } catch (e) { // Catches 'Error: ENOENT: no such file or directory, open ' @@ -117,7 +106,8 @@ const KeyStore = async ({ storage } = {}) => { const addKey = async (id, key) => { try { - await storage.put(id, JSON.stringify(key)) + await storage.put('public_' + id, key.publicKey) + await storage.put('private_' + id, key.privateKey) } catch (e) { console.log(e) } @@ -127,10 +117,6 @@ const KeyStore = async ({ storage } = {}) => { if (!id) { throw new Error('id needed to create a key') } - // if (storage.status && storage.status !== 'open') { - // console.log("22::", id) - // return null - // } // Generate a private key const pair = await crypto.keys.generateKeyPair('secp256k1') @@ -139,8 +125,8 @@ const KeyStore = async ({ storage } = {}) => { const decompressedKey = secp256k1.publicKeyConvert(Buffer.from(pubKey), false) const key = { - publicKey: Buffer.from(decompressedKey).toString('hex'), - privateKey: Buffer.from(keys.marshal()).toString('hex') + publicKey: Buffer.from(decompressedKey),//.toString('hex'), + privateKey: Buffer.from(keys.marshal())//.toString('hex') } await addKey(id, key) @@ -153,13 +139,9 @@ const KeyStore = async ({ storage } = {}) => { throw new Error('id needed to get a key') } - if (storage.status && storage.status !== 'open') { - return null - } - let storedKey try { - storedKey = await storage.get(id) + storedKey = await storage.get('private_' + id) } catch (e) { // ignore ENOENT error } @@ -168,13 +150,8 @@ const KeyStore = async ({ storage } = {}) => { return } - const deserializedKey = JSON.parse(storedKey) - - if (!deserializedKey) { - return - } - - return unmarshal(Buffer.from(deserializedKey.privateKey, 'hex')) + // return unmarshal(Buffer.from(deserializedKey.privateKey, 'hex')) + return unmarshal(storedKey) } const getPublic = (keys, options = {}) => { diff --git a/test/db/keyvalue-persisted.js b/test/db/keyvalue-persisted.js index 3ce2874..0e12967 100644 --- a/test/db/keyvalue-persisted.js +++ b/test/db/keyvalue-persisted.js @@ -79,14 +79,14 @@ Object.keys(testAPIs).forEach((IPFS) => { }) it('sets a key/value pair', async () => { - const expected = 'zdpuAyRbzMUs1v7B1gqRRHe6rnxwYbHKzDhxh3rJanEjoucHt' + const expected = 'zdpuAuXyxGeC6QC2rykxcdZFUoyRromkc9zMHz3LwLHxVVz2x' const actual = await db.set('key1', 'value1') strictEqual(actual, expected) }) it('puts a key/value pair', async () => { - const expected = 'zdpuAyRbzMUs1v7B1gqRRHe6rnxwYbHKzDhxh3rJanEjoucHt' + const expected = 'zdpuAuXyxGeC6QC2rykxcdZFUoyRromkc9zMHz3LwLHxVVz2x' const actual = await db.put('key1', 'value1') strictEqual(actual, expected) diff --git a/test/db/keyvalue.test.js b/test/db/keyvalue.test.js index a8ade8e..ff72727 100644 --- a/test/db/keyvalue.test.js +++ b/test/db/keyvalue.test.js @@ -80,14 +80,14 @@ Object.keys(testAPIs).forEach((IPFS) => { }) it('sets a key/value pair', async () => { - const expected = 'zdpuAyRbzMUs1v7B1gqRRHe6rnxwYbHKzDhxh3rJanEjoucHt' + const expected = 'zdpuAuXyxGeC6QC2rykxcdZFUoyRromkc9zMHz3LwLHxVVz2x' const actual = await db.set('key1', 'value1') strictEqual(actual, expected) }) it('puts a key/value pair', async () => { - const expected = 'zdpuAyRbzMUs1v7B1gqRRHe6rnxwYbHKzDhxh3rJanEjoucHt' + const expected = 'zdpuAuXyxGeC6QC2rykxcdZFUoyRromkc9zMHz3LwLHxVVz2x' const actual = await db.put('key1', 'value1') strictEqual(actual, expected) diff --git a/test/fixtures/newtestkeys2/001232.ldb b/test/fixtures/newtestkeys2/001232.ldb new file mode 100644 index 0000000000000000000000000000000000000000..a3df6b5e38fcd792f7a757d971a7d7c02d7d8dbb GIT binary patch literal 3272 zcmb_edpK438n#Da6S+j9%&y!@SogJ;8EU(gp^S)-w5}3zzfDo0RJzcq(bRS;%+x_M z)YPbm(B#rd$t2fG5)GL&rHjsD@AKz*p7T7;`fu&u_j}*>yX^N{3hPA?QNeL!3>yeQ zfEa@@n4m$NfN6$?A(G-angnSAfH?wVATh+SC<+541%nvQU>Hb~`~f9~a2O+K!5oF@ zCqvzC^Wc%5B5+-!!C>3Olep_Q4%X0>Ug|fs1+LylaMd*;m;qT1CqaaP7=*_MNsfX^ z2tmX+N)uu+hp;>#&At}X zq--0ySS#O@_%oJqzBeN{o{G-{qHva>5QJmJEWja*n85*#rNksb!vF;XI0S+qK*A`> z(gX(q0EMs!K_VE8Gl;-kMy2M^=y@gG|H6{Ivisk^8tq=}av*K7TE%$pEX@9g5eGG% zip3!WClMM%Q3~Tz6hkBg(>Op=7*9=+5Q`ubO|S%mK@5$ceCh~aNIp-NKnVm9%#u-U z+OX^EBF^K9)z*=C^`D+Mhil(g*z>pQ*M*lRUY{$P=oPw%V2}k_ny))WgA~b75C&oh ziNFLxK>&-2aT*aLBtswoL9!6Q04Rczd8p%e0X(~3-?l_&uKAs9h1Tuku)9lqOK(AMB>A)#U0!y_W2qGMv?;u8|T6U>tV zN&|j4XJQf${GmGV&4gag(PWzvvol%}?qt5&Z0pm zba&N+L%p~D82#6*E5=m^bpi$&ww(FV7Ezfa3SkpL!7EvK)&A#K9m*2k`v!B>#vMt$ zcUyPtFF5}pZ%KE?qC@KgcByEHB4R05Hc_B0yI}b_`j(IxY?AJ1GV1L<^wey0!|v!;aVx!q8iQo%ad|=F&SBXcn_+){V#qBp&W4~?k4GrCMq0>5zwKpGL5xdIHjpbt<1g5gsJ#UX> zn@AsLj+2rmKigAM1=dGBb?6!a5v~@gL!9&6)85&IgKWtA;#yQLWy?t>OI@Du?53N`GeI(ad;}g}S z(K}nOTzliip#g8>g{vMpuQ;K9+Mp*@k|l2RP4gelym#7g(95Dne5hzbxJEK#UtA#9 z(v#=kx0!VvD0KWKDCx)7dAWrprdk2xr1j{6aA5D^)&yS5jE&PwuH~isNpxR>cK&i%3G7G=cA6z;Zac6eSL*?71ArJNs-LDwuwanOA73EqE)vF#Gn?t;H zI9?E+^SS(1PFF~hVb#03wCk?bsX(3SVx?8;S4VW3PnB$NK9D@2zr%skV0>QR(N}YS z)puN)q2eN$x%KMG#heOy6=!#PS#5pKkiAuc z@<1Pu*u2fI^k8{I@3Me+&(a2;t=DQ66dRQAg_*H8&zEblHF{gH*3vE0;De3vn%LX> z#|NY>wqrhieY>;I&TD>m!>`eGzDW4yo@i6Y(eY@*)N2Q}6{Jiq+wj18wMYLiA+x3i zW~Sz?{GTSB&Cj<~-G&VYibC`&EjpH-==wQHH!SzFbmK9l#&F5gU4hQ`BEC%h%EXT` z_v>FD;T~A@_{1}$*pM_?`LB}fsezfPyU&$t$wMz2tuesUmmbbKzt(ueqImcA#PA?E z#Nd<7S1+DdWoQqx!m&o>mx*Txi+&*3N*(hy#Z9Y$9AD7W zZkYW}Nk1*YroVT2p3P?^%cdx{TIHfgC8oNzzvfDd@*K(Z0vVO#owqpoy0 z73ar)ywjHZlb83rLLWP0&5}*a+aL&nnJ+qPn!e%fRS(Y_L2>?R-!F<%seYcG zchsgrS!2q4r#6h195M2obI?QE+|0uP+o3+~=PP#{JjI39g#u~q{e3-Z-d8{Es@C;X z#1H#89o@XB-@3BITI3eABz4}mXV<+6&CXHeQz{&Z`~nXe<7!Urp@ z9Ixf(P+d-Kb$UBnUidnW*;d?Aeq`16^ixO?*f^TEZ#Yp$v@{{KT_?AG_)qsTHKD{_ zFk22*Tep8l#fKX3?{pX6{0{@pZ8oAg?=-!iSRbEV^CqKMOVxtiyC_rhv)S7RdR;n~ z8ip2R { - rmrf('./keys_1') + // rmrf('./keys_1') - const keystore = await KeyStore() - for (const [key, value] of Object.entries(identityKeys)) { - await keystore.addKey(key, value) - } + // const keystore = await KeyStore() + const keystore = await KeyStore({ path: testKeysPath }) + // for (const [key, value] of Object.entries(identityKeys)) { + // await keystore.addKey(key, value) + // } - for (const [key, value] of Object.entries(signingKeys)) { - await keystore.addKey(key, value) - } + // for (const [key, value] of Object.entries(signingKeys)) { + // await keystore.addKey(key, value) + // } // Create an identity for each peers const identities1 = await Identities({ keystore, ipfs: ipfs1 }) diff --git a/test/fixtures/test-keys-path.js b/test/fixtures/test-keys-path.js new file mode 100644 index 0000000..0ac277f --- /dev/null +++ b/test/fixtures/test-keys-path.js @@ -0,0 +1 @@ +export default './test/fixtures/newtestkeys2' \ No newline at end of file diff --git a/test/identities/identities.test.js b/test/identities/identities.test.js index 763fa94..2af63ec 100644 --- a/test/identities/identities.test.js +++ b/test/identities/identities.test.js @@ -9,6 +9,7 @@ import fs from 'fs-extra' const fixturesPath = path.resolve('./test/identities/fixtures/keys') const savedKeysPath = path.resolve('./test/identities/fixtures/savedKeys') const identityKeysPath = path.resolve('./test/identities/identityKeys') +import testKeysPath from '../fixtures/test-keys-path.js ' const type = 'orbitdb' describe('Identities', function () { @@ -21,7 +22,7 @@ describe('Identities', function () { }) describe('Creating Identities', () => { - const id = 'A' + const id = 'userA' let identities let identity @@ -42,7 +43,7 @@ describe('Identities', function () { }) describe('Get Identity', () => { - const id = 'A' + const id = 'userA' let identities let identity @@ -67,17 +68,15 @@ describe('Identities', function () { }) }) - describe('Passing in custom keystore', async () => { - const id = 'B' + describe.skip('Passing in custom keystore', async () => { + const id = 'userB' let identity let identities let keystore before(async () => { - const storage = await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) - keystore = await KeyStore({ storage }) - + keystore = await KeyStore({ path: testKeysPath }) identities = await Identities({ keystore }) }) @@ -132,20 +131,18 @@ describe('Identities', function () { }) describe('create an identity with saved keys', () => { - const id = 'QmPhnEjVkYE1Ym7F5MkRUfkD6NtuSptE7ugu1Ggr149W2X' + const id = 'userX' - const expectedPublicKey = '040d78ff62afb656ac62db1aae3b1536a614991e28bb4d721498898b7d4194339640cd18c37b259e2c77738de0d6f9a5d52e0b936611de6b6ba78891a8b2a38317' - const expectedIdSignature = '30450221009de7b91952d73f577e85962aa6301350865212e3956862f80f4ebb626ffc126b022027d57415fb145b7e06cf06320fbfa63ea98a958b065726fe86eaab809a6bf607' - const expectedPkIdSignature = '304402202806e7c2406ca1f35961d38adc3997c179e142d54e1ca838ace373fae27124fd02200d6ca3aea6e1341bf5e4e0b84b559bbeefecfade34115de266a69d04d924905e' + const expectedPublicKey = '0442fa42a69135eade1e37ea520bc8ee9e240efd62cb0edf0516b21258b4eae656241c40da462c95189b1ade83419138ca59845beb90d29b1be8542bde388ca5f9' + const expectedIdSignature = '3044022068b4bc360d127e39164fbc3b5184f5bd79cc5976286f793d9b38d1f2818e0259022027b875dc8c73635b32db72177b9922038ec4b1eabc8f1fd0919806b0b2519419' + const expectedPkIdSignature = '304402206d1aeff3a874b7bd83300219badf68bbcb514e2c60a7b40cec5f78ff2b7ba0f20220085f5f138730603418a0570ba12720f0a46997527bb4a077cd26b545e7811c31' let identities let identity let savedKeysKeyStore before(async () => { - await fs.copy(fixturesPath, savedKeysPath) - const storage = await LevelStorage({ path: savedKeysPath, valueEncoding: 'json' }) - savedKeysKeyStore = await KeyStore({ storage }) + savedKeysKeyStore = await KeyStore({ path: testKeysPath }) identities = await Identities({ keystore: savedKeysKeyStore }) identity = await identities.createIdentity({ id }) @@ -198,8 +195,7 @@ describe('Identities', function () { let keystore before(async () => { - const storage = await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) - keystore = await KeyStore({ storage }) + keystore = await KeyStore({ path: testKeysPath }) }) after(async () => { @@ -248,9 +244,7 @@ describe('Identities', function () { let keystore before(async () => { - const storage = await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) - keystore = await KeyStore({ storage }) - + keystore = await KeyStore({ path: testKeysPath }) identities = await Identities({ keystore }) }) @@ -276,9 +270,7 @@ describe('Identities', function () { let keystore before(async () => { - const storage = await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) - keystore = await KeyStore({ storage }) - + keystore = await KeyStore({ path: testKeysPath }) identities = await Identities({ keystore }) identity = await identities.createIdentity({ id }) }) @@ -322,8 +314,7 @@ describe('Identities', function () { let signature before(async () => { - const storage = await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) - keystore = await KeyStore({ storage }) + keystore = await KeyStore({ path: testKeysPath }) }) after(async () => { diff --git a/test/key-store.test.js b/test/key-store.test.js index 2e6ce8f..eab27f1 100644 --- a/test/key-store.test.js +++ b/test/key-store.test.js @@ -7,7 +7,9 @@ import { testAPIs } from 'orbit-db-test-utils' import path from 'path' import fs from 'fs-extra' import rmrf from 'rimraf' -import { signingKeys } from './fixtures/orbit-db-identity-keys.js' +import { identityKeys, signingKeys } from './fixtures/orbit-db-identity-keys.js' +import { Identities } from '../src/identities/index.js' +import testKeysPath from './fixtures/test-keys-path.js ' Object.keys(testAPIs).forEach((IPFS) => { describe('KeyStore (' + IPFS + ')', () => { @@ -15,11 +17,10 @@ Object.keys(testAPIs).forEach((IPFS) => { describe('Creating and retrieving keys', () => { beforeEach(async () => { - keystore = await KeyStore() + keystore = await KeyStore({ path: testKeysPath }) }) afterEach(async () => { - await keystore.clear() await keystore.close() }) @@ -63,7 +64,7 @@ Object.keys(testAPIs).forEach((IPFS) => { }) it('returns false if key does not exist', async () => { - const id = 'key1' + const id = 'key1234567890' const hasKey = await keystore.hasKey(id) strictEqual(hasKey, false) }) @@ -116,7 +117,7 @@ Object.keys(testAPIs).forEach((IPFS) => { it('gets a non-existent key', async () => { const expected = undefined - const id = 'key1' + const id = 'key111111111' const actual = await keystore.getKey(id) @@ -125,29 +126,23 @@ Object.keys(testAPIs).forEach((IPFS) => { }) describe('Using keys for signing and verifying', () => { - const fixturePath = path.join('test', 'fixtures', 'keys', 'signing-keys') - const storagePath = path.join('test', 'keys', 'signing-keys') - beforeEach(async () => { - await fs.copy(fixturePath, storagePath) - - // load existing keystore - const storage = await LevelStorage({ path: storagePath, valueEncoding: 'json' }) - const cache = await LRUStorage({ size: 1000 }) - const composedStorage = await ComposedStorage(storage, cache) - keystore = await KeyStore({ storage: composedStorage }) + keystore = await KeyStore({ path: testKeysPath }) + // const identities = await Identities({ keystore }) + // const a = await identities.createIdentity({ id: 'userA' }) + // const b = await identities.createIdentity({ id: 'userB' }) + // const c = await identities.createIdentity({ id: 'userC' }) + // const d = await identities.createIdentity({ id: 'userD' }) + // const x = await identities.createIdentity({ id: 'userX' }) }) afterEach(async () => { - await keystore.clear() await keystore.close() - - rmrf.sync(path.join('test', 'keys')) }) describe('Signing', () => { it('signs data', async () => { - const expected = '304402207eb6e4f4b2c56665c505696c41ec0831c6c2998620589d4b6f405d49134dea5102207e71ba37d94b7a70e3d9fb3bea7c8d8b7082c3c880b6831e9613a0a3e7aabd9f' + const expected = '3045022100df961fa46bb8a3cb92594a24205e6008a84daa563ac3530f583bb9f9cef5af3b02207b84c5d63387d0a710e42e05785fbccdaf2534c8ed16adb8afd57c3eba930529' const key = await keystore.getKey('userA') const actual = await signMessage(key, 'data data data') @@ -186,7 +181,7 @@ Object.keys(testAPIs).forEach((IPFS) => { }) it('gets the public key', async () => { - const expected = '04e0480538c2a39951d054e17ff31fde487cb1031d0044a037b53ad2e028a3e77c34e864b8579e7c7b24542959e7325361a96f1efb41ed5d3c08f0ea1e5dd0c8ed' + const expected = '04e7247a4c155b63d182a23c70cb6fe8ba2e44bc9e9d62dc45d4c4167ccde95944f13db3c707da2ee0e3fd6ba531caef9f86eb79132023786cd6139ec5ebed4fae' const publicKey = await keystore.getPublic(key) strictEqual(publicKey, expected) }) @@ -194,12 +189,14 @@ Object.keys(testAPIs).forEach((IPFS) => { it('gets the public key buffer', async () => { const expected = { type: 'Buffer', - data: [4, 224, 72, 5, 56, 194, 163, 153, 81, 208, 84, - 225, 127, 243, 31, 222, 72, 124, 177, 3, 29, 0, - 68, 160, 55, 181, 58, 210, 224, 40, 163, 231, 124, - 52, 232, 100, 184, 87, 158, 124, 123, 36, 84, 41, - 89, 231, 50, 83, 97, 169, 111, 30, 251, 65, 237, - 93, 60, 8, 240, 234, 30, 93, 208, 200, 237] + data: [ + 4, 231, 36, 122, 76, 21, 91, 99, 209, 130, 162, + 60, 112, 203, 111, 232, 186, 46, 68, 188, 158, 157, + 98, 220, 69, 212, 196, 22, 124, 205, 233, 89, 68, + 241, 61, 179, 199, 7, 218, 46, 224, 227, 253, 107, + 165, 49, 202, 239, 159, 134, 235, 121, 19, 32, 35, + 120, 108, 214, 19, 158, 197, 235, 237, 79, 174 + ] } const publicKey = await keystore.getPublic(key, { format: 'buffer' }) @@ -207,7 +204,8 @@ Object.keys(testAPIs).forEach((IPFS) => { }) it('gets the public key when decompress is false', async () => { - const expectedCompressedKey = signingKeys.userA.publicKey + // const expectedCompressedKey = signingKeys.userA.publicKey + const expectedCompressedKey = '02e7247a4c155b63d182a23c70cb6fe8ba2e44bc9e9d62dc45d4c4167ccde95944' const publicKey = await keystore.getPublic(key, { decompress: false }) strictEqual(publicKey, expectedCompressedKey) }) @@ -215,11 +213,13 @@ Object.keys(testAPIs).forEach((IPFS) => { it('gets the public key buffer when decompressed is false', async () => { const expected = { type: 'Buffer', - data: [3, 224, 72, 5, 56, 194, 163, 153, - 81, 208, 84, 225, 127, 243, 31, 222, - 72, 124, 177, 3, 29, 0, 68, 160, - 55, 181, 58, 210, 224, 40, 163, 231, - 124] + data: [ + 2, 231, 36, 122, 76, 21, 91, 99, + 209, 130, 162, 60, 112, 203, 111, 232, + 186, 46, 68, 188, 158, 157, 98, 220, + 69, 212, 196, 22, 124, 205, 233, 89, + 68 + ] } const publicKey = await keystore.getPublic(key, { format: 'buffer', decompress: false }) @@ -253,8 +253,11 @@ Object.keys(testAPIs).forEach((IPFS) => { }) it('verifies content', async () => { - const signature = '304402207eb6e4f4b2c56665c505696c41ec0831c6c2998620589d4b6f405d49134dea5102207e71ba37d94b7a70e3d9fb3bea7c8d8b7082c3c880b6831e9613a0a3e7aabd9f' - const verified = await verifyMessage(signature, publicKey, 'data data data') + const signature = await signMessage(key, 'data data data') + const expectedSignature = '3045022100df961fa46bb8a3cb92594a24205e6008a84daa563ac3530f583bb9f9cef5af3b02207b84c5d63387d0a710e42e05785fbccdaf2534c8ed16adb8afd57c3eba930529' + strictEqual(expectedSignature, signature) + + const verified = await verifyMessage(expectedSignature, publicKey, 'data data data') strictEqual(verified, true) }) diff --git a/test/oplog/crdt.test.js b/test/oplog/crdt.test.js index cac935c..93b6eac 100644 --- a/test/oplog/crdt.test.js +++ b/test/oplog/crdt.test.js @@ -5,9 +5,9 @@ import { Log } from '../../src/oplog/index.js' import { Identities } from '../../src/identities/index.js' import KeyStore from '../../src/key-store.js' import MemoryStorage from '../../src/storage/memory.js' - -// Test utils +import LevelStorage from '../../src/storage/level.js' import { config, testAPIs } from 'orbit-db-test-utils' +import testKeysPath from '../fixtures/test-keys-path.js ' const { sync: rmrf } = rimraf const { createIdentity } = Identities @@ -24,20 +24,14 @@ Object.keys(testAPIs).forEach((IPFS) => { let identities1, identities2, identities3 before(async () => { - rmrf(identityKeysPath) - await copy(identityKeyFixtures, identityKeysPath) - await copy(signingKeyFixtures, identityKeysPath) - - keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath }) }) + keystore = await KeyStore({ path: testKeysPath }) const storage = await MemoryStorage() - identities1 = await Identities({ keystore, storage }) - identities2 = await Identities({ keystore, storage }) - identities3 = await Identities({ keystore, storage }) + identities1 = await Identities({ keystore }) testIdentity = await identities1.createIdentity({ id: 'userA' }) - testIdentity2 = await identities2.createIdentity({ id: 'userB' }) - testIdentity3 = await identities3.createIdentity({ id: 'userC' }) + testIdentity2 = await identities1.createIdentity({ id: 'userB' }) + testIdentity3 = await identities1.createIdentity({ id: 'userC' }) }) after(async () => { diff --git a/test/oplog/entry.test.js b/test/oplog/entry.test.js index a204737..35035e2 100644 --- a/test/oplog/entry.test.js +++ b/test/oplog/entry.test.js @@ -6,8 +6,7 @@ import { Identities } from '../../src/identities/index.js' import KeyStore from '../../src/key-store.js' import LevelStorage from '../../src/storage/level.js' import { config, testAPIs, startIpfs, stopIpfs } from 'orbit-db-test-utils' -// import IdentityStorage from '../src/identity-storage.js' -// import IPFSBlockStorage from '../src/ipfs-block-storage.js' +import testKeysPath from '../fixtures/test-keys-path.js ' const { sync: rmrf } = rimraf const { createIdentity } = Identities @@ -31,9 +30,8 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - const storage = await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) - keystore = await KeyStore({ storage }) - + keystore = await KeyStore({ path: testKeysPath }) + identities = await Identities({ keystore, ipfs }) testIdentity = await identities.createIdentity({ id: 'userA' }) }) diff --git a/test/oplog/heads.test.js b/test/oplog/heads.test.js index e1222f6..0286ce3 100644 --- a/test/oplog/heads.test.js +++ b/test/oplog/heads.test.js @@ -5,9 +5,8 @@ import { Log } from '../../src/oplog/index.js' import { Identities } from '../../src/identities/index.js' import KeyStore from '../../src/key-store.js' import MemoryStorage from '../../src/storage/memory.js' - -// Test utils import { config, testAPIs } from 'orbit-db-test-utils' +import testKeysPath from '../fixtures/test-keys-path.js ' const { sync: rmrf } = rimraf const { createIdentity } = Identities @@ -33,7 +32,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath }) }) + keystore = await KeyStore({ path: testKeysPath }) const storage = await MemoryStorage() identities = await Identities({ keystore, storage }) diff --git a/test/oplog/join-concurrent.test.js b/test/oplog/join-concurrent.test.js index e95997c..f27d8cf 100644 --- a/test/oplog/join-concurrent.test.js +++ b/test/oplog/join-concurrent.test.js @@ -6,12 +6,10 @@ import { Identities } from '../../src/identities/index.js' import KeyStore from '../../src/key-store.js' import LevelStorage from '../../src/storage/level.js' import MemoryStorage from '../../src/storage/memory.js' - -// Test utils import { config, testAPIs } from 'orbit-db-test-utils' +import testKeysPath from '../fixtures/test-keys-path.js ' const { sync: rmrf } = rimraf -const { createIdentity } = Identities let testIdentity, testIdentity2 @@ -25,24 +23,15 @@ Object.keys(testAPIs).forEach(IPFS => { let identities1, identities2 before(async () => { - rmrf(identityKeysPath) + keystore = await KeyStore({ path: testKeysPath }) - await copy(identityKeyFixtures, identityKeysPath) - await copy(signingKeyFixtures, identityKeysPath) - - keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) }) - - const storage = await MemoryStorage() - - identities1 = await Identities({ keystore, storage }) - identities2 = await Identities({ keystore, storage }) + identities1 = await Identities({ keystore }) testIdentity = await identities1.createIdentity({ id: 'userA' }) - testIdentity2 = await identities2.createIdentity({ id: 'userB' }) + testIdentity2 = await identities1.createIdentity({ id: 'userB' }) }) after(async () => { await keystore.close() - rmrf(identityKeysPath) }) describe('join ', async () => { diff --git a/test/oplog/log.test.js b/test/oplog/log.test.js index 80fe57b..f49681e 100644 --- a/test/oplog/log.test.js +++ b/test/oplog/log.test.js @@ -6,9 +6,8 @@ import KeyStore from '../../src/key-store.js' import { copy } from 'fs-extra' import LevelStorage from '../../src/storage/level.js' import MemoryStorage from '../../src/storage/memory.js' - -// Test utils import { config, testAPIs } from 'orbit-db-test-utils' +import testKeysPath from '../fixtures/test-keys-path.js ' const { sync: rmrf } = rimraf const { create } = Entry @@ -29,7 +28,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) }) + keystore = await KeyStore({ path: testKeysPath }) const storage = await MemoryStorage() diff --git a/test/oplog/references.test.js b/test/oplog/references.test.js index ca2734f..8b3f1be 100644 --- a/test/oplog/references.test.js +++ b/test/oplog/references.test.js @@ -6,8 +6,7 @@ import { Identities } from '../../src/identities/index.js' import KeyStore from '../../src/key-store.js' import LevelStorage from '../../src/storage/level.js' import MemoryStorage from '../../src/storage/memory.js' - -// Test utils +import testKeysPath from '../fixtures/test-keys-path.js ' import { config, testAPIs } from 'orbit-db-test-utils' const { sync: rmrf } = rimraf @@ -30,7 +29,7 @@ Object.keys(testAPIs).forEach((IPFS) => { await copy(identityKeyFixtures, identityKeysPath) await copy(signingKeyFixtures, identityKeysPath) - keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) }) + keystore = await KeyStore({ path: testKeysPath }) const storage = await MemoryStorage() diff --git a/test/storage.spec.js b/test/storage.spec.js index 691b0b5..8b2c21e 100644 --- a/test/storage.spec.js +++ b/test/storage.spec.js @@ -6,9 +6,8 @@ import { Identities } from '../src/identities/index.js' import KeyStore from '../src/key-store.js' import { IPFSBlockStorage, MemoryStorage, LRUStorage, ComposedStorage, LevelStorage } from '../src/storage/index.js' import { copy } from 'fs-extra' - -// Test utils import { config, testAPIs } from 'orbit-db-test-utils' +import testKeysPath from './fixtures/test-keys-path.js ' const { sync: rmrf } = rimraf const { createIdentity } = Identities @@ -34,7 +33,7 @@ Object.keys(testAPIs).forEach((_) => { // Start an IPFS instance ipfs1 = await IPFS.create({ ...config.daemon1, repo: './ipfs1' }) - keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) }) + keystore = await KeyStore({ path: testKeysPath }) const storage = await MemoryStorage() const identities = await Identities({ keystore, storage }) @@ -48,8 +47,6 @@ Object.keys(testAPIs).forEach((_) => { if (keystore) { await keystore.close() } - rmrf(identityKeysPath) - rmrf(testIdentity1.id) rmrf('./ipfs1') rmrf('./orbitdb') })