Granular permissions can be setup in smart contracts by defining specific roles, using AccessControl.sol. In the OpenZeppelin docs, the following example is given for defining granular permissions to the following MINTER_ROLE and BURNER_ROLE:

contract MyToken is ERC20, AccessControl {
   bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
   bytes32 public constant BURNER_ROLE = keccak256("BURNER_ROLE");

However, I do not understand why we must hash the name of the role using keccak256.

What is achieved by hashing the name of the role with keccak256?

2 Answers 2


It's a gas optimisation feature. Fixed length byte values are cheaper than stings, also having them all of an equal type and defined together saves gas upon deployment.

The hashing returns the bytes32 value no matter the length of the string, so you can have as many roles as you need, identified by strings of variable lengths, but your deployment cost will be lower.

You could also have just uint8 type of values to specify roles, but this is less error- and overhead-prone.


According to this link in the Access Control documentation, roles are referred to by their bytes32 identifier.

When you use the keccak256() function on a string, it returns a hash of length 32 in bytes, no matter the size of the name of your Role.

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.