mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-22 01:02:05 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9825ec1bab | ||
|
|
fb61cfeca3 | ||
|
|
5f9a635ab5 | ||
|
|
41c5504846 | ||
|
|
e5f9455fbb |
+3
-1
@@ -46,6 +46,7 @@ const ControlV5 = require('./routes/v5/full-node/control')
|
||||
const MiningV5 = require('./routes/v5/full-node/mining')
|
||||
const networkV5 = require('./routes/v5/full-node/network')
|
||||
const RawtransactionsV5 = require('./routes/v5/full-node/rawtransactions')
|
||||
const DSProofV5 = require('./routes/v5/full-node/dsproof')
|
||||
const UtilV5 = require('./routes/v5/util')
|
||||
const SlpV5 = require('./routes/v5/slp')
|
||||
const xpubV5 = require('./routes/v5/xpub')
|
||||
@@ -78,7 +79,7 @@ const electrumxv5 = new ElectrumXV5()
|
||||
const encryptionv5 = new EncryptionV5()
|
||||
const pricev5 = new PriceV5()
|
||||
const utilV5 = new UtilV5({ electrumx: electrumxv5 })
|
||||
|
||||
const dsproofV5 = new DSProofV5()
|
||||
const app = express()
|
||||
|
||||
app.locals.env = process.env
|
||||
@@ -187,6 +188,7 @@ app.use(`/${v5prefix}/` + 'electrumx', electrumxv5.router)
|
||||
app.use(`/${v5prefix}/` + 'encryption', encryptionv5.router)
|
||||
app.use(`/${v5prefix}/` + 'price', pricev5.router)
|
||||
app.use(`/${v5prefix}/` + 'util', utilV5.router)
|
||||
app.use(`/${v5prefix}/` + 'dsproof', dsproofV5.router)
|
||||
|
||||
// const ninsight = new Ninsight()
|
||||
app.use(`/${v5prefix}/` + 'ninsight', ninsight.router)
|
||||
|
||||
+113
-135
@@ -59,8 +59,8 @@ class Electrum {
|
||||
this.router.post('/block/headers', this.blockHeadersBulk)
|
||||
this.router.get('/transactions/:address', this.getTransactions)
|
||||
this.router.post('/transactions', this.transactionsBulk)
|
||||
// this.router.get('/unconfirmed/:address', this.getMempool)
|
||||
// this.router.post('/unconfirmed', this.mempoolBulk)
|
||||
this.router.get('/unconfirmed/:address', this.getMempool)
|
||||
this.router.post('/unconfirmed', this.mempoolBulk)
|
||||
|
||||
_this = this
|
||||
}
|
||||
@@ -950,63 +950,53 @@ class Electrum {
|
||||
*
|
||||
*/
|
||||
// GET handler for single balance
|
||||
// async getMempool (req, res, next) {
|
||||
// try {
|
||||
// const address = req.params.address
|
||||
//
|
||||
// // Reject if address is an array.
|
||||
// if (Array.isArray(address)) {
|
||||
// res.status(400)
|
||||
// return res.json({
|
||||
// success: false,
|
||||
// error: 'address can not be an array. Use POST for bulk upload.'
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// // Ensure the address is in cash address format.
|
||||
// const cashAddr = _this.bchjs.Address.toCashAddress(address)
|
||||
//
|
||||
// // Prevent a common user error. Ensure they are using the correct network address.
|
||||
// const networkIsValid = _this.routeUtils.validateNetwork(cashAddr)
|
||||
// if (!networkIsValid) {
|
||||
// res.status(400)
|
||||
// return res.json({
|
||||
// success: false,
|
||||
// error:
|
||||
// 'Invalid network. Trying to use a testnet address on mainnet, or vice versa.'
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// wlogger.debug(
|
||||
// 'Executing electrumx/getMempool with this address: ',
|
||||
// cashAddr
|
||||
// )
|
||||
//
|
||||
// // Get data from ElectrumX server.
|
||||
// const electrumResponse = await _this._mempoolFromElectrumx(cashAddr)
|
||||
// // console.log(`_mempoolFromElectrumx(): ${JSON.stringify(electrumResponse, null, 2)}`)
|
||||
//
|
||||
// // Pass the error message if ElectrumX reports an error.
|
||||
// if (Object.prototype.hasOwnProperty.call(electrumResponse, 'code')) {
|
||||
// res.status(400)
|
||||
// return res.json({
|
||||
// success: false,
|
||||
// message: electrumResponse.message
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// res.status(200)
|
||||
// return res.json({
|
||||
// success: true,
|
||||
// utxos: electrumResponse
|
||||
// })
|
||||
// } catch (err) {
|
||||
// // Write out error to error log.
|
||||
// wlogger.error('Error in elecrumx.js/getMempool().', err)
|
||||
//
|
||||
// return _this.errorHandler(err, res)
|
||||
// }
|
||||
// }
|
||||
async getMempool (req, res, next) {
|
||||
try {
|
||||
const address = req.params.address
|
||||
|
||||
// Reject if address is an array.
|
||||
if (Array.isArray(address)) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
success: false,
|
||||
error: 'address can not be an array. Use POST for bulk upload.'
|
||||
})
|
||||
}
|
||||
|
||||
// Ensure the address is in cash address format.
|
||||
const cashAddr = _this.bchjs.Address.toCashAddress(address)
|
||||
|
||||
// Prevent a common user error. Ensure they are using the correct network address.
|
||||
const networkIsValid = _this.routeUtils.validateNetwork(cashAddr)
|
||||
if (!networkIsValid) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
success: false,
|
||||
error:
|
||||
'Invalid network. Trying to use a testnet address on mainnet, or vice versa.'
|
||||
})
|
||||
}
|
||||
|
||||
wlogger.debug(
|
||||
'Executing electrumx/getMempool with this address: ',
|
||||
cashAddr
|
||||
)
|
||||
|
||||
// Get data from ElectrumX server.
|
||||
const response = await _this.axios.get(
|
||||
`${_this.fulcrumApi}electrumx/unconfirmed/${address}`
|
||||
)
|
||||
// console.log('response', response, _this.fulcrumApi)
|
||||
|
||||
res.status(200)
|
||||
return res.json(response.data)
|
||||
} catch (err) {
|
||||
// Write out error to error log.
|
||||
wlogger.error('Error in elecrumx.js/getMempool().', err)
|
||||
|
||||
return _this.errorHandler(err, res)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {post} /electrumx/unconfirmed Get unconfirmed utxos for an array of addresses.
|
||||
@@ -1021,82 +1011,70 @@ class Electrum {
|
||||
*
|
||||
*/
|
||||
// POST handler for bulk queries on address details
|
||||
// async mempoolBulk (req, res, next) {
|
||||
// try {
|
||||
// let addresses = req.body.addresses
|
||||
//
|
||||
// // Reject if addresses is not an array.
|
||||
// if (!Array.isArray(addresses)) {
|
||||
// res.status(400)
|
||||
// return res.json({
|
||||
// error: 'addresses needs to be an array. Use GET for single address.'
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// // Enforce array size rate limits
|
||||
// if (!_this.routeUtils.validateArraySize(req, addresses)) {
|
||||
// res.status(400) // https://github.com/Bitcoin-com/rest.bitcoin.com/issues/330
|
||||
// return res.json({
|
||||
// error: 'Array too large.'
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// wlogger.debug(
|
||||
// 'Executing electrumx.js/mempoolBulk with these addresses: ',
|
||||
// addresses
|
||||
// )
|
||||
//
|
||||
// // Validate each element in the address array.
|
||||
// for (let i = 0; i < addresses.length; i++) {
|
||||
// const thisAddress = addresses[i]
|
||||
//
|
||||
// // Ensure the input is a valid BCH address.
|
||||
// try {
|
||||
// _this.bchjs.Address.toLegacyAddress(thisAddress)
|
||||
// } catch (err) {
|
||||
// res.status(400)
|
||||
// return res.json({
|
||||
// error: `Invalid BCH address. Double check your address is valid: ${thisAddress}`
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// // Prevent a common user error. Ensure they are using the correct network address.
|
||||
// const networkIsValid = _this.routeUtils.validateNetwork(thisAddress)
|
||||
// if (!networkIsValid) {
|
||||
// res.status(400)
|
||||
// return res.json({
|
||||
// error: `Invalid network for address ${thisAddress}. Trying to use a testnet address on mainnet, or vice versa.`
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Loops through each address and creates an array of Promises, querying
|
||||
// // Insight API in parallel.
|
||||
// addresses = addresses.map(async (address, index) => {
|
||||
// // console.log(`address: ${address}`)
|
||||
// const utxos = await _this._mempoolFromElectrumx(address)
|
||||
//
|
||||
// return {
|
||||
// utxos,
|
||||
// address
|
||||
// }
|
||||
// })
|
||||
//
|
||||
// // Wait for all parallel Insight requests to return.
|
||||
// const result = await Promise.all(addresses)
|
||||
//
|
||||
// // Return the array of retrieved address information.
|
||||
// res.status(200)
|
||||
// return res.json({
|
||||
// success: true,
|
||||
// utxos: result
|
||||
// })
|
||||
// } catch (err) {
|
||||
// wlogger.error('Error in electrumx.js/mempoolBulk().', err)
|
||||
//
|
||||
// return _this.errorHandler(err, res)
|
||||
// }
|
||||
// }
|
||||
async mempoolBulk (req, res, next) {
|
||||
try {
|
||||
const addresses = req.body.addresses
|
||||
|
||||
// Reject if addresses is not an array.
|
||||
if (!Array.isArray(addresses)) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
success: false,
|
||||
error: 'addresses needs to be an array. Use GET for single address.'
|
||||
})
|
||||
}
|
||||
|
||||
// Enforce array size rate limits
|
||||
if (!_this.routeUtils.validateArraySize(req, addresses)) {
|
||||
res.status(400) // https://github.com/Bitcoin-com/rest.bitcoin.com/issues/330
|
||||
return res.json({
|
||||
success: false,
|
||||
error: 'Array too large.'
|
||||
})
|
||||
}
|
||||
|
||||
wlogger.debug(
|
||||
'Executing electrumx.js/mempoolBulk with these addresses: ',
|
||||
addresses
|
||||
)
|
||||
|
||||
// Validate each element in the address array.
|
||||
for (let i = 0; i < addresses.length; i++) {
|
||||
const thisAddress = addresses[i]
|
||||
|
||||
// Ensure the input is a valid BCH address.
|
||||
try {
|
||||
_this.bchjs.Address.toLegacyAddress(thisAddress)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
success: false,
|
||||
error: `Invalid BCH address. Double check your address is valid: ${thisAddress}`
|
||||
})
|
||||
}
|
||||
|
||||
// Prevent a common user error. Ensure they are using the correct network address.
|
||||
const networkIsValid = _this.routeUtils.validateNetwork(thisAddress)
|
||||
if (!networkIsValid) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
success: false,
|
||||
error: `Invalid network for address ${thisAddress}. Trying to use a testnet address on mainnet, or vice versa.`
|
||||
})
|
||||
}
|
||||
}
|
||||
const response = await _this.axios.post(
|
||||
`${_this.fulcrumApi}electrumx/unconfirmed/`,
|
||||
{ addresses }
|
||||
)
|
||||
res.status(200)
|
||||
return res.json(response.data)
|
||||
} catch (err) {
|
||||
wlogger.error('Error in electrumx.js/mempoolBulk().', err)
|
||||
|
||||
return _this.errorHandler(err, res)
|
||||
}
|
||||
}
|
||||
|
||||
// DRY error handler.
|
||||
errorHandler (err, res) {
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
'use strict'
|
||||
|
||||
const express = require('express')
|
||||
const router = express.Router()
|
||||
|
||||
const axios = require('axios')
|
||||
const wlogger = require('../../../util/winston-logging')
|
||||
|
||||
const RouteUtils = require('../../../util/route-utils')
|
||||
const routeUtils = new RouteUtils()
|
||||
|
||||
// Used to convert error messages to strings, to safely pass to users.
|
||||
const util = require('util')
|
||||
util.inspect.defaultOptions = { depth: 1 }
|
||||
|
||||
let _this
|
||||
class DSProof {
|
||||
constructor () {
|
||||
_this = this
|
||||
_this.axios = axios
|
||||
_this.routeUtils = routeUtils
|
||||
|
||||
_this.router = router
|
||||
_this.router.get('/', this.root)
|
||||
_this.router.get('/getdsproof/:txid', _this.getDSProof)
|
||||
}
|
||||
|
||||
// DRY error handler.
|
||||
errorHandler (err, res) {
|
||||
// Attempt to decode the error message.
|
||||
const { msg, status } = _this.routeUtils.decodeError(err)
|
||||
if (msg) {
|
||||
res.status(status)
|
||||
return res.json({ error: msg })
|
||||
}
|
||||
|
||||
res.status(500)
|
||||
return res.json({ error: util.inspect(err) })
|
||||
}
|
||||
|
||||
root (req, res, next) {
|
||||
return res.json({ status: 'dsproof' })
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {get} /dsproof/getdsproof/:txid Get Double-Spend Proof.
|
||||
* @apiName DS Proof.
|
||||
* @apiGroup DSProof
|
||||
* @apiDescription Get information for a double-spend proof.
|
||||
*
|
||||
*
|
||||
* @apiExample Example usage:
|
||||
* curl -X GET "https://api.fullstack.cash/v5/dsproof/getdsproof/a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d" -H "accept: application/json"
|
||||
*
|
||||
*
|
||||
*/
|
||||
async getDSProof (req, res, next) {
|
||||
try {
|
||||
const txid = req.params.txid
|
||||
|
||||
let verbose = 2 // default
|
||||
if (req.query.verbose === 'true') verbose = 3
|
||||
|
||||
if (!txid || txid === '') {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
success: false,
|
||||
error: 'txid can not be empty'
|
||||
})
|
||||
}
|
||||
|
||||
if (txid.length !== 64) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
success: false,
|
||||
error: `txid must be of length 64 (not ${txid.length})`
|
||||
})
|
||||
}
|
||||
|
||||
const options = _this.routeUtils.getAxiosOptions()
|
||||
options.data.id = 'getdsproof'
|
||||
options.data.method = 'getdsproof'
|
||||
options.data.params = [txid, verbose]
|
||||
|
||||
const response = await _this.axios.request(options)
|
||||
return res.json(response.data.result)
|
||||
} catch (err) {
|
||||
wlogger.error('Error in dsproof.js/getDSProof().', err)
|
||||
|
||||
return _this.errorHandler(err, res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DSProof
|
||||
@@ -1347,6 +1347,246 @@ describe('#Electrumx', () => {
|
||||
assert.property(result.transactions[0].transactions[0], 'tx_hash')
|
||||
})
|
||||
})
|
||||
describe('#getMempool', () => {
|
||||
it('should throw 400 if address is empty', async () => {
|
||||
const result = await electrumxRoute.getMempool(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 422, 'Expect 422 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Unsupported address format')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should throw 400 on array input', async () => {
|
||||
req.params.address = ['qzs02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c']
|
||||
|
||||
const result = await electrumxRoute.getMempool(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'address can not be an array')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should throw an error for an invalid address', async () => {
|
||||
req.params.address = '02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c'
|
||||
|
||||
const result = await electrumxRoute.getMempool(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 422, 'Expect 422 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Unsupported address format')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should detect a network mismatch', async () => {
|
||||
req.params.address = 'bchtest:qq89kjkeqz9mngp8kl3dpmu43y2wztdjqu500gn4c4'
|
||||
|
||||
const result = await electrumxRoute.getMempool(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Invalid network', 'Proper error message')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should pass errors from electrum-cash to user', async () => {
|
||||
// Address has invalid checksum.
|
||||
req.params.address =
|
||||
'bitcoincash:qr69kyzha07dcecrsvjwsj4s6slnlq4r8c30lxnur2'
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.getMempool(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Unsupported address format')
|
||||
})
|
||||
|
||||
it('should get mempool for a single address', async () => {
|
||||
req.params.address =
|
||||
'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
|
||||
|
||||
// Mock unit tests to prevent live network calls.
|
||||
if (process.env.TEST === 'unit') {
|
||||
electrumxRoute.isReady = true // Force flag.
|
||||
|
||||
sandbox
|
||||
.stub(electrumxRoute.axios, 'get')
|
||||
.resolves({ data: { success: true, utxos: mockData.utxos } })
|
||||
}
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.getMempool(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, true)
|
||||
|
||||
assert.property(result, 'utxos')
|
||||
assert.isArray(result.utxos)
|
||||
})
|
||||
})
|
||||
describe('#mempoolBulk', () => {
|
||||
it('should throw 400 if addresses is empty', async () => {
|
||||
const result = await electrumxRoute.mempoolBulk(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'addresses needs to be an array')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should throw 400 if input provided is not array', async () => {
|
||||
req.body.addresses = 'qzs02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c'
|
||||
|
||||
const result = await electrumxRoute.mempoolBulk(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'addresses needs to be an array')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should throw 400 error if addresses array is too large', async () => {
|
||||
const testArray = []
|
||||
for (var i = 0; i < 25; i++) testArray.push('')
|
||||
|
||||
req.body.addresses = testArray
|
||||
|
||||
const result = await electrumxRoute.mempoolBulk(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Array too large')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should throw an error for an invalid address', async () => {
|
||||
req.body.addresses = ['02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c']
|
||||
|
||||
const result = await electrumxRoute.mempoolBulk(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Invalid BCH address')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should detect a network mismatch', async () => {
|
||||
req.body.addresses = [
|
||||
'bchtest:qq89kjkeqz9mngp8kl3dpmu43y2wztdjqu500gn4c4'
|
||||
]
|
||||
|
||||
const result = await electrumxRoute.mempoolBulk(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Invalid network', 'Proper error message')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should pass errors from electrum-cash to user', async () => {
|
||||
// Address has invalid checksum.
|
||||
req.body.addresses = [
|
||||
'bitcoincash:qr69kyzha07dcecrsvjwsj4s6slnlq4r8c30lxnur2'
|
||||
]
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.mempoolBulk(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Invalid BCH address')
|
||||
})
|
||||
it('should handle error', async () => {
|
||||
req.body.addresses = [
|
||||
'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7',
|
||||
'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
|
||||
]
|
||||
// Force error
|
||||
sandbox.stub(electrumxRoute.axios, 'post').throws(new Error('Test error'))
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.mempoolBulk(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error, 'Test error')
|
||||
})
|
||||
it('should get mempool for multiple addresses', async () => {
|
||||
req.body.addresses = [
|
||||
'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7',
|
||||
'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
|
||||
]
|
||||
|
||||
// Mock unit tests to prevent live network calls.
|
||||
if (process.env.TEST === 'unit') {
|
||||
electrumxRoute.isReady = true // Force flag.
|
||||
|
||||
sandbox
|
||||
.stub(electrumxRoute.axios, 'post')
|
||||
.resolves({ data: mockData.utxosArray })
|
||||
}
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.mempoolBulk(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, true)
|
||||
|
||||
assert.property(result, 'utxos')
|
||||
assert.property(result.utxos[0], 'utxos')
|
||||
assert.property(result.utxos[0], 'address')
|
||||
|
||||
assert.isArray(result.utxos[0].utxos)
|
||||
assert.isString(result.utxos[0].address)
|
||||
})
|
||||
})
|
||||
|
||||
// describe('#_utxosFromElectrumx', () => {
|
||||
// it('should throw error for invalid address', async () => {
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
/*
|
||||
TESTS FOR THE DSPROOF.JS LIBRARY
|
||||
|
||||
This test file uses the environment variable TEST to switch between unit
|
||||
and integration tests. By default, TEST is set to 'unit'. Set this variable
|
||||
to 'integration' to run the tests against BCH mainnet.
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
const assert = chai.assert
|
||||
const DSProofRoute = require('../../src/routes/v5/full-node/dsproof')
|
||||
const uut = new DSProofRoute()
|
||||
|
||||
// const nock = require('nock') // HTTP mocking
|
||||
const sinon = require('sinon')
|
||||
|
||||
let originalEnvVars // Used during transition from integration to unit tests.
|
||||
|
||||
// Mocking data.
|
||||
const { mockReq, mockRes } = require('./mocks/express-mocks')
|
||||
const mockData = require('./mocks/dsproof-mocks')
|
||||
|
||||
// Used for debugging.
|
||||
const util = require('util')
|
||||
util.inspect.defaultOptions = { depth: 1 }
|
||||
|
||||
describe('#DSProof', () => {
|
||||
let req, res
|
||||
let sandbox
|
||||
before(() => {
|
||||
// Save existing environment variables.
|
||||
originalEnvVars = {
|
||||
BITCOINCOM_BASEURL: process.env.BITCOINCOM_BASEURL,
|
||||
RPC_BASEURL: process.env.RPC_BASEURL,
|
||||
RPC_USERNAME: process.env.RPC_USERNAME,
|
||||
RPC_PASSWORD: process.env.RPC_PASSWORD
|
||||
}
|
||||
|
||||
// Set default environment variables for unit tests.
|
||||
if (!process.env.TEST) process.env.TEST = 'unit'
|
||||
if (process.env.TEST === 'unit') {
|
||||
process.env.BITCOINCOM_BASEURL = 'http://fakeurl/api/'
|
||||
process.env.RPC_BASEURL = 'http://fakeurl/api'
|
||||
process.env.RPC_USERNAME = 'fakeusername'
|
||||
process.env.RPC_PASSWORD = 'fakepassword'
|
||||
}
|
||||
})
|
||||
|
||||
// Setup the mocks before each test.
|
||||
beforeEach(() => {
|
||||
// Mock the req and res objects used by Express routes.
|
||||
req = mockReq
|
||||
res = mockRes
|
||||
|
||||
// Explicitly reset the parmas and body.
|
||||
req.params = {}
|
||||
req.body = {}
|
||||
req.query = {}
|
||||
|
||||
sandbox = sinon.createSandbox()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
// Restore Sandbox
|
||||
sandbox.restore()
|
||||
})
|
||||
|
||||
after(() => {
|
||||
// Restore any pre-existing environment variables.
|
||||
process.env.BITCOINCOM_BASEURL = originalEnvVars.BITCOINCOM_BASEURL
|
||||
process.env.RPC_BASEURL = originalEnvVars.RPC_BASEURL
|
||||
process.env.RPC_USERNAME = originalEnvVars.RPC_USERNAME
|
||||
process.env.RPC_PASSWORD = originalEnvVars.RPC_PASSWORD
|
||||
})
|
||||
|
||||
describe('#root', async () => {
|
||||
// root route handler.
|
||||
it('should respond to GET for base route', async () => {
|
||||
const result = uut.root(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(result.status, 'dsproof', 'Returns static string')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getDSProof', async () => {
|
||||
it('should throw 400 error if txid is missing', async () => {
|
||||
const result = await uut.getDSProof(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.hasAllKeys(result, ['error', 'success'])
|
||||
assert.isFalse(result.success)
|
||||
assert.include(result.error, 'txid can not be empty')
|
||||
})
|
||||
it('should throw 400 error if txid is invalid', async () => {
|
||||
req.params.txid = 'abc123'
|
||||
|
||||
const result = await uut.getDSProof(req, res)
|
||||
|
||||
assert.hasAllKeys(result, ['error', 'success'])
|
||||
assert.isFalse(result.success)
|
||||
|
||||
assert.equal(res.statusCode, 400, 'HTTP status code 400 expected.')
|
||||
assert.include(result.error, 'txid must be of length 64')
|
||||
})
|
||||
it('should throw 503 when network issues', async () => {
|
||||
req.params.txid =
|
||||
'ee0df780b58f6f24467605b2589c44c3a50fc849fb8f91b89669a4ae0d86bc7e'
|
||||
|
||||
// Save the existing RPC URL.
|
||||
const savedUrl2 = process.env.RPC_BASEURL
|
||||
|
||||
// Manipulate the URL to cause a 500 network error.
|
||||
process.env.RPC_BASEURL = 'http://fakeurl/api/'
|
||||
|
||||
await uut.getDSProof(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
// Restore the saved URL.
|
||||
process.env.RPC_BASEURL = savedUrl2
|
||||
|
||||
assert.isAbove(
|
||||
res.statusCode,
|
||||
499,
|
||||
'HTTP status code 500 or greater expected.'
|
||||
)
|
||||
// assert.include(result.error,"Network error: Could not communicate with full node","Error message expected")
|
||||
})
|
||||
it('returns proper error when downstream service stalls', async () => {
|
||||
req.params.txid =
|
||||
'ee0df780b58f6f24467605b2589c44c3a50fc849fb8f91b89669a4ae0d86bc7e'
|
||||
// Mock the timeout error.
|
||||
sandbox.stub(uut.axios, 'request').throws({ code: 'ECONNABORTED' })
|
||||
|
||||
const result = await uut.getDSProof(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.isAbove(res.statusCode, 499, 'HTTP status code 503 expected.')
|
||||
assert.include(
|
||||
result.error,
|
||||
'Could not communicate with full node',
|
||||
'Error message expected'
|
||||
)
|
||||
})
|
||||
|
||||
it('returns proper error when downstream service is down', async () => {
|
||||
req.params.txid =
|
||||
'ee0df780b58f6f24467605b2589c44c3a50fc849fb8f91b89669a4ae0d86bc7e'
|
||||
// Mock the timeout error.
|
||||
sandbox.stub(uut.axios, 'request').throws({ code: 'ECONNREFUSED' })
|
||||
|
||||
const result = await uut.getDSProof(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.isAbove(res.statusCode, 499, 'HTTP status code 503 expected.')
|
||||
assert.include(
|
||||
result.error,
|
||||
'Could not communicate with full node',
|
||||
'Error message expected'
|
||||
)
|
||||
})
|
||||
|
||||
it('should GET double-spend proof', async function () {
|
||||
req.params.txid =
|
||||
'ee0df780b58f6f24467605b2589c44c3a50fc849fb8f91b89669a4ae0d86bc7e'
|
||||
// Mock the RPC call for unit tests.
|
||||
if (process.env.TEST === 'unit') {
|
||||
sandbox
|
||||
.stub(uut.axios, 'request')
|
||||
.resolves({ data: { result: mockData.mockGetDsProof } })
|
||||
} else {
|
||||
return this.skip()
|
||||
}
|
||||
|
||||
const result = await uut.getDSProof(req, res)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.property(result, 'dspid')
|
||||
assert.property(result, 'txid')
|
||||
assert.property(result, 'outpoint')
|
||||
assert.property(result, 'path')
|
||||
assert.property(result, 'descendants')
|
||||
|
||||
assert.property(result.outpoint, 'txid')
|
||||
assert.property(result.outpoint, 'vout')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
This library contains mocking data for running unit tests on the dsproof route.
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
const mockGetDsProof = {
|
||||
dspid: '6234fff2a9dbcaa50e2c50d74e1d725a2bed1757d7a05a2db7a82df659554397',
|
||||
txid: 'ee0df780b58f6f24467605b2589c44c3a50fc849fb8f91b89669a4ae0d86bc7e',
|
||||
outpoint: {
|
||||
txid: '17ebef34f7e9a0692317dd6fbb43ffccff45b7d688a2a754dbccfc5e6d93ce3e',
|
||||
vout: 1
|
||||
},
|
||||
path: ['ee0df780b58f6f24467605b2589c44c3a50fc849fb8f91b89669a4ae0d86bc7e'],
|
||||
descendants: [
|
||||
'ee0df780b58f6f24467605b2589c44c3a50fc849fb8f91b89669a4ae0d86bc7e'
|
||||
]
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
mockGetDsProof
|
||||
}
|
||||
Reference in New Issue
Block a user