fix tests and stringify for localstorage

This commit is contained in:
Matias Alejo Garcia 2014-08-15 12:43:43 -04:00
parent f14d83cee1
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
// 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

View File

@ -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();
});
}
});

View File

@ -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() {

View File

@ -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,

View File

@ -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;
};

View File

@ -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('/');

View File

@ -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();
};

View File

@ -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) {

View File

@ -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')