mirror of https://github.com/BTCPrivate/copay.git
fix tests and stringify for localstorage
This commit is contained in:
parent
f14d83cee1
commit
18aadede29
11
config.js
11
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',
|
||||
|
@ -89,6 +89,7 @@ var defaultConfig = {
|
|||
verbose: 1,
|
||||
// will duplicate itself after each try
|
||||
reconnectDelay: 5000,
|
||||
idleDurationMin: 1
|
||||
},
|
||||
|
||||
// blockchain service API config
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
|
@ -497,7 +497,12 @@ Wallet.prototype.getRegisteredPeerIds = function() {
|
|||
};
|
||||
|
||||
Wallet.prototype.keepAlive = function() {
|
||||
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() {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
20
js/routes.js
20
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('/');
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue