Skip to content

Instantly share code, notes, and snippets.

@skynxt

skynxt/create.js Secret

Last active September 28, 2017 15:42
Show Gist options
  • Save skynxt/d395a115ae9bfbde6e9744b0e962beaa to your computer and use it in GitHub Desktop.
Save skynxt/d395a115ae9bfbde6e9744b0e962beaa to your computer and use it in GitHub Desktop.
Bitcoin Create raw tx
let targets = [{ address: sendData.address, value: Number(sendData.amount) }];
let result = coinselect(response, targets, satByte)
if (!result.inputs || !result.outputs) return
let txb = new btc.TransactionBuilder(this.m_currentNetwork)
let scriptIO = btc.script.compile([btc.opcodes.OP_DUP, btc.opcodes.OP_HASH160, btc.crypto.hash160(this.m_ecpair.getPublicKeyBuffer()), btc.opcodes.OP_EQUALVERIFY, btc.opcodes.OP_CHECKSIG])
result.inputs.forEach(input => txb.addInput(input.txId, input.vout))
txb.addOutput(sendData.address, result.outputs[0].value);
txb.addOutput(scriptIO, result.outputs[1].value);
let hashType = btc.Transaction.SIGHASH_ALL;
let txRaw = txb.buildIncomplete()
for(let i = 0; i < result.inputs.length; i++) {
let signatureHash = txRaw.hashForSignature(i, scriptIO, hashType)
let redeemScriptSig = btc.script.scriptHash.input.encode([this.m_ecpair.sign(signatureHash).toScriptSignature(hashType)], this.m_ecpair.getPublicKeyBuffer())
txRaw.setInputScript(i, redeemScriptSig)
}
console.log(txRaw.toHex());
callBack({trans: txRaw, fee: result.fee})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment