copay/js/plugins/EncryptedLocalStorage.js

33 lines
1012 B
JavaScript
Raw Normal View History

2014-10-27 06:36:17 -07:00
var cryptoUtil = require('../util/crypto');
var LocalStorage = require('./LocalStorage');
var inherits = require('inherits');
function EncryptedLocalStorage(config) {
LocalStorage.apply(this, [config]);
}
inherits(EncryptedLocalStorage, LocalStorage);
EncryptedLocalStorage.prototype.getItem = function(name, callback) {
2014-10-27 18:15:23 -07:00
var key = cryptoUtil.kdf(this.password + this.email);
LocalStorage.prototype.getItem.apply(this, [name,
function(err, body) {
var decryptedJson = cryptoUtil.decrypt(key, body);
if (!decryptedJson) {
return callback('PNOTFOUND');
}
return callback(null, decryptedJson);
2014-10-27 06:36:17 -07:00
}
]);
2014-10-27 06:36:17 -07:00
};
EncryptedLocalStorage.prototype.setItem = function(name, value, callback) {
2014-10-27 18:15:23 -07:00
var key = cryptoUtil.kdf(this.password + this.email);
2014-10-27 06:36:17 -07:00
if (!_.isString(value)) {
value = JSON.stringify(value);
}
var record = cryptoUtil.encrypt(key, value);
LocalStorage.prototype.setItem.apply(this, [name, record, callback]);
};
module.exports = EncryptedLocalStorage;