I have some solidity contract, and I have code, generated by a custom compiler. This code supposed to call functions from the Solidity contract (they both will be composed into one contract).
So, to call them, I need to know the offset of the functions. Is there a way to get them?
I’ve looked in combined json, but didn’t find anything suitable.
I’ve tried to analyze EVM bytecode of the contract to figure out the offset, but function selector looks not so simple.
It means, that the bytecode for function selector is not so simple to be easily reverse engineered. The idea is to get offset from the push instruction, which push offset of the function body when hash is matched.
You don’t need to reverse-engineer anything.
There’s a simple way to retrieve the selector of any function.
The first four bytes of the call data for a function call specifies the function to be called. It is the first (left, high-order in big-endian) four bytes of the Keccak-256 hash of the signature of the function. The signature is defined as the canonical expression of the basic prototype without data location specifier, i.e. the function name with the parenthesised list of parameter types. Parameter types are split by a single comma — no spaces are used.
For example, the function selector of:
function func(uint256 x, address y, bool z) public returns (int)
Can be obtained via:
const selector = keccak("func(uint256,address,bool)").slice(2, 10);
2 in order to remove the
0x, and then use
10 and order to keep the following 8 characters.