I am working on a small project designed to show and teach people how the blockchain and smart contracts operate. In order to do so I had in mind to showcase the transactions on https://testnet.etherscan.io corresponding to my function calls. What I want to do here is to read the payloadData field from the transaction and to translate it into human readable code (using the ABI I guess) such as: createReferendum("Name"). I am using Node.js. In a nutshell, I want to do the opposite of what the following code does :

const _ = require('lodash')
const SolidityFunction = require('web3/lib/web3/function') 
var closeReferendumsFormat = new SolidityFunction('', _.find(ABI, { name: 'closeReferendums' }), '')   
var payloadData  = closeReferendumsFormat.toPayload([]).data

I have been looking thoroughly but haven't been able to find any way to achieve that. Thank you for your insights!

  • 1
    It's a hash function, so there's no way to do the reverse of that snippet. What you should do is just run through the ABI and make a dictionary with the 4-byte signature as the key and the full name as the value – Tjaden Hess Mar 23 '17 at 19:39

There is no way to go from the 4-byte hashed input back to the function signature programmatically. The only way I've figured to do it is, if you have the ABI already, you can make each function signature canonical (see the yellow paper) and then take the keccak hash of that after removing all spaces. The first four bytes are the encoding. Then just compare the first four bytes of the encoding to each encoded function signature.

A centralized solution (if that works for you) is https://www.4byte.directory, but this only holds functions that people have submitted directly or that come from smart contracts whose ABI is easily found online. I always look for a decentralized solution, but this directory is easy to use and works well.

  • 1
    There is a way to go from hash to signature: you can look it up in function signature database. E.g. 4byte.directory – max taldykin Mar 23 '17 at 19:37
  • 1
    I assumed the op was looking for a decentralized solution. 4byte is totally centralized. You're right though you can use that. – Thomas Jay Rush Mar 24 '17 at 3:47
  • Thank you that was exactly what I needed ( a centralized solution is fine for demonstration purpose) – Alex Triquet Mar 24 '17 at 9:08

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.