Support spending wallets with multiple UTXOs
This commit is contained in:
parent
0ec764142c
commit
9ddda9b565
@ -41,15 +41,33 @@ function scanPrivateKeyQrCode(callback) {
|
|||||||
* @param {type} privateKeyString Private key from wallet QR code
|
* @param {type} privateKeyString Private key from wallet QR code
|
||||||
* @param {type} sourceAddress Sender's wallet address
|
* @param {type} sourceAddress Sender's wallet address
|
||||||
* @param {type} destinationAddress Recipient'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} txHash From UXTO (unspent output)
|
||||||
* @param {type} txOutputIndex From UXTO (unspent output)
|
* @param {type} txOutputIndex From UXTO (unspent output)
|
||||||
* @param {type} script From UXTO (unspent output)
|
* @param {type} script From UXTO (unspent output)
|
||||||
* @param {type} inputSatoshis From UXTO (unspent output)
|
* @param {type} inputSatoshis From UXTO (unspent output)
|
||||||
* @param {type} outputSatoshis Amount to send to recipient's wallet
|
* @returns {createUtxo.utxo}
|
||||||
* @returns {string} Hex of serialized transaction, suitable for broadcast via Bitcoin Core or an API.
|
|
||||||
*/
|
*/
|
||||||
function createSignedTransaction(bitcoreLib, privateKeyString, sourceAddress, destinationAddress, txHash, txOutputIndex, script, inputSatoshis, outputSatoshis) {
|
function createUtxo(sourceAddress, txHash, txOutputIndex, script, inputSatoshis) {
|
||||||
var privateKey = new bitcoreLib.PrivateKey(privateKeyString);
|
|
||||||
var utxo = {
|
var utxo = {
|
||||||
"txId": txHash,
|
"txId": txHash,
|
||||||
"outputIndex": txOutputIndex,
|
"outputIndex": txOutputIndex,
|
||||||
@ -57,14 +75,7 @@ function createSignedTransaction(bitcoreLib, privateKeyString, sourceAddress, de
|
|||||||
"script": script,
|
"script": script,
|
||||||
"satoshis": inputSatoshis
|
"satoshis": inputSatoshis
|
||||||
};
|
};
|
||||||
|
return utxo;
|
||||||
var transaction = new bitcoreLib.Transaction()
|
|
||||||
.from(utxo)
|
|
||||||
.to(destinationAddress, outputSatoshis)
|
|
||||||
.change(sourceAddress)
|
|
||||||
.sign(privateKey);
|
|
||||||
|
|
||||||
return transaction.serialize();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -113,10 +124,10 @@ function sendCoins(privatekey, fromaddress, toaddress, amount) {
|
|||||||
app.dialog.close();
|
app.dialog.close();
|
||||||
app.dialog.alert("Your wallet has no available funds (ZERO_LENGTH_UXTO).", "Error");
|
app.dialog.alert("Your wallet has no available funds (ZERO_LENGTH_UXTO).", "Error");
|
||||||
return;
|
return;
|
||||||
} else if (success.uxtos.length > 1) {
|
}
|
||||||
app.dialog.close();
|
var utxos = [];
|
||||||
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");
|
for (var i = 0; i < success.utxos.length; i++) {
|
||||||
return;
|
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 = amount * 100000000;
|
||||||
@ -132,9 +143,7 @@ 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,
|
var txdata = createSignedTransaction(bitcore, privatekey, fromaddress, toaddress, utxos, satoshis);
|
||||||
success.uxtos[0].txHash, success.uxtos[0].txOutputIndex, success.uxtos[0].script,
|
|
||||||
success.uxtos[0].value, satoshis);
|
|
||||||
|
|
||||||
progressdialog.setProgress(75);
|
progressdialog.setProgress(75);
|
||||||
progressdialog.setText("Sending payment...");
|
progressdialog.setText("Sending payment...");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user