mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-10-07 22:57:07 +00:00
@@ -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
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@ 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 { LRUStorage, IPFSBlockStorage, MemoryStorage } from '../storage/index.js'
|
||||
import KeyStore, { signMessage, verifyMessage } from '../key-store.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 || new KeyStore(identityKeysPath || DefaultIdentityKeysPath)
|
||||
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 })
|
||||
@@ -35,10 +35,9 @@ 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 KeyStore.sign(privateKey, id)
|
||||
const idSignature = await signMessage(privateKey, id)
|
||||
const publicKeyAndIdSignature = await identityProvider.signIdentity(publicKey + idSignature, options)
|
||||
const signatures = {
|
||||
id: idSignature,
|
||||
@@ -59,7 +58,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
|
||||
}
|
||||
@@ -86,11 +85,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 {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import IdentityProvider from './interface.js'
|
||||
import 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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
197
src/key-store.js
197
src/key-store.js
@@ -1,13 +1,10 @@
|
||||
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
|
||||
import ComposedStorage from './storage/composed.js'
|
||||
import LevelStorage from './storage/level.js'
|
||||
import LRUStorage from './storage/lru.js'
|
||||
|
||||
const ec = new EC('secp256k1')
|
||||
const unmarshal = crypto.keys.supportedKeys.secp256k1.unmarshalSecp256k1PrivateKey
|
||||
|
||||
const verifySignature = async (signature, publicKey, data) => {
|
||||
@@ -36,56 +33,68 @@ const verifySignature = async (signature, publicKey, data) => {
|
||||
} catch (e) {
|
||||
// Catch error: sig length wrong
|
||||
}
|
||||
|
||||
return Promise.resolve(res)
|
||||
}
|
||||
|
||||
function createStore (path = './keystore') {
|
||||
if (fs && fs.mkdirSync) {
|
||||
fs.mkdirSync(path, { recursive: true })
|
||||
const signMessage = 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 verifiedCache = new LRU(1000)
|
||||
const verifiedCache = await LRUStorage({ size: 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()
|
||||
const verifyMessage = async (signature, publicKey, data) => {
|
||||
const cached = await verifiedCache.get(signature)
|
||||
|
||||
let res = false
|
||||
|
||||
if (!cached) {
|
||||
const verified = await verifySignature(signature, publicKey, data)
|
||||
res = verified
|
||||
if (verified) {
|
||||
await verifiedCache.put(signature, { publicKey, data })
|
||||
}
|
||||
this._cache = input.cache || new LRU(100)
|
||||
}
|
||||
|
||||
async open () {
|
||||
if (!this._store) {
|
||||
throw new Error('KeyStore: No store found to open')
|
||||
} else {
|
||||
const compare = (cached, data) => {
|
||||
const match = Buffer.isBuffer(data) ? Buffer.compare(cached, data) === 0 : cached.toString() === data.toString()
|
||||
return match
|
||||
}
|
||||
await this._store.open()
|
||||
res = cached.publicKey === publicKey && compare(cached.data, data)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
const KeyStore = async ({ storage, path } = {}) => {
|
||||
storage = storage || await ComposedStorage(await LevelStorage({ path: path || './keystore' }), await LRUStorage({ size: 1000 }))
|
||||
|
||||
const close = async () => {
|
||||
await storage.close()
|
||||
}
|
||||
|
||||
async close () {
|
||||
if (!this._store) return
|
||||
await this._store.close()
|
||||
const clear = async () => {
|
||||
await storage.clear()
|
||||
}
|
||||
|
||||
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') {
|
||||
return null
|
||||
}
|
||||
|
||||
let hasKey = false
|
||||
try {
|
||||
const storedKey = this._cache.get(id) || await this._store.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 <path>'
|
||||
@@ -95,67 +104,44 @@ 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('public_' + id, key.publicKey)
|
||||
await storage.put('private_' + id, key.privateKey)
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
this._cache.set(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') {
|
||||
// console.log("22::", id)
|
||||
// return null
|
||||
// }
|
||||
|
||||
// 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')
|
||||
publicKey: Buffer.from(decompressedKey),//.toString('hex'),
|
||||
privateKey: Buffer.from(keys.marshal())//.toString('hex')
|
||||
}
|
||||
|
||||
try {
|
||||
await this._store.put(id, JSON.stringify(key))
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
this._cache.set(id, key)
|
||||
await addKey(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 (this._store.status && this._store.status !== 'open') {
|
||||
return null
|
||||
}
|
||||
|
||||
const cachedKey = this._cache.get(id)
|
||||
let storedKey
|
||||
try {
|
||||
storedKey = cachedKey || await this._store.get(id)
|
||||
storedKey = await storage.get('private_' + id)
|
||||
} catch (e) {
|
||||
// ignore ENOENT error
|
||||
}
|
||||
@@ -163,20 +149,12 @@ export default class KeyStore {
|
||||
if (!storedKey) {
|
||||
return
|
||||
}
|
||||
|
||||
const deserializedKey = cachedKey || JSON.parse(storedKey)
|
||||
if (!deserializedKey) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!cachedKey) {
|
||||
this._cache.set(id, deserializedKey)
|
||||
}
|
||||
|
||||
return unmarshal(Buffer.from(deserializedKey.privateKey, 'hex'))
|
||||
|
||||
// return unmarshal(Buffer.from(deserializedKey.privateKey, 'hex'))
|
||||
return unmarshal(storedKey)
|
||||
}
|
||||
|
||||
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 +169,19 @@ 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 {
|
||||
clear,
|
||||
close,
|
||||
hasKey,
|
||||
addKey,
|
||||
createKey,
|
||||
getKey,
|
||||
getPublic
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
KeyStore as default,
|
||||
verifyMessage,
|
||||
signMessage
|
||||
}
|
||||
|
||||
@@ -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'
|
||||
@@ -80,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)
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
@@ -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)
|
||||
|
||||
BIN
test/fixtures/newtestkeys2/001232.ldb
vendored
Normal file
BIN
test/fixtures/newtestkeys2/001232.ldb
vendored
Normal file
Binary file not shown.
0
test/fixtures/newtestkeys2/001274.log
vendored
Normal file
0
test/fixtures/newtestkeys2/001274.log
vendored
Normal file
1
test/fixtures/newtestkeys2/CURRENT
vendored
Normal file
1
test/fixtures/newtestkeys2/CURRENT
vendored
Normal file
@@ -0,0 +1 @@
|
||||
MANIFEST-001273
|
||||
0
test/fixtures/newtestkeys2/LOCK
vendored
Normal file
0
test/fixtures/newtestkeys2/LOCK
vendored
Normal file
3
test/fixtures/newtestkeys2/LOG
vendored
Normal file
3
test/fixtures/newtestkeys2/LOG
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
2023/03/01-14:41:38.623433 17234f000 Recovering log #1272
|
||||
2023/03/01-14:41:38.623903 17234f000 Delete type=3 #1271
|
||||
2023/03/01-14:41:38.623944 17234f000 Delete type=0 #1272
|
||||
3
test/fixtures/newtestkeys2/LOG.old
vendored
Normal file
3
test/fixtures/newtestkeys2/LOG.old
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
2023/03/01-14:41:38.531437 173b67000 Recovering log #1270
|
||||
2023/03/01-14:41:38.531817 173b67000 Delete type=3 #1269
|
||||
2023/03/01-14:41:38.531856 173b67000 Delete type=0 #1270
|
||||
BIN
test/fixtures/newtestkeys2/MANIFEST-001273
vendored
Normal file
BIN
test/fixtures/newtestkeys2/MANIFEST-001273
vendored
Normal file
Binary file not shown.
19
test/fixtures/orbit-db-identity-keys.js
vendored
19
test/fixtures/orbit-db-identity-keys.js
vendored
@@ -3,6 +3,7 @@ import { Identities } from '../../src/identities/index.js'
|
||||
import rimraf from 'rimraf'
|
||||
|
||||
const { sync: rmrf } = rimraf
|
||||
import testKeysPath from './test-keys-path.js '
|
||||
|
||||
import userA from "./keys/identity-keys/03e0480538c2a39951d054e17ff31fde487cb1031d0044a037b53ad2e028a3e77c.json" assert { type: "json" }
|
||||
import userB from "./keys/identity-keys/0358df8eb5def772917748fdf8a8b146581ad2041eae48d66cc6865f11783499a6.json" assert { type: "json" }
|
||||
@@ -29,17 +30,17 @@ const signingKeys = {
|
||||
}
|
||||
|
||||
const createTestIdentities = async (ipfs1, ipfs2) => {
|
||||
rmrf('./keys_1')
|
||||
// rmrf('./keys_1')
|
||||
|
||||
const keystore = new KeyStore('./keys_1')
|
||||
await keystore.open()
|
||||
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 })
|
||||
|
||||
1
test/fixtures/test-keys-path.js
vendored
Normal file
1
test/fixtures/test-keys-path.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export default './test/fixtures/newtestkeys2'
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
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 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'
|
||||
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 () {
|
||||
@@ -19,7 +22,7 @@ describe('Identities', function () {
|
||||
})
|
||||
|
||||
describe('Creating Identities', () => {
|
||||
const id = 'A'
|
||||
const id = 'userA'
|
||||
|
||||
let identities
|
||||
let identity
|
||||
@@ -40,7 +43,7 @@ describe('Identities', function () {
|
||||
})
|
||||
|
||||
describe('Get Identity', () => {
|
||||
const id = 'A'
|
||||
const id = 'userA'
|
||||
|
||||
let identities
|
||||
let identity
|
||||
@@ -65,16 +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 () => {
|
||||
keystore = new KeyStore(identityKeysPath)
|
||||
await keystore.open()
|
||||
keystore = await KeyStore({ path: testKeysPath })
|
||||
identities = await Identities({ keystore })
|
||||
})
|
||||
|
||||
@@ -110,9 +112,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,29 +123,26 @@ 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)
|
||||
})
|
||||
})
|
||||
|
||||
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)
|
||||
|
||||
savedKeysKeyStore = new KeyStore(savedKeysPath)
|
||||
await savedKeysKeyStore.open()
|
||||
savedKeysKeyStore = await KeyStore({ path: testKeysPath })
|
||||
|
||||
identities = await Identities({ keystore: savedKeysKeyStore })
|
||||
identity = await identities.createIdentity({ id })
|
||||
@@ -181,8 +180,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 +195,7 @@ describe('Identities', function () {
|
||||
let keystore
|
||||
|
||||
before(async () => {
|
||||
keystore = new KeyStore(identityKeysPath)
|
||||
await keystore.open()
|
||||
keystore = await KeyStore({ path: testKeysPath })
|
||||
})
|
||||
|
||||
after(async () => {
|
||||
@@ -209,14 +207,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 +244,7 @@ describe('Identities', function () {
|
||||
let keystore
|
||||
|
||||
before(async () => {
|
||||
keystore = new KeyStore(identityKeysPath)
|
||||
await keystore.open()
|
||||
keystore = await KeyStore({ path: testKeysPath })
|
||||
identities = await Identities({ keystore })
|
||||
})
|
||||
|
||||
@@ -273,8 +270,7 @@ describe('Identities', function () {
|
||||
let keystore
|
||||
|
||||
before(async () => {
|
||||
keystore = new KeyStore(identityKeysPath)
|
||||
await keystore.open()
|
||||
keystore = await KeyStore({ path: testKeysPath })
|
||||
identities = await Identities({ keystore })
|
||||
identity = await identities.createIdentity({ id })
|
||||
})
|
||||
@@ -287,7 +283,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 +314,7 @@ describe('Identities', function () {
|
||||
let signature
|
||||
|
||||
before(async () => {
|
||||
keystore = new KeyStore(identityKeysPath)
|
||||
await keystore.open()
|
||||
keystore = await KeyStore({ path: testKeysPath })
|
||||
})
|
||||
|
||||
after(async () => {
|
||||
|
||||
284
test/key-store.test.js
Normal file
284
test/key-store.test.js
Normal file
@@ -0,0 +1,284 @@
|
||||
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'
|
||||
import fs from 'fs-extra'
|
||||
import rmrf from 'rimraf'
|
||||
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 + ')', () => {
|
||||
let keystore
|
||||
|
||||
describe('Creating and retrieving keys', () => {
|
||||
beforeEach(async () => {
|
||||
keystore = await KeyStore({ path: testKeysPath })
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await keystore.close()
|
||||
})
|
||||
|
||||
it('creates a key', async () => {
|
||||
const id = 'key1'
|
||||
await keystore.createKey(id)
|
||||
const hasKey = await keystore.hasKey(id)
|
||||
strictEqual(hasKey, true)
|
||||
})
|
||||
|
||||
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 = 'key1234567890'
|
||||
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 () => {
|
||||
const id = 'key1'
|
||||
const keys = await keystore.createKey(id)
|
||||
deepStrictEqual(await keystore.getKey(id), keys)
|
||||
})
|
||||
|
||||
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 = 'key111111111'
|
||||
|
||||
const actual = await keystore.getKey(id)
|
||||
|
||||
strictEqual(actual, expected)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Using keys for signing and verifying', () => {
|
||||
beforeEach(async () => {
|
||||
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.close()
|
||||
})
|
||||
|
||||
describe('Signing', () => {
|
||||
it('signs data', async () => {
|
||||
const expected = '3045022100df961fa46bb8a3cb92594a24205e6008a84daa563ac3530f583bb9f9cef5af3b02207b84c5d63387d0a710e42e05785fbccdaf2534c8ed16adb8afd57c3eba930529'
|
||||
|
||||
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
|
||||
|
||||
beforeEach(async () => {
|
||||
key = await keystore.getKey('userA')
|
||||
})
|
||||
|
||||
it('gets the public key', async () => {
|
||||
const expected = '04e7247a4c155b63d182a23c70cb6fe8ba2e44bc9e9d62dc45d4c4167ccde95944f13db3c707da2ee0e3fd6ba531caef9f86eb79132023786cd6139ec5ebed4fae'
|
||||
const publicKey = await keystore.getPublic(key)
|
||||
strictEqual(publicKey, expected)
|
||||
})
|
||||
|
||||
it('gets the public key buffer', async () => {
|
||||
const expected = {
|
||||
type: 'Buffer',
|
||||
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' })
|
||||
|
||||
deepStrictEqual(publicKey.toJSON(), expected)
|
||||
})
|
||||
|
||||
it('gets the public key when decompress is false', async () => {
|
||||
// const expectedCompressedKey = signingKeys.userA.publicKey
|
||||
const expectedCompressedKey = '02e7247a4c155b63d182a23c70cb6fe8ba2e44bc9e9d62dc45d4c4167ccde95944'
|
||||
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: [
|
||||
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 })
|
||||
|
||||
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 = await signMessage(key, 'data data data')
|
||||
const expectedSignature = '3045022100df961fa46bb8a3cb92594a24205e6008a84daa563ac3530f583bb9f9cef5af3b02207b84c5d63387d0a710e42e05785fbccdaf2534c8ed16adb8afd57c3eba930529'
|
||||
strictEqual(expectedSignature, signature)
|
||||
|
||||
const verified = await verifyMessage(expectedSignature, 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)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -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 = new KeyStore(identityKeysPath)
|
||||
keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath }) })
|
||||
|
||||
const storage = await MemoryStorage()
|
||||
|
||||
|
||||
@@ -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 = new KeyStore(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 () => {
|
||||
|
||||
@@ -4,9 +4,9 @@ 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'
|
||||
import testKeysPath from '../fixtures/test-keys-path.js '
|
||||
|
||||
const { sync: rmrf } = rimraf
|
||||
const { createIdentity } = Identities
|
||||
@@ -29,9 +29,9 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
||||
|
||||
await copy(identityKeyFixtures, identityKeysPath)
|
||||
await copy(signingKeyFixtures, identityKeysPath)
|
||||
|
||||
keystore = new KeyStore(identityKeysPath)
|
||||
|
||||
keystore = await KeyStore({ path: testKeysPath })
|
||||
|
||||
identities = await Identities({ keystore, ipfs })
|
||||
testIdentity = await identities.createIdentity({ id: 'userA' })
|
||||
})
|
||||
|
||||
@@ -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 = new KeyStore(identityKeysPath)
|
||||
keystore = await KeyStore({ path: testKeysPath })
|
||||
const storage = await MemoryStorage()
|
||||
|
||||
identities = await Identities({ keystore, storage })
|
||||
|
||||
@@ -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,8 +27,8 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
||||
let testIdentity, testIdentity2, testIdentity3
|
||||
|
||||
before(async () => {
|
||||
keystore = new KeyStore('./keys_1')
|
||||
await keystore.open()
|
||||
keystore = await KeyStore({ storage: await LevelStorage({ path: './keys_1', valueEncoding: 'json' })})
|
||||
|
||||
for (const [key, value] of Object.entries(identityKeys)) {
|
||||
await keystore.addKey(key, value)
|
||||
}
|
||||
|
||||
@@ -4,13 +4,12 @@ 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
|
||||
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
|
||||
|
||||
@@ -24,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 = new KeyStore(identityKeysPath)
|
||||
|
||||
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 () => {
|
||||
|
||||
@@ -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,8 +27,8 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
||||
let testIdentity, testIdentity2, testIdentity3, testIdentity4
|
||||
|
||||
before(async () => {
|
||||
keystore = new KeyStore('./keys_1')
|
||||
await keystore.open()
|
||||
keystore = await KeyStore({ storage: await LevelStorage({ path: './keys_1', valueEncoding: 'json' }) })
|
||||
|
||||
for (const [key, value] of Object.entries(identityKeys)) {
|
||||
await keystore.addKey(key, value)
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
||||
await copy(identityKeyFixtures, identityKeysPath)
|
||||
await copy(signingKeyFixtures, identityKeysPath)
|
||||
|
||||
keystore = new KeyStore(identityKeysPath)
|
||||
keystore = await KeyStore({ storage: await LevelStorage({ path: identityKeysPath, valueEncoding: 'json' }) })
|
||||
|
||||
testIdentity = await createIdentity({ id: 'userC', keystore })
|
||||
testIdentity2 = await createIdentity({ id: 'userB', keystore })
|
||||
|
||||
@@ -4,10 +4,10 @@ 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
|
||||
import { config, testAPIs } from 'orbit-db-test-utils'
|
||||
import testKeysPath from '../fixtures/test-keys-path.js '
|
||||
|
||||
const { sync: rmrf } = rimraf
|
||||
const { create } = Entry
|
||||
@@ -28,7 +28,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
||||
await copy(identityKeyFixtures, identityKeysPath)
|
||||
await copy(signingKeyFixtures, identityKeysPath)
|
||||
|
||||
keystore = new KeyStore(identityKeysPath)
|
||||
keystore = await KeyStore({ path: testKeysPath })
|
||||
|
||||
const storage = await MemoryStorage()
|
||||
|
||||
|
||||
@@ -4,9 +4,9 @@ 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
|
||||
import testKeysPath from '../fixtures/test-keys-path.js '
|
||||
import { config, testAPIs } from 'orbit-db-test-utils'
|
||||
|
||||
const { sync: rmrf } = rimraf
|
||||
@@ -29,7 +29,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
||||
await copy(identityKeyFixtures, identityKeysPath)
|
||||
await copy(signingKeyFixtures, identityKeysPath)
|
||||
|
||||
keystore = new KeyStore(identityKeysPath)
|
||||
keystore = await KeyStore({ path: testKeysPath })
|
||||
|
||||
const storage = await MemoryStorage()
|
||||
|
||||
|
||||
@@ -4,11 +4,10 @@ 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
|
||||
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 = new KeyStore(identityKeysPath)
|
||||
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')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user