refactor address management
This commit is contained in:
parent
38b8240341
commit
1323ad48db
5 changed files with 110 additions and 61 deletions
|
|
@ -258,7 +258,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="row m20t">
|
<div class="row m20t">
|
||||||
<div class="large-12 columns">
|
<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>
|
ng-style="{'background-color':index.backgroundColor}" ng-disabled="home.blockUx || index.isOffline ||home.generatingAddress" translate>
|
||||||
Generate new address
|
Generate new address
|
||||||
</button>
|
</button>
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
'use strict';
|
'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;
|
var self = this;
|
||||||
self.isCordova = isCordova;
|
self.isCordova = isCordova;
|
||||||
|
|
@ -464,7 +464,12 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
||||||
self.alternativeIsoCode = config.alternativeIsoCode;
|
self.alternativeIsoCode = config.alternativeIsoCode;
|
||||||
|
|
||||||
// Check address
|
// 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() {
|
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) {
|
self.clientError = function(err) {
|
||||||
if (isCordova) {
|
if (isCordova) {
|
||||||
|
|
@ -729,7 +720,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
||||||
$rootScope.$on('Local/WalletImported', function(event, walletId) {
|
$rootScope.$on('Local/WalletImported', function(event, walletId) {
|
||||||
self.needsBackup = false;
|
self.needsBackup = false;
|
||||||
storageService.setBackupFlag(walletId, function() {
|
storageService.setBackupFlag(walletId, function() {
|
||||||
storageService.clearLastAddress(walletId, function(err) {
|
addressService.expireAddress(walletId, function(err) {
|
||||||
self.startScan(walletId);
|
self.startScan(walletId);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -792,7 +783,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
||||||
if (val) {
|
if (val) {
|
||||||
$log.debug('Clear last address cache and Scan');
|
$log.debug('Clear last address cache and Scan');
|
||||||
lodash.each(lodash.keys(profileService.walletClients), function(walletId) {
|
lodash.each(lodash.keys(profileService.walletClients), function(walletId) {
|
||||||
storageService.clearLastAddress(walletId, function(err) {
|
addressService.expireAddress(walletId, function(err) {
|
||||||
self.startScan(walletId);
|
self.startScan(walletId);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
'use strict';
|
'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;
|
var self = this;
|
||||||
$rootScope.hideMenuBar = false;
|
$rootScope.hideMenuBar = false;
|
||||||
|
|
@ -38,7 +38,7 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
|
||||||
});
|
});
|
||||||
|
|
||||||
var disableAddrListener = $rootScope.$on('Local/NeedNewAddress', function() {
|
var disableAddrListener = $rootScope.$on('Local/NeedNewAddress', function() {
|
||||||
self.setNewAddress();
|
self.setAddress(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
var disableFocusListener = $rootScope.$on('Local/NewFocusedWallet', function() {
|
var disableFocusListener = $rootScope.$on('Local/NewFocusedWallet', function() {
|
||||||
|
|
@ -90,6 +90,8 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
|
||||||
|
|
||||||
|
|
||||||
var parseError = function(err) {
|
var parseError = function(err) {
|
||||||
|
if (!err) return;
|
||||||
|
|
||||||
if (err.message) {
|
if (err.message) {
|
||||||
// TODO : this is not used anymore?
|
// TODO : this is not used anymore?
|
||||||
if (err.message.indexOf('CORS') >= 0) {
|
if (err.message.indexOf('CORS') >= 0) {
|
||||||
|
|
@ -361,56 +363,31 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Receive
|
this.setAddress = function(forceNew) {
|
||||||
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() {
|
|
||||||
self.addrError = null;
|
self.addrError = null;
|
||||||
var fc = profileService.focusedClient;
|
var fc = profileService.focusedClient;
|
||||||
if (!fc)
|
if (!fc)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (self.addr[fc.credentials.walletId]) {
|
// Address already set?
|
||||||
|
if (!forceNew && self.addr[fc.credentials.walletId]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.generatingAddress = true;
|
||||||
$timeout(function() {
|
$timeout(function() {
|
||||||
storageService.getLastAddress(fc.credentials.walletId, function(err, addr) {
|
addressService.getAddress(fc.credentials.walletId, forceNew, function(err,addr){
|
||||||
if (addr) {
|
self.generatingAddress = false;
|
||||||
self.addr[fc.credentials.walletId] = addr;
|
|
||||||
$scope.$digest();
|
if (err) {
|
||||||
} else {
|
parseError(err);
|
||||||
self.setNewAddress();
|
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.bindTouchDown();
|
||||||
this.setAddress();
|
this.setAddress();
|
||||||
|
|
|
||||||
75
src/js/services/addressService.js
Normal file
75
src/js/services/addressService.js
Normal 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;
|
||||||
|
});
|
||||||
|
|
@ -233,6 +233,11 @@ angular.module('copayApp.services')
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
root.getClient = function(walletId) {
|
||||||
|
return root.walletClients[walletId];
|
||||||
|
};
|
||||||
|
|
||||||
root.deleteWalletFC = function(opts, cb) {
|
root.deleteWalletFC = function(opts, cb) {
|
||||||
var fc = root.focusedClient;
|
var fc = root.focusedClient;
|
||||||
$log.debug('Deleting Wallet:', fc.credentials.walletName);
|
$log.debug('Deleting Wallet:', fc.credentials.walletName);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue