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

Enable anvil / default accounts to be unlocked by default in scripting during local tests #8225

Open
PatrickAlphaC opened this issue Jun 21, 2024 · 0 comments
Labels
C-forge Command: forge Cmd-forge-script Command: forge script T-feature Type: feature
Milestone

Comments

@PatrickAlphaC
Copy link

Component

Forge

Describe the feature you would like

I have the following line in my script:

(uint8 v, bytes32 r, bytes32 s) = vm.sign(digest);

I am able to run this line without issue if I'm running my script as a script, because I unlock the address by passing in the password:

forge script MyScript.sol --account MY_ACCOUNT

What is cool, is that I can even pass in my address to this API, and use a HelperConfig type contract to pick the address based on the chain I'm working on:

(uint8 v, bytes32 r, bytes32 s) = vm.sign(MY_ADDRESS, digest);

However, if I use this script in a test, it fails, because their is no unlocked account. I'd like to be able to run my test suite (which includes my scripts) without having to pass in my password. However, if you run this on a test suite, you'll get:

[FAIL. Reason: no wallets are available] testGetV() (gas: 7961

Right now, I can do a work around like so:

        uint256 ANVIL_DEFAULT_KEY = 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80;
        uint8 v;
        bytes32 r;
        bytes32 s;
        if (block.chainid == 31337) {
            (v, r, s) = vm.sign(ANVIL_DEFAULT_KEY, digest);
        } else {
            // It'll use whatever account is unlocked, BUT there is no unlocked account for tests
            (v, r, s) = vm.sign(MY_UNLOCKED_ACCOUNT, digest);
        }

You can see a full example of how I'm using my scripts in my test suite here: https://github.com/PatrickAlphaC/no-unlocked-accounts-example

Rationale

Developers who are using the scripting feature should be able to test their scripts in the same way they test their smart contracts. We are seeing more people deploy their projects without the correct access controls, or with exposed private keys, and we want to make their devops process as easy as foundry makes their dev process.

Issue Summary

I think this could be summarized into adding the anvil default key as a consistently unlocked account for locally running tests. Maybe we restrict the vm.sign API so that you can only use the default anvil key if you give it the anvil address or something... Or maybe an API like vm.unlockAnvilAccounts() would be great as well.

Let me know if you have any questions.

Additional context

No response

@PatrickAlphaC PatrickAlphaC added the T-feature Type: feature label Jun 21, 2024
@zerosnacks zerosnacks added the C-forge Command: forge label Jun 27, 2024
@zerosnacks zerosnacks added the Cmd-forge-script Command: forge script label Jul 16, 2024
@zerosnacks zerosnacks added this to the v1.0.0 milestone Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-forge Command: forge Cmd-forge-script Command: forge script T-feature Type: feature
Projects
None yet
Development

No branches or pull requests

2 participants