Add some error handling to sending crypto
This commit is contained in:
parent
ff87f00f71
commit
334fb85c51
@ -46,15 +46,31 @@ function scanPrivateKeyQrCode(callback) {
|
|||||||
* @returns {string} Hex of serialized transaction, suitable for broadcast via Bitcoin Core or an API.
|
* @returns {string} Hex of serialized transaction, suitable for broadcast via Bitcoin Core or an API.
|
||||||
*/
|
*/
|
||||||
function createSignedTransaction(bitcoreLib, privateKeyString, sourceAddress, destinationAddress, utxos, outputSatoshis) {
|
function createSignedTransaction(bitcoreLib, privateKeyString, sourceAddress, destinationAddress, utxos, outputSatoshis) {
|
||||||
var privateKey = new bitcoreLib.PrivateKey(privateKeyString);
|
try {
|
||||||
|
var privateKey = new bitcoreLib.PrivateKey(privateKeyString);
|
||||||
|
|
||||||
var transaction = new bitcoreLib.Transaction()
|
var transaction = new bitcoreLib.Transaction()
|
||||||
.from(utxos)
|
.from(utxos)
|
||||||
.to(destinationAddress, outputSatoshis)
|
.to(destinationAddress, outputSatoshis)
|
||||||
.change(sourceAddress)
|
.change(sourceAddress)
|
||||||
.sign(privateKey);
|
.sign(privateKey);
|
||||||
|
|
||||||
return transaction.serialize();
|
var inputTotal = transaction._getInputAmount();
|
||||||
|
|
||||||
|
var outputTotal = transaction.getFee() + outputSatoshis;
|
||||||
|
} catch (ex) {
|
||||||
|
throw new Error("There was an internal error while creating the transaction. Details: " + ex.message);
|
||||||
|
}
|
||||||
|
console.log(inputTotal, outputTotal);
|
||||||
|
if (outputTotal > inputTotal) {
|
||||||
|
throw new Error("You have insufficient funds to cover the payment and transaction fees.");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return transaction.serialize();
|
||||||
|
} catch (ex) {
|
||||||
|
throw new Error("Couldn't create the transaction. It's likely you typed something wrong. Check that you have enough funds.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -130,7 +146,7 @@ function sendCoins(privatekey, fromaddress, toaddress, amount) {
|
|||||||
utxos.push(createUtxo(fromaddress, success.utxos[i].txHash, success.utxos[i].txOutputIndex, success.utxos[i].script, success.utxos[i].value));
|
utxos.push(createUtxo(fromaddress, success.utxos[i].txHash, success.utxos[i].txOutputIndex, success.utxos[i].script, success.utxos[i].value));
|
||||||
}
|
}
|
||||||
var bitcore = null;
|
var bitcore = null;
|
||||||
var satoshis = amount * 100000000;
|
var satoshis = parseInt((amount * 100000000).toFixed(0)); // Make sure it's an int and not something like 10.0000000001 or 9.532999999999
|
||||||
switch (success.currency) {
|
switch (success.currency) {
|
||||||
case "DOGE":
|
case "DOGE":
|
||||||
bitcore = require("bitcore-lib-doge");
|
bitcore = require("bitcore-lib-doge");
|
||||||
@ -143,7 +159,15 @@ function sendCoins(privatekey, fromaddress, toaddress, amount) {
|
|||||||
app.dialog.alert("This app version doesn't support " + success.currency + ".", "Error");
|
app.dialog.alert("This app version doesn't support " + success.currency + ".", "Error");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var txdata = createSignedTransaction(bitcore, privatekey, fromaddress, toaddress, utxos, satoshis);
|
|
||||||
|
try {
|
||||||
|
var txdata = createSignedTransaction(bitcore, privatekey, fromaddress, toaddress, utxos, satoshis);
|
||||||
|
} catch (ex) {
|
||||||
|
console.error(ex);
|
||||||
|
app.dialog.close();
|
||||||
|
app.dialog.alert(ex.message, "Error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
progressdialog.setProgress(75);
|
progressdialog.setProgress(75);
|
||||||
progressdialog.setText("Sending payment...");
|
progressdialog.setText("Sending payment...");
|
||||||
@ -286,7 +310,7 @@ $("#app").on("input change paste keyup", "#transactionAmountFiat", function () {
|
|||||||
var fiatamount = parseFloat($("#transactionAmountFiat").val());
|
var fiatamount = parseFloat($("#transactionAmountFiat").val());
|
||||||
var exchangerate = parseFloat($("#transactionAmountFiat").data("exchange-rate"));
|
var exchangerate = parseFloat($("#transactionAmountFiat").data("exchange-rate"));
|
||||||
$("#transactionAmount").val((fiatamount / exchangerate).toFixed(8));
|
$("#transactionAmount").val((fiatamount / exchangerate).toFixed(8));
|
||||||
$("#transactionAmountFiat").val(fiatamount.toFixed(2));
|
//$("#transactionAmountFiat").val(fiatamount.toFixed(2));
|
||||||
});
|
});
|
||||||
|
|
||||||
$("#app").on("paste blur", "#walletFromAddress", function () {
|
$("#app").on("paste blur", "#walletFromAddress", function () {
|
||||||
@ -304,5 +328,5 @@ $("#app").on("input change paste keyup", "#transactionAmount", function () {
|
|||||||
var amount = parseFloat($("#transactionAmount").val());
|
var amount = parseFloat($("#transactionAmount").val());
|
||||||
var exchangerate = parseFloat($("#transactionAmountFiat").data("exchange-rate"));
|
var exchangerate = parseFloat($("#transactionAmountFiat").data("exchange-rate"));
|
||||||
$("#transactionAmountFiat").val((amount * exchangerate).toFixed(2));
|
$("#transactionAmountFiat").val((amount * exchangerate).toFixed(2));
|
||||||
$("#transactionAmount").val(amount.toFixed(8));
|
//$("#transactionAmount").val(amount.toFixed(8));
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user