disable sweep button without balance

This commit is contained in:
Javier 2015-10-07 17:49:34 -03:00
commit 06e12baa6e
2 changed files with 54 additions and 57 deletions

View file

@ -59,7 +59,7 @@
</div>
<button
ng-disabled="paperWallet.sending"
ng-disabled="paperWallet.sending || paperWallet.balanceSat <= 0"
ng-style="{'background-color':index.backgroundColor}"
class="button black round expand"
ng-click="paperWallet.sweepWallet()"

View file

@ -15,7 +15,7 @@ angular.module('copayApp.controllers').controller('paperWalletController',
self.isPkEncrypted = (data.charAt(0) == '6');
}
self.scanFunds = function() {
self._scanFunds = function(cb) {
function getPrivateKey(scannedKey, isPkEncrypted, passphrase, cb) {
if (!isPkEncrypted) return cb(null, scannedKey);
fc.decryptBIP38PrivateKey(scannedKey, passphrase, null, cb);
@ -34,80 +34,77 @@ angular.module('copayApp.controllers').controller('paperWalletController',
return true;
}
getPrivateKey(self.scannedKey, self.isPkEncrypted, $scope.passphrase, function(err, privateKey) {
if (err) return cb(err);
if (!checkPrivateKey(privateKey)) return cb(new Error('Invalid private key'));
getBalance(privateKey, function(err, balance) {
if (err) return cb(err);
return cb(null, privateKey, balance);
});
});
}
self.scanFunds = function() {
self.scanning = true;
self.privateKey = '';
self.balanceSat = 0;
self.error = '';
$timeout(function() {
getPrivateKey(self.scannedKey, self.isPkEncrypted, $scope.passphrase, function(err, privateKey) {
self._scanFunds(function(err, privateKey, balance) {
if (err) {
$log.error(err);
self.error = 'Could not get private key';
self.scanning = false;
return;
}
if (!checkPrivateKey(privateKey)) {
self.error = 'Invalid private key';
self.scanning = false;
return;
}
self.privateKey = privateKey;
getBalance(privateKey, function(err, balance) {
self.scanning = false;
if (err) {
$log.error(err);
self.error = 'Could not get balance';
self.scanning = false;
return;
}
self.error = err.message || err.toString();
} else {
self.privateKey = privateKey;
self.balanceSat = balance;
var config = configService.getSync().wallet.settings;
self.balance = profileService.formatAmount(balance) + ' ' + config.unitName;
$timeout(function() {
$scope.$apply();
}, 1);
});
}
self.scanning = false;
$timeout(function() {
$scope.$apply();
}, 1);
});
}, 100);
}
self._sweepWallet = function(cb) {
addressService.getAddress(fc.credentials.walletId, true, function(err, destinationAddress) {
if (err) return cb(err);
fc.buildTxFromPrivateKey(self.privateKey, destinationAddress, null, function(err, tx) {
if (err) return cb(err);
fc.broadcastRawTx({
rawTx: tx.serialize(),
network: 'livenet'
}, function(err, txid) {
if (err) return cb(err);
return cb(null, destinationAddress, txid);
});
});
});
};
self.sweepWallet = function() {
self.sending = true;
self.error = '';
$timeout(function() {
addressService.getAddress(fc.credentials.walletId, true, function(err, destinationAddress) {
self._sweepWallet(function(err, destinationAddress, txid) {
if (err) {
self.error = err.message || err.toString();
$log.error(err);
self.error = 'Could not get destination address';
self.sending = false;
return;
}
fc.buildTxFromPrivateKey(self.privateKey, destinationAddress, null, function(err, tx) {
if (err) {
$log.error(err);
self.error = 'Could not build transaction';
self.sending = false;
return;
}
fc.broadcastRawTx({
rawTx: tx.serialize(),
network: 'livenet'
}, function(err, txid) {
if (err) {
$log.error(err);
self.error = 'Could not broadcast transaction';
self.sending = false;
return;
}
$timeout(function() {
$scope.$apply();
}, 1);
go.walletHome();
});
});
self.sending = false;
$timeout(function() {
$scope.$apply();
if (!err) go.walletHome();
}, 1);
});
}, 100);
};
}
});