rm storage.storage to storage.db

This commit is contained in:
Matias Alejo Garcia 2014-09-27 18:00:27 -03:00
commit 028a300012
7 changed files with 39 additions and 34 deletions

View file

@ -8,6 +8,7 @@ var Wallet = require('./Wallet');
var _ = require('underscore'); var _ = require('underscore');
var log = require('../log'); var log = require('../log');
var PluginManager = require('./PluginManager'); var PluginManager = require('./PluginManager');
var Profile = require('./Profile');
var Async = module.exports.Async = require('./Async'); var Async = module.exports.Async = require('./Async');
var Insight = module.exports.Insight = require('./Insight'); var Insight = module.exports.Insight = require('./Insight');
var preconditions = require('preconditions').singleton(); var preconditions = require('preconditions').singleton();
@ -48,7 +49,7 @@ function Identity(config, version, pluginManager) {
if (pluginManager) { if (pluginManager) {
storageOpts = { storageOpts = {
storage: pluginManager.get('STORAGE') storage: pluginManager.get('DB')
}; };
} }

View file

@ -24,7 +24,7 @@ var KIND_UNIQUE = PluginManager.KIND_UNIQUE = 1;
var KIND_MULTIPLE = PluginManager.KIND_MULTIPLE = 2; var KIND_MULTIPLE = PluginManager.KIND_MULTIPLE = 2;
PluginManager.TYPE = {}; PluginManager.TYPE = {};
PluginManager.TYPE['STORAGE'] = KIND_UNIQUE; PluginManager.TYPE['DB'] = KIND_UNIQUE;
PluginManager.prototype._register = function(obj, name) { PluginManager.prototype._register = function(obj, name) {
preconditions.checkArgument(obj.type, 'Plugin has not type:' + name); preconditions.checkArgument(obj.type, 'Plugin has not type:' + name);

View file

@ -4,35 +4,40 @@ var _ = require('underscore');
var log = require('../log'); var log = require('../log');
var bitcore = require('bitcore'); var bitcore = require('bitcore');
function Profile(opts, storage) { function Profile(opts, password, storage) {
preconditions.checkArgument(opts.email); preconditions.checkArgument(opts.email);
preconditions.checkArgument(opts.password); preconditions.checkArgument(password);
preconditions.checkArgument(storage); preconditions.checkArgument(storage);
preconditions.checkArgument(storage.getItem); preconditions.checkArgument(storage.getItem);
this.email = opts.email; this.email = opts.email;
this.password = opts.password; this.hash = bitcore.util.sha256ripe160(this.email + this.password).toString('hex');
this.hash = bitcore.util.sha256ripe160(this.email + this.password); this.storage = storage;
this.extra = opts.extra; this.extra = opts.extra;
}; };
Profile.fromObj = function(obj, storage) { Profile.fromObj = function(obj, password, storage) {
return new Profile(obj, storage); var o = _.clone(obj);
return new Profile(obj, password, storage);
}; };
Profile.prototype.toObj = function() { Profile.prototype.toObj = function() {
return JSON.parse(JSON.stringify(this)); var obj = _.clone(this);
delete obj['hash'];
return JSON.parse(JSON.stringify(obj));
}; };
Profile.prototype.store = function(cb) { Profile.prototype.store = function(cb) {
// TODO var val = this.toObj();
return cb(); var key = 'identity::' + this.hash + '_' + this.email;
// this.storage.setItem(this.hash, this.toObj());
this.storage.setFromObj(key, val, function(err) {
log.debug('Identity stored');
if (cb)
cb(err);
});
}; };
module.exports = Profile; module.exports = Profile;

View file

@ -7,11 +7,10 @@ var timeStamp = Date.now();
describe('Storage model', function() { describe('Storage model', function() {
var s; var s;
beforeEach(function() { beforeEach(function(done) {
s = new Storage(requireMock('FakeLocalStorage').storageParams); s = new Storage(requireMock('FakeLocalStorage').storageParams);
s.setPassphrase('mysupercoolpassword'); s.setPassphrase('mysupercoolpassword');
s.storage.clear(); s.clearAll(done);
s.sessionStorage.clear();
}); });
@ -22,7 +21,7 @@ describe('Storage model', function() {
it('should fail when encrypting without a password', function() { it('should fail when encrypting without a password', function() {
var s2 = new Storage(requireMock('FakeLocalStorage').storageParams); var s2 = new Storage(requireMock('FakeLocalStorage').storageParams);
(function() { (function() {
var params = _.clone(require('./mocks/FakeLocalStorage').storageParams); var params = _.clone(requireMock('FakeLocalStorage').storageParams);
params.password = undefined; params.password = undefined;
new Storage(params); new Storage(params);
}).should.throw('Illegal Argument'); }).should.throw('Illegal Argument');
@ -325,7 +324,7 @@ describe('Storage model', function() {
'id1::b': 'y', 'id1::b': 'y',
'id2::c': 'z', 'id2::c': 'z',
}; };
s.storage.allKeys = sinon.stub().yields(_.keys(data)); s.db.allKeys = sinon.stub().yields(_.keys(data));
sinon.stub(s, '_read', function(k, cb) { sinon.stub(s, '_read', function(k, cb) {
return cb(data[k]); return cb(data[k]);
}); });
@ -354,7 +353,7 @@ describe('Storage model', function() {
c: 'z' c: 'z'
}, },
}; };
s.storage.allKeys = sinon.stub().yields(_.keys(data)); s.db.allKeys = sinon.stub().yields(_.keys(data));
sinon.stub(s, '_read', function(k, cb) { sinon.stub(s, '_read', function(k, cb) {
return cb(data[k]); return cb(data[k]);
}); });

View file

@ -11,8 +11,7 @@ describe('WalletLock model', function() {
beforeEach(function() { beforeEach(function() {
storage = new Storage(requireMock('FakeLocalStorage').storageParams); storage = new Storage(requireMock('FakeLocalStorage').storageParams);
storage.setPassphrase('mysupercoolpassword'); storage.setPassphrase('mysupercoolpassword');
storage.storage.clear(); storage.clearAll();
storage.sessionStorage.clear();
}); });
it('should fail with missing args', function() { it('should fail with missing args', function() {
@ -85,9 +84,9 @@ describe('WalletLock model', function() {
w.keepAlive(function() { w.keepAlive(function() {
storage.setSessionId('session2', function() { storage.setSessionId('session2', function() {
var json = JSON.parse(storage.storage.ls['lock::walletId']); var json = JSON.parse(storage.db.ls['lock::walletId']);
json.expireTs -= 3600 * 1000; json.expireTs -= 3600 * 1000;
storage.storage.ls['lock::walletId'] = JSON.stringify(json); storage.db.ls['lock::walletId'] = JSON.stringify(json);
var w2 = new WalletLock(storage, 'walletId'); var w2 = new WalletLock(storage, 'walletId');
w2.keepAlive(function(locked) { w2.keepAlive(function(locked) {
w2.sessionId.should.equal('session2'); w2.sessionId.should.equal('session2');

View file

@ -21,14 +21,15 @@ FakeLocalStorage.prototype.setItem = function(k, v, cb) {
this.ls[k] = v; this.ls[k] = v;
return cb(); return cb();
}; };
FakeLocalStorage.prototype.clear = function() { FakeLocalStorage.prototype.clear = function(cb) {
this.ls = {}; this.ls = {};
if (cb) return cb();
} }
module.exports = FakeLocalStorage; module.exports = FakeLocalStorage;
module.exports.storageParams = { module.exports.storageParams = {
password: '123', password: '123',
storage: new FakeLocalStorage(), db: new FakeLocalStorage(),
sessionStorage: new FakeLocalStorage(), sessionStorage: new FakeLocalStorage(),
}; };

View file

@ -14,11 +14,12 @@ describe('Profile model', function() {
var storage = new FakeStorage(); var storage = new FakeStorage();
var opts = { var opts = {
email: email, email: email,
password: password,
}; };
beforeEach(function() { beforeEach(function() {
storage.getItem = sinon.stub(); storage.getItem = sinon.stub();
storage.setFromObj = sinon.stub();
storage.setFromObj.yields(null);
}); });
it('should fail create an instance', function() { it('should fail create an instance', function() {
@ -32,21 +33,20 @@ describe('Profile model', function() {
it('should create an instance', function() { it('should create an instance', function() {
var p = new Profile({ var p = new Profile({
email: email, email: email,
password: password }, password, storage);
}, storage);
should.exist(p); should.exist(p);
}); });
it('#fromObj #toObj round trip', function() { it('#fromObj #toObj round trip', function() {
var p = new Profile(opts, password, storage);
var p = new Profile(opts, storage); var p2 = Profile.fromObj(p.toObj(), password, storage);
var p2 = Profile.fromObj(p.toObj(), storage);
p2.should.deep.equal(p); p2.should.deep.equal(p);
}); });
it('#store', function(done) { it('#store', function(done) {
var p = new Profile(opts, storage); var p = new Profile(opts, password, storage);
p.store(function(err) { p.store(function(err) {
storage.setFromObj.getCall(0).args[1].should.deep.equal(p.toObj());
should.not.exist(err); should.not.exist(err);
done(); done();
}) })