refactor address management
This commit is contained in:
parent
38b8240341
commit
1323ad48db
5 changed files with 110 additions and 61 deletions
|
|
@ -1,6 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, lodash, go, profileService, configService, isCordova, rateService, storageService, gettextCatalog, gettext, amMoment) {
|
||||
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettextCatalog, gettext, amMoment) {
|
||||
|
||||
var self = this;
|
||||
self.isCordova = isCordova;
|
||||
|
|
@ -464,7 +464,12 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
self.alternativeIsoCode = config.alternativeIsoCode;
|
||||
|
||||
// Check address
|
||||
self.checkLastAddress(balance.byAddress);
|
||||
addressService.isUsed(self.walletId, balance.byAddress, function(err, used){
|
||||
if (used) {
|
||||
$log.debug('Address used. Creating new');
|
||||
$rootScope.$emit('Local/NeedNewAddress');
|
||||
}
|
||||
});
|
||||
|
||||
rateService.whenAvailable(function() {
|
||||
|
||||
|
|
@ -488,20 +493,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
}
|
||||
};
|
||||
|
||||
self.checkLastAddress = function(byAddress, cb) {
|
||||
storageService.getLastAddress(self.walletId, function(err, addr) {
|
||||
var used = lodash.find(byAddress, {
|
||||
address: addr
|
||||
});
|
||||
if (used) {
|
||||
$log.debug('Address ' + addr + ' was used. Cleaning Cache.')
|
||||
storageService.clearLastAddress(self.walletId, function(err) {
|
||||
$rootScope.$emit('Local/NeedNewAddress', err);
|
||||
if (cb) return cb();
|
||||
});
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
self.clientError = function(err) {
|
||||
if (isCordova) {
|
||||
|
|
@ -729,7 +720,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
$rootScope.$on('Local/WalletImported', function(event, walletId) {
|
||||
self.needsBackup = false;
|
||||
storageService.setBackupFlag(walletId, function() {
|
||||
storageService.clearLastAddress(walletId, function(err) {
|
||||
addressService.expireAddress(walletId, function(err) {
|
||||
self.startScan(walletId);
|
||||
});
|
||||
});
|
||||
|
|
@ -792,7 +783,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
if (val) {
|
||||
$log.debug('Clear last address cache and Scan');
|
||||
lodash.each(lodash.keys(profileService.walletClients), function(walletId) {
|
||||
storageService.clearLastAddress(walletId, function(err) {
|
||||
addressService.expireAddress(walletId, function(err) {
|
||||
self.startScan(walletId);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('walletHomeController', function($scope, $rootScope, $timeout, $filter, $modal, $log, notification, txStatus, isCordova, profileService, lodash, configService, rateService, storageService, bitcore, isChromeApp, gettext, gettextCatalog, nodeWebkit) {
|
||||
angular.module('copayApp.controllers').controller('walletHomeController', function($scope, $rootScope, $timeout, $filter, $modal, $log, notification, txStatus, isCordova, profileService, lodash, configService, rateService, storageService, bitcore, isChromeApp, gettext, gettextCatalog, nodeWebkit, addressService) {
|
||||
|
||||
var self = this;
|
||||
$rootScope.hideMenuBar = false;
|
||||
|
|
@ -38,7 +38,7 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
|
|||
});
|
||||
|
||||
var disableAddrListener = $rootScope.$on('Local/NeedNewAddress', function() {
|
||||
self.setNewAddress();
|
||||
self.setAddress(true);
|
||||
});
|
||||
|
||||
var disableFocusListener = $rootScope.$on('Local/NewFocusedWallet', function() {
|
||||
|
|
@ -90,6 +90,8 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
|
|||
|
||||
|
||||
var parseError = function(err) {
|
||||
if (!err) return;
|
||||
|
||||
if (err.message) {
|
||||
// TODO : this is not used anymore?
|
||||
if (err.message.indexOf('CORS') >= 0) {
|
||||
|
|
@ -361,56 +363,31 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
|
|||
|
||||
};
|
||||
|
||||
// Receive
|
||||
this.setNewAddress = function() {
|
||||
var fc = profileService.focusedClient;
|
||||
self.generatingAddress = true;
|
||||
self.addrError = null;
|
||||
fc.createAddress(function(err, addr) {
|
||||
self.generatingAddress = false;
|
||||
if (err) {
|
||||
if (err.error && err.error.match(/locked/gi)) {
|
||||
$log.debug(err.error);
|
||||
$timeout(function() {
|
||||
self.setNewAddress();
|
||||
}, 5000);
|
||||
} else {
|
||||
$log.debug('Creating address ERROR:', err);
|
||||
parseError(err);
|
||||
self.addrError = err.message || gettext('Could not create address. Check you connection and try again');
|
||||
$scope.$digest();
|
||||
}
|
||||
return;
|
||||
}
|
||||
self.addrError = null;
|
||||
self.addr[fc.credentials.walletId] = addr.address;
|
||||
storageService.storeLastAddress(fc.credentials.walletId, addr.address, function() {
|
||||
|
||||
self.generatingAddress = false;
|
||||
$scope.$digest();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
this.setAddress = function() {
|
||||
this.setAddress = function(forceNew) {
|
||||
self.addrError = null;
|
||||
var fc = profileService.focusedClient;
|
||||
if (!fc)
|
||||
return;
|
||||
|
||||
if (self.addr[fc.credentials.walletId]) {
|
||||
// Address already set?
|
||||
if (!forceNew && self.addr[fc.credentials.walletId]) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
self.generatingAddress = true;
|
||||
$timeout(function() {
|
||||
storageService.getLastAddress(fc.credentials.walletId, function(err, addr) {
|
||||
if (addr) {
|
||||
self.addr[fc.credentials.walletId] = addr;
|
||||
$scope.$digest();
|
||||
} else {
|
||||
self.setNewAddress();
|
||||
addressService.getAddress(fc.credentials.walletId, forceNew, function(err,addr){
|
||||
self.generatingAddress = false;
|
||||
|
||||
if (err) {
|
||||
parseError(err);
|
||||
self.addrError = err.message || gettext('Could not create address. Check you connection and try again');
|
||||
}
|
||||
|
||||
if (addr)
|
||||
self.addr[fc.credentials.walletId] = addr;
|
||||
|
||||
$scope.$digest();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
@ -1050,6 +1027,7 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
|
|||
}
|
||||
}
|
||||
|
||||
/* Start setup */
|
||||
|
||||
this.bindTouchDown();
|
||||
this.setAddress();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue