test: Persisted keyvalue with KeyStore.

This commit is contained in:
Hayden Young 2023-02-26 23:23:27 +00:00
parent 0dac15ba7f
commit 2e8530e7b1
7 changed files with 13 additions and 19 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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