refactor address management

This commit is contained in:
Matias Alejo Garcia 2015-06-27 13:22:56 -03:00
commit 1323ad48db
5 changed files with 110 additions and 61 deletions

View file

@ -258,7 +258,7 @@
</div>
<div class="row m20t">
<div class="large-12 columns">
<button class="button black expand round" ng-click="home.setNewAddress()"
<button class="button black expand round" ng-click="home.setAddress(true)"
ng-style="{'background-color':index.backgroundColor}" ng-disabled="home.blockUx || index.isOffline ||home.generatingAddress" translate>
Generate new address
</button>

View file

@ -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);
});
});

View file

@ -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();

View file

@ -0,0 +1,75 @@
'use strict';
'use strict';
angular.module('copayApp.services')
.factory('addressService', function(storageService, profileService, $log, $timeout, lodash) {
var root = {};
root.expireAddress = function(walletId,cb) {
$log.debug('Cleaning Address ' + addr );
storageService.clearLastAddress(walletId, function(err) {
return cb(err);
});
};
root.isUsed = function(walletId, byAddress, cb) {
storageService.getLastAddress(walletId, function(err, addr) {
var used = lodash.find(byAddress, {
address: addr
});
return cb(null, used);
});
};
root._createAddress = function(walletId, cb) {
var client = profileService.getClient(walletId);
$log.debug('Creating address for wallet:', walletId);
client.createAddress(function(err, addr) {
if (err) {
if (err.error && err.error.match(/locked/gi)) {
$log.debug(err.error);
return $timeout(function() {
root._createAddress(walletId, cb);
}, 5000);
}
$log.debug('Creating address ERROR:', err);
return cb(err);
}
return cb(null, addr.address);
});
};
root.getAddress = function(walletId, forceNew, cb) {
var firstStep;
if (forceNew) {
firstStep = storageService.clearLastAddress;
} else {
firstStep = function(walletId, cb) {
return cb();
};
}
firstStep(walletId, function(err) {
if (err) return cb(err);
storageService.getLastAddress(walletId, function(err, addr) {
if (err) return cb(err);
if (addr) return cb(null, addr);
root._createAddress(walletId, function(err, addr) {
if (err) return cb(err);
storageService.storeLastAddress(walletId, addr, function() {
if (err) return cb(err);
return cb(null, addr);
});
});
});
});
};
return root;
});

View file

@ -233,6 +233,11 @@ angular.module('copayApp.services')
})
};
root.getClient = function(walletId) {
return root.walletClients[walletId];
};
root.deleteWalletFC = function(opts, cb) {
var fc = root.focusedClient;
$log.debug('Deleting Wallet:', fc.credentials.walletName);