Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  • The following code snippet creates a signature hash using the keccak256 hashing algorithm.
  • The following code snippet creates a signature hash from a transaction.
  1. STEP 5

48: Function Signature Hash

A function signature hash is a four-byte representation of a function's signature. It is used to uniquely identify functions within a contract.

The following code snippet creates a signature hash using the keccak256 hashing algorithm.

contract SignHash {    
    function signHash(string memory message) 
    public view returns (bytes32) {        
        return keccak256(abi.encodePacked(message));    
    }
}
  • Test

string memory message : "getBalance(address)"
Function Name: getBalance
Parameters: address _account
Function Signature: 0x70a08231

string memory message : "transfer(address,uint256)"
Function Name: transfer
Parameters: address _to, uint256 _value
Function Signature: 0xa9059cbb

The following code snippet creates a signature hash from a transaction.

contract SignHash {    
    function signHash(address to, uint256 value, uint256 nonce) 
    public view returns (bytes32) {        
        return keccak256(abi.encodePacked(to, value, nonce));    
    }
}
  • Example 1: Consider a contract with a function foo(). The signature of the function is foo(). The corresponding signature hash is 0x7f12a5a6.

  • Example 2: Consider a contract with a function bar(uint a, string memory b). The signature of the function is bar(uint,string). The corresponding signature hash is 0xacb08c82.

  • Example 3: Consider a contract with a function baz(address payable c, uint256 d). The signature of the function is baz(address,uint256). The corresponding signature hash is 0x8b7d828d.

Function Signature Hash A function signature hash is a unique identifier for a function in a smart contract. It is the result of taking the function parameters and hashing them together with a unique algorithm. This hash can then be used to call the function in the future.

For example, consider the following function:

function addNumbers(uint a, uint b) public returns (uint) {  
    return a + b;
}

The function signature hash of this function is 0x2d2b2ece. This can be calculated by hashing together the function name (addNumbers) and the parameters (uint a, uint b) using a hashing algorithm such as Keccak-256.

Another example is the following function:

function multiplyNumbers(uint a, uint b) public returns (uint) {  
    return a * b;
}

The function signature hash of this function is 0xfe710e6f. This can be calculated by hashing together the function name (multiplyNumbers) and the parameters (uint a, uint b) using a hashing algorithm such as Keccak-256.

That's it for the lesson 48!

Previous47: Keccak256NextTips: Solidity by "Immunable"

Last updated 1 year ago