Created
September 29, 2019 10:14
-
-
Save gianlucamazza/9d28edd690017403af8eb9a80dcb110b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function getOutputScript(address) { | |
let script = await bitcoin.address.toOutputScript(address, getNetwork()); | |
return script; | |
} | |
exports.prepareTx = async function (myaddresses, destination, satoshi, feeRate) { | |
let addresses = myaddresses.filter(hasUTXO) | |
for(let i = 0; i < addresses.length; i++) { | |
addresses[i].txs = await explorer.getAddressTxs(addresses[i].address); | |
} | |
let utxo = await getUTXO(addresses); | |
let targets = [{ | |
"address": destination, | |
"value": parseFloat(satoshi) * 100000000 | |
}] | |
let { inputs, outputs, fee } = coinselect(utxo, targets, feeRate) | |
if (!inputs || !outputs) return | |
const psbt = new bitcoin.Psbt({ network: network }); | |
inputs.forEach(input => psbt.addInput( | |
{ | |
"hash": input.txId, | |
"index": input.vout, | |
"sequence": 0xfffffffe, | |
"witnessUtxo": { | |
"script": input.pubKeyScript, | |
"value": input.value | |
} | |
} | |
)) | |
for(let i = 0; i < outputs.length; i++) { | |
if (!outputs[i].address) { | |
outputs[i].address = await this.getCurrentChangeAddress() | |
} | |
let script = await getOutputScript(outputs[i].address) | |
psbt.addOutput({ | |
"script": script, | |
"value": outputs[i].value | |
}) | |
} | |
return psbt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment