diff --git a/js/models/Identity.js b/js/models/Identity.js index d496918ae..b7c389517 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -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') }; } diff --git a/js/models/PluginManager.js b/js/models/PluginManager.js index 57eaea6b9..6a58b6eb6 100644 --- a/js/models/PluginManager.js +++ b/js/models/PluginManager.js @@ -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); diff --git a/js/models/Profile.js b/js/models/Profile.js index 9665710c7..ecb6926f8 100644 --- a/js/models/Profile.js +++ b/js/models/Profile.js @@ -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; diff --git a/test/Storage.js b/test/Storage.js index dafda888d..cd5c87104 100644 --- a/test/Storage.js +++ b/test/Storage.js @@ -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]); }); diff --git a/test/WalletLock.js b/test/WalletLock.js index 6f680c395..14b86409d 100644 --- a/test/WalletLock.js +++ b/test/WalletLock.js @@ -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'); diff --git a/test/mocks/FakeLocalStorage.js b/test/mocks/FakeLocalStorage.js index 47fdb8c1c..56a6b5aab 100644 --- a/test/mocks/FakeLocalStorage.js +++ b/test/mocks/FakeLocalStorage.js @@ -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(), }; diff --git a/test/test.Profile.js b/test/test.Profile.js index e7a066100..7d16d09fb 100644 --- a/test/test.Profile.js +++ b/test/test.Profile.js @@ -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(); })