'Constructing a Method ID on Solidity
Good Afternoon Everybody! Based off the example on the official website. The example they provided was:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;
contract Foo {
function bar(bytes3[2] memory) public pure {}
function baz(uint32 x, bool y) public pure returns (bool r) { r = x > 32 || y; }
function sam(bytes memory, bool, uint[] memory) public pure {}
}
In Following the example; I Understand to take the method name along with the parameters in its ASCII form and you should be able to produce 0xcdcd77c0(After taking its first 4 Bytes)
So in my quest to understand the process so that I may recreate it programmatically, I found an online Keccak-256 Converter and it produced:
cdcd77c0992ec5bbfc459984220f8c45084cc24d9b6efed1fae540db8de801d2
From My understanding the first 4 Bytes should be cdcd and that is not correct. So then I found myself down a rabbit hole and attempted to do a text > ASCII > Keccak Signature and that still did not produce what I needed.. So I am asking the community to help me understand how 4 bytes have turned into 10 characters.... Please and thank you.
Solution 1:[1]
Response from every hashing function, like keccak-256, are bytes or hex string representing those bytes. Every byte is represented by 2 characters in hex string. So 4 first bytes are 8 first characters. In you example there are
['cd', 'cd', '77', 'c0']
To indicate that some string should be interpreted as a bytes, there are prefixed with 0x.
When you combine those two rules you get 0xcdcd77c0 function selector.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
