Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cast): add selectors command to extract function selectors and arguments from bytecode #6684

Merged
merged 2 commits into from
Jan 5, 2024

Conversation

cdump
Copy link
Contributor

@cdump cdump commented Dec 29, 2023

Motivation

Sometimes, you don't have the source code/ABI for the contract but want to determine which functions exist, having only the deployed bytecode.

Solution

I am the author of the EVMole library, which addresses this problem. The library includes reproducible benchmarks for accuracy and speed, along with a comparison with other existing tools.

In this PR, I have added the selectors command to the cast tool, which uses my library for the task.

Example of using cast code to obtain the bytecode of the USD-T contract and cast selectors to extract selectors and arguments:

$ cast selectors $(cast code 0xdAC17F958D2ee523a2206206994597C13D831ec7)
0x06fdde03
0x0753c30c      address
0x095ea7b3      address,uint256
0x0e136b19
0x0ecb93c0      address
0x18160ddd
0x23b872dd      address,address,uint256
0x26976e3f
0x27e235e3      address
0x313ce567
0x35390714
0x3eaaf86b
0x3f4ba83a
0x59bf1abe      address
0x5c658165      address,address
0x5c975abb
0x70a08231      address
0x8456cb59
0x893d20e8
0x8da5cb5b
0x95d89b41
0xa9059cbb      address,uint256
0xc0324c77      uint256,uint256
0xcc872b66      uint256
0xdb006a75      uint256
0xdd62ed3e      address,address
0xdd644f72
0xe47d6060      address
0xe4997dc5      address
0xe5b5019a
0xf2fde38b      address
0xf3bdc228      address

@cdump
Copy link
Contributor Author

cdump commented Dec 30, 2023

I've fixed fmt issue, checked by cargo +nightly fmt -- --check

@cdump
Copy link
Contributor Author

cdump commented Dec 30, 2023

Failed clippy check is not related to changes in this PR, same on foundry master branch.
I've created clippy bug-report rust-lang/rust-clippy#12058

@tynes
Copy link
Contributor

tynes commented Jan 5, 2024

I have wanted this sort of functionality in cast for a long time. It would be nice to do 4byte selector lookups and also print the human readable names but thats gonna be a lot of RPC requests. Maybe could gate behind an extra flag?

@cdump
Copy link
Contributor Author

cdump commented Jan 5, 2024

@tynes selectors lookup is possible, but it will require a little refactoring in the existing code. Currently, decode_selector accepts only one selector in arguments, but the openchain.xyz API allows multiple selectors in one request (I use this feature in the evmole demo), and it greatly speeds up the entire process. I can work on it, but I think it should be a different MR/Issue after this one is merged.

At this moment, we can use something like this:

$ cast selectors $(cast code 0xdAC17F958D2ee523a2206206994597C13D831ec7) | awk '{"cast 4b "$1|getline r; print $0 "\t\t" r}'
0x06fdde03			name()
0x0753c30c	address		deprecate(address)
0x095ea7b3	address,uint256		approve(address,uint256)
0x0e136b19			deprecated()
0x0ecb93c0	address		addBlackList(address)
...

Copy link
Member

@Evalir Evalir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! wdyt we work on a followup to implement @tynes 's suggestions @cdump ?

@Evalir Evalir merged commit 8bd27fb into foundry-rs:master Jan 5, 2024
19 of 20 checks passed
@cdump
Copy link
Contributor Author

cdump commented Jan 5, 2024

@Evalir I've already begun implementing signatures lookup. What do you think would be better – opening a "Feature request" issue now, or waiting until everything is ready for a pull request (probably in a couple of days)?

I have a question regarding the interface stability of the foundry-common crate. Am I allowed to modify the interface of public functions, such as decode_selector? It seems to be an internal crate with an empty package on crates.io, and it is not intended for external dependencies beyond the Foundry project, am I right?

@rmcmk
Copy link

rmcmk commented Jan 5, 2024

@Evalir @cdump Thoughts on implementing a cheatcode to pull selectors of an address? Would be very useful for proxy-standard contract tooling.

RPate97 pushed a commit to RPate97/foundry that referenced this pull request Jan 12, 2024
… arguments from bytecode (foundry-rs#6684)

* add evmole library to dependencies

* cast: add 'selectors' command to extract function selectors and arguments from bytecode using the evmole library
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants