Skip to content

Commit

Permalink
Merge pull request #1176 from orbitdb/cache-keys
Browse files Browse the repository at this point in the history
Cache keys in key-store
  • Loading branch information
haydenyoung committed Apr 17, 2024
2 parents f3e15ec + 31433f3 commit 7ff642b
Showing 1 changed file with 42 additions and 20 deletions.
62 changes: 42 additions & 20 deletions src/key-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,13 @@ const KeyStore = async ({ storage, path } = {}) => {
* @namespace module:KeyStore~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 }))

// Cache for deserialized/unmarshaled keys
const keyCache = await LRUStorage({ size: 1000 })

/**
* Closes the KeyStore's underlying storage.
* @memberof module:KeyStore~KeyStore
Expand All @@ -133,6 +138,7 @@ const KeyStore = async ({ storage, path } = {}) => {
*/
const close = async () => {
await storage.close()
await keyCache.close()
}

/**
Expand All @@ -143,6 +149,7 @@ const KeyStore = async ({ storage, path } = {}) => {
*/
const clear = async () => {
await storage.clear()
await keyCache.clear()
}

/**
Expand All @@ -160,12 +167,17 @@ const KeyStore = async ({ storage, path } = {}) => {
}

let hasKey = false
try {
const storedKey = await storage.get('private_' + id)
hasKey = storedKey !== undefined && storedKey !== null
} catch (e) {
// Catches 'Error: ENOENT: no such file or directory, open <path>'
console.error('Error: ENOENT: no such file or directory')
let key = await keyCache.get(id)
if (key) {
hasKey = true
} else {
try {
key = await storage.get('private_' + id)
hasKey = key !== undefined && key !== null
} catch (e) {
// Catches 'Error: ENOENT: no such file or directory, open <path>'
console.error('Error: ENOENT: no such file or directory')
}
}

return hasKey
Expand All @@ -180,7 +192,11 @@ const KeyStore = async ({ storage, path } = {}) => {
* @instance
*/
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)
}

/**
Expand All @@ -197,12 +213,11 @@ const KeyStore = async ({ storage, path } = {}) => {
}

// Generate a private key
const pair = await crypto.keys.generateKeyPair('secp256k1')
const keys = await crypto.keys.unmarshalPrivateKey(pair.bytes)
const pubKey = keys.public.marshal()
const keyPair = await crypto.keys.generateKeyPair('secp256k1')
const keys = await crypto.keys.unmarshalPrivateKey(keyPair.bytes)

const key = {
publicKey: pubKey,
publicKey: keys.public.marshal(),
privateKey: keys.marshal()
}

Expand All @@ -225,18 +240,25 @@ const KeyStore = async ({ storage, path } = {}) => {
throw new Error('id needed to get a key')
}

let storedKey
try {
storedKey = await storage.get('private_' + id)
} catch (e) {
// ignore ENOENT error
}
let key = await keyCache.get(id)

if (!key) {
let storedKey
try {
storedKey = await storage.get('private_' + id)
} catch (e) {
// ignore ENOENT error
}

if (!storedKey) {
return
}

if (!storedKey) {
return
key = unmarshal(storedKey)
await keyCache.put(id, key)
}

return unmarshal(storedKey)
return key
}

/**
Expand Down

0 comments on commit 7ff642b

Please sign in to comment.