Skip to content

Instantly share code, notes, and snippets.

@cgcardona
Last active September 6, 2018 17:47
Show Gist options
  • Save cgcardona/09d4226c36722ba46c1c30f3cb30adcd to your computer and use it in GitHub Desktop.
Save cgcardona/09d4226c36722ba46c1c30f3cb30adcd to your computer and use it in GitHub Desktop.
Bitcoin Cash P2PKH input -> P2PK 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 HDNode
let node = BITBOX.HDNode.derivePath(account, "432/123");
// create instance of Transaction Builder class
let transactionBuilder = new BITBOX.TransactionBuilder();
// set original amount, txid and vout
let originalAmount = 19602;
let txid = 'cf2c1e71a5c5d91263ba90b0a1eb308c6be94a46a86085a45ab7afa0b03b2c0a';
let vout = 0;
// add input
transactionBuilder.addInput(txid, vout);
// pubkey from HDNode
let pubKey = BITBOX.HDNode.toPublicKey(node);
// encode pubkey as P2PK output
let buf = BITBOX.Script.pubKey.output.encode(pubKey);
// set fee and send amount
let fee = 250;
let sendAmount = originalAmount - fee;
// add output
transactionBuilder.addOutput(buf, 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);
// built 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); });
// 72c2b807e3c9a009ec515bfa86e0fa95a36255f67f7ddda831d4dd1bba313678
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment