Ethers.js: How to decode tx data

Created on 12 Feb 2019  路  3Comments  路  Source: ethers-io/ethers.js

After your explanations in #422 I have also managed to decode the data of a transaction that was created by a contract function call by:

iface = new ethers.utils.Interface(["sendMessage(bytes32, string)"]);
iface.parseTransaction(tx);

Is there also the possibility to decode it with ethers.utils.defaultAbiCoder.decode?

This

ethers.utils.defaultAbiCoder.decode(
    [ 'bytes', 'string' ],
    tx.data
)

does not work.

And an additional question: If I do not know with which function a transaction has been created, How do I parse/decode it at all?

Thanks for your help in advance!

discussion

Most helpful comment

Perfect!

As a matter of form the totally correct code (based on sendMessage(bytes32,string) is:

ethers.utils.defaultAbiCoder.decode(
    [ 'bytes32', 'string' ],
    ethers.utils.hexDataSlice(tx.data, 4)
);

All 3 comments

The first 4 bytes of the data is the function sighash (in this case, hexDataSlice(id("sendMessage(bytes32,string)"), 0, 4)). So, if you have data that has already been assembled, you willed to snip off these 4 bytes:

ethers.utils.defaultAbiCoder.decode(
    [ 'bytes', 'string' ],
    hexDataSlice(tx.data, 4)
)

If you do not know at all what the function was, you can probably make an educated guess by looking at the data; once you've looked at enough call data and know the ABI coding scheme inside and out, you can often guess what the types are, but it takes some time and would be hard to write generic code to attempt this. You would also not be able to figure out the method call name, since it is hashed, unless you made a large rainbow table, and even then you would likely miss many.

So, short answer is that if you do not know what the method is, it is very difficult to figure it out.

Perfect!

As a matter of form the totally correct code (based on sendMessage(bytes32,string) is:

ethers.utils.defaultAbiCoder.decode(
    [ 'bytes32', 'string' ],
    ethers.utils.hexDataSlice(tx.data, 4)
);

Awesome! Thanks! :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pcowgill picture pcowgill  路  3Comments

Neozaru picture Neozaru  路  3Comments

jochenonline picture jochenonline  路  3Comments

zemse picture zemse  路  3Comments

thegostep picture thegostep  路  3Comments