Solidity Function Signature
1. What is function signature?
Function signature is the part of bytes code which EVM decides to run in compiled smart contracts.
2. How it is calculated:
In this example, I use Remix IDE to compile the contract, take a look at the
Compilation Details
:![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fd75824a8-069d-4e93-8ec9-d216c0987cdf%2FUntitled.png?table=block&id=8cdd01db-ccd6-4257-9d39-96e03d83d77d&cache=v2)
list(bytes32, uint256)
is the function get called, with the hashed signature 345c550f
The hash is the first 8 bytes after
0x
. It’s calculated through keccak256
Here is the transaction hash after we executed the function:
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fbafdb5ae-dff7-4926-ba4f-4ce1d7e56721%2FUntitled.png?table=block&id=add45230-2667-4572-b8a7-80e4b5fd5504&cache=v2)
3. Implementations in web3
and ethers.js
➡️ Ether.js
import { utils } from 'ethers'; utils.keccak256(utils.toUtf8Bytes("list(bytes32, uint256)")) // -> output: 0x345c550f93e02078756215e8aae88bae5cb50c3d564d276e23fb8af9099940e1 // take the first 8 bytes: 345c550f
➡️ Web3.js
import { utils } from 'web3'; utils.sha3("list(bytes32, uint256)"); // -> output: 0x345c550f93e02078756215e8aae88bae5cb50c3d564d276e23fb8af9099940e1 // take the first 8 bytes: 345c550f