diff --git a/js/controllers/sidebar.js b/js/controllers/sidebar.js index b52545e43..870ddbc82 100644 --- a/js/controllers/sidebar.js +++ b/js/controllers/sidebar.js @@ -80,11 +80,9 @@ angular.module('copayApp.controllers').controller('SidebarController', function( if (err) { notification.warning('Could not open wallet'); } else { - iden.closeWallet($rootScope.wallet.id, function() { - $scope.loading = false; - $rootScope.wallet = w; - controllerUtils.bindWallet(w, $scope); - }); + $scope.loading = false; + $rootScope.wallet = w; + controllerUtils.bindWallet(w, $scope); } }); }; diff --git a/js/models/Async.js b/js/models/Async.js index ee1e897ed..6f916e242 100644 --- a/js/models/Async.js +++ b/js/models/Async.js @@ -264,7 +264,7 @@ Network.prototype._setupConnectionHandlers = function(opts, cb) { self.socket.on('connect', function() { var pubkey = self.getKey().public.toString('hex'); - log.debug('Async subcribing to:pubkey:',pubkey); + log.debug('Async subscribing to:pubkey:',pubkey); self.socket.emit('subscribe', pubkey); @@ -325,7 +325,10 @@ Network.prototype.start = function(opts, openCallback) { preconditions.checkState(this.connectedPeers && this.connectedPeers.length === 0); - if (this.started) return openCallback(); + if (this.started) { + log.debug('Async: Networing already started for this wallet.') + return openCallback(); + } this.privkey = opts.privkey; this.setCopayerId(opts.copayerId); @@ -336,8 +339,7 @@ Network.prototype.start = function(opts, openCallback) { }; Network.prototype.createSocket = function() { - - console.log('Async: Connecting to socket:', this.url, this.socketOptions); //TODO + log.debug('Async: Connecting to socket:', this.url, this.socketOptions); return io.connect(this.url, this.socketOptions); }; diff --git a/js/models/Identity.js b/js/models/Identity.js index d64fab6f3..b68124184 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -127,7 +127,8 @@ Identity.create = function(email, password, opts, cb) { cb(null, iden); // default wallet - var wopts = _.extend(opts.walletDefaults, { + var dflt = _.clone(opts.walletDefaults); + var wopts = _.extend(dflt, { nickname: email, networkName: opts.networkName, requiredCopayers: 1, @@ -447,7 +448,6 @@ Identity.prototype._checkVersion = function(inVersion) { * @return */ Identity.prototype.openWallet = function(walletId, password, cb) { - console.log('[Identity.js.434:openWallet:]', walletId); //TODO preconditions.checkArgument(cb); var self = this; diff --git a/js/models/Insight.js b/js/models/Insight.js index 4c1a4be52..018c47b53 100644 --- a/js/models/Insight.js +++ b/js/models/Insight.js @@ -124,6 +124,7 @@ Insight.prototype.subscribeToBlocks = function() { /** @private */ Insight.prototype._getSocketIO = function(url, opts) { + log.debug('Insight: Connecting to socket:', this.url, this.opts); return io(this.url, this.opts); }; diff --git a/js/models/Wallet.js b/js/models/Wallet.js index 18869f218..149894cd4 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -869,6 +869,12 @@ Wallet.prototype.netStart = function() { var self = this; var net = this.network; + if (net.started) { + log.debug('Wallet networking was ready') + self.emit('ready', net.getPeer()); + return; + } + net.removeAllListeners(); net.on('connect', self._onConnect.bind(self)); net.on('data', self._onData.bind(self)); @@ -2539,6 +2545,7 @@ Wallet.prototype.indexDiscovery = function(start, change, copayerIndex, gap, cb) */ Wallet.prototype.close = function(cb) { var self = this; + this.network.cleanUp(); log.debug('## CLOSING Wallet: ' + this.id); this.lock.release(function() { if (cb) return cb(); diff --git a/js/services/controllerUtils.js b/js/services/controllerUtils.js index d2bd34750..4202acb02 100644 --- a/js/services/controllerUtils.js +++ b/js/services/controllerUtils.js @@ -40,20 +40,29 @@ angular.module('copayApp.services') } }; + + root.isFocusedWallet = function(wid) { + return wid === $rootScope.wallet.getId(); + }; + root.installWalletHandlers = function(w, $scope) { + + var wid = w.getId(); w.on('connectionError', function() { - var message = "Could not connect to the Insight server. Check your settings and network configuration"; - notification.error('Networking Error', message); - root.onErrorDigest($scope); - }); - w.on('ready', function() { - $scope.loading = false; + if (root.isFocusedWallet(wid)) { + var message = "Could not connect to the Insight server. Check your settings and network configuration"; + notification.error('Networking Error', message); + root.onErrorDigest($scope); + } }); w.on('corrupt', function(peerId) { - notification.error('Error', $filter('translate')('Received corrupt message from ') + peerId); + if (root.isFocusedWallet(wid)) { + notification.error('Error', $filter('translate')('Received corrupt message from ') + peerId); + } }); w.on('ready', function(myPeerID) { + $scope.loading = false; $rootScope.wallet = w; if ($rootScope.initialConnection) { $rootScope.initialConnection = false; @@ -66,38 +75,48 @@ angular.module('copayApp.services') }); w.on('publicKeyRingUpdated', function(dontDigest) { - root.updateAddressList(); - if (!dontDigest) { - $rootScope.$digest(); + if (root.isFocusedWallet(wid)) { + root.updateAddressList(); + if (!dontDigest) { + $rootScope.$digest(); + } } }); w.on('tx', function(address, isChange) { - if (!isChange) { - notification.funds('Funds received!', address); + if (root.isFocusedWallet(wid)) { + if (!isChange) { + notification.funds('Funds received!', address); + } + root.updateBalance(function() { + $rootScope.$digest(); + }); } - root.updateBalance(function() { - $rootScope.$digest(); - }); }); w.on('balanceUpdated', function() { - root.updateBalance(function() { - $rootScope.$digest(); - }); + if (root.isFocusedWallet(wid)) { + root.updateBalance(function() { + $rootScope.$digest(); + }); + } }); w.on('insightReconnected', function() { - $rootScope.reconnecting = false; - root.updateAddressList(); - root.updateBalance(function() { - $rootScope.$digest(); - }); + if (root.isFocusedWallet(wid)) { + $rootScope.reconnecting = false; + root.updateAddressList(); + root.updateBalance(function() { + $rootScope.$digest(); + }); + } }); w.on('insightError', function() { - $rootScope.reconnecting = true; - $rootScope.$digest(); + if (root.isFocusedWallet(wid)) { + $rootScope.reconnecting = true; + $rootScope.$digest(); + } }); var updateTxsAndBalance = _.debounce(function() { @@ -108,11 +127,14 @@ angular.module('copayApp.services') }, 3000); w.on('txProposalsUpdated', function(dontDigest) { - updateTxsAndBalance(); + if (root.isFocusedWallet(wid)) { + updateTxsAndBalance(); + } }); w.on('txProposalEvent', function(e) { + // TODO: add wallet name notification var user = w.publicKeyRing.nicknameForCopayer(e.cId); switch (e.type) { case 'signed': @@ -127,8 +149,10 @@ angular.module('copayApp.services') } }); w.on('addressBookUpdated', function(dontDigest) { - if (!dontDigest) { - $rootScope.$digest(); + if (root.isFocusedWallet(wid)) { + if (!dontDigest) { + $rootScope.$digest(); + } } }); w.on('connect', function(peerID) { @@ -156,7 +180,7 @@ angular.module('copayApp.services') root.unbindWallet = function($scope) { - var w =$rootScope.wallet; + var w = $rootScope.wallet; w.removeAllListeners(); }; diff --git a/test/models/Identity.js b/test/models/Identity.js index bf6bb3167..8f1c9fb59 100644 --- a/test/models/Identity.js +++ b/test/models/Identity.js @@ -72,7 +72,7 @@ describe('Identity model', function() { var password = 'password'; var config = { - wallet: { + walletDefaults: { requiredCopayers: 3, totalCopayers: 5, spendUnconfirmed: 1, @@ -107,7 +107,7 @@ describe('Identity model', function() { it('should create an identity', function() { var iden = new Identity(email, password, config); should.exist(iden); - iden.walletDefaults.should.deep.equal(config.wallet); + iden.walletDefaults.should.deep.equal(config.walletDefaults); }); }); @@ -183,7 +183,7 @@ describe('Identity model', function() { it('should add wallet to profile', function(done) { iden.createWallet(null, function(err, w) { - profile.addWallet.getCall(0).args[0].should.contain('spy#'); + profile.addWallet.getCall(0).args[0].should.contain('wid:123'); done(); }); });