From 31433f3ff6b763dc1b9037af1f24faac9a86c3fd Mon Sep 17 00:00:00 2001 From: haad Date: Wed, 17 Apr 2024 12:45:01 +0300 Subject: [PATCH] Cache keys in key-store --- src/key-store.js | 64 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/src/key-store.js b/src/key-store.js index d747603..9c8e919 100644 --- a/src/key-store.js +++ b/src/key-store.js @@ -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 @@ -133,6 +138,7 @@ const KeyStore = async ({ storage, path } = {}) => { */ const close = async () => { await storage.close() + await keyCache.close() } /** @@ -143,6 +149,7 @@ const KeyStore = async ({ storage, path } = {}) => { */ const clear = async () => { await storage.clear() + await keyCache.clear() } /** @@ -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 ' - 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 ' + console.error('Error: ENOENT: no such file or directory') + } } return hasKey @@ -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) } /** @@ -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() } @@ -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 + } + + key = unmarshal(storedKey) + await keyCache.put(id, key) } - if (!storedKey) { - return - } - - return unmarshal(storedKey) + return key } /**