UNPKG

@btc-vision/bitcoin-rpc

Version:

The one and only fully typed Bitcoin RPC client for Node.js

56 lines (55 loc) 1.94 kB
import { Fetch } from 'rpc-request'; export class RESTClient extends Fetch { constructor(opts) { if (!opts.url) { throw new Error('URL is required'); } const params = { ...opts, base_url: opts.base_url || opts.url + ':' + (opts.port || 8332).toString(), transform: 'json', headers: { 'Content-Type': 'application/json' }, }; if (opts.auth) { if (!params.headers) { params.headers = {}; } params.headers.Authorization = 'Basic ' + Buffer.from(opts.auth.user + ':' + opts.auth.pass, 'utf8').toString('base64'); } super(params); } getBlock({ hash, format = 'json' }) { return this.get(`/rest/block/${hash}.${format}`); } getBlockNoTxDetails({ hash, format = 'json' }) { return this.get(`/rest/block/notxdetails/${hash}.${format}`); } getBlockHashByHeight({ height, format = 'json' }) { return this.get(`/rest/blockhashbyheight/${height}.${format}`); } getChainInfo() { return this.get('rest/chaininfo.json'); } getUtxos({ checkmempool = true, outpoints, format = 'json' }) { let uri = `rest/getutxos${checkmempool ? '/checkmempool' : ''}`; outpoints = !Array.isArray(outpoints) ? [outpoints] : outpoints; for (const { txid, n } of outpoints) { uri += `/${txid}-${n}`; } return this.get(uri + '.' + format); } getHeaders({ count, hash, format = 'json' }) { return this.get(`rest/headers/${count}/${hash}.${format}`); } getMemPoolContents() { return this.get('rest/mempool/contents.json'); } getMemPoolInfo() { return this.get('rest/mempool/info.json'); } getTx({ txid, format = 'json' }) { return this.get(`rest/tx/${txid}.${format}`); } }