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(cheatcodes): allow forge script to access wallet addresses with vm.getScriptWallets() #7213

Closed
superical opened this issue Feb 22, 2024 · 5 comments · Fixed by #9052
Closed
Assignees
Labels
A-cheatcodes Area: cheatcodes Cmd-forge-script Command: forge script T-feature Type: feature
Milestone

Comments

@superical
Copy link

superical commented Feb 22, 2024

Component

Forge

Describe the feature you would like

To have a feature where we can access in our foundry scripts the private keys input from --interactives mode or the resolved public addresses of those private keys. This way, we don't have to repeat the sender address in the --sender because we can decide which private keys to use as the sender and pass the intended address as the sender in vm.startBroadcast(sender).

For example:

uint256[] privateKeys = vm.interactives();
// Or, an optional index parameter to get from the list of input private keys
// uint256 privateKey = vm.interactives(0);   // gets the first private key entered from interactive mode

address sender = vm.addr(privateKeys[0]);

vm.startBroadcast(sender);
// ...
vm.stopBroadcast();

Additional context

Currently, when forge is run with --interactives, it doesn't seem like there is a way to access the private keys or the public addresses of those private keys provided through the interactive input in the foundry scripts. The only way (as far as I have tried) to run the foundry scripts with --interactives is:

  • Provide the address of the private key through --sender on top of --interactives
  • AND only use vm.startBroadcast(). I cannot use vm.startBroadcast(sender) since I cannot access the private key supplied through the interactive input and so I won't know the sender address.

Supplying the public address through --sender feels repeated because the address should already be resolvable from the private key.

Moreover, the --interactives parameter can accept more than one private keys which seems to suggest a script should be able to select one of the keys supplied to be the actual sender.

If there is a feature where we can access the input private keys or their translated public addresses in our foundry scripts, we wouldn't have to repeat the address in the --sender because our foundry script can decide which private key to use as sender and pass the correct address as the sender in vm.startBroadcast(sender).

@superical superical added the T-feature Type: feature label Feb 22, 2024
@superical superical changed the title Allowing foundry scripts to access the private keys or their addresses provided through --interactives Allow foundry scripts to access the private keys or their addresses provided through --interactives Feb 22, 2024
@klkvr
Copy link
Member

klkvr commented Feb 26, 2024

This should be very easy to implement with new ScriptWallets struct from #7141. We can add something like vm.getScriptWallets() returning all unlocked keys/keystores/hw wallet addresses.

We'd have to decide what the behavior of this cheat would be in scope of tests, afaik we don't have any script-only cheats rn.

And we can't really give any guarantees on the order in which senders will be returned. We can probably keep ordering for interactive keys being entered, but if script is ran with ledger + 1 interactive key, it's unclear how this should be handled.

cc @mattsse @Evalir @mds1

@mds1
Copy link
Collaborator

mds1 commented Feb 26, 2024

Can vm.getScriptWallets() could return an array of Wallet structs in scripts and tests? This way you can write tests for your scripts and do something like forge test script/MyScript.s.sol --private-key $DUMMY_PRIVATE_KEY to test your scripts

@klkvr
Copy link
Member

klkvr commented Feb 26, 2024

We can't always get a private/public key for script wallets as such info is not available for hw wallets

@klkvr
Copy link
Member

klkvr commented Feb 26, 2024

This should be solvable with pattern similar to the one discussed in #7012 (comment)

That way, the script will look like

contract Script {
    function run() public {
        address[] memory wallets = vm.getScriptWallets();
        // logic to determine which wallets to use for broadcasting
        run(wallet1, wallet2);
    }

    function run(address broadcaster1, address broadcaster2) public {
        // actual logic
    }
}

do something like forge test script/MyScript.s.sol --private-key $DUMMY_PRIVATE_KEY to test your

this is similar to dry-running a script I believe

Probably more often it's useful do determine broadcaster at runtime when testing script, by calling .owner() on some contract for example

@zerosnacks zerosnacks changed the title Allow foundry scripts to access the private keys or their addresses provided through --interactives feat(cheatcodes): allow foundry script to access the private keys or their addresses with vm.getScriptWallets() Jul 11, 2024
@zerosnacks zerosnacks added A-cheatcodes Area: cheatcodes Cmd-forge-script Command: forge script labels Jul 11, 2024
@zerosnacks zerosnacks changed the title feat(cheatcodes): allow foundry script to access the private keys or their addresses with vm.getScriptWallets() feat(cheatcodes): allow forge script to access the private keys or their addresses with vm.getScriptWallets() Jul 11, 2024
@zerosnacks zerosnacks changed the title feat(cheatcodes): allow forge script to access the private keys or their addresses with vm.getScriptWallets() feat(cheatcodes): allow forge script to access wallet addresses with vm.getScriptWallets() Jul 11, 2024
@zerosnacks zerosnacks added this to the v1.0.0 milestone Jul 26, 2024
@grandizzy
Copy link
Collaborator

going to close in favor of #8225 and track impl there

@grandizzy grandizzy closed this as not planned Won't fix, can't repro, duplicate, stale Oct 3, 2024
@yash-atreya yash-atreya reopened this Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cheatcodes Area: cheatcodes Cmd-forge-script Command: forge script T-feature Type: feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants