Skip to content

Instantly share code, notes, and snippets.

@cgcardona
Created May 16, 2018 14:50
Show Gist options
  • Save cgcardona/2fe2c45bf4a72ec03c3455adc7c310aa to your computer and use it in GitHub Desktop.
Save cgcardona/2fe2c45bf4a72ec03c3455adc7c310aa to your computer and use it in GitHub Desktop.
Donation script from the BITBOX 5-15-18 hackathon donation 1 BCH each to the Bitcoin Cash Fun and Bitcoin ABC.
let change = BITBOX.HDNode.fromXPriv("my-x-priv");
// get the cash address
let cashAddress = BITBOX.HDNode.toCashAddress(change);
BITBOX.Address.utxo('bitcoincash:qprjht8an8quhwnymrvcnnhs3eca4accx5rdsp0clh').then((result) => {
console.log(result);
// instance of transaction builder
let transactionBuilder = new BITBOX.TransactionBuilder('bitcoincash');
// original amount of satoshis in vin
let originalAmount = result[0].satoshis;
// index of vout
let vout = result[0].vout;
// txid of vout
let txid = result[0].txid;
// add input with txid and index of vout
transactionBuilder.addInput(txid, vout);
// get byte count to calculate fee. paying 1 sat/byte
let byteCount = BITBOX.BitcoinCash.getByteCount({ P2PKH: 1 }, { P2PKH: 2 });
// amount to send to receiver. It's the original amount - 1 sat/byte for tx size
let sendAmount = originalAmount - byteCount;
// BCF
transactionBuilder.addOutput('bitcoincash:ppfla28mmaezfhyfs9adupafnzlyy5xteqrr2vamtt', sendAmount / 2);
// ABC
transactionBuilder.addOutput('bitcoincash:qqeht8vnwag20yv8dvtcrd4ujx09fwxwsqqqw93w88', sendAmount / 2);
// keypair
let keyPair = BITBOX.HDNode.toKeyPair(change);
// sign w/ HDNode
let redeemScript;
transactionBuilder.sign(0, keyPair, redeemScript, transactionBuilder.hashTypes.SIGHASH_ALL, originalAmount);
// build tx
let tx = transactionBuilder.build();
// output rawhex
let hex = tx.toHex();
// sendRawTransaction to running BCH node
BITBOX.RawTransactions.sendRawTransaction(hex).then((result) => { console.log(result); }, (err) => { console.log(err); });
}, (err) => { console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment