A contract has many functions and how to automatically generate the full selector list, including inherited functions, for the whole contract?
Preferred solution shouldn’t require more than a web browser.
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityMaybe you can use the online Ethereum IDE (Remix):
v0.4.25+commit.59dbf8f1
.SaiProxyCreateAndExecute
.Press on Compilation Details and check the functionHashes
section. You will get something like the following:
{
"581f3c50": "createAndOpen(address,address)",
"eefe3818": "createOpenAndLock(address,address)",
"d3140a65": "createOpenLockAndDraw(address,address,uint256)",
"0344a36f": "draw(address,bytes32,uint256)",
"f9ef04be": "free(address,bytes32,uint256)",
"da93dfcf": "give(address,bytes32,address)",
"bc25a810": "lock(address,bytes32)",
"1edf0c1e": "lockAndDraw(address,bytes32,uint256)",
"516e9aec": "lockAndDraw(address,uint256)",
"b95460f8": "open(address)",
"bc244c11": "shut(address,bytes32)",
"792037e3": "shut(address,bytes32,address)",
"a3dc65a7": "wipe(address,bytes32,uint256)",
"8a9fc475": "wipe(address,bytes32,uint256,address)",
"faed77ab": "wipeAndFree(address,bytes32,uint256,uint256)",
"1b968160": "wipeAndFree(address,bytes32,uint256,uint256,address)"
}
82661dc4
.
Aug 15, 2019 at 12:48
For every function, you want to calculate keccak256(signature) (try https://emn178.github.io/online-tools/keccak_256.html)
signature is a concatenation of function's name and parameters' types in parentheses, omitting all spaces and parameter names.
For example function
transfer(address to, uint256 amount)
has signature
transfer(address,uint256)
keccak256(signature) result is
a9059cbb2ab09eb219583f4a59a5d0623ade346d962bcd4e46b11da047c9049b
Function selector is first 4 bytes (according to https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html#function-selector) which is
a9059cbb
As ABI can be interpreted in JavaScript as simple array of JSON objects, it would be simple to iterate through this array and compose signature for every function and calculate selectors
8a9fc475
match for this contract : etherscan.io/address/…. So my point is to process the whole contract source code or it’s ᴀʙɪ in the way you described.
Jul 29, 2019 at 9:48
It's kind of ridiculous that it's not part of the artifacts. Here's a quick js script that will extract them out of the artifacts/build-info json data:
#!/usr/bin/env node
// Reads JSON from stdin and writes equivalent
// nicely-formatted JSON to stdout.
var stdin = process.stdin,
stdout = process.stdout,
inputChunks = [];
stdin.resume();
stdin.setEncoding('utf8');
stdin.on('data', function (chunk) {
inputChunks.push(chunk);
});
// Search for path of item with name and value
function path(c, name, currentPath, t){
var currentPath = currentPath || "root";
for(var i in c){
if(i == name){
t = currentPath;
console.log(t + "." + name + ' -> ' + JSON.stringify(c[name], null, ' '));
}
else if(typeof c[i] == "object"){
path(c[i], name, currentPath + "." + i);
}
}
};
stdin.on('end', function () {
var inputJSON = inputChunks.join("");
var parsedData = JSON.parse(inputJSON);
path(parsedData,"methodIdentifiers", '');
});
It works on stdin so , just run like this: cat artifacts/build-info/2e388aff5c540fc7cba05810417a6a1d.json | ./extractSelectors.js
There you go (using web3.js v1.0.0-beta.34):
const Web3 = require("web3");
const request = require("request");
request("http://api.etherscan.io/api?module=contract&action=getabi&address=0x526af336d614ade5cc252a407062b8861af998f5&format=raw", function(error, response, body) {
const web3 = new Web3();
for (const func of JSON.parse(body))
console.log(func.name, web3.eth.abi.encodeFunctionSignature(func));
});
web3.eth.abi.encodeFunctionSignature
doesn’t exist. web3.eth.abi
isn’t there at all.
Aug 15, 2019 at 8:21
This can get tricky for functions that have complex argument definitions. slither
has a feature that can help here, though:
slither --print function-id .