19

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

eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(1,"ether"),data:web3.toHex("http://localhost:8545")})

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

10

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

Read web3.toAscii

  • 4
    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 May 19 '17 at 12:04
  • 1
    @SwapnilKumbhar check your data parameter. – niksmac May 19 '17 at 15:34
  • 1
    If you run into encoding issues using hexToAscii(), try using web3.hexToString()! Worked for me when neither toAscii or toUtf8 were behaving properly. – ohsully Jun 7 at 21:10
11

In order to decode data you can use a library called abi-decoder

You will need to provide an ABI for the smart contract you want to decode, then just paste the input data. Very simple.

The only downfall would happen if your input data happens to be smart-contract creation "Constructor" then, in that case, you will have to change the library to accommodate with that.

5

We've made a nice tool for decoding. Just paste the transaction hash into input and get the result. Check it It works with Mainnet, Kovan, Ropsten and Rinkeby, but contract code must be verified in Etherscan. We use Etherscan API to get transaction and contract data, and web3 for decoding.

  • 3
    It is not recommended to write link only answers. If should be good if you add a little explanation about the page, like what tools/libraries you use for the decoder. – Ismael Jan 29 '18 at 18:46
  • 1
    @WTD It would be nice if it also accepted a raw transaction (signed and unsigned) as input, not only a tx hash. – Thorkil Værge Apr 11 '18 at 12:18
  • We just updated our tool. Now it is easy to check the data(input and output) of all calls that execute during the transaction. – WTD Oct 16 '18 at 17:34
3

A full definition of what you're trying to decode (the input data field) is here: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI. It's tough-going, but I was able to write some C++ that fully parses function arguments as recorded in the transactions. For your particular question, decoding the hex is correct, but for the more general case of the input data representing the function call and its parameters you have to decode the interface described in this document.

  • 4
    Your C++ doesn't happen to be open source does it? – Joël Jan 23 '17 at 8:18
  • 1
    Wow. It took me a year to answer, but "yes", it is open source (it wasn't in January). More information is here: quickblocks.io. – Thomas Jay Rush Dec 21 '17 at 0:54
2

This JavaScript/Node.js library can decode smart contract input data and contract creation input data given the JSON abi:

https://github.com/miguelmota/ethereum-input-data-decoder

0

In python, this is done using decode_function_input Contract method:

contract.decode_function_input(transaction.input)

see:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.