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

View file

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