boradcast transaction working with balance

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

View file

@ -43,7 +43,7 @@
ng-disabled="paperWallet.scanning || !paperWallet.scannedKey" ng-disabled="paperWallet.scanning || !paperWallet.scannedKey"
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.createTx(inputData, passphrase)" ng-click="paperWallet.scanFunds()"
translate>Scan Wallet Funds translate>Scan Wallet Funds
</button> </button>
</div> </div>
@ -62,7 +62,7 @@
ng-disabled="paperWallet.sending" ng-disabled="paperWallet.sending"
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.transaction()" ng-click="paperWallet.sweepWallet()"
translate>Sweep Wallet translate>Sweep Wallet
</button> </button>
<div class="text-center"> <div class="text-center">

View file

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