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

How to decode tx data #423

Closed
jochenonline opened this issue Feb 12, 2019 · 5 comments
Closed

How to decode tx data #423

jochenonline opened this issue Feb 12, 2019 · 5 comments
Labels
discussion Questions, feedback and general information.

Comments

@jochenonline
Copy link

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!

@ricmoo
Copy link
Member

ricmoo commented Feb 12, 2019

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.

@ricmoo ricmoo added the discussion Questions, feedback and general information. label Feb 12, 2019
@jochenonline
Copy link
Author

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)
);

@ricmoo
Copy link
Member

ricmoo commented Feb 12, 2019

Awesome! Thanks! :)

@daweth
Copy link

daweth commented Oct 28, 2021

@ricmoo Just a follow up on this, it seems as though MethodID is 10 characters, not 4? Method ID: 0xab834bab

@zemse
Copy link
Collaborator

zemse commented Oct 30, 2021

it seems as though MethodID is 10 characters, not 4? Method ID: 0xab834bab

Method id is 4 bytes (characters can be a confusing term). Each byte is 2 hex characters along with 0x prefix. So in 0x ab 83 4b ab if you ignore the 0x, there are 4 bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Questions, feedback and general information.
Projects
None yet
Development

No branches or pull requests

4 participants