mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-22 09:12:05 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
87c2faa6af | ||
|
|
10a840f48d | ||
|
|
61f9531046 | ||
|
|
1ab408a757 | ||
|
|
84df2be3b8 | ||
|
|
17376d9dba |
+2
-2
@@ -24,8 +24,8 @@
|
||||
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
||||
"coverage:report": "export NETWORK=mainnet && nyc --reporter=html mocha --timeout 25000 test/v4/",
|
||||
"docs": "./node_modules/.bin/apidoc -i src/routes/v4 -o docs",
|
||||
"test:temp1": "export NETWORK=mainnet && export TEST=integration && mocha -g '#balanceBulk' --exit --timeout 30000 test/v5/",
|
||||
"test:temp2": "export NETWORK=mainnet && mocha -g '#balanceBulk' --exit --timeout 30000 test/v5/"
|
||||
"test:temp1": "export NETWORK=mainnet && export TEST=integration && mocha -g '#utxosBulk' --exit --timeout 30000 test/v5/",
|
||||
"test:temp2": "export NETWORK=mainnet && mocha -g '#utxosBulk' --exit --timeout 30000 test/v5/"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.15.1"
|
||||
|
||||
+308
-391
@@ -50,13 +50,13 @@ class Electrum {
|
||||
this.router.get('/', this.root)
|
||||
this.router.get('/balance/:address', this.getBalance)
|
||||
this.router.post('/balance', this.balanceBulk)
|
||||
// this.router.get('/utxos/:address', this.getUtxos)
|
||||
// this.router.post('/utxos', this.utxosBulk)
|
||||
// this.router.get('/tx/data/:txid', this.getTransactionDetails)
|
||||
// this.router.post('/tx/data', this.transactionDetailsBulk)
|
||||
// this.router.post('/tx/broadcast', this.broadcastTransaction)
|
||||
// this.router.get('/block/headers/:height', this.getBlockHeaders)
|
||||
// this.router.post('/block/headers', this.blockHeadersBulk)
|
||||
this.router.get('/utxos/:address', this.getUtxos)
|
||||
this.router.post('/utxos', this.utxosBulk)
|
||||
this.router.get('/tx/data/:txid', this.getTransactionDetails)
|
||||
this.router.post('/tx/data', this.transactionDetailsBulk)
|
||||
this.router.post('/tx/broadcast', this.broadcastTransaction)
|
||||
this.router.get('/block/headers/:height', this.getBlockHeaders)
|
||||
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)
|
||||
@@ -249,62 +249,52 @@ class Electrum {
|
||||
*
|
||||
*/
|
||||
// GET handler for single balance
|
||||
// async getUtxos (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.'
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// 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/getUtxos with this address: ',
|
||||
// cashAddr
|
||||
// )
|
||||
//
|
||||
// // Get data from ElectrumX server.
|
||||
// const electrumResponse = await _this._utxosFromElectrumx(cashAddr)
|
||||
// // console.log(`_utxosFromElectrumx(): ${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/getUtxos().', err)
|
||||
//
|
||||
// return _this.errorHandler(err, res)
|
||||
// }
|
||||
// }
|
||||
async getUtxos (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.'
|
||||
})
|
||||
}
|
||||
|
||||
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/getUtxos with this address: ',
|
||||
cashAddr
|
||||
)
|
||||
|
||||
// Get data from ElectrumX server.
|
||||
const response = await _this.axios.get(
|
||||
`${_this.fulcrumApi}electrumx/utxos/${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/getUtxos().', err)
|
||||
|
||||
return _this.errorHandler(err, res)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {post} /electrumx/utxo Get utxos for an array of addresses.
|
||||
@@ -319,83 +309,73 @@ class Electrum {
|
||||
*
|
||||
*/
|
||||
// POST handler for bulk queries on address details
|
||||
// async utxosBulk (req, res, next) {
|
||||
// try {
|
||||
// let addresses = req.body.addresses
|
||||
// // const currentPage = req.body.page ? parseInt(req.body.page, 10) : 0
|
||||
//
|
||||
// // 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/utxoBulk 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._utxosFromElectrumx(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/utxoBulk().', err)
|
||||
//
|
||||
// return _this.errorHandler(err, res)
|
||||
// }
|
||||
// }
|
||||
async utxosBulk (req, res, next) {
|
||||
try {
|
||||
const addresses = req.body.addresses
|
||||
// const currentPage = req.body.page ? parseInt(req.body.page, 10) : 0
|
||||
|
||||
// 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/utxoBulk 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/utxos/`,
|
||||
{ addresses }
|
||||
)
|
||||
|
||||
res.status(200)
|
||||
return res.json(response.data)
|
||||
} catch (err) {
|
||||
wlogger.error('Error in electrumx.js/utxoBulk().', err)
|
||||
|
||||
return _this.errorHandler(err, res)
|
||||
}
|
||||
}
|
||||
|
||||
// Returns a promise that resolves to transaction details data for a txid.
|
||||
// Expects input to be a txid string, and input validation to have already
|
||||
@@ -443,53 +423,40 @@ class Electrum {
|
||||
*
|
||||
*/
|
||||
// GET handler for single transaction
|
||||
// async getTransactionDetails (req, res, next) {
|
||||
// try {
|
||||
// const txid = req.params.txid
|
||||
// const verbose = req.query.verbose
|
||||
//
|
||||
// // Reject if txid is anything other than a string
|
||||
// if (typeof txid !== 'string') {
|
||||
// res.status(400)
|
||||
// return res.json({
|
||||
// success: false,
|
||||
// error: 'txid must be a string'
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// wlogger.debug(
|
||||
// 'Executing electrumx/getTransactionDetails with this txid: ',
|
||||
// txid
|
||||
// )
|
||||
//
|
||||
// // Get data from ElectrumX server.
|
||||
// const electrumResponse = await _this._transactionDetailsFromElectrum(
|
||||
// txid,
|
||||
// verbose
|
||||
// )
|
||||
// // console.log(`_transactionDetailsFromElectrum(): ${JSON.stringify(electrumResponse, null, 2)}`)
|
||||
//
|
||||
// // Pass the error message if ElectrumX reports an error.
|
||||
// if (electrumResponse instanceof Error) {
|
||||
// res.status(400)
|
||||
// return res.json({
|
||||
// success: false,
|
||||
// error: electrumResponse.message
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// res.status(200)
|
||||
// return res.json({
|
||||
// success: true,
|
||||
// details: electrumResponse
|
||||
// })
|
||||
// } catch (err) {
|
||||
// // Write out error to error log.
|
||||
// wlogger.error('Error in elecrumx.js/getTransactionDetails().', err)
|
||||
//
|
||||
// return _this.errorHandler(err, res)
|
||||
// }
|
||||
// }
|
||||
async getTransactionDetails (req, res, next) {
|
||||
try {
|
||||
const txid = req.params.txid
|
||||
// const verbose = req.query.verbose
|
||||
|
||||
// Reject if txid is anything other than a string
|
||||
if (typeof txid !== 'string') {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
success: false,
|
||||
error: 'txid must be a string'
|
||||
})
|
||||
}
|
||||
|
||||
wlogger.debug(
|
||||
'Executing electrumx/getTransactionDetails with this txid: ',
|
||||
txid
|
||||
)
|
||||
|
||||
// Get data from ElectrumX server.
|
||||
const response = await _this.axios.get(
|
||||
`${_this.fulcrumApi}electrumx/tx/data/${txid}`
|
||||
)
|
||||
// console.log(`_transactionDetailsFromElectrum(): ${JSON.stringify(electrumResponse, null, 2)}`)
|
||||
|
||||
res.status(200)
|
||||
return res.json(response.data)
|
||||
} catch (err) {
|
||||
// Write out error to error log.
|
||||
wlogger.error('Error in elecrumx.js/getTransactionDetails().', err)
|
||||
|
||||
return _this.errorHandler(err, res)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {post} /electrumx/tx/data Get transaction details for an array of TXIDs
|
||||
@@ -504,61 +471,47 @@ class Electrum {
|
||||
*
|
||||
*/
|
||||
// POST handler for bulk queries on transaction details
|
||||
// async transactionDetailsBulk (req, res, next) {
|
||||
// try {
|
||||
// const txids = req.body.txids
|
||||
// const verbose = req.body.verbose || true
|
||||
//
|
||||
// // Reject if txids is not an array.
|
||||
// if (!Array.isArray(txids)) {
|
||||
// res.status(400)
|
||||
// return res.json({
|
||||
// success: false,
|
||||
// error: 'txids needs to be an array. Use GET for single txid.'
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// // Enforce array size rate limits
|
||||
// if (!_this.routeUtils.validateArraySize(req, txids)) {
|
||||
// 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/transactionDetailsBulk with these txids: ',
|
||||
// txids
|
||||
// )
|
||||
//
|
||||
// // Loops through each address and creates an array of Promises, querying
|
||||
// // the Electrum server in parallel.
|
||||
// const transactions = txids.map(async (txid, index) => {
|
||||
// // console.log(`address: ${address}`)
|
||||
// const details = await _this._transactionDetailsFromElectrum(
|
||||
// txid,
|
||||
// verbose
|
||||
// )
|
||||
//
|
||||
// return { details, txid }
|
||||
// })
|
||||
//
|
||||
// // Wait for all parallel Electrum requests to return.
|
||||
// const result = await Promise.all(transactions)
|
||||
//
|
||||
// // Return the array of retrieved transaction details.
|
||||
// res.status(200)
|
||||
// return res.json({
|
||||
// success: true,
|
||||
// transactions: result
|
||||
// })
|
||||
// } catch (err) {
|
||||
// wlogger.error('Error in electrumx.js/transactionDetailsBulk().', err)
|
||||
//
|
||||
// return _this.errorHandler(err, res)
|
||||
// }
|
||||
// }
|
||||
async transactionDetailsBulk (req, res, next) {
|
||||
try {
|
||||
const txids = req.body.txids
|
||||
const verbose = req.body.verbose || true
|
||||
// Reject if txids is not an array.
|
||||
if (!Array.isArray(txids)) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
success: false,
|
||||
error: 'txids needs to be an array. Use GET for single txid.'
|
||||
})
|
||||
}
|
||||
|
||||
// Enforce array size rate limits
|
||||
if (!_this.routeUtils.validateArraySize(req, txids)) {
|
||||
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/transactionDetailsBulk with these txids: ',
|
||||
txids
|
||||
)
|
||||
|
||||
const response = await _this.axios.post(
|
||||
`${_this.fulcrumApi}electrumx/tx/data`,
|
||||
{ txids, verbose }
|
||||
)
|
||||
|
||||
// Return the array of retrieved transaction details.
|
||||
res.status(200)
|
||||
return res.json(response.data)
|
||||
} catch (err) {
|
||||
wlogger.error('Error in electrumx.js/transactionDetailsBulk().', err)
|
||||
|
||||
return _this.errorHandler(err, res)
|
||||
}
|
||||
}
|
||||
|
||||
// Returns a promise that resolves to transaction ID of the broadcasted transaction or an error.
|
||||
// Expects input to be a txHex string, and input validation to have already
|
||||
@@ -604,47 +557,36 @@ class Electrum {
|
||||
*
|
||||
*/
|
||||
// POST handler for broadcasting a single transaction
|
||||
// async broadcastTransaction (req, res, next) {
|
||||
// try {
|
||||
// const txHex = req.body.txHex
|
||||
//
|
||||
// if (typeof txHex !== 'string') {
|
||||
// res.status(400)
|
||||
// return res.json({
|
||||
// success: false,
|
||||
// error: 'request body must be a string.'
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// wlogger.debug(
|
||||
// 'Executing electrumx/broadcastTransaction with this tx hex: ',
|
||||
// txHex
|
||||
// )
|
||||
//
|
||||
// // Get data from ElectrumX server.
|
||||
// const electrumResponse = await _this._broadcastTransactionWithElectrum(txHex)
|
||||
// // console.log(`_utxosFromElectrumx(): ${JSON.stringify(electrumResponse, null, 2)}`)
|
||||
//
|
||||
// // Pass the error message if ElectrumX reports an error.
|
||||
// if (electrumResponse instanceof Error) {
|
||||
// res.status(400)
|
||||
// return res.json({
|
||||
// success: false,
|
||||
// error: electrumResponse.message
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// res.status(200)
|
||||
// return res.json({
|
||||
// success: true,
|
||||
// txid: electrumResponse
|
||||
// })
|
||||
// } catch (err) {
|
||||
// wlogger.error('Error in electrumx.js/broadcastTransaction().', err)
|
||||
//
|
||||
// return _this.errorHandler(err, res)
|
||||
// }
|
||||
// }
|
||||
async broadcastTransaction (req, res, next) {
|
||||
try {
|
||||
const txHex = req.body.txHex
|
||||
if (typeof txHex !== 'string') {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
success: false,
|
||||
error: 'request body must be a string.'
|
||||
})
|
||||
}
|
||||
|
||||
wlogger.debug(
|
||||
'Executing electrumx/broadcastTransaction with this tx hex: ',
|
||||
txHex
|
||||
)
|
||||
|
||||
// Get data from ElectrumX server.
|
||||
const response = await _this.axios.post(
|
||||
`${_this.fulcrumApi}electrumx/tx/broadcast`,
|
||||
{ txHex }
|
||||
)
|
||||
|
||||
res.status(200)
|
||||
return res.json(response.data)
|
||||
} catch (err) {
|
||||
wlogger.error('Error in electrumx.js/broadcastTransaction().', err)
|
||||
|
||||
return _this.errorHandler(err, res)
|
||||
}
|
||||
}
|
||||
|
||||
// Returns a promise that resolves to block header data for a block height.
|
||||
// Expects input to be a height number, and input validation to have already
|
||||
@@ -691,63 +633,51 @@ class Electrum {
|
||||
*
|
||||
*
|
||||
* @apiExample Example usage:
|
||||
* curl -X GET "https://api.fullstack.cash/v5/electrumx/block/header/42?count=2" -H "accept: application/json"
|
||||
* curl -X GET "https://api.fullstack.cash/v5/electrumx/block/headers/42?count=2" -H "accept: application/json"
|
||||
*
|
||||
*/
|
||||
// GET handler for single block headers
|
||||
// async getBlockHeaders (req, res, next) {
|
||||
// try {
|
||||
// const height = Number(req.params.height)
|
||||
// const count = req.query.count === undefined ? 1 : Number(req.query.count)
|
||||
//
|
||||
// // Reject if height is not a number
|
||||
// if (Number.isNaN(height) || height < 0) {
|
||||
// res.status(400)
|
||||
// return res.json({
|
||||
// success: false,
|
||||
// error: 'height must be a positive number'
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// // Reject if height is not a number
|
||||
// if (Number.isNaN(count) || count < 0) {
|
||||
// res.status(400)
|
||||
// return res.json({
|
||||
// success: false,
|
||||
// error: 'count must be a positive number'
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// wlogger.debug(
|
||||
// 'Executing electrumx/getBlockHeaders with this height: ',
|
||||
// height
|
||||
// )
|
||||
//
|
||||
// // Get data from ElectrumX server.
|
||||
// const electrumResponse = await _this._blockHeadersFromElectrum(height, count)
|
||||
// // console.log(`_transactionDetailsFromElectrum(): ${JSON.stringify(electrumResponse, null, 2)}`)
|
||||
//
|
||||
// // Pass the error message if ElectrumX reports an error.
|
||||
// if (electrumResponse instanceof Error) {
|
||||
// res.status(400)
|
||||
// return res.json({
|
||||
// success: false,
|
||||
// error: electrumResponse.message
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// res.status(200)
|
||||
// return res.json({
|
||||
// success: true,
|
||||
// headers: electrumResponse
|
||||
// })
|
||||
// } catch (err) {
|
||||
// // Write out error to error log.
|
||||
// wlogger.error('Error in elecrumx.js/getBlockHeader().', err)
|
||||
//
|
||||
// return _this.errorHandler(err, res)
|
||||
// }
|
||||
// }
|
||||
async getBlockHeaders (req, res, next) {
|
||||
try {
|
||||
const height = Number(req.params.height)
|
||||
const count = req.query.count === undefined ? 1 : Number(req.query.count)
|
||||
|
||||
// Reject if height is not a number
|
||||
if (Number.isNaN(height) || height < 0) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
success: false,
|
||||
error: 'height must be a positive number'
|
||||
})
|
||||
}
|
||||
|
||||
// Reject if height is not a number
|
||||
if (Number.isNaN(count) || count < 0) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
success: false,
|
||||
error: 'count must be a positive number'
|
||||
})
|
||||
}
|
||||
|
||||
wlogger.debug(
|
||||
'Executing electrumx/getBlockHeaders with this height: ',
|
||||
height
|
||||
)
|
||||
|
||||
const response = await _this.axios.get(
|
||||
`${_this.fulcrumApi}electrumx/block/headers/${height}?count=${count}`
|
||||
)
|
||||
|
||||
res.status(200)
|
||||
return res.json(response.data)
|
||||
} catch (err) {
|
||||
// Write out error to error log.
|
||||
wlogger.error('Error in elecrumx.js/getBlockHeader().', err)
|
||||
|
||||
return _this.errorHandler(err, res)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {post} /electrumx/block/headers Get block headers for an array of height + count pairs
|
||||
@@ -757,63 +687,50 @@ class Electrum {
|
||||
* Limited to 20 items per request.
|
||||
*
|
||||
* @apiExample Example usage:
|
||||
* curl -X POST "https://api.fullstack.cash/v5/electrumx/block/headers" -H "accept: application/json" -H "Content-Type: application/json" -d '{"heights":[{ "height": 42, count: 2 }, { "height": 100, count: 5 }]}'
|
||||
* curl -X POST "https://api.fullstack.cash/v5/electrumx/block/headers" -H "accept: application/json" -H "Content-Type: application/json" -d '{"heights":[{ "height": 42, "count": 2 }, { "height": 100, "count": 5 }]}'
|
||||
*
|
||||
*/
|
||||
// POST handler for bulk queries on block headers
|
||||
// async blockHeadersBulk (req, res, next) {
|
||||
// try {
|
||||
// const heights = req.body.heights
|
||||
//
|
||||
// // Reject if heights is not an array.
|
||||
// if (!Array.isArray(heights)) {
|
||||
// res.status(400)
|
||||
// return res.json({
|
||||
// success: false,
|
||||
// error: 'heights needs to be an array. Use GET for single height.'
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// // Enforce array size rate limits
|
||||
// if (!_this.routeUtils.validateArraySize(req, heights)) {
|
||||
// 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/blockHeadersBulk with these txids: ',
|
||||
// heights
|
||||
// )
|
||||
//
|
||||
// // Loops through each address and creates an array of Promises, querying
|
||||
// // the Electrum server in parallel.
|
||||
// const transactions = heights.map(async (obj) => {
|
||||
// const headers = await _this._blockHeadersFromElectrum(
|
||||
// obj.height,
|
||||
// obj.count
|
||||
// )
|
||||
//
|
||||
// return { headers }
|
||||
// })
|
||||
//
|
||||
// // Wait for all parallel Electrum requests to return.
|
||||
// const result = await Promise.all(transactions)
|
||||
//
|
||||
// // Return the array of retrieved transaction details.
|
||||
// res.status(200)
|
||||
// return res.json({
|
||||
// success: true,
|
||||
// headers: result
|
||||
// })
|
||||
// } catch (err) {
|
||||
// wlogger.error('Error in electrumx.js/blockHeadersBulk().', err)
|
||||
//
|
||||
// return _this.errorHandler(err, res)
|
||||
// }
|
||||
// }
|
||||
async blockHeadersBulk (req, res, next) {
|
||||
try {
|
||||
const heights = req.body.heights
|
||||
|
||||
// Reject if heights is not an array.
|
||||
if (!Array.isArray(heights)) {
|
||||
res.status(400)
|
||||
return res.json({
|
||||
success: false,
|
||||
error: 'heights needs to be an array. Use GET for single height.'
|
||||
})
|
||||
}
|
||||
|
||||
// Enforce array size rate limits
|
||||
if (!_this.routeUtils.validateArraySize(req, heights)) {
|
||||
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/blockHeadersBulk with these txids: ',
|
||||
heights
|
||||
)
|
||||
|
||||
const response = await _this.axios.post(
|
||||
`${_this.fulcrumApi}electrumx/block/headers`,
|
||||
{ heights }
|
||||
)
|
||||
|
||||
res.status(200)
|
||||
return res.json(response.data)
|
||||
} catch (err) {
|
||||
wlogger.error('Error in electrumx.js/blockHeadersBulk().', err)
|
||||
|
||||
return _this.errorHandler(err, res)
|
||||
}
|
||||
}
|
||||
|
||||
// Returns a promise that resolves an array of transaction history for an
|
||||
// address. Expects input to be a cash address, and input validation to have
|
||||
|
||||
@@ -356,6 +356,752 @@ describe('#Electrumx', () => {
|
||||
assert.property(result.balances[0].balance, 'unconfirmed')
|
||||
})
|
||||
})
|
||||
describe('#getUtxos', () => {
|
||||
it('should throw 400 if address is empty', async () => {
|
||||
const result = await electrumxRoute.getUtxos(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.getUtxos(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.getUtxos(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.getUtxos(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.getUtxos(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 utxos for a single address', async () => {
|
||||
req.params.address =
|
||||
'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
|
||||
|
||||
// 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.getUtxos(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)
|
||||
|
||||
assert.property(result.utxos[0], 'height')
|
||||
assert.property(result.utxos[0], 'tx_hash')
|
||||
assert.property(result.utxos[0], 'tx_pos')
|
||||
assert.property(result.utxos[0], 'value')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#utxosBulk', () => {
|
||||
it('should throw 400 if addresses is empty', async () => {
|
||||
const result = await electrumxRoute.utxosBulk(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.utxosBulk(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.utxosBulk(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.utxosBulk(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.utxosBulk(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.utxosBulk(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.utxosBulk(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 utxos for an array of 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.utxosBulk(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)
|
||||
|
||||
const firtsAddrUtxos = result.utxos[0].utxos[0]
|
||||
assert.property(firtsAddrUtxos, 'height')
|
||||
assert.property(firtsAddrUtxos, 'tx_hash')
|
||||
assert.property(firtsAddrUtxos, 'tx_pos')
|
||||
assert.property(firtsAddrUtxos, 'value')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getTransactionDetails', () => {
|
||||
it('should throw 400 if tx is empty', async () => {
|
||||
const result = await electrumxRoute.getTransactionDetails(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, 'txid must be a string')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should throw 400 on array input', async () => {
|
||||
req.params.txid = [
|
||||
'a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d'
|
||||
]
|
||||
|
||||
const result = await electrumxRoute.getTransactionDetails(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, 'txid must be a string')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should pass errors from electrum-cash to user', async () => {
|
||||
req.params.txid = '02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c'
|
||||
|
||||
const result = await electrumxRoute.getTransactionDetails(req, res)
|
||||
|
||||
assert.property(result, 'error')
|
||||
assert.include(result.error.error, 'Invalid tx hash')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should get details for a single tx', async () => {
|
||||
req.params.txid =
|
||||
'a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d'
|
||||
|
||||
// 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, details: mockData.txDetails } })
|
||||
}
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.getTransactionDetails(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, true)
|
||||
|
||||
assert.property(result, 'details')
|
||||
assert.property(result.details, 'blockhash')
|
||||
assert.property(result.details, 'blocktime')
|
||||
assert.property(result.details, 'confirmations')
|
||||
assert.property(result.details, 'hash')
|
||||
assert.property(result.details, 'hex')
|
||||
assert.property(result.details, 'locktime')
|
||||
assert.property(result.details, 'size')
|
||||
assert.property(result.details, 'time')
|
||||
assert.property(result.details, 'txid')
|
||||
assert.property(result.details, 'version')
|
||||
assert.property(result.details, 'vin')
|
||||
assert.property(result.details, 'vout')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#transactionDetailsBulk', () => {
|
||||
it('should throw 400 if txids is empty', async () => {
|
||||
const result = await electrumxRoute.transactionDetailsBulk(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, 'txids 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.txids =
|
||||
'a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d'
|
||||
|
||||
const result = await electrumxRoute.transactionDetailsBulk(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, 'txids 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.txids = testArray
|
||||
|
||||
const result = await electrumxRoute.transactionDetailsBulk(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 handle error', async () => {
|
||||
req.body.txids = [
|
||||
'a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d',
|
||||
'a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d'
|
||||
]
|
||||
// Force error
|
||||
sandbox.stub(electrumxRoute.axios, 'post').throws(new Error('Test error'))
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.transactionDetailsBulk(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 details for an array of tx', async () => {
|
||||
req.body.txids = [
|
||||
'a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d'
|
||||
]
|
||||
|
||||
// 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.txDetailsBulk })
|
||||
}
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.transactionDetailsBulk(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, true)
|
||||
|
||||
assert.property(result, 'transactions')
|
||||
const tx = result.transactions[0]
|
||||
|
||||
assert.property(tx, 'details')
|
||||
assert.property(tx.details, 'blockhash')
|
||||
assert.property(tx.details, 'blocktime')
|
||||
assert.property(tx.details, 'confirmations')
|
||||
assert.property(tx.details, 'hash')
|
||||
assert.property(tx.details, 'hex')
|
||||
assert.property(tx.details, 'locktime')
|
||||
assert.property(tx.details, 'size')
|
||||
assert.property(tx.details, 'time')
|
||||
assert.property(tx.details, 'txid')
|
||||
assert.property(tx.details, 'version')
|
||||
assert.property(tx.details, 'vin')
|
||||
assert.property(tx.details, 'vout')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#broadcastTransaction', () => {
|
||||
it('should throw 400 if txHex is empty', async () => {
|
||||
const result = await electrumxRoute.broadcastTransaction(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, 'request body must be a string')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should throw 400 on invalid input type', async () => {
|
||||
req.body.txHex = [mockData.txDetails.hex]
|
||||
|
||||
const result = await electrumxRoute.broadcastTransaction(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, 'request body must be a string')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should pass errors from electrum-cash to user', async () => {
|
||||
req.body.txHex = mockData.txDetails.hex.substring(10)
|
||||
const result = await electrumxRoute.broadcastTransaction(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.error, 'the transaction was rejected')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should broadcast transaction', async function () {
|
||||
req.body.txHex = mockData.txDetails.hex
|
||||
|
||||
// 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: { success: true, txid: mockData.txDetails.hash } })
|
||||
} else {
|
||||
return this.skip()
|
||||
}
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.broadcastTransaction(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, true)
|
||||
|
||||
assert.property(result, 'txid')
|
||||
assert.isString(result.txid)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getBlockHeaders', () => {
|
||||
it('should throw 400 if height is empty', async () => {
|
||||
const result = await electrumxRoute.getBlockHeaders(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, 'height must be a positive number')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
it('should throw 400 if height is not a number', async () => {
|
||||
req.params.height = 'wrong type'
|
||||
|
||||
const result = await electrumxRoute.getBlockHeaders(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, 'height must be a positive number')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
it('should throw 400 if height is negative', async () => {
|
||||
req.params.height = -1
|
||||
|
||||
const result = await electrumxRoute.getBlockHeaders(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, 'height must be a positive number')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should throw 400 if count is not a number', async () => {
|
||||
req.params.height = 2
|
||||
req.query.count = 'wrong type'
|
||||
|
||||
const result = await electrumxRoute.getBlockHeaders(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, 'count must be a positive number')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should throw 400 if count is negative', async () => {
|
||||
req.params.height = 2
|
||||
req.query.count = -1
|
||||
|
||||
const result = await electrumxRoute.getBlockHeaders(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, 'count must be a positive number')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should pass errors from electrum-cash to user', async () => {
|
||||
req.params.height = 99999999999999
|
||||
req.query.count = 99999999999999
|
||||
|
||||
// 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: false, error: { error: 'Invalid height' } }
|
||||
})
|
||||
}
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.getBlockHeaders(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.error, 'Invalid height')
|
||||
})
|
||||
it('should handle error', async () => {
|
||||
req.params.height = 42
|
||||
req.query.count = 2
|
||||
// Force error
|
||||
sandbox.stub(electrumxRoute.axios, 'get').throws(new Error('Test error'))
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.getBlockHeaders(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 headers for a single block height with count 2', async () => {
|
||||
req.params.height = 42
|
||||
req.query.count = 2
|
||||
// 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, headers: mockData.blockHeaders }
|
||||
})
|
||||
}
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.getBlockHeaders(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, true)
|
||||
|
||||
assert.property(result, 'headers')
|
||||
assert.isArray(result.headers)
|
||||
assert.deepEqual(result.headers, mockData.blockHeaders)
|
||||
})
|
||||
})
|
||||
describe('#blockHeadersBulk', () => {
|
||||
it('should throw 400 for an empty body', async () => {
|
||||
const result = await electrumxRoute.blockHeadersBulk(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, 'heights needs to be an array')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, false)
|
||||
})
|
||||
|
||||
it('should NOT throw 400 error for an invalid height', async () => {
|
||||
req.body = {
|
||||
heights: [{ height: -10, count: 2 }]
|
||||
}
|
||||
if (process.env.TEST === 'unit') {
|
||||
electrumxRoute.isReady = true // Force flag.
|
||||
|
||||
sandbox.stub(electrumxRoute.axios, 'post').resolves({
|
||||
data: { success: true, headers: [{ headers: {} }] }
|
||||
})
|
||||
}
|
||||
const result = await electrumxRoute.blockHeadersBulk(req, res)
|
||||
console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.equal(res.statusCode, 200, 'Expect 200 status code')
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, true)
|
||||
|
||||
assert.property(result, 'headers')
|
||||
assert.isArray(result.headers)
|
||||
assert.property(result.headers[0], 'headers')
|
||||
})
|
||||
|
||||
it('should throw 400 error if heights array is too large', async () => {
|
||||
const testArray = []
|
||||
for (var i = 0; i < 25; i++) testArray.push('')
|
||||
|
||||
req.body.heights = testArray
|
||||
|
||||
const result = await electrumxRoute.blockHeadersBulk(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 handle error', async () => {
|
||||
req.body = {
|
||||
heights: [
|
||||
{ height: 42, count: 2 },
|
||||
{ height: 42, count: 2 }
|
||||
]
|
||||
}
|
||||
|
||||
// Force error
|
||||
sandbox.stub(electrumxRoute.axios, 'post').throws(new Error('Test error'))
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.blockHeadersBulk(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 block heights', async () => {
|
||||
req.body = {
|
||||
heights: [
|
||||
{ height: 42, count: 2 },
|
||||
{ height: 42, count: 2 }
|
||||
]
|
||||
}
|
||||
|
||||
// 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.blockHeadersBulk })
|
||||
}
|
||||
|
||||
// Call the details API.
|
||||
const result = await electrumxRoute.blockHeadersBulk(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.property(result, 'success')
|
||||
assert.equal(result.success, true)
|
||||
|
||||
assert.property(result, 'headers')
|
||||
assert.isArray(result.headers)
|
||||
assert.property(result.headers[0], 'headers')
|
||||
})
|
||||
})
|
||||
|
||||
// describe('#_utxosFromElectrumx', () => {
|
||||
// it('should throw error for invalid address', async () => {
|
||||
|
||||
@@ -19,6 +19,43 @@ const utxos = [
|
||||
}
|
||||
]
|
||||
|
||||
const utxosArray = {
|
||||
success: true,
|
||||
utxos: [
|
||||
{
|
||||
utxos: [
|
||||
{
|
||||
height: 601861,
|
||||
tx_hash:
|
||||
'6181c669614fa18039a19b23eb06806bfece1f7514ab457c3bb82a40fe171a6d',
|
||||
tx_pos: 0,
|
||||
value: 1000
|
||||
}
|
||||
],
|
||||
address: 'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
|
||||
},
|
||||
{
|
||||
utxos: [
|
||||
{
|
||||
height: 604392,
|
||||
tx_hash:
|
||||
'7774e449c5a3065144cefbc4c0c21e6b69c987f095856778ef9f45ddd8ae1a41',
|
||||
tx_pos: 0,
|
||||
value: 1000
|
||||
},
|
||||
{
|
||||
height: 630834,
|
||||
tx_hash:
|
||||
'4fe60a51e0d8f5134bfd8e5f872d6e502d7f01b28a6afebb27f4438a4f638d53',
|
||||
tx_pos: 0,
|
||||
value: 6000
|
||||
}
|
||||
],
|
||||
address: 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
const balance = {
|
||||
success: true,
|
||||
balance: {
|
||||
@@ -129,17 +166,29 @@ const txDetails = {
|
||||
]
|
||||
}
|
||||
|
||||
const txDetailsBulk = {
|
||||
success: true,
|
||||
transactions: [{ details: txDetails }]
|
||||
}
|
||||
|
||||
const blockHeaders = [
|
||||
'010000008b52bbd72c2f49569059f559c1b1794de5192e4f7d6d2b03c7482bad0000000083e4f8a9d502ed0c419075c1abb5d56f878a2e9079e5612bfb76a2dc37d9c42741dd6849ffff001d2b909dd6',
|
||||
'01000000f528fac1bcb685d0cd6c792320af0300a5ce15d687c7149548904e31000000004e8985a786d864f21e9cbb7cbdf4bc9265fe681b7a0893ac55a8e919ce035c2f85de6849ffff001d385ccb7c'
|
||||
]
|
||||
|
||||
const blockHeadersBulk = {
|
||||
success: true,
|
||||
headers: [{ headers: blockHeaders }, { headers: blockHeaders }]
|
||||
}
|
||||
module.exports = {
|
||||
utxos,
|
||||
utxosArray,
|
||||
balance,
|
||||
txHistory,
|
||||
mempool,
|
||||
txDetails,
|
||||
txDetailsBulk,
|
||||
blockHeaders,
|
||||
blockHeadersBulk,
|
||||
balances
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user