refactor redundant code

This commit is contained in:
Javier 2015-10-07 15:08:40 -03:00
parent 8ff87520fe
commit be30eb39c3
1 changed files with 15 additions and 23 deletions

View File

@ -19,13 +19,13 @@ angular.module('copayApp.controllers').controller('paperWalletController',
self.error = null;
self.scanning = true;
$timeout(function() {
self.getRawTx(privateKey, passphrase, function(err, rawtx, utxos) {
self.getRawTx(privateKey, passphrase, function(err, rawtx, balance) {
self.scanning = false;
if (err)
self.error = err.toString();
else {
self.balance = profileService.formatAmount(utxos) + ' ' + config.unitName;
self.balance = profileService.formatAmount(balance) + ' ' + config.unitName;
rawTx = rawtx;
}
@ -46,26 +46,9 @@ angular.module('copayApp.controllers').controller('paperWalletController',
return true;
}
self.getRawTx = function(privateKey, passphrase, cb) {
if (privateKey.charAt(0) == 6) {
fc.decryptBIP38PrivateKey(privateKey, passphrase, null, function(err, privateKey) {
if (err) return cb(err);
fc.getBalanceFromPrivateKey(privateKey, function(err, utxos) {
if (err) return cb(err);
addressService.getAddress(fc.credentials.walletId, true, function(err, destinationAddress) {
if (err) return cb(err);
fc.buildTxFromPrivateKey(privateKey, destinationAddress, null, function(err, tx) {
if (err) return cb(err);
return cb(null, tx.serialize(), utxos);
});
});
});
});
} else {
fc.getBalanceFromPrivateKey(privateKey, function(err, utxos) {
self.getRawTx = function(scannedKey, passphrase, cb) {
function buildTx(privateKey, cb) {
fc.getBalanceFromPrivateKey(privateKey, function(err, balance) {
if (err) return cb(err)
addressService.getAddress(fc.credentials.walletId, true, function(err, destinationAddress) {
@ -73,11 +56,20 @@ angular.module('copayApp.controllers').controller('paperWalletController',
fc.buildTxFromPrivateKey(privateKey, destinationAddress, null, function(err, tx) {
if (err) return cb(err);
return cb(null, tx.serialize(), utxos);
return cb(null, tx.serialize(), balance);
});
});
});
}
if (scannedKey.charAt(0) == '6') {
fc.decryptBIP38PrivateKey(scannedKey, passphrase, null, function(err, privateKey) {
if (err) return cb(err);
buildTx(privateKey, cb);
});
} else {
buildTx(scannedKey, cb);
}
};
self.transaction = function() {