diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index daac04995..5d6948fdf 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -1670,7 +1670,7 @@ Wallet.prototype.indexDiscovery = function(start, change, cosigner, gap, cb) { Wallet.prototype.disconnect = function() { this.log('## DISCONNECTING'); - this.unlock(); + this.lock.release(); this.network.disconnect(); }; diff --git a/js/models/core/WalletFactory.js b/js/models/core/WalletFactory.js index 8d7ef29a4..db1d597ba 100644 --- a/js/models/core/WalletFactory.js +++ b/js/models/core/WalletFactory.js @@ -170,7 +170,7 @@ WalletFactory.prototype._checkNetwork = function(inNetworkName) { WalletFactory.prototype.open = function(walletId, passphrase) { this.storage._setPassphrase(passphrase); - var w = this.read(walletId, opts); + var w = this.read(walletId); if (w) w.store(); diff --git a/js/models/storage/LocalEncrypted.js b/js/models/storage/LocalEncrypted.js index acfcf696f..ac084a948 100644 --- a/js/models/storage/LocalEncrypted.js +++ b/js/models/storage/LocalEncrypted.js @@ -141,7 +141,7 @@ Storage.prototype.getWalletIds = function() { if (split.length == 2) { var walletId = split[0]; - if (walletId === 'nameFor' || walletId === 'lock') + if (!walletId || walletId === 'nameFor') continue; if (typeof uniq[walletId] === 'undefined') { @@ -190,19 +190,6 @@ Storage.prototype.getLastOpened = function() { return this.getGlobal('lastOpened'); } -// Lock related -Storage.prototype.setLock = function(walletId) { - this.setGlobal(this._key(walletId, 'Lock'), this.sessionId()); -} - -Storage.prototype.getLock = function(walletId) { - return this.getGlobal(this._key(walletId, 'Lock')); -} - -Storage.prototype.removeLock = function(walletId) { - this.removeGlobal(this._key(walletId, 'Lock')); -} - //obj contains keys to be set Storage.prototype.setFromObj = function(walletId, obj) { for (var k in obj) { diff --git a/test/mocks/FakeStorage.js b/test/mocks/FakeStorage.js index c83855306..81a0dcbae 100644 --- a/test/mocks/FakeStorage.js +++ b/test/mocks/FakeStorage.js @@ -69,7 +69,11 @@ FakeStorage.prototype.getWalletIds = function() { var split = ii.split('::'); if (split.length == 2) { var walletId = split[0]; - if (walletId !== 'nameFor' && typeof uniq[walletId] === 'undefined') { + + if (!walletId || walletId === 'nameFor' || walletId ==='lock') + continue; + + if (typeof uniq[walletId] === 'undefined') { walletIds.push(walletId); uniq[walletId] = 1; } diff --git a/test/test.LocalEncrypted.js b/test/test.LocalEncrypted.js index 5fcd6068c..f5c96ba98 100644 --- a/test/test.LocalEncrypted.js +++ b/test/test.LocalEncrypted.js @@ -1,14 +1,17 @@ 'use strict'; -var copay = copay || require('../copay'); var chai = chai || require('chai'); var should = chai.should(); -var LocalEncrypted = copay.StorageLocalEncrypted; - - var fakeWallet = 'fake-wallet-id'; var timeStamp = Date.now(); var localMock = require('./mocks/FakeLocalStorage'); +var is_browser = typeof process == 'undefined' || typeof process.versions === 'undefined'; +if (is_browser) { + var copay = require('copay'); //browser +} else { + var copay = require('../copay'); //node +} +var LocalEncrypted = copay.StorageLocalEncrypted; describe('Storage/LocalEncrypted model', function() { var s = new LocalEncrypted({ @@ -22,199 +25,214 @@ describe('Storage/LocalEncrypted model', function() { }); should.exist(s2); }); - it('should fail when encrypting without a password', function() { - var s2 = new LocalEncrypted({ - localStorage: localMock, - }); - (function() { - s2.set(fakeWallet, timeStamp, 1); - }).should.throw(); +}); + +it('should fail when encrypting without a password', function() { + var s2 = new LocalEncrypted({ + localStorage: localMock, }); - it('should be able to encrypt and decrypt', function() { - s._write(fakeWallet + timeStamp, 'value'); - s._read(fakeWallet + timeStamp).should.equal('value'); - localMock.removeItem(fakeWallet + timeStamp); - }); - it('should be able to set a value', function() { - s.set(fakeWallet, timeStamp, 1); + (function() { + s2.set(fakeWallet, timeStamp, 1); + }).should.throw(); +}); +it('should be able to encrypt and decrypt', function() { + s._write(fakeWallet + timeStamp, 'value'); + s._read(fakeWallet + timeStamp).should.equal('value'); + localMock.removeItem(fakeWallet + timeStamp); +}); +it('should be able to set a value', function() { + s.set(fakeWallet, timeStamp, 1); + localMock.removeItem(fakeWallet + '::' + timeStamp); +}); +var getSetData = [ + 1, 1000, -15, -1000, + 0.1, -0.5, -0.5e-10, Math.PI, + 'hi', 'auydoaiusyodaisudyoa', '0b5b8556a0c2ce828c9ccfa58b3dd0a1ae879b9b', + '1CjPR7Z5ZSyWk6WtXvSFgkptmpoi4UM9BC', 'OP_DUP OP_HASH160 80ad90d4035', [1, 2, 3, 4, 5, 6], { + x: 1, + y: 2 + }, { + x: 'hi', + y: null + }, { + a: {}, + b: [], + c: [1, 2, 'hi'] + }, + null +]; +getSetData.forEach(function(obj) { + it('should be able to set a value and get it for ' + JSON.stringify(obj), function() { + s.set(fakeWallet, timeStamp, obj); + var obj2 = s.get(fakeWallet, timeStamp); + JSON.stringify(obj2).should.equal(JSON.stringify(obj)); localMock.removeItem(fakeWallet + '::' + timeStamp); }); - var getSetData = [ - 1, 1000, -15, -1000, - 0.1, -0.5, -0.5e-10, Math.PI, - 'hi', 'auydoaiusyodaisudyoa', '0b5b8556a0c2ce828c9ccfa58b3dd0a1ae879b9b', - '1CjPR7Z5ZSyWk6WtXvSFgkptmpoi4UM9BC', 'OP_DUP OP_HASH160 80ad90d4035', [1, 2, 3, 4, 5, 6], { - x: 1, - y: 2 - }, { - x: 'hi', - y: null - }, { - a: {}, - b: [], - c: [1, 2, 'hi'] - }, - null - ]; - getSetData.forEach(function(obj) { - it('should be able to set a value and get it for ' + JSON.stringify(obj), function() { - s.set(fakeWallet, timeStamp, obj); - var obj2 = s.get(fakeWallet, timeStamp); - JSON.stringify(obj2).should.equal(JSON.stringify(obj)); - localMock.removeItem(fakeWallet + '::' + timeStamp); +}); + +describe('#export', function() { + it('should export the encrypted wallet', function() { + var storage = new LocalEncrypted({ + localStorage: localMock, + password: 'password', }); + storage.set(fakeWallet, timeStamp, 'testval'); + var obj = { + test: 'testval' + }; + var encrypted = storage.export(obj); + encrypted.length.should.be.greaterThan(10); + localMock.removeItem(fakeWallet + '::' + timeStamp); + //encrypted.slice(0,6).should.equal("53616c"); }); - - describe('#export', function() { - it('should export the encrypted wallet', function() { - var storage = new LocalEncrypted({ - localStorage: localMock, - password: 'password', - }); - storage.set(fakeWallet, timeStamp, 'testval'); - var obj = { - test: 'testval' - }; - var encrypted = storage.export(obj); - encrypted.length.should.be.greaterThan(10); - localMock.removeItem(fakeWallet + '::' + timeStamp); - //encrypted.slice(0,6).should.equal("53616c"); +}); +describe('#_decryptObj', function() { + it('should decrypt and Obj', function() { + var storage = new LocalEncrypted({ + password: 'password', + localStorage: localMock, }); - }); - - describe('#remove', function() { - it('should remove an item', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' - }); - s.set('1', "hola", 'juan'); - s.get('1', 'hola').should.equal('juan'); - s.remove('1', 'hola'); - - should.not.exist(s.get('1', 'hola')); - }); - }); - - - describe('#getWalletIds', function() { - it('should get wallet ids', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' - }); - s.set('1', "hola", 'juan'); - s.set('2', "hola", 'juan'); - s.getWalletIds().should.deep.equal(['1', '2']); - }); - }); - - describe('#getName #setName', function() { - it('should get/set names', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' - }); - s.setName(1, 'hola'); - s.getName(1).should.equal('hola'); - }); - }); - - describe('#getLastOpened #setLastOpened', function() { - it('should get/set names', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' - }); - s.setLastOpened('hey'); - s.getLastOpened().should.equal('hey'); - }); - }); - - describe('#WalletLock', function() { - it('should get/set/remove opened', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' - }); - s.setLock('walletId'); - s.getLock('walletId').should.equal(true); - s.removeLock('walletId'); - should.not.exist(s.getLock('walletId')); - }); - }); - - describe('#getWallets', function() { - it('should retreive wallets from storage', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' - }); - s.set('1', "hola", 'juan'); - s.set('2', "hola", 'juan'); - s.setName(1, 'hola'); - s.getWallets()[0].should.deep.equal({ - id: '1', - name: 'hola', - }); - s.getWallets()[1].should.deep.equal({ - id: '2', - name: undefined - }); - }); - }); - describe('#deleteWallet', function() { - it('should delete a wallet', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' - }); - s.set('1', "hola", 'juan'); - s.set('2', "hola", 'juan'); - s.setName(1, 'hola'); - - s.deleteWallet('1'); - s.getWallets().length.should.equal(1); - s.getWallets()[0].should.deep.equal({ - id: '2', - name: undefined - }); - }); - }); - - describe('#setFromObj', function() { - it('set localstorage from an object', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' - }); - s.setFromObj('id1', { - 'key': 'val', - 'opts': { - 'name': 'nameid1' - }, - }); - - s.get('id1', 'key').should.equal('val'); - - }); - }); - - - describe('#globals', function() { - it('should set, get and remove keys', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' - }); - s.setGlobal('a', { - b: 1 - }); - JSON.parse(s.getGlobal('a')).should.deep.equal({ - b: 1 - }); - s.removeGlobal('a'); - should.not.exist(s.getGlobal('a')); + storage._decryptObj('{"a":"2"}').should.deep.equal({ + a: "2" }); }); }); + + +describe('#remove', function() { + it('should remove an item', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + password: 'password' + }); + s.set('1', "hola", 'juan'); + s.get('1', 'hola').should.equal('juan'); + s.remove('1', 'hola'); + + should.not.exist(s.get('1', 'hola')); + }); +}); + + +describe('#getWalletIds', function() { + it('should get wallet ids', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + password: 'password' + }); + s.set('1', "hola", 'juan'); + s.set('2', "hola", 'juan'); + s.getWalletIds().should.deep.equal(['1', '2']); + }); +}); + +describe('#getName #setName', function() { + it('should get/set names', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + password: 'password' + }); + s.setName(1, 'hola'); + s.getName(1).should.equal('hola'); + }); +}); + +describe('#getLastOpened #setLastOpened', function() { + it('should get/set names', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + password: 'password' + }); + s.setLastOpened('hey'); + s.getLastOpened().should.equal('hey'); + }); +}); + +if (is_browser) { + describe('#getSessionId', function() { + it('should get SessionId', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + password: 'password' + }); + var sid = s.getSessionId(); + should.exist(sid); + var sid2 = s.getSessionId(); + sid2.should.equal(sid); + }); + }); +} + +describe('#getWallets', function() { + it('should retreive wallets from storage', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + password: 'password' + }); + s.set('1', "hola", 'juan'); + s.set('2', "hola", 'juan'); + s.setName(1, 'hola'); + s.getWallets()[0].should.deep.equal({ + id: '1', + name: 'hola', + }); + s.getWallets()[1].should.deep.equal({ + id: '2', + name: undefined + }); + }); +}); +describe('#deleteWallet', function() { + it('should delete a wallet', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + password: 'password' + }); + s.set('1', "hola", 'juan'); + s.set('2', "hola", 'juan'); + s.setName(1, 'hola'); + + s.deleteWallet('1'); + s.getWallets().length.should.equal(1); + s.getWallets()[0].should.deep.equal({ + id: '2', + name: undefined + }); + }); +}); + +describe('#setFromObj', function() { + it('set localstorage from an object', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + password: 'password' + }); + s.setFromObj('id1', { + 'key': 'val', + 'opts': { + 'name': 'nameid1' + }, + }); + + s.get('id1', 'key').should.equal('val'); + + }); +}); + + +describe('#globals', function() { + it('should set, get and remove keys', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + password: 'password' + }); + s.setGlobal('a', { + b: 1 + }); + JSON.parse(s.getGlobal('a')).should.deep.equal({ + b: 1 + }); + s.removeGlobal('a'); + should.not.exist(s.getGlobal('a')); + }); +}); diff --git a/test/test.Wallet.js b/test/test.Wallet.js index da692b12b..52cb41297 100644 --- a/test/test.Wallet.js +++ b/test/test.Wallet.js @@ -1024,22 +1024,6 @@ describe('Wallet model', function() { w.network.start.getCall(0).args[0].privkey.length.should.equal(64); }); - it('should not start if locked', function() { - var w = cachedCreateW2(); - w.netStart(); - w.emit = sinon.spy(); - w.netStart(); - w.emit.getCall(0).args[0].should.equal('locked'); - }); - - it('should accept ignoreLocked', function() { - var w = cachedCreateW2(); - w.netStart(); - w.network.start = sinon.spy(); - w.ignoreLock=1; - w.netStart(); - w.network.start.getCall(0).args[0].privkey.length.should.equal(64); - }); }); describe('#forceNetwork in config', function() { diff --git a/test/test.WalletFactory.js b/test/test.WalletFactory.js index d390c2641..0c501d7e4 100644 --- a/test/test.WalletFactory.js +++ b/test/test.WalletFactory.js @@ -298,6 +298,7 @@ describe('WalletFactory model', function() { var w = wf.create({ name: 'test wallet' }); + ws = wf.getWallets(); ws.length.should.equal(1); ws[0].name.should.equal('test wallet');