Created
November 3, 2018 03:31
-
-
Save cgcardona/54119c1312ced854664bff467a7f60cb to your computer and use it in GitHub Desktop.
BITBOX to spend P2SH output w/ custom script generated w/ https://gist.github.com/cgcardona/f72dcd476b9860bfaeba431f4a576eb8
This file contains 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
let BITBOXCli = require("bitbox-sdk/lib/bitbox-sdk").default; | |
let BITBOX = new BITBOXCli(); | |
let main = async () => { | |
let mnemonic = "put your mnemonic here"; | |
// root seed buffer | |
let rootSeed = BITBOX.Mnemonic.toSeed(mnemonic); | |
// master HDNode | |
let masterHDNode = BITBOX.HDNode.fromSeed(rootSeed, "bitcoincash"); | |
// HDNode of BIP44 account | |
let account = BITBOX.HDNode.derivePath(masterHDNode, "m/44'/145'/0'"); | |
// derive HDNode | |
let node = BITBOX.HDNode.derivePath(account, "0/0"); | |
// cashAddress | |
let cashAddress = BITBOX.HDNode.toCashAddress(node); | |
// custom script from create-p2sh-output.js | |
const script = BITBOX.Script.encode([ | |
Buffer.from("BOX", "ascii"), | |
BITBOX.Script.opcodes.OP_CAT, | |
Buffer.from("BITBOX", "ascii"), | |
BITBOX.Script.opcodes.OP_EQUAL | |
]); | |
// hash160 script buffer | |
const p2sh_hash160 = BITBOX.Crypto.hash160(script); | |
// encode hash160 as P2SH output | |
const scriptPubKey = BITBOX.Script.scriptHash.output.encode(p2sh_hash160); | |
// get p2sh address from output script | |
const p2shAddress = BITBOX.Address.fromOutputScript(scriptPubKey); | |
// fetch uxto from rest.bitcoin | |
let result = await BITBOX.Address.utxo(p2shAddress); | |
// create instance of TransactionBuilder | |
const transactionBuilder = new BITBOX.TransactionBuilder(); | |
// original amount of satoshis in vout | |
const originalAmount = result[0].satoshis; | |
// index of vout | |
const vout = result[0].vout; | |
// txid of vout | |
const txid = result[0].txid; | |
// add input | |
transactionBuilder.addInput(txid, vout); | |
// calculate tx size for fee of 1 sat/b | |
let byteCount = BITBOX.BitcoinCash.getByteCount({ P2PKH: 2 }, { P2PKH: 1 }); | |
let sendAmount = originalAmount - byteCount; | |
// add output | |
transactionBuilder.addOutput(cashAddress, sendAmount); | |
// encode locking script | |
let encodedScript = BITBOX.Script.encode(script); | |
// HDNode to keypair | |
let keyPair = BITBOX.HDNode.toKeyPair(node); | |
// create unlocking script | |
let script2 = [Buffer.from("BIT", "ascii")]; | |
// concat scripts together | |
let children = script2.concat(script); | |
// encode scripts | |
let encodedScript2 = BITBOX.Script.encode(children); | |
// set input script | |
transactionBuilder.addInputScript(vout, encodedScript2); | |
// build to hex | |
const hex = transactionBuilder.build().toHex(); | |
// POST to BCH network | |
let success = await BITBOX.RawTransactions.sendRawTransaction(hex); | |
console.log("success: ", success); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment