feat: Implement new keystore across tests.

This commit is contained in:
Hayden Young 2023-03-01 10:01:17 +00:00
parent ba71a7985e
commit f6368c2645
15 changed files with 42 additions and 32 deletions

View File

@ -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)

View File

@ -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'))
}

View File

@ -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 () => {

View File

@ -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 })

View File

@ -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()

View File

@ -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()

View File

@ -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' })

View File

@ -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 })

View File

@ -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)

View File

@ -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()

View File

@ -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)

View File

@ -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 })

View File

@ -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()

View File

@ -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()

View File

@ -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 })