From efd669e5fe5fe394dc9b97f5696058bde8b9894f Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Tue, 6 Oct 2020 13:15:53 -0700 Subject: [PATCH] fix(hydrateUtxos): Adding support for a corner case --- package.json | 2 +- src/routes/v3/slp.js | 8 ++++++++ test/v3/slp.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 38b7c23..ae22554 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "test": "npm run lint && npm run test-v3", "lint": "standard --env mocha --fix", "test-v3": "export NETWORK=mainnet && nyc --reporter=text mocha --timeout 60000 test/v3/", - "test:temp": "export NETWORK=mainnet && mocha --timeout 25000 test/v3/util.js", + "test:temp": "export NETWORK=mainnet && mocha --timeout 25000 test/v3/slp.js", "test:integration": "mocha test/v3/integration", "test:integration:slpdb": "mocha --timeout 25000 test/v3/integration/slp*.js", "coverage": "nyc report --reporter=text-lcov | coveralls", diff --git a/src/routes/v3/slp.js b/src/routes/v3/slp.js index accd8c8..8452722 100644 --- a/src/routes/v3/slp.js +++ b/src/routes/v3/slp.js @@ -1570,6 +1570,13 @@ class Slp { }) } + if (!utxos[0].utxos) { + res.status(422) + return res.json({ + error: 'Each element in array should have a utxos property' + }) + } + // Loop through each address and query the UTXOs for that element. for (let i = 0; i < utxos.length; i++) { const theseUtxos = utxos[i].utxos @@ -1586,6 +1593,7 @@ class Slp { return res.json({ slpUtxos: utxos }) } catch (err) { wlogger.error('Error in slp.js/hydrateUtxos().', err) + console.error('Error in slp.js/hydrateUtxos().', err) // Decode the error message. const { msg, status } = routeUtils.decodeError(err) diff --git a/test/v3/slp.js b/test/v3/slp.js index 4c1d517..47d5537 100644 --- a/test/v3/slp.js +++ b/test/v3/slp.js @@ -1034,6 +1034,34 @@ describe('#SLP', () => { assert.property(result.slpUtxos[0].utxos[1], 'tokenType') assert.property(result.slpUtxos[0].utxos[1], 'tokenQty') }) + + it('should throw error for missing properties', async () => { + const utxos = [ + { + height: 639443, + tx_hash: + '30707fffb9b295a06a68d217f49c198e9e1dbe1edc3874a0928ca1905f1709df', + tx_pos: 0, + value: 6000 + }, + { + height: 639443, + tx_hash: + '8962566e413501224d178a02effc89be5ac0d8e4195f617415d443dc4c38fe50', + tx_pos: 1, + value: 546 + } + ] + + req.body.utxos = utxos + const result = await slpRoute.hydrateUtxos(req, res) + + assert.hasAllKeys(result, ['error']) + assert.include( + result.error, + 'Each element in array should have a utxos property' + ) + }) }) })