How to get Ethereum encoded function signatures

  • 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 function sendMessage(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

Working demo

Related article

Reference

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hideyoshi Moriya

Hideyoshi Moriya

More from Medium

How to Optimize Solidity Smart Contracts Effectively!

80/20 #4 — Building a notification app for BAYC transactions on ethereum

Smart contract address creation method & difference between smart contract address and wallet…

How to make VSC Solidity lint recognize your @chainlink and @openzeppelin libraries