diff --git a/config/electrumx.js b/config/electrumx.js index 17e9dce..7f6f36e 100644 --- a/config/electrumx.js +++ b/config/electrumx.js @@ -18,6 +18,7 @@ const config = { // 'electron.jochen-hoenicke.de:51002' // ] serverUrl: 'fulcrum.fountainhead.cash', + // serverUrl: 'badurl.com', serverPort: '50002' }, ratelimit: { diff --git a/src/routes/v3/electrumx.js b/src/routes/v3/electrumx.js index 2cef7ac..e8c725b 100644 --- a/src/routes/v3/electrumx.js +++ b/src/routes/v3/electrumx.js @@ -52,12 +52,7 @@ class Electrum { _this.router = router _this.router.get('/', _this.root) - // _this.router.get('/balance/:address', _this.balanceSingle) - // _this.router.post('/balance', _this.balanceBulk) - // _this.router.get('/utxos/:address', _this.utxosSingle) - // _this.router.post('/utxos', _this.utxosBulk) - // _this.router.get('/tx/:txid', _this.txSingle) - // _this.router.post('/tx', _this.txBulk) + _this.router.get('/utxos/:address', _this.getUtxos) } // Initializes a connection to electrum servers. @@ -113,6 +108,13 @@ class Electrum { return res.json({ error: msg }) } + // Handle error patterns specific to this route. + if (err.message) { + res.status(400) + return res.json({ success: false, error: err.message }) + } + + // If error can be handled, return the stack trace res.status(500) return res.json({ error: util.inspect(err) }) } @@ -122,114 +124,36 @@ class Electrum { return res.json({ status: 'electrumx' }) } - // Query the Blockbook Node API for a balance on a single BCH address. - // Returns a Promise. - async balanceFromBlockbook (thisAddress) { - try { - // console.log(`BLOCKBOOK_URL: ${BLOCKBOOK_URL}`) - - // Convert the address to a cashaddr without a prefix. - const addr = _this.bchjs.Address.toCashAddress(thisAddress) - - const path = `${_this.BLOCKBOOKPATH.addrPath}${addr}` - // console.log(`path: ${path}`) - - // Query the Blockbook Node API. - const options = { - method: 'get', - baseURL: path - } - - const axiosResponse = await _this.axios.request(options) - const retData = axiosResponse.data - // console.log(`retData: ${util.inspect(retData)}`) - - return retData - } catch (err) { - // Dev Note: Do not log error messages here. Throw them instead and let the - // parent function handle it. - wlogger.debug('Error in blockbook.js/balanceFromBlockbook()') - throw err - } - } - - /** - * @api {get} /electrumx/balance/{addr} Get balance for a single address. - * @apiName Balance for a single address - * @apiGroup Blockbook - * @apiDescription Returns an object with balance and details about an address. - * - * - * @apiExample Example usage: - * curl -X GET "https://api.fullstack.cash/v3/blockbook/balance/bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf" -H "accept: application/json" - * - */ - // GET handler for single balance - async balanceSingle (req, res, next) { + async getUtxos (req, res, next) { try { const address = req.params.address - if (!address || address === '') { - res.status(400) - return res.json({ error: 'address can not be empty' }) - } - // 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.' }) } - wlogger.debug( - 'Executing blockbook/balanceSingle with this address: ', - address - ) - - // Ensure the input is a valid BCH address. - try { - // const legacyAddr = bchjs.Address.toLegacyAddress(address) - _this.bchjs.Address.toLegacyAddress(address) - } catch (err) { - res.status(400) - return res.json({ - error: `Invalid BCH address. Double check your address is valid: ${address}` - }) - } + 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(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.' }) } - // Query the Blockbook Node API. - const retData = await _this.balanceFromBlockbook(address) - - // Return the retrieved address information. - res.status(200) - return res.json(retData) - } catch (err) { - // Write out error to error log. - wlogger.error('Error in blockbook.js/balanceSingle().', err) - - return _this.errorHandler(err, res) - } - } - - async getUtxos (req, res, next) { - try { - const address = _this.bchjs.Address.toCashAddress(req.params.address) - wlogger.debug('Executing electrumx/getUtxos with this address: ', address) // Convert the address to a scripthash. - const scripthash = _this.addressToScripthash(address) + const scripthash = _this.addressToScripthash(cashAddr) if (!_this.isReady) { throw new Error( diff --git a/test/v3/electrumx.js b/test/v3/electrumx.js index f8eb715..9bb621e 100644 --- a/test/v3/electrumx.js +++ b/test/v3/electrumx.js @@ -106,53 +106,65 @@ describe('#ElectrumX Router', () => { }) describe('#UTXO', () => { - // details route handler. - // const balanceSingle = blockbookRoute.balanceSingle + it('should throw 400 if address is empty', async () => { + const result = await electrumxRoute.getUtxos(req, res) + // console.log(`result: ${util.inspect(result)}`) - // it('should throw 400 if address is empty', async () => { - // const result = await blockbookRoute.balanceSingle(req, res) - // // console.log(`result: ${util.inspect(result)}`) - // - // assert.hasAllKeys(result, ['error']) - // assert.include(result.error, 'address can not be empty') - // }) + assert.equal(res.statusCode, 400, 'Expect 400 status code') - // it('should error on an array', async () => { - // req.params.address = ['qzs02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c'] - // - // const result = await blockbookRoute.balanceSingle(req, res) - // - // assert.equal(res.statusCode, 400, 'HTTP status code 400 expected.') - // assert.include( - // result.error, - // 'address can not be an array', - // 'Proper error message' - // ) - // }) + assert.property(result, 'error') + assert.include(result.error, 'Unsupported address format') - // it('should throw an error for an invalid address', async () => { - // req.params.address = - // '02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c' - // - // const result = await blockbookRoute.balanceSingle(req, res) - // - // assert.equal(res.statusCode, 400, 'HTTP status code 400 expected.') - // assert.include( - // result.error, - // 'Invalid BCH address', - // 'Proper error message' - // ) - // }) + assert.property(result, 'success') + assert.equal(result.success, false) + }) - // it('should detect a network mismatch', async () => { - // req.params.address = - // 'bchtest:qq89kjkeqz9mngp8kl3dpmu43y2wztdjqu500gn4c4' - // - // const result = await blockbookRoute.balanceSingle(req, res) - // - // assert.equal(res.statusCode, 400, 'HTTP status code 400 expected.') - // assert.include(result.error, 'Invalid network', 'Proper error message') - // }) + 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, 400, 'Expect 400 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 throw 500 when network issues', async () => { // const savedUrl = process.env.BLOCKBOOK_URL