refactor address management
This commit is contained in:
parent
38b8240341
commit
1323ad48db
5 changed files with 110 additions and 61 deletions
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) {
|
||||
var fc = root.focusedClient;
|
||||
$log.debug('Deleting Wallet:', fc.credentials.walletName);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue