Wallet/js/models/Storage.js

360 lines
8.7 KiB
JavaScript
Raw Normal View History

2014-04-07 14:36:57 -03:00
'use strict';
2014-09-03 01:25:08 -03:00
var preconditions = require('preconditions').singleton();
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-09-04 18:07:09 -03:00
var _ = require('underscore');
2014-09-18 19:20:00 -03:00
var CACHE_DURATION = 1000 * 60 * 5;
2014-04-15 19:14:42 -03:00
var id = 0;
function Storage(opts) {
opts = opts || {};
2014-09-18 19:20:00 -03:00
this.wListCache = {};
2014-04-15 19:14:42 -03:00
this.__uniqueid = ++id;
if (opts.password)
2014-09-18 16:38:18 -03:00
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-04 18:07:09 -03:00
preconditions.checkState(this.storage, 'No storage defined');
preconditions.checkState(this.sessionStorage, 'No sessionStorage defined');
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-09-18 16:38:18 -03:00
Storage.prototype._getPassphrase = function() {
2014-09-17 09:42:23 -03:00
2014-07-08 19:11:48 -03:00
if (!pps[this.__uniqueid])
2014-09-04 18:07:09 -03:00
throw new Error('NOPASSPHRASE: No passphrase set');
2014-07-08 19:11:48 -03:00
2014-04-15 19:14:42 -03:00
return pps[this.__uniqueid];
2014-04-15 18:13:25 -03:00
}
2014-09-18 16:38:18 -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) {
2014-09-18 16:38:18 -03:00
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 {
2014-09-18 16:38:18 -03:00
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
2014-09-03 01:25:08 -03:00
Storage.prototype._read = function(k, cb) {
preconditions.checkArgument(cb);
var self = this;
this.storage.getItem(k, function(ret) {
if (!ret) return cb(null);
var ret = self._decrypt(ret);
if (!ret) return cb(null);
ret = ret.toString(CryptoJS.enc.Utf8);
ret = JSON.parse(ret);
return cb(ret);
});
2014-04-15 16:31:00 -03:00
};
2014-09-03 01:25:08 -03:00
Storage.prototype._write = function(k, v, cb) {
preconditions.checkArgument(cb);
2014-04-15 18:13:25 -03:00
v = JSON.stringify(v);
v = this._encrypt(v);
2014-09-03 01:25:08 -03:00
this.storage.setItem(k, v, cb);
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
2014-09-03 01:25:08 -03:00
Storage.prototype.getGlobal = function(k, cb) {
preconditions.checkArgument(cb);
this.storage.getItem(k, function(item) {
cb(item == 'undefined' ? undefined : item);
});
2014-05-01 10:00:55 -03:00
};
// set value for key
2014-09-03 01:25:08 -03:00
Storage.prototype.setGlobal = function(k, v, cb) {
preconditions.checkArgument(cb);
this.storage.setItem(k, typeof v === 'object' ? JSON.stringify(v) : v, cb);
2014-05-01 10:00:55 -03:00
};
// remove value for key
2014-09-03 01:25:08 -03:00
Storage.prototype.removeGlobal = function(k, cb) {
preconditions.checkArgument(cb);
this.storage.removeItem(k, cb);
2014-05-01 10:00:55 -03:00
};
2014-09-08 10:46:57 -03:00
Storage.prototype.getSessionId = function(cb) {
preconditions.checkArgument(cb);
var self = this;
self.sessionStorage.getItem('sessionId', function(sessionId) {
if (sessionId)
return cb(sessionId);
2014-08-15 10:35:25 -04:00
sessionId = bitcore.SecureRandom.getRandomBuffer(8).toString('hex');
2014-09-15 13:56:38 -03:00
self.sessionStorage.setItem('sessionId', sessionId, function() {
2014-09-08 10:46:57 -03:00
return cb(sessionId);
});
});
2014-08-14 18:46:42 -04:00
};
2014-09-17 12:10:26 -03:00
Storage.prototype.setSessionId = function(sessionId, cb) {
this.sessionStorage.setItem('sessionId', sessionId, cb);
};
2014-09-03 15:43:27 -03:00
Storage.prototype._readHelper = function(walletId, k, cb) {
var wk = this._key(walletId, k);
2014-09-08 10:46:57 -03:00
this._read(wk, function(v) {
return cb(v, k);
2014-09-03 15:43:27 -03:00
});
};
Storage.prototype.readWallet_Old = function(walletId, cb) {
var self = this;
this.storage.allKeys(function(allKeys) {
var obj = {};
var keys = _.filter(allKeys, function(k) {
if (k.indexOf(walletId + '::') === 0) return true;
});
if (keys.length === 0) return cb(new Error('Wallet ' + walletId + ' not found'));
var count = keys.length;
_.each(keys, function(k) {
self._read(k, function(v) {
obj[k.split('::')[1]] = v;
if (--count === 0) return cb(null, obj);
})
});
});
};
Storage.prototype.readWallet = function(walletId, cb) {
var self = this;
this.storage.allKeys(function(allKeys) {
var keys = _.filter(allKeys, function(k) {
if ((k === 'wallet::' + walletId) || k.indexOf('wallet::' + walletId) === 0) return true;
});
if (keys.length === 0) return cb(new Error('Wallet ' + walletId + ' not found'));
self._read(keys[0], function(v) {
if (_.isNull(v)) return cb(new Error('Could not decrypt wallet data'));
return cb(null, v);
})
});
};
2014-09-03 01:25:08 -03:00
Storage.prototype.getMany = function(walletId, keys, cb) {
preconditions.checkArgument(cb);
var self = this;
var ret = {};
2014-09-03 15:43:27 -03:00
2014-09-08 10:46:57 -03:00
var l = keys.length,
i = 0;
2014-09-03 01:25:08 -03:00
for (var ii in keys) {
2014-09-03 15:43:27 -03:00
this._readHelper(walletId, keys[ii], function(v, k) {
2014-09-03 01:25:08 -03:00
ret[k] = v;
2014-09-03 15:43:27 -03:00
if (++i == l) {
2014-09-08 10:46:57 -03:00
return cb(ret);
2014-09-03 15:43:27 -03:00
}
2014-09-03 01:25:08 -03:00
});
}
};
Storage.prototype._getWalletIds = function(cb) {
2014-09-04 18:07:09 -03:00
preconditions.checkArgument(cb);
2014-05-01 10:00:55 -03:00
var walletIds = [];
var uniq = {};
2014-09-03 01:25:08 -03:00
this.storage.allKeys(function(keys) {
for (var ii in keys) {
var key = keys[ii];
var split = key.split('::');
if (split.length == 2) {
var walletId = split[0];
2014-05-01 10:00:55 -03:00
if (!walletId || walletId === 'nameFor' || walletId === 'lock' || walletId === 'wallet')
2014-09-03 01:25:08 -03:00
continue;
2014-05-01 10:00:55 -03:00
2014-09-03 01:25:08 -03:00
if (typeof uniq[walletId] === 'undefined') {
walletIds.push(walletId);
uniq[walletId] = 1;
}
2014-05-01 10:00:55 -03:00
}
}
2014-09-03 01:25:08 -03:00
return cb(walletIds);
});
2014-05-01 10:00:55 -03:00
};
Storage.prototype.getWallets_Old = function(cb) {
2014-09-04 18:07:09 -03:00
preconditions.checkArgument(cb);
2014-09-18 19:20:00 -03:00
if (this.wListCache.ts > Date.now())
return cb(this.wListCache.data)
2014-05-01 10:00:55 -03:00
var wallets = [];
2014-09-03 01:25:08 -03:00
var self = this;
this._getWalletIds(function(ids) {
2014-09-08 10:46:57 -03:00
var l = ids.length,
i = 0;
2014-09-03 01:25:08 -03:00
if (!l)
return cb([]);
2014-09-19 15:26:57 -03:00
_.each(ids, function(id) {
self.getGlobal('nameFor::' + id, function(name) {
2014-09-19 15:26:57 -03:00
wallets.push({
id: id,
name: name,
2014-09-03 01:25:08 -03:00
});
2014-09-19 15:26:57 -03:00
if (++i == l) {
self.wListCache.data = wallets;
self.wListCache.ts = Date.now() + CACHE_DURATION;
return cb(wallets);
}
});
});
2014-09-03 01:25:08 -03:00
});
2014-05-01 10:00:55 -03:00
};
Storage.prototype.getWallets2 = function(cb) {
var self = this;
var re = /wallet::([^_]+)(_?(.*))/;
this.storage.allKeys(function(allKeys) {
var wallets = _.compact(_.map(allKeys, function(key) {
if (key.indexOf('wallet::') !== 0)
return null;
var match = key.match(re);
if (match.length != 4)
return null;
return {
id: match[1],
name: match[3] ? match[3] : undefined,
};
}));
return cb(wallets);
});
};
Storage.prototype.getWallets = function(cb) {
var self = this;
self.getWallets2(function(wallets) {
self.getWallets_Old(function(wallets2) {
var ids = _.pluck(wallets, 'id');
_.each(wallets2, function(w) {
if (!_.contains(ids, w.id))
wallets.push(w);
});
return cb(wallets);
});
})
};
Storage.prototype.deleteWallet_Old = function(walletId, cb) {
2014-09-04 18:07:09 -03:00
preconditions.checkArgument(walletId);
2014-09-08 10:46:57 -03:00
preconditions.checkArgument(cb);
var err;
2014-09-15 13:56:38 -03:00
var self = this;
2014-09-04 18:07:09 -03:00
2014-06-16 17:37:33 -03:00
var toDelete = {};
2014-09-08 10:46:57 -03:00
this.storage.allKeys(function(allKeys) {
2014-09-15 13:56:38 -03:00
for (var ii in allKeys) {
var key = allKeys[ii];
2014-09-08 10:46:57 -03:00
var split = key.split('::');
if (split.length == 2 && split[0] === walletId) {
toDelete[key] = 1;
};
2014-06-16 15:51:19 -03:00
}
2014-09-15 13:56:38 -03:00
var l = Object.keys(toDelete).length,
j = 0;
if (!l)
return cb(new Error('WNOTFOUND: Wallet not found'));
2014-09-08 10:46:57 -03:00
2014-09-15 13:56:38 -03:00
toDelete['nameFor::' + walletId] = 1;
l++;
for (var i in toDelete) {
self.removeGlobal(i, function() {
if (++j == l)
return cb(err);
});
}
});
};
Storage.prototype.deleteWallet = function(walletId, cb) {
preconditions.checkArgument(walletId);
preconditions.checkArgument(cb);
var self = this;
this.getWallets2(function(wallets) {
var w = _.findWhere(wallets, {
id: walletId
});
if (!w)
return cb(new Error('WNOTFOUND: Wallet not found'));
self.removeGlobal('wallet::' + walletId + (w.name ? '_' + w.name : ''), function() {
return cb();
});
});
2014-06-16 15:51:19 -03:00
};
2014-09-03 01:25:08 -03:00
Storage.prototype.setLastOpened = function(walletId, cb) {
this.setGlobal('lastOpened', walletId, cb);
};
2014-08-04 15:10:01 -03:00
2014-09-03 01:25:08 -03:00
Storage.prototype.getLastOpened = function(cb) {
this.getGlobal('lastOpened', cb);
};
2014-06-16 15:51:19 -03:00
2014-09-03 01:25:08 -03:00
Storage.prototype.setFromObj = function(walletId, obj, cb) {
preconditions.checkArgument(cb);
var self = this;
var key = 'wallet::' + walletId + ((obj.opts && obj.opts.name) ? '_' + obj.opts.name : '');
self._write(key, obj, function() {
return cb();
});
};
2014-05-01 10:00:55 -03:00
// remove all values
2014-09-03 01:25:08 -03:00
Storage.prototype.clearAll = function(cb) {
this.storage.clear(cb);
};
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;