From fe8dcd2f942335a380011d70975e8f12b01c46a9 Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Mon, 14 Nov 2016 10:09:11 -0300 Subject: [PATCH 1/2] Next address after receive funds --- src/js/controllers/headController.js | 2 +- src/js/controllers/tab-home.js | 6 ----- src/js/controllers/tab-receive.js | 34 +++++++++++++++++++++++++++- src/js/services/walletService.js | 26 +++++++++++---------- 4 files changed, 48 insertions(+), 20 deletions(-) diff --git a/src/js/controllers/headController.js b/src/js/controllers/headController.js index 663126321..a33c2d8be 100644 --- a/src/js/controllers/headController.js +++ b/src/js/controllers/headController.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('copayApp.controllers').controller('headController', - function($scope, $window, $log, glideraService) { + function($scope, $window, $log) { $scope.appConfig = $window.appConfig; $log.info('Running head controller:' + $window.appConfig.nameCase) }); diff --git a/src/js/controllers/tab-home.js b/src/js/controllers/tab-home.js index bf8443081..d5f6ea1a8 100644 --- a/src/js/controllers/tab-home.js +++ b/src/js/controllers/tab-home.js @@ -102,12 +102,6 @@ angular.module('copayApp.controllers').controller('tabHomeController', }); }); - $scope.$on("$ionicView.leave", function(event, data) { - lodash.each(listeners, function(x) { - x(); - }); - }); - $scope.openExternalLink = function(url, optIn, title, message, okText, cancelText) { externalLinkService.open(url, optIn, title, message, okText, cancelText); }; diff --git a/src/js/controllers/tab-receive.js b/src/js/controllers/tab-receive.js index d98893c5b..bc2c7db45 100644 --- a/src/js/controllers/tab-receive.js +++ b/src/js/controllers/tab-receive.js @@ -1,7 +1,8 @@ 'use strict'; -angular.module('copayApp.controllers').controller('tabReceiveController', function($scope, $timeout, $log, $ionicModal, $state, $ionicHistory, storageService, platformInfo, walletService, profileService, configService, lodash, gettextCatalog, popupService) { +angular.module('copayApp.controllers').controller('tabReceiveController', function($rootScope, $scope, $timeout, $log, $ionicModal, $state, $ionicHistory, storageService, platformInfo, walletService, profileService, configService, lodash, gettextCatalog, popupService) { + var listeners = []; $scope.isCordova = platformInfo.isCordova; $scope.isNW = platformInfo.isNW; $scope.walletAddrs = {}; @@ -113,10 +114,41 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi $log.debug('Wallet changed: ' + wallet.name); }); + $scope.updateCurrentWallet = function() { + walletService.getStatus($scope.wallet, {}, function(err, status) { + if (err) { + $log.error(err); + } + $timeout(function() { + $scope.wallet = profileService.getWallet($scope.wallet.id); + $scope.wallet.status = status; + $scope.setAddress(); + $scope.$apply(); + }, 200); + }); + }; + $scope.$on("$ionicView.beforeEnter", function(event, data) { $scope.wallets = profileService.getWallets(); + lodash.each($scope.wallets, function(wallet, index) { $scope.loadAddresses(wallet); }); + + listeners = [ + $rootScope.$on('bwsEvent', function(e, walletId, type, n) { + // Update current address + if ($scope.wallet && walletId == $scope.wallet.id) $scope.updateCurrentWallet(); + }) + ]; + + // Update current wallet + if ($scope.wallet) $scope.updateCurrentWallet(); + }); + + $scope.$on("$ionicView.leave", function(event, data) { + lodash.each(listeners, function(x) { + x(); + }); }); }); diff --git a/src/js/services/walletService.js b/src/js/services/walletService.js index a2dea86dc..0e3c95ab0 100644 --- a/src/js/services/walletService.js +++ b/src/js/services/walletService.js @@ -29,16 +29,6 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim }); }; - // // RECEIVE - // // Check address - // root.isUsed(wallet.walletId, balance.byAddress, function(err, used) { - // if (used) { - // $log.debug('Address used. Creating new'); - // $rootScope.$emit('Local/AddressIsUsed'); - // } - // }); - // - var _signWithLedger = function(wallet, txp, cb) { $log.info('Requesting Ledger Chrome app to sign the transaction'); @@ -231,6 +221,17 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim cache.alternativeName = config.settings.alternativeName; cache.alternativeIsoCode = config.settings.alternativeIsoCode; + // Check address + root.isAddressUsed(wallet, balance.byAddress, function(err, used) { + if (used) { + $log.debug('Address used. Creating new'); + // Force new address + root.getAddress(wallet, true, function(err, addr) { + $log.debug('New address: ', addr); + }); + } + }); + rateService.whenAvailable(function() { var totalBalanceAlternative = rateService.toFiat(cache.totalBalanceSat, cache.alternativeIsoCode); @@ -760,12 +761,13 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim }); }; - root.isUsed = function(wallet, byAddress, cb) { + // Check address + root.isAddressUsed = function(wallet, byAddress, cb) { storageService.getLastAddress(wallet.id, function(err, addr) { var used = lodash.find(byAddress, { address: addr }); - return cb(null, used); + return cb(err, used); }); }; From 7e9d47c8e9f151fa741ed924ccd6860a2c090b0c Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Mon, 14 Nov 2016 12:31:38 -0300 Subject: [PATCH 2/2] Fix next address --- src/js/controllers/tab-receive.js | 22 ++++++++++++++-------- src/js/services/walletService.js | 15 +++++++++------ 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/js/controllers/tab-receive.js b/src/js/controllers/tab-receive.js index bc2c7db45..04fc88728 100644 --- a/src/js/controllers/tab-receive.js +++ b/src/js/controllers/tab-receive.js @@ -31,7 +31,7 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi $scope.loadAddresses = function(wallet, index) { walletService.getAddress(wallet, false, function(err, addr) { - $scope.walletAddrs[wallet.id] = addr || null; + $scope.walletAddrs[wallet.id] = addr; }); } @@ -95,13 +95,6 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi $scope.$on('Wallet/Changed', function(event, wallet) { - $scope.wallet = wallet; - $scope.walletIndex = lodash.findIndex($scope.wallets, function(wallet) { - return wallet.id == $scope.wallet.id; - }); - if (!$scope.walletAddrs[wallet.id]) $scope.setAddress(false); - else $scope.addr = $scope.walletAddrs[wallet.id]; - $scope.$apply(); if (!wallet) { $log.debug('No wallet provided'); return; @@ -112,6 +105,18 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi } $scope.wallet = wallet; $log.debug('Wallet changed: ' + wallet.name); + + $scope.walletIndex = lodash.findIndex($scope.wallets, function(wallet) { + return wallet.id == $scope.wallet.id; + }); + + if (!$scope.walletAddrs[wallet.id]) $scope.setAddress(false); + else $scope.addr = $scope.walletAddrs[wallet.id]; + + $timeout(function() { + $scope.$apply(); + }, 100); + }); $scope.updateCurrentWallet = function() { @@ -135,6 +140,7 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi $scope.loadAddresses(wallet); }); + listeners = [ $rootScope.$on('bwsEvent', function(e, walletId, type, n) { // Update current address diff --git a/src/js/services/walletService.js b/src/js/services/walletService.js index 0e3c95ab0..95309c0f7 100644 --- a/src/js/services/walletService.js +++ b/src/js/services/walletService.js @@ -800,17 +800,20 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim }; root.getAddress = function(wallet, forceNew, cb) { - storageService.getLastAddress(wallet.id, function(err, addr) { if (err) return cb(err); if (!forceNew && addr) return cb(null, addr); - createAddress(wallet, function(err, _addr) { - if (err) return cb(err, addr); - storageService.storeLastAddress(wallet.id, _addr, function() { - if (err) return cb(err); - return cb(null, _addr); + root.isReady(wallet, function(err) { + if (err) return cb(err); + + createAddress(wallet, function(err, _addr) { + if (err) return cb(err, addr); + storageService.storeLastAddress(wallet.id, _addr, function() { + if (err) return cb(err); + return cb(null, _addr); + }); }); }); });