|
|
|
@ -45,19 +45,34 @@ function scanPrivateKeyQrCode(callback) {
|
|
|
|
|
* @param {type} outputSatoshis Amount to send to recipient's wallet
|
|
|
|
|
* @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, feePerByte) {
|
|
|
|
|
if (typeof feePerByte == "undefined") {
|
|
|
|
|
feePerByte = -1;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
var privateKey = new bitcoreLib.PrivateKey(privateKeyString);
|
|
|
|
|
|
|
|
|
|
var transaction = new bitcoreLib.Transaction()
|
|
|
|
|
.from(utxos)
|
|
|
|
|
.to(destinationAddress, outputSatoshis)
|
|
|
|
|
.change(sourceAddress)
|
|
|
|
|
.sign(privateKey);
|
|
|
|
|
.change(sourceAddress);
|
|
|
|
|
|
|
|
|
|
var size = transaction._estimateSize();
|
|
|
|
|
var fee = size * feePerByte;
|
|
|
|
|
|
|
|
|
|
if (feePerByte > -1) {
|
|
|
|
|
// use our fee
|
|
|
|
|
transaction = transaction.fee(fee);
|
|
|
|
|
} else {
|
|
|
|
|
// use lib's fee
|
|
|
|
|
fee = transaction.getFee();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
transaction = transaction.sign(privateKey);
|
|
|
|
|
|
|
|
|
|
var inputTotal = transaction._getInputAmount();
|
|
|
|
|
|
|
|
|
|
var outputTotal = transaction.getFee() + outputSatoshis;
|
|
|
|
|
var outputTotal = fee + outputSatoshis;
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
throw new Error("There was an internal error while creating the transaction. Details: " + ex.message);
|
|
|
|
|
}
|
|
|
|
@ -132,9 +147,9 @@ function getUTXOData(walletaddress, successCallback, errorCallback) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sendCoins(privatekey, fromaddress, toaddress, amount) {
|
|
|
|
|
var progressdialog = app.dialog.progress("Querying blockchain...", 25);
|
|
|
|
|
var progressdialog = app.dialog.progress("Querying blockchain...", 20);
|
|
|
|
|
getUTXOData(fromaddress, function (success) {
|
|
|
|
|
progressdialog.setProgress(50);
|
|
|
|
|
progressdialog.setProgress(40);
|
|
|
|
|
progressdialog.setText("Creating transaction...");
|
|
|
|
|
if (success.utxos.length == 0) {
|
|
|
|
|
app.dialog.close();
|
|
|
|
@ -160,8 +175,14 @@ function sendCoins(privatekey, fromaddress, toaddress, amount) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
progressdialog.setProgress(60);
|
|
|
|
|
progressdialog.setText("Calculating fees...");
|
|
|
|
|
apirequest(SETTINGS.apis.cryptofees, {
|
|
|
|
|
currency: success.currency
|
|
|
|
|
}, function (resp) {
|
|
|
|
|
if (resp.status == "OK") {
|
|
|
|
|
try {
|
|
|
|
|
var txdata = createSignedTransaction(bitcore, privatekey, fromaddress, toaddress, utxos, satoshis);
|
|
|
|
|
var txdata = createSignedTransaction(bitcore, privatekey, fromaddress, toaddress, utxos, satoshis, resp.feePerByte);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
console.error(ex);
|
|
|
|
|
app.dialog.close();
|
|
|
|
@ -169,7 +190,7 @@ function sendCoins(privatekey, fromaddress, toaddress, amount) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
progressdialog.setProgress(75);
|
|
|
|
|
progressdialog.setProgress(80);
|
|
|
|
|
progressdialog.setText("Sending payment...");
|
|
|
|
|
|
|
|
|
|
apirequest(SETTINGS.apis.broadcasttransaction, {
|
|
|
|
@ -183,6 +204,7 @@ function sendCoins(privatekey, fromaddress, toaddress, amount) {
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
app.dialog.close();
|
|
|
|
|
app.dialog.alert(resp.msg, "Error");
|
|
|
|
|
}
|
|
|
|
|
}, function (errorData) {
|
|
|
|
|
app.dialog.close();
|
|
|
|
@ -200,6 +222,26 @@ function sendCoins(privatekey, fromaddress, toaddress, amount) {
|
|
|
|
|
sendErrorReport("Crypto", "Couldn't broadcast transaction", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
app.dialog.close();
|
|
|
|
|
app.dialog.alert(resp.msg, "Error");
|
|
|
|
|
}
|
|
|
|
|
}, function (errorData) {
|
|
|
|
|
app.dialog.close();
|
|
|
|
|
try {
|
|
|
|
|
var error = $.parseJSON(errorData.responseText);
|
|
|
|
|
if (error && typeof error.msg != 'undefined') {
|
|
|
|
|
app.dialog.alert(error.msg, "Error");
|
|
|
|
|
sendErrorReport("Crypto", "Couldn't get transaction fees", error.msg);
|
|
|
|
|
} else {
|
|
|
|
|
app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later. Your funds are safe.", "Error");
|
|
|
|
|
sendErrorReport("Crypto", "Couldn't get transaction fees", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
|
|
|
|
|
}
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later. Your funds are safe.", "Error");
|
|
|
|
|
sendErrorReport("Crypto", "Couldn't get transaction fees", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, function (error) {
|
|
|
|
|
app.dialog.close();
|
|
|
|
|
app.dialog.alert(error, "Error");
|
|
|
|
|