Skip to content

Instantly share code, notes, and snippets.

@cgcardona
Created October 3, 2019 17:03
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/cf20b43722f0526105183ad200eb2071 to your computer and use it in GitHub Desktop.
Save cgcardona/cf20b43722f0526105183ad200eb2071 to your computer and use it in GitHub Desktop.
// 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