How to get Ethereum encoded function signatures
- Function signatures are needed when writing a code to call smart contract’s functions in a primitive way or calling contract functions from multisig wallets.
- To get function signature, you need to hash prototype string of function like
functionName(type1,type2,...)
with Keccak256. Then extract the first 4 bytes. - For example, if you want to get encoded function signature of the function
sendMessage(string message, address to)
, hash prototype string of the functionsendMessage(string,address)
with Keccak256. Then extract the first 4bytes “0xc48d6d5e”.
Get encoded function signatures with Web3.js
With Web3.js 1.0.0, encoded function signatures can be obtained by a utility function.
Example code
let encodedFunctionSignature = web3.eth.abi.encodeFunctionSignature('sendMessage(string,address)');
console.log(encodedFunctionSignature);
// => 0xc48d6d5e