Wallet/js/models/Storage.js

213 lines
5 KiB
JavaScript
Raw Normal View History

2014-04-07 14:36:57 -03:00
'use strict';
2014-08-14 16:26:24 -04:00
var CryptoJS = require('node-cryptojs-aes').CryptoJS;
2014-08-14 18:46:42 -04:00
var bitcore = require('bitcore');
2014-09-02 15:49:22 -03:00
var preconditions = require('preconditions').instance();
2014-04-15 19:14:42 -03:00
var id = 0;
function Storage(opts) {
opts = opts || {};
2014-04-15 19:14:42 -03:00
this.__uniqueid = ++id;
if (opts.password)
this._setPassphrase(opts.password);
2014-07-08 19:52:47 -03:00
2014-09-02 15:49:22 -03:00
try {
2014-09-01 15:40:31 -03:00
this.storage = opts.storage || localStorage;
2014-08-15 10:26:31 -04:00
this.sessionStorage = opts.sessionStorage || sessionStorage;
2014-09-01 15:40:31 -03:00
} catch (e) {
console.log('Error in storage:', e); //TODO
};
2014-09-02 15:49:22 -03:00
preconditions.checkState(this.localStorage, 'No localstorage found');
preconditions.checkState(this.sessionStorage, 'No sessionStorage found');
2014-04-07 14:36:57 -03:00
}
2014-04-15 18:13:25 -03:00
2014-04-15 19:14:42 -03:00
var pps = {};
2014-04-15 18:13:25 -03:00
Storage.prototype._getPassphrase = function() {
2014-07-08 19:11:48 -03:00
if (!pps[this.__uniqueid])
throw new Error('No passprase set');
2014-04-15 19:14:42 -03:00
return pps[this.__uniqueid];
2014-04-15 18:13:25 -03:00
}
Storage.prototype._setPassphrase = function(password) {
2014-04-15 19:14:42 -03:00
pps[this.__uniqueid] = password;
2014-04-15 16:31:00 -03:00
}
Storage.prototype._encrypt = function(string) {
var encrypted = CryptoJS.AES.encrypt(string, this._getPassphrase());
var encryptedBase64 = encrypted.toString();
return encryptedBase64;
2014-04-15 16:31:00 -03:00
};
Storage.prototype._decrypt = function(base64) {
var decryptedStr = null;
try {
var decrypted = CryptoJS.AES.decrypt(base64, this._getPassphrase());
if (decrypted)
decryptedStr = decrypted.toString(CryptoJS.enc.Utf8);
} catch (e) {
2014-06-03 18:38:56 -03:00
// Error while decrypting
return null;
}
return decryptedStr;
};
2014-04-15 16:31:00 -03:00
Storage.prototype._read = function(k) {
var ret;
2014-09-01 15:40:31 -03:00
ret = this.storage.getItem(k);
if (!ret) return null;
ret = this._decrypt(ret);
if (!ret) return null;
ret = ret.toString(CryptoJS.enc.Utf8);
ret = JSON.parse(ret);
2014-04-15 16:31:00 -03:00
return ret;
};
Storage.prototype._write = function(k, v) {
2014-04-15 18:13:25 -03:00
v = JSON.stringify(v);
v = this._encrypt(v);
2014-05-01 10:00:55 -03:00
2014-09-01 15:40:31 -03:00
this.storage.setItem(k, v);
2014-04-15 16:31:00 -03:00
};
2014-04-07 14:36:57 -03:00
2014-05-01 10:00:55 -03:00
// get value by key
Storage.prototype.getGlobal = function(k) {
2014-09-01 15:40:31 -03:00
var item = this.storage.getItem(k);
return item == 'undefined' ? undefined : item;
2014-05-01 10:00:55 -03:00
};
// set value for key
Storage.prototype.setGlobal = function(k, v) {
2014-09-01 15:40:31 -03:00
this.storage.setItem(k, typeof v === 'object' ? JSON.stringify(v) : v);
2014-05-01 10:00:55 -03:00
};
// remove value for key
Storage.prototype.removeGlobal = function(k) {
2014-09-01 15:40:31 -03:00
this.storage.removeItem(k);
2014-05-01 10:00:55 -03:00
};
2014-08-14 18:46:42 -04:00
Storage.prototype.getSessionId = function() {
2014-09-02 15:49:22 -03:00
var sessionId = this.sessionStorage.getItem('sessionId');
2014-08-14 18:46:42 -04:00
if (!sessionId) {
2014-08-15 10:35:25 -04:00
sessionId = bitcore.SecureRandom.getRandomBuffer(8).toString('hex');
this.sessionStorage.setItem('sessionId', sessionId);
2014-08-14 18:46:42 -04:00
}
return sessionId;
};
2014-05-01 10:00:55 -03:00
Storage.prototype._key = function(walletId, k) {
return walletId + '::' + k;
};
// get value by key
Storage.prototype.get = function(walletId, k) {
var ret = this._read(this._key(walletId, k));
2014-05-01 10:00:55 -03:00
return ret;
};
2014-05-01 10:00:55 -03:00
// set value for key
Storage.prototype.set = function(walletId, k, v) {
this._write(this._key(walletId, k), v);
};
2014-05-01 10:00:55 -03:00
// remove value for key
Storage.prototype.remove = function(walletId, k) {
this.removeGlobal(this._key(walletId, k));
2014-05-01 10:00:55 -03:00
};
Storage.prototype.setName = function(walletId, name) {
this.setGlobal('nameFor::' + walletId, name);
2014-05-01 10:00:55 -03:00
};
Storage.prototype.getName = function(walletId) {
2014-07-08 19:11:48 -03:00
var ret = this.getGlobal('nameFor::' + walletId);
return ret;
2014-05-01 10:00:55 -03:00
};
Storage.prototype.getWalletIds = function() {
var walletIds = [];
var uniq = {};
2014-07-08 19:11:48 -03:00
2014-09-01 23:44:35 -03:00
console.log('[Encrypted.js.144]', this.storage); //TODO
console.log('[Encrypted.js.144]', this.storage.length); //TODO
2014-09-01 15:40:31 -03:00
for (var i = 0; i < this.storage.length; i++) {
var key = this.storage.key(i);
var split = key.split('::');
if (split.length == 2) {
2014-05-01 10:00:55 -03:00
var walletId = split[0];
2014-09-02 15:49:22 -03:00
if (!walletId || walletId === 'nameFor' || walletId === 'lock')
2014-08-14 18:46:42 -04:00
continue;
2014-05-01 10:00:55 -03:00
if (typeof uniq[walletId] === 'undefined') {
2014-05-01 10:00:55 -03:00
walletIds.push(walletId);
uniq[walletId] = 1;
}
}
}
2014-05-01 10:00:55 -03:00
return walletIds;
};
Storage.prototype.getWallets = function() {
var wallets = [];
var ids = this.getWalletIds();
for (var i in ids) {
2014-05-01 10:00:55 -03:00
wallets.push({
id: ids[i],
2014-05-01 10:00:55 -03:00
name: this.getName(ids[i]),
});
}
2014-05-01 10:00:55 -03:00
return wallets;
};
2014-06-16 15:51:19 -03:00
Storage.prototype.deleteWallet = function(walletId) {
2014-06-16 17:37:33 -03:00
var toDelete = {};
toDelete['nameFor::' + walletId] = 1;
2014-09-01 15:40:31 -03:00
for (var i = 0; i < this.storage.length; i++) {
var key = this.storage.key(i);
2014-06-16 15:51:19 -03:00
var split = key.split('::');
2014-06-16 17:37:33 -03:00
if (split.length == 2 && split[0] === walletId) {
toDelete[key] = 1;
2014-06-16 15:51:19 -03:00
}
}
2014-06-16 17:37:33 -03:00
for (var i in toDelete) {
this.removeGlobal(i);
}
2014-06-16 15:51:19 -03:00
};
2014-08-04 15:10:01 -03:00
Storage.prototype.setLastOpened = function(walletId) {
this.setGlobal('lastOpened', walletId);
}
Storage.prototype.getLastOpened = function() {
return this.getGlobal('lastOpened');
}
2014-06-16 15:51:19 -03:00
2014-05-01 10:00:55 -03:00
//obj contains keys to be set
Storage.prototype.setFromObj = function(walletId, obj) {
for (var k in obj) {
this.set(walletId, k, obj[k]);
}
this.setName(walletId, obj.opts.name);
};
2014-05-01 10:00:55 -03:00
// remove all values
Storage.prototype.clearAll = function() {
2014-09-01 15:40:31 -03:00
this.storage.clear();
};
2014-09-02 15:49:22 -03:00
Storage.prototype.import = function(base64) {
var decryptedStr = this._decrypt(base64);
return JSON.parse(decryptedStr);
2014-05-01 18:32:22 -03:00
};
2014-09-02 15:49:22 -03:00
Storage.prototype.export = function(obj) {
var string = JSON.stringify(obj);
return this._encrypt(string);
2014-05-01 18:32:22 -03:00
};
2014-08-14 14:39:15 -04:00
module.exports = Storage;