fix(blockbook-path): Added blockbook-path util lib

This commit is contained in:
Chris Troutner
2019-09-29 12:08:13 -07:00
parent 161b9419b2
commit eb78e91946
2 changed files with 52 additions and 3 deletions
+12 -3
View File
@@ -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)
+40
View File
@@ -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