foundry icon indicating copy to clipboard operation
foundry copied to clipboard

feat(cast) Generate a vanity CREATE2 contract address

Open iFrostizz opened this issue 3 years ago • 15 comments

Motivation

This PR aims to implement https://github.com/foundry-rs/foundry/issues/2359

And it would extend into

  • Adding the --create2 flag into the cast wallet vanity command
  • Adding the vm.mineSalt cheatcode in order to call it as a cheatcode from solidity

Solution

cast

With the deployer address and the init code hash, we are brute forcing the hashing function until having a match, just like the vanity command, but this one works with CREATE for now.

forge-std

Adding the cheatcode in Test.sol and make it call the function described before and return the salt as explained in the issue.

Note

I haven't got any green flag from the team to implement this feature so please tell me if I can continue.

iFrostizz avatar Jul 25 '22 13:07 iFrostizz

I feel like mineSalt could be a library that wraps cast with this functionality

tynes avatar Jul 25 '22 15:07 tynes

I feel like mineSalt could be a library that wraps cast with this functionality

You mean a library called cast wrapping mineSalt ? Why so ? All cheatcodes are in forge-std/Test.sol if I'm not mistaken

iFrostizz avatar Jul 25 '22 15:07 iFrostizz

Just as a general thought I don't think this should be on cast wallet vanity since you're not creating a wallet, should probably just be its own thing although it seems @mds1 disagrees, don't have any strong opinion on this, it might just be a bit odd

I haven't got any green flag from the team to implement this feature so please tell me if I can continue.

Feel free, you don't need to be greenlit to contribute :) Assigned you to the issue, let us know if something blocks you

onbjerg avatar Jul 25 '22 16:07 onbjerg

True, what about calling it cast create2 ?

Also, I thought that it would be great to display a progress bar along with an estimated time remaining by calculating the average of generated addresses in the last 5 sec or so and comparing it to all the possibilities. Although I can already see an issue which is that it's just an estimate and even though it should tend to be accurate if the randomness is good, it would lead to weird UI stuff if the bar gets over 100% or hangs for a long period of time.

Anyway I think that it would be more communicative than just a text saying that a vanity address is being created.

What about multithreading also !

iFrostizz avatar Jul 25 '22 18:07 iFrostizz

I just noticed that mining a salt for a create2 address is like 10x faster than mining a private key wallet, though it shouldn't be that significant, right ? It looks like that the slowness is due to the wallet creation, what if we create a raw private key by deriving the random number into a private key, rather than instanciate the full wallet, or maybe do it at the end only ?

iFrostizz avatar Jul 27 '22 16:07 iFrostizz

franfran@franfran:~$ cast create2 --deployer 0x1F2C452e6d63664bb6c602FAdA3c5501A2f44A6f --init-code 0x --starts-with dead
Starting to generate deterministic contract address...
Successfully found contract address in 0 seconds.
Address: 0xDeaD862e91B43c7e55eAfB20B696BEB72871691B
Salt: 16157366503150255834

Blazingly fast !

iFrostizz avatar Jul 27 '22 16:07 iFrostizz

should probably just be its own thing although it seems @mds1 disagrees, don't have any strong opinion on this, it might just be a bit odd

Sorry missed this, but I'm totally ok with it being a separate command (as it currently is), I think that makes sense!

mds1 avatar Jul 28 '22 15:07 mds1

I have a question regarding the --matching flag. If implemented, should we forget the --starts-with and --ends-with flags ? If so, we would need to validate the data by ensuring that matching hasn't been mentionned if either starts or ends flag have been used. I believe that starts and ends are more handy to type on the command line, whereas matching is more suited in the solidity code.

iFrostizz avatar Jul 29 '22 10:07 iFrostizz

as @gakonst stated, we'll use a cheatcode. Making it rn, but I cannot import foundry_cli as it's making a circular dependency because of cast which imports evm. The reason I wanna do that is because the create2 address generation is in cli and well I cannot import it because of that. Is there a simple fix for this circular dependency, like having to change a bit the tree of modules or should I copy and paste the function in the cheatcodes file ? (not what we want IMO)

iFrostizz avatar Aug 11 '22 20:08 iFrostizz

because the create2 address generation is in cli and well I cannot import it because of that.

I'd refactor this to the foundry-common crate and import it from there!

gakonst avatar Aug 11 '22 20:08 gakonst

because the create2 address generation is in cli and well I cannot import it because of that.

I'd refactor this to the foundry-common crate and import it from there!

Thanks for the advice boss :)

iFrostizz avatar Aug 11 '22 20:08 iFrostizz

because the create2 address generation is in cli and well I cannot import it because of that.

I'd refactor this to the foundry-common crate and import it from there!

Huh, importing cast from froundry-common also creates a dependency cycle

iFrostizz avatar Aug 11 '22 21:08 iFrostizz

Should we make the forge's default CREATE2 deployer, the default address for --deployer ? I think it makes sense

joshieDo avatar Aug 16 '22 09:08 joshieDo

Ah nice, yea I like that idea

mds1 avatar Aug 16 '22 13:08 mds1

I've been struggling to find a valid refactor in order to implement the cheatcode here. What about merging the PR only with the cast create2 feature after that I've added the @joshieDo idea, just in case that might need a bigger refactor ?

iFrostizz avatar Sep 06 '22 22:09 iFrostizz

is it fine now ?

iFrostizz avatar Sep 24 '22 08:09 iFrostizz

Is this safe to use?

sambacha avatar Oct 14 '22 20:10 sambacha

Is this safe to use?

If you compare it to profanity recent issues well yes, the randomly generated number is not critical because salt is public. Even cast wallet which uses the same uses a "safe" random generator

iFrostizz avatar Oct 14 '22 20:10 iFrostizz

I have just pushed the last fix on my branch. Can we open this PR again please ?

iFrostizz avatar Nov 06 '22 17:11 iFrostizz