mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-21 16:52:04 -07:00
Merge pull request #146 from Permissionless-Software-Foundation/dh-v5-fulcrum-unconfirmed
feat(unconfirmed): Created v5 Fulcrum /unconfirmed endpoints
This commit is contained in:
+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) {
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in New Issue
Block a user