Wallet/js/models/Storage.js

301 lines
6.9 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-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-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-04-15 18:13:25 -03:00
Storage.prototype._getPassphrase = function() {
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
}
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
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-08 10:46:57 -03:00
self.sessionStorage.setItem('sessionId', sessionId, function(){
return cb(sessionId);
});
});
2014-08-14 18:46:42 -04:00
};
2014-05-01 10:00:55 -03:00
Storage.prototype._key = function(walletId, k) {
return walletId + '::' + k;
};
// get value by key
2014-09-03 01:25:08 -03:00
Storage.prototype.get = function(walletId, k, cb) {
2014-09-08 10:46:57 -03:00
preconditions.checkArgument(walletId, k, cb);
2014-09-03 01:25:08 -03:00
this._read(this._key(walletId, k), 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
});
};
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
});
}
};
2014-05-01 10:00:55 -03:00
// set value for key
2014-09-03 01:25:08 -03:00
Storage.prototype.set = function(walletId, k, v, cb) {
2014-09-04 18:07:09 -03:00
preconditions.checkArgument(walletId && k && !_.isUndefined(v) && cb);
2014-09-03 01:25:08 -03:00
this._write(this._key(walletId, k), v, cb);
};
2014-05-01 10:00:55 -03:00
// remove value for key
2014-09-03 01:25:08 -03:00
Storage.prototype.remove = function(walletId, k, cb) {
2014-09-04 18:07:09 -03:00
preconditions.checkArgument(walletId && k && cb);
2014-09-03 01:25:08 -03:00
this.removeGlobal(this._key(walletId, k), cb);
2014-05-01 10:00:55 -03:00
};
2014-09-03 01:25:08 -03:00
Storage.prototype.setName = function(walletId, name, cb) {
2014-09-04 18:07:09 -03:00
preconditions.checkArgument(walletId && name && cb);
2014-09-03 01:25:08 -03:00
this.setGlobal('nameFor::' + walletId, name, cb);
2014-05-01 10:00:55 -03:00
};
2014-09-03 15:43:27 -03:00
Storage.prototype.getName = function(walletId, cb) {
2014-09-04 18:07:09 -03:00
preconditions.checkArgument(walletId && cb);
2014-09-03 15:43:27 -03:00
this.getGlobal('nameFor::' + walletId, cb);
2014-05-01 10:00:55 -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-07-08 19:11:48 -03:00
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
2014-09-03 01:25:08 -03:00
if (!walletId || walletId === 'nameFor' || walletId === 'lock')
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
};
2014-09-03 01:25:08 -03:00
Storage.prototype.getWallets = function(cb) {
2014-09-04 18:07:09 -03:00
preconditions.checkArgument(cb);
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-03 15:43:27 -03:00
for (var ii in ids) {
var id = ids[ii];
self.getName(id, function(name) {
2014-09-03 01:25:08 -03:00
wallets.push({
2014-09-03 15:43:27 -03:00
id: id,
2014-09-03 01:25:08 -03:00
name: name,
});
2014-09-03 15:43:27 -03:00
if (++i == l) {
2014-09-03 01:25:08 -03:00
return cb(wallets);
}
})
}
});
2014-05-01 10:00:55 -03:00
};
2014-09-08 10:46:57 -03:00
Storage.prototype.deleteWallet = 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-04 18:07:09 -03:00
2014-06-16 17:37:33 -03:00
var toDelete = {};
toDelete['nameFor::' + walletId] = 1;
2014-09-08 10:46:57 -03:00
this.storage.allKeys(function(allKeys) {
for (var key in allKeys) {
var split = key.split('::');
if (split.length == 2 && split[0] === walletId) {
toDelete[key] = 1;
};
2014-06-16 15:51:19 -03:00
}
2014-09-08 10:46:57 -03:00
});
var l = toDelete.length,
j = 0;
if (!l)
return cb(new Error('WNOTFOUND: Wallet not found'));
2014-06-16 17:37:33 -03:00
for (var i in toDelete) {
2014-09-08 10:46:57 -03:00
this.removeGlobal(i, function() {
if (++j == l)
return cb(err);
2014-09-08 10:46:57 -03:00
});
2014-06-16 17:37:33 -03:00
}
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-08-04 15:10:01 -03:00
}
2014-06-16 15:51:19 -03:00
2014-05-01 10:00:55 -03:00
//obj contains keys to be set
2014-09-03 01:25:08 -03:00
Storage.prototype.setFromObj = function(walletId, obj, cb) {
preconditions.checkArgument(cb);
var self = this;
var l = Object.keys(obj).length,
i = 0;
2014-05-01 10:00:55 -03:00
for (var k in obj) {
2014-09-03 01:25:08 -03:00
self.set(walletId, k, obj[k], function() {
if (++i == l) {
self.setName(walletId, obj.opts.name, cb);
}
});
2014-05-01 10:00:55 -03:00
}
};
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;