Skip to content

Instantly share code, notes, and snippets.

@cgcardona
Last active September 6, 2018 17:46
Show Gist options
  • Save cgcardona/21c2656a4730007956d648b22ec5292c to your computer and use it in GitHub Desktop.
Save cgcardona/21c2656a4730007956d648b22ec5292c to your computer and use it in GitHub Desktop.
Bitcoin Cash P2PK 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 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 = 19352;
let txid = '72c2b807e3c9a009ec515bfa86e0fa95a36255f67f7ddda831d4dd1bba313678';
let vout = 0;
// pubkey from HDNode
let pubKey = BITBOX.HDNode.toPublicKey(node);
// encode pubkey as P2PK output
let buf = BITBOX.Script.pubKey.output.encode(pubKey);
// add input
transactionBuilder.addInput(txid, vout, transactionBuilder.DEFAULT_SEQUENCE, buf);
// 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);
// 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); });
// d34a096383ecc0773e8fb16cfd0b9a87d22a5d561372dd583fcff7b9dad7ba3e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment