From 1f01e6ae47f293e244cd60d5f34ee1aa68db5e9f Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 6 Dec 2020 18:40:14 -0800 Subject: [PATCH] fix(tokenUtxoDetails): Catching and invalidating malformed SLP OP_RETURNs --- package.json | 2 +- src/slp/utils.js | 16 ++++++++++++---- test/unit/slp-utils.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index de0f794..8bb0ba7 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "test:temp": "RESTURL=https://bchn.fullstack.cash/v4/ mocha --timeout 30000 -g '#validateTxid' test/integration/bchn/", "test:temp2": "RESTURL=https://abc.fullstack.cash/v4/ mocha --timeout 30000 -g '#validateTxid' test/integration/", "test:temp3": "ISBCHN=true RESTURL=http://localhost:3000/v4/ mocha --timeout 30000 -g '#validateTxid' test/integration/bchn/", - "test:temp4": "mocha --trace-warnings --unhandled-rejections=strict -g '#SLP Utils' test/unit/", + "test:temp4": "mocha --timeout=30000 -g '#tokenUtxoDetails' test/unit/", "coverage": "nyc report --reporter=text-lcov | coveralls", "coverage:report": "nyc --reporter=html mocha --timeout 25000 test/unit/", "docs": "./node_modules/.bin/apidoc -i src/ -o docs" diff --git a/src/slp/utils.js b/src/slp/utils.js index dffd5b7..a6aea56 100644 --- a/src/slp/utils.js +++ b/src/slp/utils.js @@ -1111,9 +1111,9 @@ class Utils { let slpData = false try { slpData = await this.decodeOpReturn(utxo.txid, decodeOpReturnCache) - // console.log(`slpData: ${JSON.stringify(slpData, null, 2)}`) + console.log(`slpData: ${JSON.stringify(slpData, null, 2)}`) } catch (err) { - // console.log(`error from decodeOpReturn(${utxo.txid}): `, err) + console.log(`error from decodeOpReturn(${utxo.txid}): `, err) // An error will be thrown if the txid is not SLP. // If error is for some other reason, like a 429 error, mark utxo as 'null' @@ -1121,9 +1121,13 @@ class Utils { if ( !err.message || (err.message.indexOf("scriptpubkey not op_return") === -1 && - err.message.indexOf("lokad id") === -1) + err.message.indexOf("lokad id") === -1 && + err.message.indexOf("trailing data") === -1) ) { - // console.log(`error from decodeOpReturn(${utxo.txid}): `, err) + console.log( + `unknown error from decodeOpReturn(). Marking as 'null'`, + err + ) utxo.isValid = null outAry.push(utxo) @@ -1132,8 +1136,10 @@ class Utils { // an SLP UTXO. // Mark as false and continue the loop. } else { + console.log("marking as invalid") utxo.isValid = false outAry.push(utxo) + console.log(`outAry: ${JSON.stringify(outAry, null, 2)}`) } // Halt the execution of the loop and increase to the next index. @@ -1336,6 +1342,8 @@ class Utils { } } + console.log(`pt2 outAry: ${JSON.stringify(outAry, null, 2)}`) + return outAry } catch (error) { if (error.response && error.response.data) throw error.response.data diff --git a/test/unit/slp-utils.js b/test/unit/slp-utils.js index f55707a..7f32c10 100644 --- a/test/unit/slp-utils.js +++ b/test/unit/slp-utils.js @@ -1673,7 +1673,43 @@ describe("#SLP Utils", () => { assert.equal(data[0].isValid, false) }) + it("should invalidate a malformed SLP OP_RETURN", async () => { + sandbox + .stub(uut.Utils, "decodeOpReturn") + .rejects(new Error("trailing data")) + + const utxos = [ + // Malformed SLP tx + { + note: "Malformed SLP tx", + tx_hash: + "f7e5199ef6669ad4d078093b3ad56e355b6ab84567e59ad0f08a5ad0244f783a", + tx_pos: 1, + value: 546 + } + ] + + const data = await uut.Utils.tokenUtxoDetails(utxos) + // console.log(`data: ${JSON.stringify(data, null, 2)}`) + + assert.equal(data[0].isValid, false) + }) + + // it("should validate against the whitelist SLPDB when regular SLPDB returns null", async () => { + // const utxos = [ + // // Malformed SLP tx + // { + // note: "Malformed SLP tx", + // tx_hash: + // "f7e5199ef6669ad4d078093b3ad56e355b6ab84567e59ad0f08a5ad0244f783a", + // tx_pos: 1, + // value: 546 + // } + // ] // + // const data = await uut.Utils.tokenUtxoDetails(utxos) + // console.log(`data: ${JSON.stringify(data, null, 2)}`) + // }) }) describe("#txDetails", () => {