Skip to content

Instantly share code, notes, and snippets.

@cgcardona
Last active September 6, 2018 17:59
Show Gist options
  • Save cgcardona/eff86476717cb3d7892b3fd752cf3feb to your computer and use it in GitHub Desktop.
Save cgcardona/eff86476717cb3d7892b3fd752cf3feb to your computer and use it in GitHub Desktop.
Bitcoin Cash P2PKH input -> P2PKH 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
let node = BITBOX.HDNode.derivePath(account, "432/123");
// cashAddress from HDNode
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 = 19102;
let txid = 'd34a096383ecc0773e8fb16cfd0b9a87d22a5d561372dd583fcff7b9dad7ba3e';
let vout = 0;
// add input
transactionBuilder.addInput(txid, vout);
// set fee and send amount
let fee = 250;
let sendAmount = originalAmount - fee;
// add output
transactionBuilder.addOutput(cashAddress, sendAmount);
// keypair from HDNode
let keyPair = BITBOX.HDNode.toKeyPair(node);
// empty redeemScript var
let redeemScript;
// sign input
transactionBuilder.sign(0, keyPair, redeemScript, transactionBuilder.hashTypes.SIGHASH_ALL, originalAmount);
// build to hex
let tx = transactionBuilder.build();
let hex = tx.toHex();
// POST to BCH network
BITBOX.RawTransactions.sendRawTransaction(hex).then((result) => { console.log(result); }, (err) => { console.log(err); });
// ea65bb032a6e28afaf8e31653145358a1a608cc17a2fc51775a3fd16af6ef2e5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment