diff --git a/www/assets/js/crypto.js b/www/assets/js/crypto.js index 7230f7e..fdbfa1d 100644 --- a/www/assets/js/crypto.js +++ b/www/assets/js/crypto.js @@ -41,15 +41,33 @@ function scanPrivateKeyQrCode(callback) { * @param {type} privateKeyString Private key from wallet QR code * @param {type} sourceAddress Sender's wallet address * @param {type} destinationAddress Recipient's wallet address + * @param {Array} utxos Unspent transaction inputs, as array. See createUtxo() + * @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) { + var privateKey = new bitcoreLib.PrivateKey(privateKeyString); + + var transaction = new bitcoreLib.Transaction() + .from(utxos) + .to(destinationAddress, outputSatoshis) + .change(sourceAddress) + .sign(privateKey); + + return transaction.serialize(); +} + +/** + * Create a UTXO. + * + * @param {type} sourceAddress Sender's wallet address * @param {type} txHash From UXTO (unspent output) * @param {type} txOutputIndex From UXTO (unspent output) * @param {type} script From UXTO (unspent output) * @param {type} inputSatoshis From UXTO (unspent output) - * @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. + * @returns {createUtxo.utxo} */ -function createSignedTransaction(bitcoreLib, privateKeyString, sourceAddress, destinationAddress, txHash, txOutputIndex, script, inputSatoshis, outputSatoshis) { - var privateKey = new bitcoreLib.PrivateKey(privateKeyString); +function createUtxo(sourceAddress, txHash, txOutputIndex, script, inputSatoshis) { var utxo = { "txId": txHash, "outputIndex": txOutputIndex, @@ -57,14 +75,7 @@ function createSignedTransaction(bitcoreLib, privateKeyString, sourceAddress, de "script": script, "satoshis": inputSatoshis }; - - var transaction = new bitcoreLib.Transaction() - .from(utxo) - .to(destinationAddress, outputSatoshis) - .change(sourceAddress) - .sign(privateKey); - - return transaction.serialize(); + return utxo; } /** @@ -113,10 +124,10 @@ function sendCoins(privatekey, fromaddress, toaddress, amount) { app.dialog.close(); app.dialog.alert("Your wallet has no available funds (ZERO_LENGTH_UXTO).", "Error"); return; - } else if (success.uxtos.length > 1) { - app.dialog.close(); - app.dialog.alert("For technical reasons, your wallet isn't compatible with this app right now. You can still sweep this wallet into an alternative app to spend it. (MULTIPLE_UXTO)", "Error"); - return; + } + var utxos = []; + for (var i = 0; i < success.utxos.length; i++) { + utxos.push(createUtxo(fromaddress, success.utxos[i].txHash, success.utxos[i].txOutputIndex, success.utxos[i].script, success.utxos[i].value)); } var bitcore = null; var satoshis = amount * 100000000; @@ -132,9 +143,7 @@ function sendCoins(privatekey, fromaddress, toaddress, amount) { app.dialog.alert("This app version doesn't support " + success.currency + ".", "Error"); return; } - var txdata = createSignedTransaction(bitcore, privatekey, fromaddress, toaddress, - success.uxtos[0].txHash, success.uxtos[0].txOutputIndex, success.uxtos[0].script, - success.uxtos[0].value, satoshis); + var txdata = createSignedTransaction(bitcore, privatekey, fromaddress, toaddress, utxos, satoshis); progressdialog.setProgress(75); progressdialog.setText("Sending payment...");