diff --git a/src/routes/v3/blockbook.js b/src/routes/v3/blockbook.js index 1b6d0a4..2acd759 100644 --- a/src/routes/v3/blockbook.js +++ b/src/routes/v3/blockbook.js @@ -10,6 +10,12 @@ const axios = require("axios") const routeUtils = require("./route-utils") const wlogger = require("../../util/winston-logging") +// Library for easily switching the API paths to use different instances of +// Blockbook. +const BlockbookPath = require("../../util/blockbook-path") +const BLOCKBOOKPATH = new BlockbookPath() +// BLOCKBOOKPATH.toOpenBazaar() + const router = express.Router() // Used for processing error messages before sending them to the user. @@ -44,7 +50,8 @@ async function balanceFromBlockbook(thisAddress) { // Convert the address to a cashaddr without a prefix. const addr = bchjs.Address.toCashAddress(thisAddress) - const path = `${process.env.BLOCKBOOK_URL}api/v2/address/${addr}` + const path = `${BLOCKBOOKPATH.addrPath}${addr}` + // console.log(`path: ${path}`) // Query the Blockbook Node API. const axiosResponse = await axios.get(path) @@ -211,7 +218,8 @@ async function utxosFromBlockbook(thisAddress) { // Convert the address to a cashaddr without a prefix. const addr = bchjs.Address.toCashAddress(thisAddress) - const path = `${process.env.BLOCKBOOK_URL}api/v2/utxo/${addr}` + const path = `${BLOCKBOOKPATH.utxoPath}${addr}` + // console.log(`path: ${path}`) // Query the Blockbook API. const axiosResponse = await axios.get(path) @@ -375,7 +383,8 @@ async function transactionsFromBlockbook(txid) { try { //console.log(`BLOCKBOOK_URL: ${BLOCKBOOK_URL}`) - const path = `${process.env.BLOCKBOOK_URL}api/v2/tx/${txid}` + const path = `${BLOCKBOOKPATH.txPath}${txid}` + // console.log(`path: ${path}`) // Query the Blockbook Node API. const axiosResponse = await axios.get(path) diff --git a/src/util/blockbook-path.js b/src/util/blockbook-path.js new file mode 100644 index 0000000..60ff0ce --- /dev/null +++ b/src/util/blockbook-path.js @@ -0,0 +1,40 @@ +/* + A utility library that allows bch-api to easily switch from a local or default + installation of Blockbook to the public Blockbook cloud service offered by + OpenBazaar. This will be expanded to other Blockbook indexer services. + + CT 9/29/19 - Switching to OpenBazaar does not work because the integration tests + use testnet and I don't think OpenBazaar has a testnet version of their BCH + Blockbook. +*/ + +"use strict" + +class BlockbookPath { + constructor() { + // defaults + this.addrPath = `${process.env.BLOCKBOOK_URL}api/v2/address/` + this.utxoPath = `${process.env.BLOCKBOOK_URL}api/v2/utxo/` + this.txPath = `${process.env.BLOCKBOOK_URL}api/v2/tx/` + } + + toOpenBazaar() { + if (process.env.NETWORK === "testnet") { + this.addrPath = `https://tbch.blockbook.api.openbazaar.org/api/address/` + this.utxoPath = `https://tbch.blockbook.api.openbazaar.org/api/utxo/` + this.txPath = `https://tbch.blockbook.api.openbazaar.org/api/tx/` + } else { + this.addrPath = `https://bch.blockbook.api.openbazaar.org/api/address/` + this.utxoPath = `https://bch.blockbook.api.openbazaar.org/api/utxo/` + this.txPath = `https://bch.blockbook.api.openbazaar.org/api/tx/` + } + } + + toDefault() { + this.addrPath = `${process.env.BLOCKBOOK_URL}api/v2/address/` + this.utxoPath = `${process.env.BLOCKBOOK_URL}api/v2/utxo/` + this.txPath = `${process.env.BLOCKBOOK_URL}api/v2/tx/` + } +} + +module.exports = BlockbookPath