From f6368c26452ab00a5a18c9f9f60159f1c800419c Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Wed, 1 Mar 2023 10:01:17 +0000 Subject: [PATCH] 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 })