diff --git a/js/controllers/more.js b/js/controllers/more.js index b867b2aae..b25e33288 100644 --- a/js/controllers/more.js +++ b/js/controllers/more.js @@ -84,7 +84,8 @@ angular.module('copayApp.controllers').controller('MoreController', $scope.deleteWallet = function() { $rootScope.iden.deleteWallet(w.id, function() { - controllerUtils.logout(); + notification.info('Wallet deleted', $filter('translate')('wallet deleted')); + $location.path('/manage'); }); }; diff --git a/js/controllers/sidebar.js b/js/controllers/sidebar.js index 971de1820..853353905 100644 --- a/js/controllers/sidebar.js +++ b/js/controllers/sidebar.js @@ -39,7 +39,7 @@ angular.module('copayApp.controllers').controller('SidebarController', function( if (w.isReady()) { w.sendWalletReady(); if ($rootScope.addrInfos.length > 0) { - controllerUtils.updateBalance(function() { + controllerUtils.updateBalance(w, function() { $rootScope.$digest(); }); } diff --git a/js/models/Async.js b/js/models/Async.js index 5d5b5484d..60f089030 100644 --- a/js/models/Async.js +++ b/js/models/Async.js @@ -294,13 +294,11 @@ Network.prototype.greet = function(copayerId, secretNumber) { }; Network.prototype._addCopayerMap = function(peerId, copayerId) { -console.log('[Async.js.296:_addCopayerMap:]',peerId, copayerId); //TODO if (!this.copayerForPeer[peerId]) { if (Object.keys(this.copayerForPeer).length < this.maxPeers) { this.copayerForPeer[peerId] = copayerId; } } -console.log('[Async.js.296:_addCopayerMap:]',this.copayerForPeer); //TODO }; Network.prototype._setInboundPeerAuth = function(peerId) { @@ -376,7 +374,6 @@ Network.prototype.send = function(dest, payload, cb) { dest = this.getCopayerIds(); payload.isBroadcast = 1; } -console.log('[Async.js.374:dest:]',dest); //TODO if (typeof dest === 'string') dest = [dest]; diff --git a/js/models/Identity.js b/js/models/Identity.js index d2dcdd335..2fb9d102a 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -200,9 +200,10 @@ Identity.open = function(email, password, opts, cb) { var firstWallet; _.each(wids, function(wid) { iden.openWallet(wid, function(err, w) { - if (err) + if (err) { log.error('Cound not open wallet id:' + wid + '. Skipping') - else { + iden.profile.deleteWallet(wid, function() {}); + } else { log.info('Open wallet id:' + wid + ' opened'); if (!firstWallet) firstWallet = w; diff --git a/js/models/Insight.js b/js/models/Insight.js index 8847c7f85..50a22dfc0 100644 --- a/js/models/Insight.js +++ b/js/models/Insight.js @@ -223,6 +223,8 @@ Insight.prototype.subscribe = function(addresses) { s.emit('subscribe', address); s.on(address, handler); + } else { + log.debug('Already subcribed to: ', address); } }); }; diff --git a/js/models/Wallet.js b/js/models/Wallet.js index 0b0243ccf..8d9facd46 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -315,6 +315,7 @@ Wallet.prototype.seedCopayer = function(pubKey) { Wallet.prototype._newAddresses = function(dontUpdateUx) { if (this.publicKeyRing.isComplete()) { this.subscribeToAddresses(); + }; this.emit('newAddresses', dontUpdateUx); }; @@ -380,7 +381,6 @@ Wallet.prototype._onPublicKeyRing = function(senderId, data) { var inPKR = PublicKeyRing.fromObj(data.publicKeyRing); var wasIncomplete = !this.publicKeyRing.isComplete(); -console.log('[Wallet.js.382:wasIncomplete:]',wasIncomplete); //TODO var hasChanged; try { @@ -390,7 +390,6 @@ console.log('[Wallet.js.382:wasIncomplete:]',wasIncomplete); //TODO this.emit('connectionError', e.message); return; } -console.log('[Wallet.js.393:hasChanged:]',hasChanged); //TODO if (hasChanged) { if (wasIncomplete) { this.sendPublicKeyRing(); @@ -881,12 +880,11 @@ Wallet.prototype._setBlockchainListeners = function() { var self = this; self.blockchain.removeAllListeners(); + + log.debug('Setting Blockchain listeners for', this.getId()); self.blockchain.on('reconnect', function(attempts) { log.debug('Wallet:' + self.id + 'blockchain reconnect event'); self.emit('insightReconnected'); - - // Subscription should persist? TODO - //self.subscribeToAddresses(); }); self.blockchain.on('disconnect', function() { @@ -894,7 +892,7 @@ Wallet.prototype._setBlockchainListeners = function() { self.emit('insightError'); }); self.blockchain.on('tx', function(tx) { - log.debug('Wallet:' + self.id + 'blockchain tx event'); + log.debug('Wallet:' + self.id + ' blockchain tx event'); var addresses = self.getAddressesInfo(); var addr = _.findWhere(addresses, { addressStr: tx.address @@ -1305,7 +1303,7 @@ Wallet.prototype.sendIndexes = function(recipients) { * @param {string[]} recipients - the pubkeys of the recipients */ Wallet.prototype.sendAddressBook = function(recipients) { - if ( !Object.keys(this.addressBook).length ) return; + if (!Object.keys(this.addressBook).length) return; log.debug('Wallet:' + this.id + ' ### SENDING addressBook TO:', recipients || 'All', this.addressBook); this.send(recipients, { type: 'addressbook', @@ -2221,6 +2219,7 @@ Wallet.prototype.getAddressesStr = function(opts) { Wallet.prototype.subscribeToAddresses = function() { var addrInfo = this.publicKeyRing.getAddressesInfo(); this.blockchain.subscribe(_.pluck(addrInfo, 'addressStr')); + log.debug('Subscribed to ' + addrInfo.length + ' addresses'); //TODO }; /** diff --git a/js/services/controllerUtils.js b/js/services/controllerUtils.js index eee1e9efa..8e44406b4 100644 --- a/js/services/controllerUtils.js +++ b/js/services/controllerUtils.js @@ -11,7 +11,7 @@ angular.module('copayApp.services') if (w) { if (!w.isReady()) { $location.path('/copayers'); - } + } } else { $location.path('/'); } @@ -63,6 +63,16 @@ angular.module('copayApp.services') return wid === $rootScope.wallet.getId(); }; + + root.updateTxsAndBalance = _.debounce(function(w) { + root.updateTxs({ + wallet: w + }); + root.updateBalance(w, function() { + $rootScope.$digest(); + }) + }, 3000); + root.installWalletHandlers = function($scope, w) { w.removeAllListeners(); @@ -94,41 +104,33 @@ angular.module('copayApp.services') }); w.on('newAddresses', function(dontDigest) { - if (root.isFocusedWallet(wid)) { - root.updateAddressList(); - if (!dontDigest) { - $rootScope.$digest(); - } + root.updateTxsAndBalance(w); + if (!dontDigest) { + $rootScope.$digest(); } }); w.on('tx', function(address, isChange) { - if (root.isFocusedWallet(wid)) { - if (!isChange) { - notification.funds('Funds received!', address); - } - root.updateBalance(function() { - $rootScope.$digest(); - }); + if (!isChange) { + notification.funds('Funds received on ' + w.getName(), address); } + root.updateBalance(w, function() { + $rootScope.$digest(); + }); }); w.on('balanceUpdated', function() { - if (root.isFocusedWallet(wid)) { - root.updateBalance(function() { - $rootScope.$digest(); - }); - } + root.updateBalance(w, function() { + $rootScope.$digest(); + }); }); w.on('insightReconnected', function() { - if (root.isFocusedWallet(wid)) { - $rootScope.reconnecting = false; - root.updateAddressList(); - root.updateBalance(function() { - $rootScope.$digest(); - }); - } + $rootScope.reconnecting = false; + root.updateAddressList(w.getId()); + root.updateBalance(w, function() { + $rootScope.$digest(); + }); }); w.on('insightError', function() { @@ -138,17 +140,8 @@ angular.module('copayApp.services') } }); - var updateTxsAndBalance = _.debounce(function() { - root.updateTxs(); - root.updateBalance(function() { - $rootScope.$digest(); - }) - }, 3000); - w.on('txProposalsUpdated', function(dontDigest) { - if (root.isFocusedWallet(wid)) { - updateTxsAndBalance(); - } + root.updateTxsAndBalance(w); }); w.on('txProposalEvent', function(e) { @@ -212,12 +205,12 @@ angular.module('copayApp.services') }; root.setFocusedWallet = function(w) { - if (!_.isObject(w) ) + if (!_.isObject(w)) w = $rootScope.iden.getOpenWallet(w); preconditions.checkState(w && _.isObject(w)); $rootScope.wallet = w; - root.updateAddressList(); + root.updateTxsAndBalance(); root.redirIfLogged(); }; @@ -241,10 +234,11 @@ angular.module('copayApp.services') } }; - root.updateBalance = function(cb) { - var w = $rootScope.wallet; + root.updateBalance = function(w, cb) { + w = w || $rootScope.wallet; if (!w) return root.onErrorDigest(); if (!w.isReady()) return; + console.log('## Updating balance of:' + w.id) $rootScope.balanceByAddr = {}; $rootScope.updatingBalance = true; @@ -255,34 +249,39 @@ angular.module('copayApp.services') var satToUnit = 1 / w.settings.unitToSatoshi; var COIN = bitcore.util.COIN; - $rootScope.totalBalance = balanceSat * satToUnit; - $rootScope.totalBalanceBTC = (balanceSat / COIN); - $rootScope.availableBalance = safeBalanceSat * satToUnit; - $rootScope.availableBalanceBTC = (safeBalanceSat / COIN); + if (root.isFocusedWallet(w.getId())) { + $rootScope.totalBalance = balanceSat * satToUnit; + $rootScope.totalBalanceBTC = (balanceSat / COIN); + $rootScope.availableBalance = safeBalanceSat * satToUnit; + $rootScope.availableBalanceBTC = (safeBalanceSat / COIN); - $rootScope.lockedBalance = (balanceSat - safeBalanceSat) * satToUnit; - $rootScope.lockedBalanceBTC = (balanceSat - safeBalanceSat) / COIN; + $rootScope.lockedBalance = (balanceSat - safeBalanceSat) * satToUnit; + $rootScope.lockedBalanceBTC = (balanceSat - safeBalanceSat) / COIN; - var balanceByAddr = {}; - for (var ii in balanceByAddrSat) { - balanceByAddr[ii] = balanceByAddrSat[ii] * satToUnit; + var balanceByAddr = {}; + for (var ii in balanceByAddrSat) { + balanceByAddr[ii] = balanceByAddrSat[ii] * satToUnit; + } + $rootScope.balanceByAddr = balanceByAddr; + root.updateAddressList(); + $rootScope.updatingBalance = false; + + rateService.whenAvailable(function() { + $rootScope.totalBalanceAlternative = rateService.toFiat(balanceSat, w.settings.alternativeIsoCode); + $rootScope.alternativeIsoCode = w.settings.alternativeIsoCode; + $rootScope.lockedBalanceAlternative = rateService.toFiat(balanceSat - safeBalanceSat, w.settings.alternativeIsoCode); + $rootScope.alternativeConversionRate = rateService.toFiat(100000000, w.settings.alternativeIsoCode); + return cb ? cb() : null; + }); + } else { + // TODO + console.log('TODO: balance updated of a unfocused wallet'); } - $rootScope.balanceByAddr = balanceByAddr; - root.updateAddressList(); - $rootScope.updatingBalance = false; - - rateService.whenAvailable(function() { - $rootScope.totalBalanceAlternative = rateService.toFiat(balanceSat, w.settings.alternativeIsoCode); - $rootScope.alternativeIsoCode = w.settings.alternativeIsoCode; - $rootScope.lockedBalanceAlternative = rateService.toFiat(balanceSat - safeBalanceSat, w.settings.alternativeIsoCode); - $rootScope.alternativeConversionRate = rateService.toFiat(100000000, w.settings.alternativeIsoCode); - return cb ? cb() : null; - }); }); }; root.updateTxs = function(opts) { - var w = $rootScope.wallet; + var w = opts.wallet || $rootScope.wallet; if (!w) return; opts = opts || $rootScope.txsOpts || {};