bch-consumer is a JS library for retrieving Bitcoin Cash (BCH) blockchain data from ipfs-bch-wallet-consumer, which in turn gets its information from ipfs-bch-wallet-service. This library is part of the Cash Stack.
npm install --save bch-consumer
// Configure this constant for your infrastructure.
const RESTURL = 'https://free-bch.fullstack.cash'
// const RESTURL = 'http://localhost:5005'
console.log(`Using this REST URL for bch-consumer: ${RESTURL}`)
// Instantiate bch-consumer
const BchConsumer = require('bch-consumer')
const bchConsumer = new BchConsumer({ restURL: RESTURL })
Here are some simple examples illustrating how to use this library. These examples assume the BCH blockchain.
const addr = 'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
const result = await bchConsumer.bch.getBalance(addr)
console.log('result: ', result)
const addr = 'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
const result = await bchConsumer.bch.getUtxos(addr)
console.log('result: ', result)
const hex = '0100000001000000000000abcdef'
const result = await bchConsumer.bch.sendTx(hex)
console.log('result: ', result)
const addr = 'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
const result = await bchConsumer.bch.getTxHistory(addr)
console.log('result: ', result)
const txids = [
'01517ff1587fa5ffe6f5eb91c99cf3f2d22330cd7ee847e928ce90ca95bf781b'
]
const result = await bchConsumer.bch.getTxData(txids)
console.log('result: ', result)
const addr = 'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
const result = await bchConsumer.msg.getPubKey(addr)
console.log('result: ', result)
const tokenId = '22f8475fd82a4163a64cdb0d868534d016964b3acc8a6f0e6572f3c373d01866'
const result = await bchConsumer.bch.getTokenData(tokenId)
console.log('result: ', result)
The PSF Write Price is the cost of hosting 1 MB of data to the PSFFPP file pinning network for a year.
const result = await bchConsumer.bch.getPsffppWritePrice()
console.log('result: ', result)
The output of getTokenData()
returns IPFS CIDs. If an IPFS CID resolves to a JSON file (like in the case of tokens), you can give the CID and retrieve the JSON object.
const cid = 'bafkreigbgrvpagnmrqz2vhofifrqobigsxkdvnvikf5iqrkrbwrzirazhm'
const result = await bchConsumer.bch.cid2json({ cid })
console.log('result: ', result)