Skip to content

Instantly share code, notes, and snippets.

@cgcardona
Last active September 6, 2018 18:27
Show Gist options
  • Save cgcardona/79aebc4f647a79dfe83ec4700d111fcb to your computer and use it in GitHub Desktop.
Save cgcardona/79aebc4f647a79dfe83ec4700d111fcb to your computer and use it in GitHub Desktop.
Bitcoin Cash P2MS 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 HDNode
let node = BITBOX.HDNode.derivePath(account, "432/123");
// derive the second HDNode
let node2 = BITBOX.HDNode.derivePath(account, "432/124");
// first 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 = 18352;
let txid = 'da793ba87d840db564c5d568e156cc4d14320ae61bd03980e11096f74a187c65';
let vout = 0;
// first HDNode to pubkey
let pubKey1 = BITBOX.HDNode.toPublicKey(node);
// second HDNode to pubkey
let pubKey2 = BITBOX.HDNode.toPublicKey(node2);
// encode pubkeys as 1-of-2 P2MS output
let buf = BITBOX.Script.multisig.output.encode(1, [pubKey1, pubKey2]);
// set fee and send amount
transactionBuilder.addInput(txid, vout, transactionBuilder.DEFAULT_SEQUENCE, buf);
let fee = 250;
let sendAmount = originalAmount - fee;
// HDNode to keypair
transactionBuilder.addOutput(cashAddress, sendAmount);
// HDNode to keypair
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 tx to BCH network
BITBOX.RawTransactions.sendRawTransaction(hex).then((result) => { console.log(result); }, (err) => { console.log(err); });
// af2804bba5538a162d68f064d7fd2baa50036b9f6a37debf745f84f12864efcc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment