0

I need to get the first 4 bytes of several function signatures, to determine what method I need to use to decode a database of transactions that contain input data.

How can I do this in javascript?

Example:

From this input data 0x5174e8530000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003c516d5a6d62396238503364624c434c7636377374356d434632347a6a5043515a69664a6f78725854566e4a6d7a332f6d657461646174612e6a736f6e00000000 I want this function signature 0x5174e853.

2
  • 1
    Can you clarify what you have and what you need? You have transaction input data, and you want the function signature from it?
    – Undead8
    Aug 12 at 13:38
  • From this input data 0x5174e8530000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003c516d5a6d62396238503364624c434c7636377374356d434632347a6a5043515a69664a6f78725854566e4a6d7a332f6d657461646174612e6a736f6e00000000 I want this function signature 0x5174e853.
    – committer
    Aug 12 at 13:52
1

Since the input data is a string, you can simply select the first characters corresponding to the signature like so:

let input = "0x5174e8530000000000000000000000000000000000000...";
let signature = input.slice(0, 10);

In fact, this is exactly how web3js implements it (see here).

4
  • Sure, but how do I know (0, 10) will always be 4 bytes? I have to do this throughout several hundred thousand transactions.
    – committer
    Aug 12 at 16:11
  • 1
    The function signature is always 10 characters (including 0x) as far as I know.
    – Undead8
    Aug 12 at 16:53
  • 1
    I just checked web3js repo and this is how they do it (see my edit).
    – Undead8
    Aug 12 at 17:00
  • @committer actually this may not be a funciton signature, I can send a transaction with "hello" as data, or any other binary stuff like an image.
    – Nulik
    Aug 13 at 3:56

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.