From 8d2d49ce0662452f8c73d9f1410b5a586fa3ee30 Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Thu, 27 Jan 2022 13:14:43 +0500 Subject: [PATCH] '0x' strings returned by contract are incorrectly interpreted as nulls (#4730) * :bug: Fix decoding bug for the "0x" string * :art: Update the validation check * :art: Improve the condition to be more readable * :memo: Update change log doc --- CHANGELOG.md | 1 + packages/web3-eth-abi/src/index.js | 7 ++++++- packages/web3-eth-abi/types/tests/abi-coder-test.ts | 6 ++++++ test/abi.decodeParameter.js | 3 +++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2ec938d5d7..bac119c625f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -507,6 +507,7 @@ Released with 1.0.0-beta.37 code base. - Updated README to include Webpack 5 create-react-app support instructions (#4173) - Update the documentation for `methods.myMethod.estimateGas` (#4702) - Fix typos in REVIEW.md and TESTING.md (#4691) +- Fix encoding for "0x" string values (#4512) ### Changed diff --git a/packages/web3-eth-abi/src/index.js b/packages/web3-eth-abi/src/index.js index 63728ac567c..8eaebdc35d2 100644 --- a/packages/web3-eth-abi/src/index.js +++ b/packages/web3-eth-abi/src/index.js @@ -362,7 +362,12 @@ ABICoder.prototype.decodeParametersWith = function (outputs, bytes, loose) { outputs.forEach(function (output, i) { var decodedValue = res[returnValue.__length__]; - decodedValue = (decodedValue === '0x') ? null : decodedValue; + + const isStringObject = typeof output === 'object' && output.type && output.type === 'string'; + const isStringType = typeof output === 'string' && output === 'string'; + + // only convert `0x` to null if it's not string value + decodedValue = (decodedValue === '0x' && !isStringObject && !isStringType) ? null : decodedValue; returnValue[i] = decodedValue; diff --git a/packages/web3-eth-abi/types/tests/abi-coder-test.ts b/packages/web3-eth-abi/types/tests/abi-coder-test.ts index 0ed89e58c0a..677741041be 100644 --- a/packages/web3-eth-abi/types/tests/abi-coder-test.ts +++ b/packages/web3-eth-abi/types/tests/abi-coder-test.ts @@ -327,6 +327,12 @@ abiCoder.decodeParameter( 0000000000000000000000004e` ); +// $ExpectType { [key: string]: any; } +abiCoder.decodeParameter( + 'string', + '0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000023078000000000000000000000000000000000000000000000000000000000000' +); + // $ExpectError abiCoder.decodeParameter('uint256', [345]); // $ExpectError diff --git a/test/abi.decodeParameter.js b/test/abi.decodeParameter.js index 6c09e2f2ed8..0e9c79cfa7b 100644 --- a/test/abi.decodeParameter.js +++ b/test/abi.decodeParameter.js @@ -635,6 +635,9 @@ describe('lib/solidity/coder', function () { '0000000000000000000000000000000000000000000000000000000000000060' + '0000000000000000000000000000000000000000000000000000000000000006' + '737472696e670000000000000000000000000000000000000000000000000000'}) + + test( { types: ['string'], expected: ["0x"], + values: '0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000023078000000000000000000000000000000000000000000000000000000000000'}) }); });