Created
October 3, 2019 17:03
-
-
Save cgcardona/cf20b43722f0526105183ad200eb2071 to your computer and use it in GitHub Desktop.
BITBOX script used for this 1000 tBCH donation https://explorer.bitcoin.com/tbch/tx/6d53a33769adb3848f7ff845ee4170d1df4a951ff9fea1d9a6de093aed3baf73#o=0
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
// imports | |
import { BITBOX } from "bitbox-sdk" | |
import { AddressUtxoResult } from "bitcoin-com-rest" | |
import { ECPair, HDNode } from "bitcoincashjs-lib" | |
// consts | |
const bitbox: BITBOX = new BITBOX({ | |
restURL: "https://trest.bitcoin.com/v2/" | |
}) | |
// Open the wallet generated with generate-wallet. | |
const main: any = async (): Promise<any> => { | |
try { | |
let mnemonic: string = "" | |
// root seed buffer | |
let rootSeed: Buffer = bitbox.Mnemonic.toSeed(mnemonic) | |
// master HDNode | |
let masterHDNode: HDNode = bitbox.HDNode.fromSeed(rootSeed, "testnet") | |
// HDNode of BIP44 account | |
let account: HDNode = bitbox.HDNode.derivePath( | |
masterHDNode, | |
"m/44'/145'/0'" | |
) | |
// derive the first external change address HDNode which is going to spend utxo | |
let change: HDNode = bitbox.HDNode.derivePath(account, "0/0") | |
// get the cash address | |
let cashAddress: string = bitbox.HDNode.toCashAddress(change) | |
let result = (await bitbox.Address.utxo(cashAddress)) as AddressUtxoResult | |
let transactionBuilder = new bitbox.TransactionBuilder("testnet") | |
// original amount of satoshis in vin | |
let originalAmount = result.utxos[0].satoshis + result.utxos[1].satoshis | |
// index of first vout | |
let vout1 = result.utxos[0].vout | |
// txid of first vout | |
let txid1 = result.utxos[0].txid | |
// add input with txid1 and index of vout1 | |
transactionBuilder.addInput(txid1, vout1) | |
// index of second vout | |
let vout2 = result.utxos[1].vout | |
// txid of second vout | |
let txid2 = result.utxos[1].txid | |
// add input with txid2 and index of vout2 | |
transactionBuilder.addInput(txid2, vout2) | |
// get byte count to calculate fee. paying 1 sat/byte | |
let byteCount: number = bitbox.BitcoinCash.getByteCount( | |
{ P2PKH: 2 }, | |
{ P2PKH: 1 } | |
) | |
// amount to send to receiver. It's the original amount - 1 sat/byte for tx size | |
let sendAmount: number = originalAmount - byteCount | |
// add output w/ address and amount to send | |
let faucet: string = "bchtest:qqmd9unmhkpx4pkmr6fkrr8rm6y77vckjvqe8aey35" | |
transactionBuilder.addOutput(faucet, sendAmount) | |
// keypair | |
let keyPair: ECPair = bitbox.HDNode.toKeyPair(change) | |
// sign w/ HDNode | |
let redeemScript: undefined | |
transactionBuilder.sign( | |
0, | |
keyPair, | |
redeemScript, | |
transactionBuilder.hashTypes.SIGHASH_ALL, | |
result.utxos[0].satoshis | |
) | |
transactionBuilder.sign( | |
1, | |
keyPair, | |
redeemScript, | |
transactionBuilder.hashTypes.SIGHASH_ALL, | |
result.utxos[1].satoshis | |
) | |
// build tx | |
let tx = transactionBuilder.build() | |
// output rawhex | |
let hex: string = tx.toHex() | |
// sendRawTransaction to running BCH node | |
let txid: string = await bitbox.RawTransactions.sendRawTransaction(hex) | |
// 6d53a33769adb3848f7ff845ee4170d1df4a951ff9fea1d9a6de093aed3baf73 | |
} catch (err) { | |
console.log(err) | |
} | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment