-
Notifications
You must be signed in to change notification settings - Fork 0
/
secureStore.js
49 lines (43 loc) · 1.65 KB
/
secureStore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import * as openpgp from 'openpgp';
import { ERRORS } from '../constants';
import * as keyring from './keyring';
import storageProxyFactory from './storageProxy'
// TODO: remove this!
window.openpgp = openpgp;
const defaultGenerateOptions = {
numBits: 4096,
userIds: [ { name: 'pass-chrome', email: '[email protected]' } ]
};
const secureStore = ({ keyArmoredObject = {}, passphrase, generateCallback }) => {
const { publicKeyArmored, privateKeyArmored } = keyArmoredObject;
const keyLoadedHandler = (key) => {
return storageProxyFactory({ key, passphrase });
};
if (publicKeyArmored && privateKeyArmored) {
//-- TODO: maybe move these into the promise, wrap with `try/catch` and reject in the catch
const publicKey = keyring.loadKey(publicKeyArmored);
const privateKey = keyring.loadKey(privateKeyArmored);
const keyIsUnlocked = keyring.ensureUnlocked(privateKey, passphrase);
return new Promise((resolve, reject) => {
//-- TODO: is this `setTimeout` necessary in es6?
setTimeout(() => {
if (!keyIsUnlocked) {
reject(ERRORS.KEY_LOCKED);
} else {
resolve(keyLoadedHandler(privateKey));
}
}, 0)
})
} else if (generateCallback && typeof(generateCallback) === 'function') {
//-- NOTE: return from default function
return keyring.init({
keyArmoredObject,
passphrase,
generateOptions: { ...defaultGenerateOptions, callback: generateCallback }
})
.then(keyLoadedHandler)
} else {
throw new Error({ code: ERRORS.NO_KEY, message: 'No key or generate callback was provided to the secureStore' });
}
};
export default secureStore;