From 18aadede29c9cbbb130b788f38e914c34adbdefd Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 15 Aug 2014 12:43:43 -0400 Subject: [PATCH] fix tests and stringify for localstorage --- config.js | 77 +++++++++++++++-------------- js/controllers/sidebar.js | 8 ++- js/models/core/Wallet.js | 7 ++- js/models/core/WalletLock.js | 16 +++--- js/models/storage/LocalEncrypted.js | 2 +- js/routes.js | 20 ++------ js/services/controllerUtils.js | 10 +--- test/mocks/FakeStorage.js | 4 +- test/test.WalletLock.js | 7 ++- 9 files changed, 75 insertions(+), 76 deletions(-) diff --git a/config.js b/config.js index 73fde9550..a9a61a6bf 100644 --- a/config.js +++ b/config.js @@ -19,11 +19,11 @@ var defaultConfig = { // Use this to run your own local PeerJS server // with params: ./peerjs -p 10009 -k '6d6d751ea61e26f2' /* - key: '6d6d751ea61e26f2', - host: 'localhost', - port: 10009, - path: '/', - */ +key: '6d6d751ea61e26f2', +host: 'localhost', +port: 10009, +path: '/', +*/ // Use this to connect to bitpay's PeerJS server key: 'satoshirocks', @@ -44,39 +44,39 @@ var defaultConfig = { 'iceServers': [ // Pass in STUN and TURN servers for maximum network compatibility { - url: 'stun:162.242.219.26' - }, { - url: 'turn:162.242.219.26', - username: 'bitcore', - credential: 'bitcore', - } - // { - // url: 'stun:stun.l.google.com:19302' - // }, { - // url: 'stun:stun1.l.google.com:19302' - // }, { - // url: 'stun:stun2.l.google.com:19302' - // }, { - // url: 'stun:stun3.l.google.com:19302' - // }, { - // url: 'stun:stun4.l.google.com:19302' - // }, { - // url: 'stun:stunserver.org' - // } - // // Options fot TURN servers with p2p communications are not possible. - // { - // url: 'turn:numb.viagenie.ca', - // credential: 'muazkh', - // username: 'webrtc@live.com' - // }, { - // url: 'turn:192.158.29.39:3478?transport=udp', - // credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=', - // username: '28224511:1379330808' - // }, { - // url: 'turn:192.158.29.39:3478?transport=tcp', - // credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=', - // username: '28224511:1379330808' - // } + url: 'stun:162.242.219.26' + }, { + url: 'turn:162.242.219.26', + username: 'bitcore', + credential: 'bitcore', + } + // { + // url: 'stun:stun.l.google.com:19302' + // }, { + // url: 'stun:stun1.l.google.com:19302' + // }, { + // url: 'stun:stun2.l.google.com:19302' + // }, { + // url: 'stun:stun3.l.google.com:19302' + // }, { + // url: 'stun:stun4.l.google.com:19302' + // }, { + // url: 'stun:stunserver.org' + // } + // // Options fot TURN servers with p2p communications are not possible. + // { + // url: 'turn:numb.viagenie.ca', + // credential: 'muazkh', + // username: 'webrtc@live.com' + // }, { + // url: 'turn:192.158.29.39:3478?transport=udp', + // credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=', + // username: '28224511:1379330808' + // }, { + // url: 'turn:192.158.29.39:3478?transport=tcp', + // credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=', + // username: '28224511:1379330808' + // } ] } }, @@ -89,6 +89,7 @@ var defaultConfig = { verbose: 1, // will duplicate itself after each try reconnectDelay: 5000, + idleDurationMin: 1 }, // blockchain service API config diff --git a/js/controllers/sidebar.js b/js/controllers/sidebar.js index f0e907cf9..26c3f60f8 100644 --- a/js/controllers/sidebar.js +++ b/js/controllers/sidebar.js @@ -65,13 +65,17 @@ angular.module('copayApp.controllers').controller('SidebarController', function( controllerUtils.setSocketHandlers(); if ($rootScope.wallet) { - $scope.$on('$idleStart', function(a) { - notification.warning('Session will be closed', 'Your session is about to expire due to inactivity'); + $scope.$on('$idleWarn', function(a,countdown) { + if (!(countdown%5)) + notification.warning('Session will be closed', 'Your session is about to expire due to inactivity in ' + countdown + ' seconds'); }); $scope.$on('$idleTimeout', function() { $scope.signout(); notification.warning('Session closed', 'Session closed because a long time of inactivity'); }); + $scope.$on('$keepalive', function() { + $rootScope.wallet.keepAlive(); + }); } }); diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 5d6948fdf..3e68f56d1 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -497,7 +497,12 @@ Wallet.prototype.getRegisteredPeerIds = function() { }; Wallet.prototype.keepAlive = function() { - this.lock.keepAlive(); + try{ + this.lock.keepAlive(); + } catch(e){ + this.log(e); + this.emit('locked',null,'Wallet appears to be openned on other browser instance. Closing this one.' ); + } }; Wallet.prototype.store = function() { diff --git a/js/models/core/WalletLock.js b/js/models/core/WalletLock.js index 7bdf7d7b5..4e57250ca 100644 --- a/js/models/core/WalletLock.js +++ b/js/models/core/WalletLock.js @@ -17,20 +17,24 @@ WalletLock._keyFor = function(walletId) { }; WalletLock.prototype._isLockedByOther = function() { - var wl = this.storage.getGlobal(this.key); - - if (!wl || wl.expireTs < Date.now() || wl.sessionId === this.sessionId) + var json = this.storage.getGlobal(this.key); + var wl = json ? JSON.parse(json) : null; + var t = wl ? (Date.now() - wl.expireTs) : false; + // is not locked? + if (!wl || t > 0 || wl.sessionId === this.sessionId) return false; - return true; + // Seconds remainding + return parseInt(-t/1000.); }; WalletLock.prototype.keepAlive = function() { preconditions.checkState(this.sessionId); - if (this._isLockedByOther()) - throw new Error('Could not adquire lock'); + var t = this._isLockedByOther(); + if (t) + throw new Error('Wallet is already open. Close it to proceed or wait '+ t + ' seconds if you close it already' ); this.storage.setGlobal(this.key, { sessionId: this.sessionId, diff --git a/js/models/storage/LocalEncrypted.js b/js/models/storage/LocalEncrypted.js index 60fb419c7..c45dc1b85 100644 --- a/js/models/storage/LocalEncrypted.js +++ b/js/models/storage/LocalEncrypted.js @@ -101,7 +101,7 @@ Storage.prototype.getSessionId = function() { var sessionId = this.sessionStorage.getItem('sessionId'); if (!sessionId) { sessionId = bitcore.SecureRandom.getRandomBuffer(8).toString('hex'); - this.sessionStorage.setItem(sessionId, 'sessionId'); + this.sessionStorage.setItem('sessionId', sessionId); } return sessionId; }; diff --git a/js/routes.js b/js/routes.js index 152068985..93011afd4 100644 --- a/js/routes.js +++ b/js/routes.js @@ -69,13 +69,15 @@ angular //Setting HTML5 Location Mode angular .module('copayApp') -.config(function($locationProvider, $idleProvider) { +.config(function($locationProvider, $idleProvider, $keepaliveProvider) { $locationProvider .html5Mode(false) .hashPrefix('!'); // IDLE timeout - $idleProvider.idleDuration(config.wallet.idleDurationMin * 60); // in seconds + var timeout = config.wallet.idleDurationMin * 60 || 300; + $idleProvider.idleDuration(timeout); // in seconds $idleProvider.warningDuration(20); // in seconds + $keepaliveProvider.interval(5); // in seconds }) .run(function($rootScope, $location, $idle) { $idle.watch(); @@ -83,20 +85,6 @@ angular if (!util.supports.data) { $location.path('unsupported'); } else { - - // Locked? - if ($rootScope.showLockWarning) { - if ($rootScope.tmp) { - if ($location.path() !== '/warning') { - $location.path('/warning'); - } - else { - delete $rootScope['showLockWarning']; - } - } - return; - } - if ((!$rootScope.wallet || !$rootScope.wallet.id) && next.validate) { $idle.unwatch(); $location.path('/'); diff --git a/js/services/controllerUtils.js b/js/services/controllerUtils.js index ef4c0b861..dfbe4bb45 100644 --- a/js/services/controllerUtils.js +++ b/js/services/controllerUtils.js @@ -26,7 +26,7 @@ angular.module('copayApp.services') Socket.removeAllListeners(); - $rootScope.wallet = $rootScope.tmp = null; + $rootScope.wallet = null; delete $rootScope['wallet']; video.close(); @@ -72,7 +72,6 @@ angular.module('copayApp.services') root.setupRootVariables = function() { uriHandler.register(); $rootScope.unitName = config.unitName; - $rootScope.showLockWarning = false; $rootScope.txAlertCount = 0; $rootScope.insightError = 0; $rootScope.isCollapsed = true; @@ -125,12 +124,6 @@ angular.module('copayApp.services') }; notification.enableHtml5Mode(); // for chrome: if support, enable it - w.on('locked', function() { - $rootScope.tmp = w; - $rootScope.showLockWarning=true; - $location.path('/warning'); - $rootScope.$digest(); - }); w.on('badMessage', function(peerId) { notification.error('Error', 'Received wrong message from peer ' + peerId); @@ -195,6 +188,7 @@ angular.module('copayApp.services') $rootScope.$digest(); }); w.on('close', root.onErrorDigest); + w.on('locked', root.onErrorDigest.bind(this)); w.netStart(); }; diff --git a/test/mocks/FakeStorage.js b/test/mocks/FakeStorage.js index 81a0dcbae..9aea4d2ea 100644 --- a/test/mocks/FakeStorage.js +++ b/test/mocks/FakeStorage.js @@ -11,8 +11,8 @@ FakeStorage.prototype._setPassphrase = function(password) { this.storage.passphrase = password; }; -FakeStorage.prototype.setGlobal = function(id, payload) { - this.storage[id] = payload; +FakeStorage.prototype.setGlobal = function(id, v) { + this.storage[id] = typeof v === 'object' ? JSON.stringify(v) : v; }; FakeStorage.prototype.getGlobal = function(id) { diff --git a/test/test.WalletLock.js b/test/test.WalletLock.js index 6882086da..54ae794e7 100644 --- a/test/test.WalletLock.js +++ b/test/test.WalletLock.js @@ -41,7 +41,7 @@ describe('WalletLock model', function() { storage.sessionId = 'xxx'; (function() { new WalletLock(storage, 'walletId') - }).should.throw('adquire lock'); + }).should.throw('already open'); }); it('should not fail if locked by me', function() { @@ -60,7 +60,10 @@ describe('WalletLock model', function() { it('should not fail if expired', function() { var s = new Storage(); var w = new WalletLock(s, 'walletId'); - s.storage[Object.keys(s.storage)[0]].expireTs = Date.now() - 60 * 6 * 1000; + var k = Object.keys(s.storage)[0]; + var v = JSON.parse(s.storage[k]); + v.expireTs = Date.now() - 60 * 6 * 1000; + s.storage[k] = JSON.stringify(v); s.sessionId = 'xxx'; var w2 = new WalletLock(s, 'walletId')