Skip to content

Instantly share code, notes, and snippets.

@cgcardona
Created September 6, 2018 18:44
Show Gist options
  • Save cgcardona/f9247e51188f5693a212b63380d43264 to your computer and use it in GitHub Desktop.
Save cgcardona/f9247e51188f5693a212b63380d43264 to your computer and use it in GitHub Desktop.
Bitcoin Cash P2PKH input -> nulldata output example w/ BITBOX
let mnemonic = '';
// 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 the first external change address HDNode which is going to spend utxo
let node = BITBOX.HDNode.derivePath(account, "432/123");
// HDNode to cashAddress
let cashAddress = BITBOX.HDNode.toCashAddress(node);
// create instance of Transaction Builder class
let transactionBuilder = new BITBOX.TransactionBuilder();
// set original amount, txid and vout
let originalAmount = 17602;
let txid = '59c8960607a0cc4e3c8ce45d71ee1671e3d76d9b135de761ddac26360ac36302';
let vout = 0;
// add input
transactionBuilder.addInput(txid, vout);
// set fee and send amount
let fee = 250;
let sendAmount = originalAmount - fee;
// add output to send funds to
transactionBuilder.addOutput(cashAddress, sendAmount);
// encode data as null data output
let data = "BCHForEveryone";
let buf = BITBOX.Script.nullData.output.encode(Buffer.from(data, 'ascii'));
// add null data output and send it 0 satoshis
transactionBuilder.addOutput(buf, 0);
// HDNode to keypair
let key = BITBOX.HDNode.toKeyPair(node);
// empty redeemScript var
let redeemScript
// sign input
transactionBuilder.sign(0, key, redeemScript, transactionBuilder.hashTypes.SIGHASH_ALL, originalAmount)
// build to hex
let hex = transactionBuilder.build().toHex()
console.log(hex)
// POST to BCH network
BITBOX.RawTransactions.sendRawTransaction(hex).then((result) => { console.log(result); }, (err) => { console.log(err); });
// 7f55cc793477a426ec1bb30b39322d692bafd41b44c4e7ac145c6702f0e4192e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment