Skip to content

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

@PatrickAlphaC

Description

@PatrickAlphaC

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions