thien k phan

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

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