* Check if key can be unlocked without requesting the password from the user * @param {String} operation - 'decrypt' or 'sign', the operation for which the key is required * @param {Object} options - mandatory * @param {openpgp.key.Key} options.key * @param {String} [options.password]
(operation, options)
| 179 | * @return {Boolean} - true if key can be unlocked |
| 180 | */ |
| 181 | async canUnlockKey(operation, options) { |
| 182 | if (options.password) { |
| 183 | // key can always be unlocked with password |
| 184 | return true; |
| 185 | } |
| 186 | const isKeyCached = await isCached(options.key.getFingerprint()); |
| 187 | if (isKeyCached) { |
| 188 | return true; |
| 189 | } |
| 190 | try { |
| 191 | let key; |
| 192 | if (operation === 'sign') { |
| 193 | key = await options.key.getSigningKey(); |
| 194 | return key && key.isDecrypted(); |
| 195 | } else if (operation === 'decrypt') { |
| 196 | key = await options.key.getEncryptionKey(); |
| 197 | return key && key.isDecrypted(); |
| 198 | } |
| 199 | } catch (e) { |
| 200 | console.log('No valid key for operation sign or decrypt', e); |
| 201 | return false; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | sync(type, data) { |
| 206 | return new Promise((resolve, reject) => { |
no test coverage detected