How to decode input data from a transaction?

  • I'm sending a transaction to an account with some data with the following command:


    I'm getting this result, by extracting the log of the transaction (running with testRPC):

       tx hash         : 0xf654aee5ed23f9aeebd2d73c69c7b9c21a4862787966d09bcb09ed44efc1f252
       nonce           : 0
       blockHash       : 0x6ff8a0e3ac606abd2ede4331b82af52a0daa98448025051fb3b3d50f749aa49f
       blockNumber     : 1
       transactionIndex: 0
       from            : 0xf64918835bc21dff9e8210a807b3964d1be35dd0
       to              : 0x08f986b7535c2b72687d3cb193135f1c6e27c336
       value           : 1000000000000000000
       time            : 1483614904 Thu, 05 Jan 2017 11:15:04 GMT
       gasPrice        : 1
       gas             : 90000
       input           : 0x687474703a2f2f6c6f63616c686f73743a38353435

    I want to decode the last line "input" and try to get "http://localhost:8545". I saw some work similar to my question here and here, but this is not working in my case. Moreover, i tried .toString('hex')) but it remains as hex.

    How can I achieve this?

    Thanks in advance

  • niksmac

    niksmac Correct answer

    4 years ago

    You can do web3.toAscii(transactionID.input) to return the data in readable format.

    Read web3.toAscii

    This works with a transaction send through commandline, but when I send transactions to my contract by using web3 `contract.addBalance.sendTransaction(contractAddress, {from: senderAddress, to: contractAddress, gas:1000000, value: web3.toWei(7, "ether"),data:web3.toHex("http://localhost:8545")})` if I try to get web3.toAscii(transaction.input) it returns some random symbolic values

    @SwapnilKumbhar check your data parameter.

    If you run into encoding issues using `hexToAscii()`, try using `web3.hexToString()`! Worked for me when neither `toAscii` or `toUtf8` were behaving properly.

    depending on the version of web3 you're using, you may encounter the error `web3.toAscii is not a function`. To fix this issue, use `web3.utils.toAscii()` instead

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM