boradcast transaction working with balance

This commit is contained in:
Javier 2015-10-07 16:48:52 -03:00
parent 19181a9462
commit b47ca42bb2
2 changed files with 86 additions and 81 deletions

View File

@ -43,7 +43,7 @@
ng-disabled="paperWallet.scanning || !paperWallet.scannedKey"
ng-style="{'background-color':index.backgroundColor}"
class="button black round expand"
ng-click="paperWallet.createTx(inputData, passphrase)"
ng-click="paperWallet.scanFunds()"
translate>Scan Wallet Funds
</button>
</div>
@ -62,7 +62,7 @@
ng-disabled="paperWallet.sending"
ng-style="{'background-color':index.backgroundColor}"
class="button black round expand"
ng-click="paperWallet.transaction()"
ng-click="paperWallet.sweepWallet()"
translate>Sweep Wallet
</button>
<div class="text-center">

View File

@ -1,5 +1,5 @@
angular.module('copayApp.controllers').controller('paperWalletController',
function($scope, $http, $timeout, configService, profileService, go, addressService, bitcore) {
function($scope, $http, $timeout, $log, configService, profileService, go, addressService, bitcore) {
self = this;
var fc = profileService.focusedClient;
var rawTx;
@ -10,99 +10,104 @@ angular.module('copayApp.controllers').controller('paperWalletController',
}
self.onData = function(data) {
self.error = '';
self.scannedKey = data;
self.isPkEncrypted = (data.charAt(0) == '6');
}
self.createTx = function(privateKey, passphrase) {
if (privateKey.charAt(0) != '6') {
var isValidKey = self.checkPrivateKey(privateKey);
self.scanFunds = function() {
function getPrivateKey(scannedKey, isPkEncrypted, passphrase, cb) {
if (!isPkEncrypted) return cb(null, scannedKey);
fc.decryptBIP38PrivateKey(scannedKey, passphrase, null, cb);
};
if (!isValidKey) return;
function getBalance(privateKey, cb) {
fc.getBalanceFromPrivateKey(privateKey, cb);
};
function checkPrivateKey(privateKey) {
try {
new bitcore.PrivateKey(privateKey, 'livenet');
} catch (err) {
return false;
}
return true;
}
var config = configService.getSync().wallet.settings;
self.error = null;
self.scanning = true;
self.privateKey = '';
self.error = '';
$timeout(function() {
self.getRawTx(privateKey, passphrase, function(err, rawtx, balance) {
self.scanning = false;
if (err)
self.error = err.toString();
else {
self.balance = profileService.formatAmount(balance) + ' ' + config.unitName;
rawTx = rawtx;
getPrivateKey(self.scannedKey, self.isPkEncrypted, $scope.passphrase, function(err, privateKey) {
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;
$timeout(function() {
$scope.$apply();
}, 1);
});
}, 100);
};
self.checkPrivateKey = function(privateKey) {
try {
new bitcore.PrivateKey(privateKey, 'livenet');
} catch (err) {
self.error = err.toString();
return false;
}
return true;
}
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) {
if (err) return cb(err);
fc.buildTxFromPrivateKey(privateKey, destinationAddress, null, function(err, tx) {
if (err) return cb(err);
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() {
self.error = null;
self.sending = true;
$timeout(function() {
self.doTransaction(rawTx).then(function(err, response) {
self.sending = false;
self.goHome();
},
function(err) {
self.sending = false;
self.error = err.toString();
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;
self.balance = profileService.formatAmount(balance) + ' ' + config.unitName;
$timeout(function() {
$scope.$apply();
}, 1);
});
});
}, 100);
}
self.sweepWallet = function() {
self.sending = true;
self.error = '';
$timeout(function() {
addressService.getAddress(fc.credentials.walletId, true, function(err, destinationAddress) {
if (err) {
$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();
});
});
});
}, 100);
};
self.goHome = function() {
go.walletHome();
};
self.doTransaction = function(rawTx) {
return $http.post('https://insight.bitpay.com/api/tx/send', {
rawtx: rawTx
});
};
});