Created
September 6, 2018 18:01
-
-
Save cgcardona/63a98150fe96e204aad0b55b367b24ed to your computer and use it in GitHub Desktop.
Bitcoin Cash P2PKH input -> P2MS output example w/ BITBOX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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