Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when a string param is empty in data then decodeLog throws "The parameter "0x" must be a valid HEX string." #1044

Closed
szerintedmi opened this issue Sep 15, 2017 · 6 comments
Labels
Bug Addressing a bug Discussion

Comments

@szerintedmi
Copy link

web3@1.0.0-beta.20

It seems web3.eth.abi.decodeLog doesn't handle cases when a string data in event log is empty, it throws:

Uncaught Error: The parameter "0x" must be a valid HEX string.
    at Object.hexToUtf8 (utils.js:186)
    at SolidityTypeString.formatOutputString [as _outputFormatter] (formatters.js:209)
    at type.js:246
    at SolidityTypeString../node_modules/web3/packages/web3-eth-abi/src/types/type.js.SolidityType.decode (type.js:247)
    at index.js:322
    at Array.forEach (<anonymous>)
    at ABICoder../node_modules/web3/packages/web3-eth-abi/src/index.js.ABICoder.decodeParameters (index.js:321)
    at ABICoder../node_modules/web3/packages/web3-eth-abi/src/index.js.ABICoder.decodeLog (index.js:363)
    at abi-decoder.js:181
    at Array.map (<anonymous>)
    at Object.decodeLogs (abi-decoder.js:119)
    at XMLHttpRequest.xhr.onreadystatechange (ethHelper.js:150)

To reproduce, based on
Tx1 (param "narrative" is "test x" in data) and Tx2 (param "narrative" is "" in data)

let _inputs = [
                { indexed: true, name: "from", type: "address" },
                { indexed: true, name: "to", type: "address" },
                { indexed: false, name: "amount", type: "uint256" },
                { indexed: false, name: "narrative", type: "string" }
            ];
            let _topics = [
                "0x000000000000000000000000ae653250b4220835050b75d3bc91433246903a95",
                "0x00000000000000000000000094011c67bc1e6448ed4b8682047358ca6cd09470"
            ];

            let _logDataTx1 =
                "0x0000000000000000000000000000000000000000000000000000000000002710000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000067465737420780000000000000000000000000000000000000000000000000000";
            let _logDataTx2 =
                "0x000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000";

            let _decodedDataTx1 = web3.eth.abi.decodeLog(
                _inputs,
                _logDataTx1,
                _topics
            );
            console.log("Decoded data Tx1:", _decodedDataTx1);
// ^^^^ output is correct, last param is there ^^^^
            let _decodedDataTx2 = web3.eth.abi.decodeLog(
                _inputs,
                _logDataTx2,
                _topics
            );
// ^^^^  Throws: Uncaught Error: The parameter "0x" must be a valid HEX string. ^^^^ 
            console.log("Decoded data Tx2:", _decodedDataTx2);
@szerintedmi szerintedmi changed the title when a string param is empty in data decodeLog throws "The parameter "0x" must be a valid HEX string." when a string param is empty in data then decodeLog throws "The parameter "0x" must be a valid HEX string." Sep 15, 2017
@frozeman frozeman added the Bug Addressing a bug label Sep 20, 2017
@frozeman
Copy link
Contributor

The current 1.0 branch will result in the following:

>             web3.eth.abi.decodeLog(_inputs,_logDataTx1,_topics);
Result {
  '0': '0xae653250B4220835050B75D3bC91433246903A95',
  '1': '0x94011c67BC1E6448ed4b8682047358ca6cD09470',
  '2': '10000',
  '3': 'test x',
  __length__: 4,
  from: '0xae653250B4220835050B75D3bC91433246903A95',
  to: '0x94011c67BC1E6448ed4b8682047358ca6cD09470',
  amount: '10000',
  narrative: 'test x' }
>
>             web3.eth.abi.decodeLog(_inputs,_logDataTx2,_topics);
Result {
  '0': '0xae653250B4220835050B75D3bC91433246903A95',
  '1': '0x94011c67BC1E6448ed4b8682047358ca6cD09470',
  '2': '10000',
  __length__: 4,
  from: '0xae653250B4220835050B75D3bC91433246903A95',
  to: '0x94011c67BC1E6448ed4b8682047358ca6cD09470',
  amount: '10000',
  narrative: undefined }

I would have to dig deeper, to figure out how i can make it return an empty string here, as we use the same coder also for call return values etc.

@szerintedmi
Copy link
Author

szerintedmi commented Sep 20, 2017

Great, thanks. Is it a released branch? If so I will test it.
I'm already happy with undefined - that would make it possible to get rid of the current ugly, complex & non-generic workaround we had to add :)

@frozeman
Copy link
Contributor

I can make a beta release now.

@frozeman
Copy link
Contributor

will be as follows:

>             web3.eth.abi.decodeLog(_inputs,_logDataTx1,_topics);
Result {
  '0': '0xae653250B4220835050B75D3bC91433246903A95',
  '1': '0x94011c67BC1E6448ed4b8682047358ca6cD09470',
  '2': '10000',
  '3': 'test x',
  __length__: 4,
  from: '0xae653250B4220835050B75D3bC91433246903A95',
  to: '0x94011c67BC1E6448ed4b8682047358ca6cD09470',
  amount: '10000',
  narrative: 'test x' }
>
>             web3.eth.abi.decodeLog(_inputs,_logDataTx2,_topics);
Result {
  '0': '0xae653250B4220835050B75D3bC91433246903A95',
  '1': '0x94011c67BC1E6448ed4b8682047358ca6cD09470',
  '2': '10000',
  '3': '',
  __length__: 4,
  from: '0xae653250B4220835050B75D3bC91433246903A95',
  to: '0x94011c67BC1E6448ed4b8682047358ca6cD09470',
  amount: '10000',
  narrative: '' }

frozeman added a commit that referenced this issue Sep 20, 2017
@frozeman
Copy link
Contributor

released beta 21

@szerintedmi
Copy link
Author

szerintedmi commented Sep 20, 2017

works fine with 1.0.0-beta.21, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Addressing a bug Discussion
Projects
None yet
Development

No branches or pull requests

2 participants