mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-22 17:22:04 -07:00
fix(bch-js): Replaced bitbox-js with bch-js in v3 routes
This commit is contained in:
@@ -16,8 +16,8 @@ const router = express.Router()
|
||||
const util = require("util")
|
||||
util.inspect.defaultOptions = { depth: 1 }
|
||||
|
||||
const BITBOXJS = require("@chris.troutner/bitbox-js")
|
||||
const BITBOX = new BITBOXJS()
|
||||
const BCHJS = require("@chris.troutner/bch-js")
|
||||
const bchjs = new BCHJS()
|
||||
|
||||
//const BITCORE_URL = process.env.BITCORE_URL
|
||||
|
||||
@@ -40,7 +40,7 @@ async function balanceFromBitcore(thisAddress) {
|
||||
//console.log(`BITCORE_URL: ${BITCORE_URL}`)
|
||||
|
||||
// Convert the address to a cashaddr without a prefix.
|
||||
const addr = BITBOX.Address.toCashAddress(thisAddress, false)
|
||||
const addr = bchjs.Address.toCashAddress(thisAddress, false)
|
||||
|
||||
// Determine if we are working with the testnet or mainnet networks.
|
||||
let network = "mainnet"
|
||||
@@ -98,7 +98,7 @@ async function balanceSingle(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
const legacyAddr = BITBOX.Address.toLegacyAddress(address)
|
||||
const legacyAddr = bchjs.Address.toLegacyAddress(address)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
@@ -172,7 +172,7 @@ async function balanceBulk(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
BITBOX.Address.toLegacyAddress(thisAddress)
|
||||
bchjs.Address.toLegacyAddress(thisAddress)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
@@ -224,7 +224,7 @@ async function utxosFromBitcore(thisAddress) {
|
||||
//console.log(`BITCORE_URL: ${BITCORE_URL}`)
|
||||
|
||||
// Convert the address to a cashaddr without a prefix.
|
||||
const addr = BITBOX.Address.toCashAddress(thisAddress, false)
|
||||
const addr = bchjs.Address.toCashAddress(thisAddress, false)
|
||||
|
||||
// Determine if we are working with the testnet or mainnet networks.
|
||||
let network = "mainnet"
|
||||
@@ -267,7 +267,7 @@ async function utxosSingle(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
const legacyAddr = BITBOX.Address.toLegacyAddress(address)
|
||||
const legacyAddr = bchjs.Address.toLegacyAddress(address)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
@@ -339,7 +339,7 @@ async function utxosBulk(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
BITBOX.Address.toLegacyAddress(thisAddress)
|
||||
bchjs.Address.toLegacyAddress(thisAddress)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
|
||||
@@ -16,8 +16,8 @@ const router = express.Router()
|
||||
const util = require("util")
|
||||
util.inspect.defaultOptions = { depth: 1 }
|
||||
|
||||
const BITBOXJS = require("@chris.troutner/bitbox-js")
|
||||
const BITBOX = new BITBOXJS()
|
||||
const BCHJS = require("@chris.troutner/bch-js")
|
||||
const bchjs = new BCHJS()
|
||||
|
||||
//const BLOCKBOOK_URL = process.env.BLOCKBOOK_URL
|
||||
|
||||
@@ -40,7 +40,7 @@ async function balanceFromBlockbook(thisAddress) {
|
||||
//console.log(`BLOCKBOOK_URL: ${BLOCKBOOK_URL}`)
|
||||
|
||||
// Convert the address to a cashaddr without a prefix.
|
||||
const addr = BITBOX.Address.toCashAddress(thisAddress)
|
||||
const addr = bchjs.Address.toCashAddress(thisAddress)
|
||||
|
||||
const path = `${process.env.BLOCKBOOK_URL}api/v2/address/${addr}`
|
||||
|
||||
@@ -82,7 +82,7 @@ async function balanceSingle(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
const legacyAddr = BITBOX.Address.toLegacyAddress(address)
|
||||
const legacyAddr = bchjs.Address.toLegacyAddress(address)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
@@ -154,7 +154,7 @@ async function balanceBulk(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
BITBOX.Address.toLegacyAddress(thisAddress)
|
||||
bchjs.Address.toLegacyAddress(thisAddress)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
@@ -207,7 +207,7 @@ async function utxosFromBlockbook(thisAddress) {
|
||||
//console.log(`BLOCKBOOK_URL: ${BLOCKBOOK_URL}`)
|
||||
|
||||
// Convert the address to a cashaddr without a prefix.
|
||||
const addr = BITBOX.Address.toCashAddress(thisAddress)
|
||||
const addr = bchjs.Address.toCashAddress(thisAddress)
|
||||
|
||||
const path = `${process.env.BLOCKBOOK_URL}api/v2/utxo/${addr}`
|
||||
|
||||
@@ -249,7 +249,7 @@ async function utxosSingle(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
const legacyAddr = BITBOX.Address.toLegacyAddress(address)
|
||||
const legacyAddr = bchjs.Address.toLegacyAddress(address)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
@@ -321,7 +321,7 @@ async function utxosBulk(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
BITBOX.Address.toLegacyAddress(thisAddress)
|
||||
bchjs.Address.toLegacyAddress(thisAddress)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
|
||||
@@ -17,8 +17,8 @@ const router = express.Router()
|
||||
const util = require("util")
|
||||
util.inspect.defaultOptions = { depth: 1 }
|
||||
|
||||
const BITBOXJS = require("@chris.troutner/bitbox-js")
|
||||
const BITBOX = new BITBOXJS()
|
||||
const BCHJS = require("@chris.troutner/bch-js")
|
||||
const bchjs = new BCHJS()
|
||||
|
||||
// Use the default (and max) page size of 1000
|
||||
// https://github.com/bitpay/insight-api#notes-on-upgrading-from-v03
|
||||
@@ -51,10 +51,10 @@ async function detailsFromInsight(thisAddress, currentPage = 0) {
|
||||
if (
|
||||
process.env.BITCOINCOM_BASEURL === "https://bch-insight.bitpay.com/api/"
|
||||
)
|
||||
addr = BITBOX.Address.toCashAddress(thisAddress)
|
||||
else addr = BITBOX.Address.toLegacyAddress(thisAddress)
|
||||
addr = bchjs.Address.toCashAddress(thisAddress)
|
||||
else addr = bchjs.Address.toLegacyAddress(thisAddress)
|
||||
*/
|
||||
const addr = BITBOX.Address.toLegacyAddress(thisAddress)
|
||||
const addr = bchjs.Address.toLegacyAddress(thisAddress)
|
||||
|
||||
let path = `${process.env.BITCOINCOM_BASEURL}addr/${addr}`
|
||||
|
||||
@@ -75,8 +75,8 @@ async function detailsFromInsight(thisAddress, currentPage = 0) {
|
||||
const pagesTotal = Math.ceil(retData.txApperances / PAGE_SIZE)
|
||||
|
||||
// Append different address formats to the return data.
|
||||
retData.legacyAddress = BITBOX.Address.toLegacyAddress(retData.addrStr)
|
||||
retData.cashAddress = BITBOX.Address.toCashAddress(retData.addrStr)
|
||||
retData.legacyAddress = bchjs.Address.toLegacyAddress(retData.addrStr)
|
||||
retData.cashAddress = bchjs.Address.toCashAddress(retData.addrStr)
|
||||
delete retData.addrStr
|
||||
|
||||
// Append pagination information to the return data.
|
||||
@@ -123,7 +123,7 @@ async function detailsBulk(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
BITBOX.Address.toLegacyAddress(thisAddress)
|
||||
bchjs.Address.toLegacyAddress(thisAddress)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
@@ -195,7 +195,7 @@ async function detailsSingle(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
var legacyAddr = BITBOX.Address.toLegacyAddress(address)
|
||||
var legacyAddr = bchjs.Address.toLegacyAddress(address)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
@@ -242,8 +242,8 @@ async function utxoFromInsight(thisAddress) {
|
||||
if (
|
||||
process.env.BITCOINCOM_BASEURL === "https://bch-insight.bitpay.com/api/"
|
||||
)
|
||||
addr = BITBOX.Address.toCashAddress(thisAddress)
|
||||
else addr = BITBOX.Address.toLegacyAddress(thisAddress)
|
||||
addr = bchjs.Address.toCashAddress(thisAddress)
|
||||
else addr = bchjs.Address.toLegacyAddress(thisAddress)
|
||||
|
||||
const path = `${process.env.BITCOINCOM_BASEURL}addr/${addr}/utxo`
|
||||
|
||||
@@ -261,8 +261,8 @@ async function utxoFromInsight(thisAddress) {
|
||||
const spk = response.data[0].scriptPubKey
|
||||
retData.scriptPubKey = spk
|
||||
}
|
||||
retData.legacyAddress = BITBOX.Address.toLegacyAddress(thisAddress)
|
||||
retData.cashAddress = BITBOX.Address.toCashAddress(thisAddress)
|
||||
retData.legacyAddress = bchjs.Address.toLegacyAddress(thisAddress)
|
||||
retData.cashAddress = bchjs.Address.toCashAddress(thisAddress)
|
||||
retData.utxos = response.data.map(utxo => {
|
||||
delete utxo.address
|
||||
delete utxo.scriptPubKey
|
||||
@@ -303,7 +303,7 @@ async function utxoBulk(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
BITBOX.Address.toLegacyAddress(thisAddress)
|
||||
bchjs.Address.toLegacyAddress(thisAddress)
|
||||
} catch (er) {
|
||||
//if (er.message.includes("Unsupported address format"))
|
||||
res.status(400)
|
||||
@@ -376,7 +376,7 @@ async function utxoSingle(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
var legacyAddr = BITBOX.Address.toLegacyAddress(address)
|
||||
var legacyAddr = bchjs.Address.toLegacyAddress(address)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
@@ -443,7 +443,7 @@ async function unconfirmedBulk(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
BITBOX.Address.toLegacyAddress(thisAddress)
|
||||
bchjs.Address.toLegacyAddress(thisAddress)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
@@ -522,7 +522,7 @@ async function unconfirmedSingle(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
var legacyAddr = BITBOX.Address.toLegacyAddress(address)
|
||||
var legacyAddr = bchjs.Address.toLegacyAddress(address)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
@@ -584,8 +584,8 @@ async function transactionsFromInsight(thisAddress, currentPage = 0) {
|
||||
|
||||
// Append different address formats to the return data.
|
||||
const retData = response.data
|
||||
retData.legacyAddress = BITBOX.Address.toLegacyAddress(thisAddress)
|
||||
retData.cashAddress = BITBOX.Address.toCashAddress(thisAddress)
|
||||
retData.legacyAddress = bchjs.Address.toLegacyAddress(thisAddress)
|
||||
retData.cashAddress = bchjs.Address.toCashAddress(thisAddress)
|
||||
retData.currentPage = currentPage
|
||||
|
||||
return retData
|
||||
@@ -624,7 +624,7 @@ async function transactionsBulk(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
BITBOX.Address.toLegacyAddress(thisAddress)
|
||||
bchjs.Address.toLegacyAddress(thisAddress)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
@@ -696,7 +696,7 @@ async function transactionsSingle(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
var legacyAddr = BITBOX.Address.toLegacyAddress(address)
|
||||
var legacyAddr = bchjs.Address.toLegacyAddress(address)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
@@ -757,8 +757,8 @@ async function fromXPubSingle(req, res, next) {
|
||||
|
||||
wlogger.debug(`Executing address/fromXPub with this xpub: `, xpub)
|
||||
|
||||
const cashAddr = BITBOX.Address.fromXPub(xpub, hdPath)
|
||||
const legacyAddr = BITBOX.Address.toLegacyAddress(cashAddr)
|
||||
const cashAddr = bchjs.Address.fromXPub(xpub, hdPath)
|
||||
const legacyAddr = bchjs.Address.toLegacyAddress(cashAddr)
|
||||
res.status(200)
|
||||
return res.json({
|
||||
cashAddress: cashAddr,
|
||||
|
||||
@@ -17,8 +17,8 @@ const router = express.Router()
|
||||
const util = require("util")
|
||||
util.inspect.defaultOptions = { depth: 1 }
|
||||
|
||||
const BITBOXJS = require("@chris.troutner/bitbox-js")
|
||||
const BITBOX = new BITBOXJS()
|
||||
const BCHJS = require("@chris.troutner/bch-js")
|
||||
const bchjs = new BCHJS()
|
||||
|
||||
//const BITCORE_URL = process.env.BITCORE_URL
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@ const axios = require("axios")
|
||||
const routeUtils = require("../route-utils")
|
||||
const wlogger = require("../../../util/winston-logging")
|
||||
|
||||
const BITBOXJS = require("@chris.troutner/bitbox-js")
|
||||
const BITBOX = new BITBOXJS()
|
||||
const BCHJS = require("@chris.troutner/bch-js")
|
||||
const bchjs = new BCHJS()
|
||||
|
||||
// Used to convert error messages to strings, to safely pass to users.
|
||||
const util = require("util")
|
||||
@@ -23,8 +23,8 @@ const processInputs = tx => {
|
||||
vin.value = vin.valueSat
|
||||
const address = vin.addr
|
||||
if (address) {
|
||||
vin.legacyAddress = BITBOX.Address.toLegacyAddress(address)
|
||||
vin.cashAddress = BITBOX.Address.toCashAddress(address)
|
||||
vin.legacyAddress = bchjs.Address.toLegacyAddress(address)
|
||||
vin.cashAddress = bchjs.Address.toCashAddress(address)
|
||||
delete vin.addr
|
||||
}
|
||||
delete vin.valueSat
|
||||
@@ -43,7 +43,7 @@ const processInputs = tx => {
|
||||
if (vout.scriptPubKey.addresses) {
|
||||
const cashAddrs = []
|
||||
vout.scriptPubKey.addresses.forEach(addr => {
|
||||
const cashAddr = BITBOX.Address.toCashAddress(addr)
|
||||
const cashAddr = bchjs.Address.toCashAddress(addr)
|
||||
cashAddrs.push(cashAddr)
|
||||
})
|
||||
vout.scriptPubKey.cashAddrs = cashAddrs
|
||||
|
||||
@@ -10,8 +10,8 @@ const wlogger = require("../../util/winston-logging")
|
||||
const util = require("util")
|
||||
util.inspect.defaultOptions = { depth: 1 }
|
||||
|
||||
const BITBOXJS = require("@chris.troutner/bitbox-js")
|
||||
const BITBOX = new BITBOXJS()
|
||||
const BCHJS = require("@chris.troutner/bch-js")
|
||||
const bchjs = new BCHJS()
|
||||
|
||||
module.exports = {
|
||||
validateNetwork, // Prevents a common user error
|
||||
@@ -54,14 +54,14 @@ function validateNetwork(addr) {
|
||||
|
||||
// Convert the user-provided address to a cashaddress, for easy detection
|
||||
// of the intended network.
|
||||
const cashAddr = BITBOX.Address.toCashAddress(addr)
|
||||
const cashAddr = bchjs.Address.toCashAddress(addr)
|
||||
|
||||
// Return true if the network and address both match testnet
|
||||
const addrIsTest = BITBOX.Address.isTestnetAddress(cashAddr)
|
||||
const addrIsTest = bchjs.Address.isTestnetAddress(cashAddr)
|
||||
if (network === "testnet" && addrIsTest) return true
|
||||
|
||||
// Return true if the network and address both match mainnet
|
||||
const addrIsMain = BITBOX.Address.isMainnetAddress(cashAddr)
|
||||
const addrIsMain = bchjs.Address.isMainnetAddress(cashAddr)
|
||||
if (network === "mainnet" && addrIsMain) return true
|
||||
|
||||
return false
|
||||
@@ -73,7 +73,7 @@ function validateNetwork(addr) {
|
||||
|
||||
// Dynamically set these based on env vars. Allows unit testing.
|
||||
function setEnvVars() {
|
||||
const BitboxHTTP = axios.create({
|
||||
const BITBOXHTTP = axios.create({
|
||||
baseURL: process.env.RPC_BASEURL
|
||||
})
|
||||
const username = process.env.RPC_USERNAME
|
||||
|
||||
@@ -7,14 +7,14 @@ const axios = require("axios")
|
||||
const routeUtils = require("./route-utils")
|
||||
const wlogger = require("../../util/winston-logging")
|
||||
|
||||
const BITBOXJS = require("@chris.troutner/bitbox-js")
|
||||
const BITBOX = new BITBOXJS()
|
||||
const BCHJS = require("@chris.troutner/bch-js")
|
||||
const bchjs = new BCHJS()
|
||||
|
||||
// Used to convert error messages to strings, to safely pass to users.
|
||||
const util = require("util")
|
||||
util.inspect.defaultOptions = { depth: 1 }
|
||||
|
||||
const BitboxHTTP = axios.create({
|
||||
const bchjsHTTP = axios.create({
|
||||
baseURL: process.env.RPC_BASEURL
|
||||
})
|
||||
|
||||
@@ -103,7 +103,7 @@ async function validateAddressBulk(req, res, next) {
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
var legacyAddr = BITBOX.Address.toLegacyAddress(address)
|
||||
var legacyAddr = bchjs.Address.toLegacyAddress(address)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
|
||||
@@ -17,8 +17,8 @@ const router = express.Router()
|
||||
const util = require("util")
|
||||
util.inspect.defaultOptions = { depth: 1 }
|
||||
|
||||
const BITBOXJS = require("@chris.troutner/bitbox-js")
|
||||
const BITBOX = new BITBOXJS()
|
||||
const BCHJS = require("@chris.troutner/bch-js")
|
||||
const bchjs = new BCHJS()
|
||||
|
||||
// Connect the route endpoints to their handler functions.
|
||||
router.get("/", root)
|
||||
@@ -49,8 +49,8 @@ async function fromXPubSingle(req, res, next) {
|
||||
|
||||
wlogger.debug(`Executing address/fromXPub with this xpub: `, xpub)
|
||||
|
||||
const cashAddr = BITBOX.Address.fromXPub(xpub, hdPath)
|
||||
const legacyAddr = BITBOX.Address.toLegacyAddress(cashAddr)
|
||||
const cashAddr = bchjs.Address.fromXPub(xpub, hdPath)
|
||||
const legacyAddr = bchjs.Address.toLegacyAddress(cashAddr)
|
||||
res.status(200)
|
||||
return res.json({
|
||||
cashAddress: cashAddr,
|
||||
|
||||
Reference in New Issue
Block a user