({ storage, path } = {})
| 116 | * @instance |
| 117 | */ |
| 118 | const KeyStore = async ({ storage, path } = {}) => { |
| 119 | /** |
| 120 | * @namespace module:KeyStore~KeyStore |
| 121 | * @description The instance returned by {@link module:KeyStore}. |
| 122 | */ |
| 123 | |
| 124 | // Persistent storage for keys |
| 125 | storage = storage || await ComposedStorage(await LRUStorage({ size: 1000 }), await LevelStorage({ path: path || defaultPath })) |
| 126 | |
| 127 | // Cache for deserialized/unmarshaled keys |
| 128 | const keyCache = await LRUStorage({ size: 1000 }) |
| 129 | |
| 130 | /** |
| 131 | * Closes the KeyStore's underlying storage. |
| 132 | * @memberof module:KeyStore~KeyStore |
| 133 | * @async |
| 134 | * @instance |
| 135 | */ |
| 136 | const close = async () => { |
| 137 | await storage.close() |
| 138 | await keyCache.close() |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Clears the KeyStore's underlying storage. |
| 143 | * @memberof module:KeyStore~KeyStore |
| 144 | * @async |
| 145 | * @instance |
| 146 | */ |
| 147 | const clear = async () => { |
| 148 | await storage.clear() |
| 149 | await keyCache.clear() |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Checks if a key exists in the key store . |
| 154 | * @param {string} id The id of an [Identity]{@link module:Identities~Identity} to check the key for. |
| 155 | * @return {boolean} True if the key exists, false otherwise. |
| 156 | * @throws id needed to check a key if no id is specified. |
| 157 | * @memberof module:KeyStore~KeyStore |
| 158 | * @async |
| 159 | * @instance |
| 160 | */ |
| 161 | const hasKey = async (id) => { |
| 162 | if (!id) { |
| 163 | throw new Error('id needed to check a key') |
| 164 | } |
| 165 | |
| 166 | let hasKey = false |
| 167 | let key = await keyCache.get(id) |
| 168 | if (key) { |
| 169 | hasKey = true |
| 170 | } else { |
| 171 | try { |
| 172 | key = await storage.get('private_' + id) |
| 173 | hasKey = key !== undefined && key !== null |
| 174 | } catch (e) { |
| 175 | // Catches 'Error: ENOENT: no such file or directory, open <path>' |
no test coverage detected