Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active August 15, 2018 12:23
Show Gist options
  • Save miguelmota/5a4ebb478f4eeb4a6249f978d282beb0 to your computer and use it in GitHub Desktop.
Save miguelmota/5a4ebb478f4eeb4a6249f978d282beb0 to your computer and use it in GitHub Desktop.
JavaScript/Node.js Bitcoin Cash (BCH) get unspent utxos and sign transaction [testnet] example
const explorers = require('bitcore-explorers')
const insight = new explorers.Insight('https://test-bch-insight.bitpay.com')
const bch = require('bitcoincashjs')
const buf = new Buffer('__your_64_bit_private_key__', 'hex')
const privateKey = new bch.PrivateKey(buf, 'testnet')
const address = privateKey.toAddress('testnet')
insight.getUnspentUtxos(address.toString(), function (error, utxos) {
if error {
console.error(error)
return
}
const utxo = {
txid: utxos[0].txid,
outputIndex: utxos[0].vout,
script: utxos[0].scriptPubKey,
satoshis: utxos[0].satoshis
}
const transaction = new bch.Transaction()
.from(utxo)
.to(address.toString(), 1299900000)
.sign(privateKey)
console.log(transaction.toString()) // 01000000011b8a4185...
}) 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment