mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-10-07 22:57:07 +00:00
Cache keys in key-store
This commit is contained in:
parent
63b93c2168
commit
31433f3ff6
@ -123,8 +123,13 @@ const KeyStore = async ({ storage, path } = {}) => {
|
|||||||
* @namespace module:KeyStore~KeyStore
|
* @namespace module:KeyStore~KeyStore
|
||||||
* @description The instance returned by {@link module:KeyStore}.
|
* @description The instance returned by {@link module:KeyStore}.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Persistent storage for keys
|
||||||
storage = storage || await ComposedStorage(await LRUStorage({ size: 1000 }), await LevelStorage({ path: path || defaultPath }))
|
storage = storage || await ComposedStorage(await LRUStorage({ size: 1000 }), await LevelStorage({ path: path || defaultPath }))
|
||||||
|
|
||||||
|
// Cache for deserialized/unmarshaled keys
|
||||||
|
const keyCache = await LRUStorage({ size: 1000 })
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes the KeyStore's underlying storage.
|
* Closes the KeyStore's underlying storage.
|
||||||
* @memberof module:KeyStore~KeyStore
|
* @memberof module:KeyStore~KeyStore
|
||||||
@ -133,6 +138,7 @@ const KeyStore = async ({ storage, path } = {}) => {
|
|||||||
*/
|
*/
|
||||||
const close = async () => {
|
const close = async () => {
|
||||||
await storage.close()
|
await storage.close()
|
||||||
|
await keyCache.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -143,6 +149,7 @@ const KeyStore = async ({ storage, path } = {}) => {
|
|||||||
*/
|
*/
|
||||||
const clear = async () => {
|
const clear = async () => {
|
||||||
await storage.clear()
|
await storage.clear()
|
||||||
|
await keyCache.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -160,13 +167,18 @@ const KeyStore = async ({ storage, path } = {}) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let hasKey = false
|
let hasKey = false
|
||||||
|
let key = await keyCache.get(id)
|
||||||
|
if (key) {
|
||||||
|
hasKey = true
|
||||||
|
} else {
|
||||||
try {
|
try {
|
||||||
const storedKey = await storage.get('private_' + id)
|
key = await storage.get('private_' + id)
|
||||||
hasKey = storedKey !== undefined && storedKey !== null
|
hasKey = key !== undefined && key !== null
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Catches 'Error: ENOENT: no such file or directory, open <path>'
|
// Catches 'Error: ENOENT: no such file or directory, open <path>'
|
||||||
console.error('Error: ENOENT: no such file or directory')
|
console.error('Error: ENOENT: no such file or directory')
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return hasKey
|
return hasKey
|
||||||
}
|
}
|
||||||
@ -180,7 +192,11 @@ const KeyStore = async ({ storage, path } = {}) => {
|
|||||||
* @instance
|
* @instance
|
||||||
*/
|
*/
|
||||||
const addKey = async (id, key) => {
|
const addKey = async (id, key) => {
|
||||||
await storage.put('private_' + id, key.privateKey)
|
const { privateKey } = key
|
||||||
|
await storage.put('private_' + id, privateKey)
|
||||||
|
// Unmarshal the key and add it to the cache
|
||||||
|
const unmarshaledPrivateKey = unmarshal(privateKey)
|
||||||
|
await keyCache.put(id, unmarshaledPrivateKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -197,12 +213,11 @@ const KeyStore = async ({ storage, path } = {}) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate a private key
|
// Generate a private key
|
||||||
const pair = await crypto.keys.generateKeyPair('secp256k1')
|
const keyPair = await crypto.keys.generateKeyPair('secp256k1')
|
||||||
const keys = await crypto.keys.unmarshalPrivateKey(pair.bytes)
|
const keys = await crypto.keys.unmarshalPrivateKey(keyPair.bytes)
|
||||||
const pubKey = keys.public.marshal()
|
|
||||||
|
|
||||||
const key = {
|
const key = {
|
||||||
publicKey: pubKey,
|
publicKey: keys.public.marshal(),
|
||||||
privateKey: keys.marshal()
|
privateKey: keys.marshal()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,6 +240,9 @@ const KeyStore = async ({ storage, path } = {}) => {
|
|||||||
throw new Error('id needed to get a key')
|
throw new Error('id needed to get a key')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let key = await keyCache.get(id)
|
||||||
|
|
||||||
|
if (!key) {
|
||||||
let storedKey
|
let storedKey
|
||||||
try {
|
try {
|
||||||
storedKey = await storage.get('private_' + id)
|
storedKey = await storage.get('private_' + id)
|
||||||
@ -236,7 +254,11 @@ const KeyStore = async ({ storage, path } = {}) => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return unmarshal(storedKey)
|
key = unmarshal(storedKey)
|
||||||
|
await keyCache.put(id, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
return key
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user