11

I've heard the term "function signature" as well as "function selector", what are they? What are they for?

1

2 Answers 2

17

Function Signature

A function signature is a combination of the function's name and the types of parameters it takes, combined together as a string with no spaces.

For example, let's say you have a function in solidity where the method looks like this:

function transfer(address sender, uint256 amount) public {
  // Some code here
}

This function would have a function signature of:

transfer(address,uint256)

These are important because we use function signatures to get the next part, function selectors. Additionally, you'll want to use uint256 instead of uint for computing a function signature (and function selector).

Function Selector

A function selector is the first 4 bytes of the call data for a function call that specifies the function to be called. A function selector is the hash of the same function's signature.

Which might be a little confusing, but let's break it down. When someone makes a call to an EVM smart contract, the smart contract needs to know which function it should execute. The piece of code that governs this is known as the function selector, and it might look like this:

0xa9059cbb //this is the function selector for the transfer function signature above. 

You can get the function selector by hashing the string of the function signature in solidity.

bytes4(keccak256(bytes(function_signature)))

If you are a Foundry user, you can calculate the selector using the cast sig CLI:

$ cast sig "transfer(address,uint256)"

Bonus

There are EVM Signature Databases that make it easier to find out the function signature of a selector. Understanding function signatures and selectors allow developers to call functions of any contract without having an ABI.

7
  • 1
    Great content! Should you also talk about the range of the unique 4 bytes selectors we can have (4 bytes = 32 bits = 2^32 = 4294967292 unique possible selectors), and the chances of selectors collisions and how the compiler handles those collisions and how while using the Proxy pattern we need to be careful about having functions with the same name or that produce the same selector? I think that way we would learn everything we need related to the function signatures and selectors. Great content for an article. Sep 7, 2022 at 23:16
  • For sure! This would be great to link out to or answer in another question. Sep 8, 2022 at 0:11
  • 1
    Great content! Briefly, I linked to this twice and updated an answer about the ABI. On collisions, there's ethereum.stackexchange.com/questions/40297/… and I thought there may have been others. Please continue :) In docs.soliditylang.org/en/develop/abi-spec.html someone should also replace Method ID (old name) with Function Selector.
    – eth
    Sep 10, 2022 at 2:18
  • Is uint still commonly used? If so, may help others save time by pointing out For computing the function selector, uint256 ... is needed.
    – eth
    Sep 10, 2022 at 2:44
  • Good call! I'll add that Sep 12, 2022 at 12:07
1

@patrick-collins answer is useful, but I thought I'd add code to calculate the selector in TypeScript/ethers/hardhat:

/**
 * Returns an Ethereum function selector given the function signature
 * @param functionSignature The name of the function (and its arguments in parens)
 */
function ethereumFunctionSelector(functionSignature: string): BytesLike {
  const functionSignatureAsUtf8Bytes = hardhat.ethers.utils.toUtf8Bytes(functionSignature)
  const signatureCksum = ethers.utils.keccak256(functionSignatureAsUtf8Bytes)
  // end is 10 because we want the first four bytes (8 characters), and
  // signatureCksum starts with 0x (two characters).  arrayify expects
  // what ethers calls a DataHexStringOrArrayish https://docs.ethers.org/v5/api/utils/bytes/
  // which is something like 0xdd365b8b
  const firstFourBytesOfChecksum = signatureCksum.slice(0, 10)
  const result = hardhat.ethers.utils.arrayify(firstFourBytesOfChecksum)
  return result
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.