diff --git a/src/bch-js.js b/src/bch-js.js index 17be54f..0806836 100644 --- a/src/bch-js.js +++ b/src/bch-js.js @@ -34,7 +34,6 @@ const IPFS = require("./ipfs") const Encryption = require("./encryption") // Indexers -const Blockbook = require("./blockbook") const OpenBazaar = require("./openbazaar") const Ninsight = require("./ninsight") const Electrumx = require("./electrumx") @@ -80,9 +79,6 @@ class BCHJS { // console.log(`apiToken: ${this.apiToken}`) - // Populate Blockbook endpoints. - this.Blockbook = new Blockbook(libConfig) - // Populate OpenBazaar endpoints this.OpenBazaar = new OpenBazaar(libConfig) @@ -194,9 +190,6 @@ class BitboxShim { this.Mining = new Mining(libConfig) this.RawTransactions = new RawTransactions(libConfig) - // Populate Blockbook endpoints. - this.Blockbook = new Blockbook(libConfig) - // Populate OpenBazaar endpoints this.OpenBazaar = new OpenBazaar(libConfig) diff --git a/src/blockbook.js b/src/blockbook.js deleted file mode 100644 index 9494441..0000000 --- a/src/blockbook.js +++ /dev/null @@ -1,132 +0,0 @@ -/* - This library interacts with the REST API endpoints in bch-api that communicate - with the Blockbook API. - - CT 11/10/20 - These endpoints will be soon be deprecated. Blockbook is being fazed out in - favor of Fulcrum. -*/ - -const axios = require("axios") - -let _this - -class Blockbook { - constructor(config) { - this.restURL = config.restURL - this.apiToken = config.apiToken - this.authToken = config.authToken - // console.log(`Blockbook apiToken: ${this.apiToken}`) - - if (this.authToken) { - // Add Basic Authentication token to the authorization header. - this.axiosOptions = { - headers: { - authorization: this.authToken - } - } - } else { - // Add JWT token to the authorization header. - this.axiosOptions = { - headers: { - authorization: `Token ${this.apiToken}` - } - } - } - - _this = this - } - - async balance(address) { - try { - // Handle single address. - if (typeof address === "string") { - const response = await axios.get( - `${this.restURL}blockbook/balance/${address}`, - _this.axiosOptions - ) - - return response.data - - // Handle array of addresses. - } else if (Array.isArray(address)) { - const response = await axios.post( - `${this.restURL}blockbook/balance`, - { - addresses: address - }, - _this.axiosOptions - ) - - return response.data - } - - throw new Error(`Input address must be a string or array of strings.`) - } catch (error) { - if (error.response && error.response.data) throw error.response.data - else throw error - } - } - - async utxo(address) { - try { - // Handle single address. - if (typeof address === "string") { - const response = await axios.get( - `${this.restURL}blockbook/utxos/${address}`, - _this.axiosOptions - ) - return response.data - - // Handle array of addresses. - } else if (Array.isArray(address)) { - const response = await axios.post( - `${this.restURL}blockbook/utxos`, - { - addresses: address - }, - _this.axiosOptions - ) - - return response.data - } - - throw new Error(`Input address must be a string or array of strings.`) - } catch (error) { - if (error.response && error.response.data) throw error.response.data - else throw error - } - } - - async tx(txid) { - try { - // Handle single txid. - if (typeof txid === "string") { - const response = await axios.get( - `${this.restURL}blockbook/tx/${txid}`, - _this.axiosOptions - ) - return response.data - - // Handle array of addresses. - } else if (Array.isArray(txid)) { - const response = await axios.post( - `${this.restURL}blockbook/tx`, - { - txids: txid - }, - _this.axiosOptions - ) - - return response.data - } - - throw new Error(`Input txid must be a string or array of strings.`) - } catch (error) { - if (error.response && error.response.data) throw error.response.data - else throw error - } - } -} - -module.exports = Blockbook diff --git a/test/integration/blockbook.js b/test/integration/blockbook.js deleted file mode 100644 index 21f6d1b..0000000 --- a/test/integration/blockbook.js +++ /dev/null @@ -1,148 +0,0 @@ -/* - Integration tests for the Blockbook library. - -*/ - -const chai = require("chai") -const assert = chai.assert -const BCHJS = require("../../src/bch-js") -const bchjs = new BCHJS() -//const axios = require("axios") - -// Inspect utility used for debugging. -const util = require("util") -util.inspect.defaultOptions = { - showHidden: true, - colors: true, - depth: 3 -} - -describe(`#Blockbook`, () => { - beforeEach(async () => { - if (process.env.IS_USING_FREE_TIER) await sleep(1000) - }) - - describe(`#Balance`, () => { - it(`should GET balance for a single address`, async () => { - const addr = "bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf" - - const result = await bchjs.Blockbook.balance(addr) - //console.log(`result: ${util.inspect(result)}`) - - assert.hasAnyKeys(result, [ - "page", - "totalPages", - "itemsOnPage", - "address", - "balance", - "totalReceived", - "totalSent", - "unconfirmedBalance", - "unconfirmedTxs", - "txs", - "txids" - ]) - assert.isArray(result.txids) - }) - - it(`should POST request balances for an array of addresses`, async () => { - const addr = [ - "bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf", - "bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v" - ] - - const result = await bchjs.Blockbook.balance(addr) - //console.log(`result: ${util.inspect(result)}`) - - assert.isArray(result) - assert.hasAnyKeys(result[0], [ - "page", - "totalPages", - "itemsOnPage", - "address", - "balance", - "totalReceived", - "totalSent", - "unconfirmedBalance", - "unconfirmedTxs", - "txs", - "txids" - ]) - assert.isArray(result[0].txids) - }) - - it(`should throw error on array size rate limit`, async () => { - try { - const addr = [] - for (let i = 0; i < 25; i++) - addr.push("bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf") - - const result = await bchjs.Blockbook.balance(addr) - - console.log(`result: ${util.inspect(result)}`) - assert.equal(true, false, "Unexpected result!") - } catch (err) { - assert.hasAnyKeys(err, ["error"]) - assert.include(err.error, "Array too large") - } - }) - }) - - describe(`#utxo`, () => { - it(`should GET utxos for a single address`, async () => { - const addr = "bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7" - - const result = await bchjs.Blockbook.utxo(addr) - // console.log(`result: ${JSON.stringify(result, null, 2)}`) - - assert.isArray(result) - assert.hasAnyKeys(result[0], [ - "txid", - "vout", - "value", - "height", - "confirmations" - ]) - }) - - it(`should POST utxo details for an array of addresses`, async () => { - const addr = [ - "bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7", - "bitcoincash:qz0us0z6ucpqt07jgpad0shgh7xmwxyr3ynlcsq0wr" - ] - - const result = await bchjs.Blockbook.utxo(addr) - //console.log(`result: ${JSON.stringify(result, null, 2)}`) - - assert.isArray(result) - assert.isArray(result[0]) - assert.hasAnyKeys(result[0][0], [ - "txid", - "vout", - "value", - "height", - "confirmations" - ]) - }) - - it(`should throw error on array size rate limit`, async () => { - try { - const addr = [] - for (let i = 0; i < 25; i++) - addr.push("bitcoincash:qz0us0z6ucpqt07jgpad0shgh7xmwxyr3ynlcsq0wr") - - const result = await bchjs.Blockbook.utxo(addr) - //console.log(`result: ${util.inspect(result)}`) - - assert.equal(true, false, "Unexpected result!") - } catch (err) { - assert.hasAnyKeys(err, ["error"]) - assert.include(err.error, "Array too large") - } - }) - }) -}) - -function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)) -} diff --git a/test/unit/blockbook.js b/test/unit/blockbook.js deleted file mode 100644 index 9866c81..0000000 --- a/test/unit/blockbook.js +++ /dev/null @@ -1,275 +0,0 @@ -const chai = require("chai") -const assert = chai.assert -const BCHJS = require("../../src/bch-js") -const bchjs = new BCHJS() -const axios = require("axios") -const sinon = require("sinon") - -const mockData = require("./fixtures/blockbook-mock") - -describe(`#Blockbook`, () => { - let sandbox - beforeEach(() => (sandbox = sinon.createSandbox())) - afterEach(() => sandbox.restore()) - - describe(`#Balance`, () => { - it(`should GET balance for a single address`, async () => { - // Stub the network call. - sandbox.stub(axios, "get").resolves({ data: mockData.balance }) - - const addr = "bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf" - - const result = await bchjs.Blockbook.balance(addr) - //console.log(`result: ${util.inspect(result)}`) - - assert.hasAnyKeys(result, [ - "page", - "totalPages", - "itemsOnPage", - "address", - "balance", - "totalReceived", - "totalSent", - "unconfirmedBalance", - "unconfirmedTxs", - "txs", - "txids" - ]) - assert.isArray(result.txids) - }) - - it(`should POST request balances for an array of addresses`, async () => { - // Stub the network call. - sandbox - .stub(axios, "post") - .resolves({ data: [mockData.balance, mockData.balance] }) - - const addr = [ - "bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf", - "bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v" - ] - - const result = await bchjs.Blockbook.balance(addr) - //console.log(`result: ${util.inspect(result)}`) - - assert.isArray(result) - assert.hasAnyKeys(result[0], [ - "page", - "totalPages", - "itemsOnPage", - "address", - "balance", - "totalReceived", - "totalSent", - "unconfirmedBalance", - "unconfirmedTxs", - "txs", - "txids" - ]) - assert.isArray(result[0].txids) - }) - - it(`should throw an error for improper input`, async () => { - try { - // Stub the network call. - sandbox - .stub(axios, "post") - .throws(`Input address must be a string or array of strings`) - - const addr = 12345 - - await bchjs.Blockbook.balance(addr) - assert.equal(true, false, "Unexpected result!") - } catch (err) { - //console.log(`err: `, err) - assert.include( - err.message, - `Input address must be a string or array of strings` - ) - } - }) - /* - it(`should throw error on array size rate limit`, async () => { - try { - // Stub the network call. - sandbox.stub(axios, "post").throws(`Array too large`) - - const addr = [] - for (let i = 0; i < 25; i++) - addr.push("bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf") - - const result = await bchjs.Blockbook.balance(addr) - - console.log(`result: ${util.inspect(result)}`) - assert.equal(true, false, "Unexpected result!") - } catch (err) { - assert.hasAnyKeys(err, ["error"]) - assert.include(err.error, "Array too large") - } - }) - */ - }) - - describe(`#utxo`, () => { - it(`should GET utxos for a single address`, async () => { - // Stub the network call. - sandbox.stub(axios, "get").resolves({ data: mockData.utxo }) - - const addr = "bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf" - - const result = await bchjs.Blockbook.utxo(addr) - //console.log(`result: ${JSON.stringify(result, null, 2)}`) - - assert.isArray(result) - assert.hasAnyKeys(result[0], [ - "txid", - "vout", - "value", - "height", - "confirmations" - ]) - }) - - it(`should POST utxo details for an array of addresses`, async () => { - // Stub the network call. - sandbox.stub(axios, "post").resolves({ data: mockData.utxos }) - - const addr = [ - "bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf", - "bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v" - ] - - const result = await bchjs.Blockbook.utxo(addr) - //console.log(`result: ${JSON.stringify(result, null, 2)}`) - - assert.isArray(result) - assert.isArray(result[0]) - assert.hasAnyKeys(result[0][0], [ - "txid", - "vout", - "value", - "height", - "confirmations" - ]) - }) - - it(`should throw an error for improper input`, async () => { - try { - // Stub the network call. - sandbox - .stub(axios, "post") - .throws(`Input address must be a string or array of strings`) - - const addr = 12345 - - await bchjs.Blockbook.utxo(addr) - assert.equal(true, false, "Unexpected result!") - } catch (err) { - //console.log(`err: `, err) - assert.include( - err.message, - `Input address must be a string or array of strings` - ) - } - }) - /* - it(`should throw error on array size rate limit`, async () => { - try { - const addr = [] - for (let i = 0; i < 25; i++) - addr.push("bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf") - - const result = await bchjs.Blockbook.utxo(addr) - - console.log(`result: ${util.inspect(result)}`) - assert.equal(true, false, "Unexpected result!") - } catch (err) { - assert.hasAnyKeys(err, ["error"]) - assert.include(err.error, "Array too large") - } - }) - */ - }) - - describe(`#tx`, () => { - it(`should GET tx details for a single txid`, async () => { - // Stub the network call. - sandbox.stub(axios, "get").resolves({ data: mockData.utxo }) - - const addr = "bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf" - - const result = await bchjs.Blockbook.utxo(addr) - //console.log(`result: ${JSON.stringify(result, null, 2)}`) - - assert.isArray(result) - assert.hasAnyKeys(result[0], [ - "txid", - "vout", - "value", - "height", - "confirmations" - ]) - }) - - // it(`should POST utxo details for an array of addresses`, async () => { - // // Stub the network call. - // sandbox.stub(axios, "post").resolves({ data: mockData.utxos }) - // - // const addr = [ - // "bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf", - // "bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v" - // ] - // - // const result = await bchjs.Blockbook.utxo(addr) - // //console.log(`result: ${JSON.stringify(result, null, 2)}`) - // - // assert.isArray(result) - // assert.isArray(result[0]) - // assert.hasAnyKeys(result[0][0], [ - // "txid", - // "vout", - // "value", - // "height", - // "confirmations" - // ]) - // }) - - // it(`should throw an error for improper input`, async () => { - // try { - // // Stub the network call. - // sandbox - // .stub(axios, "post") - // .throws(`Input address must be a string or array of strings`) - // - // const addr = 12345 - // - // await bchjs.Blockbook.utxo(addr) - // assert.equal(true, false, "Unexpected result!") - // } catch (err) { - // //console.log(`err: `, err) - // assert.include( - // err.message, - // `Input address must be a string or array of strings` - // ) - // } - // }) - - /* - it(`should throw error on array size rate limit`, async () => { - try { - const addr = [] - for (let i = 0; i < 25; i++) - addr.push("bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf") - - const result = await bchjs.Blockbook.utxo(addr) - - console.log(`result: ${util.inspect(result)}`) - assert.equal(true, false, "Unexpected result!") - } catch (err) { - assert.hasAnyKeys(err, ["error"]) - assert.include(err.error, "Array too large") - } - }) - */ - }) -}) diff --git a/test/unit/fixtures/blockbook-mock.js b/test/unit/fixtures/blockbook-mock.js deleted file mode 100644 index 5048a31..0000000 --- a/test/unit/fixtures/blockbook-mock.js +++ /dev/null @@ -1,64 +0,0 @@ -/* - Unit test mocks for Blockbook endpoints. -*/ - -const balance = { - page: 1, - totalPages: 1, - itemsOnPage: 1000, - address: "bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf", - balance: "230000", - totalReceived: "6194528752", - totalSent: "6194298752", - unconfirmedBalance: "0", - unconfirmedTxs: 0, - txs: 3, - txids: [ - "27ec8512c1a9ee9e9ae9b98eb60375f1d2bd60e2e76a1eff5a45afdbc517cf9c", - "6e1ae1bf7db6de799ec1c05ab2816ac65549bd80141567af088e6f291385b07d", - "5d95f4e6047901fc1502a9758bbdb14ce73f24662b1784d13e0217b3d37bb7a2" - ] -} - -const utxo = [ - { - txid: "27ec8512c1a9ee9e9ae9b98eb60375f1d2bd60e2e76a1eff5a45afdbc517cf9c", - vout: 0, - value: "100000", - height: 560430, - confirmations: 29321 - }, - { - txid: "6e1ae1bf7db6de799ec1c05ab2816ac65549bd80141567af088e6f291385b07d", - vout: 0, - value: "130000", - height: 560039, - confirmations: 29712 - } -] - -const utxos = [ - [ - { - txid: "27ec8512c1a9ee9e9ae9b98eb60375f1d2bd60e2e76a1eff5a45afdbc517cf9c", - vout: 0, - value: "100000", - height: 560430, - confirmations: 29868 - }, - { - txid: "6e1ae1bf7db6de799ec1c05ab2816ac65549bd80141567af088e6f291385b07d", - vout: 0, - value: "130000", - height: 560039, - confirmations: 30259 - } - ], - [] -] - -module.exports = { - balance, - utxo, - utxos -}