Skip to main content

Get Function Signature

In Solidity, function names are not included in the compiled bytecode. Instead, they are hashed using the Keccak-256 algorithm and the resulting hash is stored in the bytecode as the function signature. Developers can retrieve the function signature for a specific function using the web3.sha3() function in Python.

The resulting hash can be useful for various purposes, such as verifying that a specific function has been called or creating function calls directly in bytecode.

Example Usage

To retrieve the function signature for a function called testFunc that takes a uint256 and a string as parameters, developers can use the following Python code:

import web3

func_signature = web3.sha3(b'testFunc(uint256,string)')[0:4].hex()
print(func_signature)

This code will output the function signature in hexadecimal format, which can be used in various contexts such as verifying that a specific function has been called correctly.