Code optimization

This commit is contained in:
Vladislav Hristov 2018-06-28 11:34:14 +03:00
parent 9660242234
commit b259ac76d6
2 changed files with 11 additions and 17 deletions

View File

@ -125,9 +125,13 @@ module.exports = class Client {
} }
async keys() { async keys() {
const keysString = await this._readKeys(); try {
const keysString = await this._readKeys();
return keysString ? new Keys(JSON.parse(this.keyEncryptor.decrypt(keysString))) : null; return new Keys(JSON.parse(this.keyEncryptor.decrypt(keysString)));
} catch (err) {
return null;
}
} }
_generateKeys() { _generateKeys() {
@ -143,21 +147,11 @@ module.exports = class Client {
await this._writeKeys(keysObject); await this._writeKeys(keysObject);
} }
async _readKeys() { _readKeys() {
try { return this.keyStorage.read();
const keys = await this.keyStorage.read();
return keys;
} catch (err) {
return null;
}
} }
async _writeKeys(keysObject) { _writeKeys(keysObject) {
try { return this.keyStorage.write(this.keyEncryptor.encrypt(stringifyKeys(keysObject.keys)));
await this.keyStorage.write(this.keyEncryptor.encrypt(stringifyKeys(keysObject.keys)));
} catch (err) {
throw err;
}
} }
}; };

View File

@ -9,7 +9,7 @@ module.exports = (pathToFile) => {
write(data) { write(data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.writeFile(path, data, { encoding: 'utf8' }, (error) => { fs.writeFile(path, data, { encoding: 'utf8' }, (error) => {
if (error) throw error; if (error) reject(error);
return resolve(); return resolve();
}); });