Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cgcardona
Created September 6, 2018 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgcardona/63a98150fe96e204aad0b55b367b24ed to your computer and use it in GitHub Desktop.
Save cgcardona/63a98150fe96e204aad0b55b367b24ed to your computer and use it in GitHub Desktop.
Bitcoin Cash P2PKH input -> P2MS 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");
// create instance of Transaction Builder class
let transactionBuilder = new BITBOX.TransactionBuilder();
// set original amount, txid and vout
let originalAmount = 18602;
let txid = 'afa0dfb9ba45658da8a247a1ad58c35486077d2139cb0d4d7f0e69864c3e6626';
let vout = 0;
// add input
transactionBuilder.addInput(txid, vout);
// set fee and send amount
let fee = 250;
let sendAmount = originalAmount - fee;
// 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]);
// add output
transactionBuilder.addOutput(buf, 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); });
// da793ba87d840db564c5d568e156cc4d14320ae61bd03980e11096f74a187c65
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment