fix tests and stringify for localstorage

This commit is contained in:
Matias Alejo Garcia 2014-08-15 12:43:43 -04:00
commit 18aadede29
9 changed files with 75 additions and 76 deletions

View file

@ -19,11 +19,11 @@ var defaultConfig = {
// Use this to run your own local PeerJS server // Use this to run your own local PeerJS server
// with params: ./peerjs -p 10009 -k '6d6d751ea61e26f2' // with params: ./peerjs -p 10009 -k '6d6d751ea61e26f2'
/* /*
key: '6d6d751ea61e26f2', key: '6d6d751ea61e26f2',
host: 'localhost', host: 'localhost',
port: 10009, port: 10009,
path: '/', path: '/',
*/ */
// Use this to connect to bitpay's PeerJS server // Use this to connect to bitpay's PeerJS server
key: 'satoshirocks', key: 'satoshirocks',
@ -44,39 +44,39 @@ var defaultConfig = {
'iceServers': [ 'iceServers': [
// Pass in STUN and TURN servers for maximum network compatibility // Pass in STUN and TURN servers for maximum network compatibility
{ {
url: 'stun:162.242.219.26' url: 'stun:162.242.219.26'
}, { }, {
url: 'turn:162.242.219.26', url: 'turn:162.242.219.26',
username: 'bitcore', username: 'bitcore',
credential: 'bitcore', credential: 'bitcore',
} }
// { // {
// url: 'stun:stun.l.google.com:19302' // url: 'stun:stun.l.google.com:19302'
// }, { // }, {
// url: 'stun:stun1.l.google.com:19302' // url: 'stun:stun1.l.google.com:19302'
// }, { // }, {
// url: 'stun:stun2.l.google.com:19302' // url: 'stun:stun2.l.google.com:19302'
// }, { // }, {
// url: 'stun:stun3.l.google.com:19302' // url: 'stun:stun3.l.google.com:19302'
// }, { // }, {
// url: 'stun:stun4.l.google.com:19302' // url: 'stun:stun4.l.google.com:19302'
// }, { // }, {
// url: 'stun:stunserver.org' // url: 'stun:stunserver.org'
// } // }
// // Options fot TURN servers with p2p communications are not possible. // // Options fot TURN servers with p2p communications are not possible.
// { // {
// url: 'turn:numb.viagenie.ca', // url: 'turn:numb.viagenie.ca',
// credential: 'muazkh', // credential: 'muazkh',
// username: 'webrtc@live.com' // username: 'webrtc@live.com'
// }, { // }, {
// url: 'turn:192.158.29.39:3478?transport=udp', // url: 'turn:192.158.29.39:3478?transport=udp',
// credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=', // credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
// username: '28224511:1379330808' // username: '28224511:1379330808'
// }, { // }, {
// url: 'turn:192.158.29.39:3478?transport=tcp', // url: 'turn:192.158.29.39:3478?transport=tcp',
// credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=', // credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
// username: '28224511:1379330808' // username: '28224511:1379330808'
// } // }
] ]
} }
}, },
@ -89,6 +89,7 @@ var defaultConfig = {
verbose: 1, verbose: 1,
// will duplicate itself after each try // will duplicate itself after each try
reconnectDelay: 5000, reconnectDelay: 5000,
idleDurationMin: 1
}, },
// blockchain service API config // blockchain service API config

View file

@ -65,13 +65,17 @@ angular.module('copayApp.controllers').controller('SidebarController', function(
controllerUtils.setSocketHandlers(); controllerUtils.setSocketHandlers();
if ($rootScope.wallet) { if ($rootScope.wallet) {
$scope.$on('$idleStart', function(a) { $scope.$on('$idleWarn', function(a,countdown) {
notification.warning('Session will be closed', 'Your session is about to expire due to inactivity'); 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.$on('$idleTimeout', function() {
$scope.signout(); $scope.signout();
notification.warning('Session closed', 'Session closed because a long time of inactivity'); notification.warning('Session closed', 'Session closed because a long time of inactivity');
}); });
$scope.$on('$keepalive', function() {
$rootScope.wallet.keepAlive();
});
} }
}); });

View file

@ -497,7 +497,12 @@ Wallet.prototype.getRegisteredPeerIds = function() {
}; };
Wallet.prototype.keepAlive = 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() { Wallet.prototype.store = function() {

View file

@ -17,20 +17,24 @@ WalletLock._keyFor = function(walletId) {
}; };
WalletLock.prototype._isLockedByOther = function() { WalletLock.prototype._isLockedByOther = function() {
var wl = this.storage.getGlobal(this.key); var json = this.storage.getGlobal(this.key);
var wl = json ? JSON.parse(json) : null;
if (!wl || wl.expireTs < Date.now() || wl.sessionId === this.sessionId) var t = wl ? (Date.now() - wl.expireTs) : false;
// is not locked?
if (!wl || t > 0 || wl.sessionId === this.sessionId)
return false; return false;
return true; // Seconds remainding
return parseInt(-t/1000.);
}; };
WalletLock.prototype.keepAlive = function() { WalletLock.prototype.keepAlive = function() {
preconditions.checkState(this.sessionId); preconditions.checkState(this.sessionId);
if (this._isLockedByOther()) var t = this._isLockedByOther();
throw new Error('Could not adquire lock'); 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, { this.storage.setGlobal(this.key, {
sessionId: this.sessionId, sessionId: this.sessionId,

View file

@ -101,7 +101,7 @@ Storage.prototype.getSessionId = function() {
var sessionId = this.sessionStorage.getItem('sessionId'); var sessionId = this.sessionStorage.getItem('sessionId');
if (!sessionId) { if (!sessionId) {
sessionId = bitcore.SecureRandom.getRandomBuffer(8).toString('hex'); sessionId = bitcore.SecureRandom.getRandomBuffer(8).toString('hex');
this.sessionStorage.setItem(sessionId, 'sessionId'); this.sessionStorage.setItem('sessionId', sessionId);
} }
return sessionId; return sessionId;
}; };

View file

@ -69,13 +69,15 @@ angular
//Setting HTML5 Location Mode //Setting HTML5 Location Mode
angular angular
.module('copayApp') .module('copayApp')
.config(function($locationProvider, $idleProvider) { .config(function($locationProvider, $idleProvider, $keepaliveProvider) {
$locationProvider $locationProvider
.html5Mode(false) .html5Mode(false)
.hashPrefix('!'); .hashPrefix('!');
// IDLE timeout // 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 $idleProvider.warningDuration(20); // in seconds
$keepaliveProvider.interval(5); // in seconds
}) })
.run(function($rootScope, $location, $idle) { .run(function($rootScope, $location, $idle) {
$idle.watch(); $idle.watch();
@ -83,20 +85,6 @@ angular
if (!util.supports.data) { if (!util.supports.data) {
$location.path('unsupported'); $location.path('unsupported');
} else { } 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) { if ((!$rootScope.wallet || !$rootScope.wallet.id) && next.validate) {
$idle.unwatch(); $idle.unwatch();
$location.path('/'); $location.path('/');

View file

@ -26,7 +26,7 @@ angular.module('copayApp.services')
Socket.removeAllListeners(); Socket.removeAllListeners();
$rootScope.wallet = $rootScope.tmp = null; $rootScope.wallet = null;
delete $rootScope['wallet']; delete $rootScope['wallet'];
video.close(); video.close();
@ -72,7 +72,6 @@ angular.module('copayApp.services')
root.setupRootVariables = function() { root.setupRootVariables = function() {
uriHandler.register(); uriHandler.register();
$rootScope.unitName = config.unitName; $rootScope.unitName = config.unitName;
$rootScope.showLockWarning = false;
$rootScope.txAlertCount = 0; $rootScope.txAlertCount = 0;
$rootScope.insightError = 0; $rootScope.insightError = 0;
$rootScope.isCollapsed = true; $rootScope.isCollapsed = true;
@ -125,12 +124,6 @@ angular.module('copayApp.services')
}; };
notification.enableHtml5Mode(); // for chrome: if support, enable it 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) { w.on('badMessage', function(peerId) {
notification.error('Error', 'Received wrong message from peer ' + peerId); notification.error('Error', 'Received wrong message from peer ' + peerId);
@ -195,6 +188,7 @@ angular.module('copayApp.services')
$rootScope.$digest(); $rootScope.$digest();
}); });
w.on('close', root.onErrorDigest); w.on('close', root.onErrorDigest);
w.on('locked', root.onErrorDigest.bind(this));
w.netStart(); w.netStart();
}; };

View file

@ -11,8 +11,8 @@ FakeStorage.prototype._setPassphrase = function(password) {
this.storage.passphrase = password; this.storage.passphrase = password;
}; };
FakeStorage.prototype.setGlobal = function(id, payload) { FakeStorage.prototype.setGlobal = function(id, v) {
this.storage[id] = payload; this.storage[id] = typeof v === 'object' ? JSON.stringify(v) : v;
}; };
FakeStorage.prototype.getGlobal = function(id) { FakeStorage.prototype.getGlobal = function(id) {

View file

@ -41,7 +41,7 @@ describe('WalletLock model', function() {
storage.sessionId = 'xxx'; storage.sessionId = 'xxx';
(function() { (function() {
new WalletLock(storage, 'walletId') new WalletLock(storage, 'walletId')
}).should.throw('adquire lock'); }).should.throw('already open');
}); });
it('should not fail if locked by me', function() { it('should not fail if locked by me', function() {
@ -60,7 +60,10 @@ describe('WalletLock model', function() {
it('should not fail if expired', function() { it('should not fail if expired', function() {
var s = new Storage(); var s = new Storage();
var w = new WalletLock(s, 'walletId'); 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'; s.sessionId = 'xxx';
var w2 = new WalletLock(s, 'walletId') var w2 = new WalletLock(s, 'walletId')