rm storage.storage to storage.db

This commit is contained in:
Matias Alejo Garcia 2014-09-27 18:00:27 -03:00
parent c4c7dc8eb1
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 log = require('../log');
var PluginManager = require('./PluginManager');
var Profile = require('./Profile');
var Async = module.exports.Async = require('./Async');
var Insight = module.exports.Insight = require('./Insight');
var preconditions = require('preconditions').singleton();
@ -48,7 +49,7 @@ function Identity(config, version, pluginManager) {
if (pluginManager) {
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;
PluginManager.TYPE = {};
PluginManager.TYPE['STORAGE'] = KIND_UNIQUE;
PluginManager.TYPE['DB'] = KIND_UNIQUE;
PluginManager.prototype._register = function(obj, name) {
preconditions.checkArgument(obj.type, 'Plugin has not type:' + name);

View File

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

View File

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

View File

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

View File

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

View File

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