From f1ae8f9c331ffbbef5aa543f3e369e43e604da8e Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 14 Oct 2014 09:58:12 -0300 Subject: [PATCH] add store/restore passphrasse methods --- js/models/Identity.js | 7 +++++-- js/models/Storage.js | 18 +++++++++++++++++- test/Identity.js | 10 ++++++++-- test/Storage.js | 2 +- test/mocks/FakeLocalStorage.js | 2 +- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/js/models/Identity.js b/js/models/Identity.js index 303298343..7b510ad3b 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -295,14 +295,17 @@ Identity.prototype.close = function(cb) { * @return {Wallet} */ Identity.prototype.importWallet = function(base64, password, skipFields, cb) { + preconditions.checkArgument(password); preconditions.checkArgument(cb); + this.storage.savePassphrase(); this.storage.setPassword(password); - var obj = this.storage.decrypt(base64); - if (!obj) return false; + this.storage.restorePassphrase(); + if (!obj) return false; var w = Identity._walletFromObj(obj, this.storage, this.networkOpts, this.blockchainOpts); +console.log('[Identity.js.307:Identity:]',w); //TODO this._checkVersion(w.version); this.addWallet(w, function(err) { if (err) return cb(err); diff --git a/js/models/Storage.js b/js/models/Storage.js index 949fc904f..f4392cbaf 100644 --- a/js/models/Storage.js +++ b/js/models/Storage.js @@ -45,8 +45,24 @@ Storage.prototype._getPassphrase = function() { throw new Error('NOPASSPHRASE: No passphrase set'); return pps[this.__uniqueid]; -} +}; +Storage.prototype.savePassphrase = function() { + if (!pps[this.__uniqueid]) + throw new Error('NOPASSPHRASE: No passphrase set'); + + this.savedPassphrase = this.savedPassphrase || {}; + this.savedPassphrase[this.__uniqueid] = pps[this.__uniqueid]; +}; + + +Storage.prototype.restorePassphrase = function() { + if (!this.savedPassphrase[this.__uniqueid]) + throw new Error('NOSTOREDPASSPHRASE: No stored passphrase'); + + pps[this.__uniqueid] = this.savedPassphrase[this.__uniqueid]; + this.savedPassphrase[this.__uniqueid] = undefined; +}; Storage.prototype.hasPassphrase = function() { return pps[this.__uniqueid] ? true : false; diff --git a/test/Identity.js b/test/Identity.js index 40c22e813..b75b42826 100644 --- a/test/Identity.js +++ b/test/Identity.js @@ -32,6 +32,8 @@ describe('Identity model', function() { beforeEach(function(done) { storage = sinon.stub(); storage.getItem = sinon.stub(); + storage.savePassphrase = sinon.spy(); + storage.restorePassphrase = sinon.spy(); storage.setPassword = sinon.spy(); storage.hasPassphrase = sinon.stub().returns(true); storage.getSessionId = sinon.spy(); @@ -127,7 +129,7 @@ describe('Identity model', function() { describe('#open', function(done) { beforeEach(function() { - storage.getFirst = sinon.stub().yields('wallet1234'); + storage.getFirst = sinon.stub().yields(null, 'wallet1234'); profile.listWallets = sinon.stub().returns([{id:'walletid'}]); Identity._openProfile = sinon.stub().callsArgWith(3, null, profile); Identity._walletRead = sinon.stub().callsArgWith(2, null, wallet); @@ -230,7 +232,7 @@ describe('Identity model', function() { beforeEach(function() { iden.migrateWallet = sinon.stub().yields(null); storage.setPassword = sinon.spy(); - storage.getFirst = sinon.stub().yields('wallet1234'); + storage.getFirst = sinon.stub().yields(null, 'wallet1234'); var wallet = sinon.stub(); wallet.store = sinon.stub().yields(null); @@ -256,6 +258,7 @@ describe('Identity model', function() { beforeEach(function() { iden.migrateWallet = sinon.stub().yields(null); + storage.getFirst = sinon.stub().yields(null, 'wallet1234'); }); it('should create wallet from encrypted object', function(done) { @@ -266,9 +269,12 @@ describe('Identity model', function() { wallet.getId = sinon.stub().returns('ID123'); Identity._walletFromObj = sinon.stub().returns(wallet); + Identity._walletRead = sinon.stub().yields(null,wallet); iden.importWallet("encrypted object", "xxx", [], function(err) { iden.openWallet('ID123', function(err, w) { + iden.storage.savePassphrase.calledOnce.should.equal(true); + iden.storage.restorePassphrase.calledOnce.should.equal(true); should.not.exist(err); should.exist(w); done(); diff --git a/test/Storage.js b/test/Storage.js index 842169528..a9545cfd7 100644 --- a/test/Storage.js +++ b/test/Storage.js @@ -22,7 +22,7 @@ describe('Storage model', function() { var s2 = new Storage(requireMock('FakeLocalStorage').storageParams); (function() { var params = _.clone(requireMock('FakeLocalStorage').storageParams); - params.password = undefined; + params.passphrase = '1234'; new Storage(params); }).should.throw('Illegal Argument'); }); diff --git a/test/mocks/FakeLocalStorage.js b/test/mocks/FakeLocalStorage.js index 3a8428f43..7af20d569 100644 --- a/test/mocks/FakeLocalStorage.js +++ b/test/mocks/FakeLocalStorage.js @@ -32,7 +32,7 @@ module.exports.storageParams = { password: '123', db: new FakeLocalStorage(), sessionStorage: new FakeLocalStorage(), - passphrase: { + passphraseConfig: { iterations: 1, }, };