foundry icon indicating copy to clipboard operation
foundry copied to clipboard

startBroadcast suddenly does not work with privatekey from the env file being passed as a param

Open Genesis3800 opened this issue 1 year ago • 13 comments

Component

Forge

Have you ensured that all of these are up to date?

  • [X] Foundry
  • [X] Foundryup

What version of Foundry are you on?

forge 0.2.0 (5be158b 2023-10-02T00:17:39.229041463Z)

What command(s) is the bug in?

forge script

Operating System

Linux

Describe the bug

This is my deployment script. Basically, I want to deploy a CREATE2 factory contract at a deterministic address. this is my script:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

import "forge-std/Script.sol";
import {Create2} from "../src/Create2.sol";


contract Create2Deploy is Script {

    Create2 create2;

    function run() external {
        
        // Using the envUint cheatcode we can read some env variables
        uint256 PrivateKey = vm.envUint("PRIVATE_KEY");

        // Anything within the broadcast cheatcodes is executed on-chain
        vm.startBroadcast(PrivateKey);

        // Deploy the Create2 contract
        create2 = new Create2();

        bytes32 salt = "12345";
        bytes memory bytecode = abi.encodePacked(vm.getCode("Create2.sol:Create2"));

        address deployedAddress = create2.deploy(0, salt , bytecode);
        console2.log("Address of Create2Factory: %s", deployedAddress);

        vm.stopBroadcast();
    }

}

I have been scripting like this for a long time, but now when I pass the private key from the env file as a param, I get this error:

image

The censored parts show my private key correctly, which means forge can definitely read the env file. But for some reason, now the private key won't work.

Genesis3800 avatar Oct 11 '23 18:10 Genesis3800

can you try with latest forge, there have been some fixes with uint parsing

mattsse avatar Oct 11 '23 18:10 mattsse

is your key hex and begin with 0x?

mattsse avatar Oct 11 '23 18:10 mattsse

No, I am using it without the 0x prefix. Should I add the prefix?

Genesis3800 avatar Oct 11 '23 18:10 Genesis3800

yeah hex values require 0x now to make this less ambiguous, however we should improve the error message

mattsse avatar Oct 11 '23 18:10 mattsse

Can't this be processed within Foundry instead of giving an error.

  1. When dev passes a private key using envUint, check if it is hex or not.
  2. Try creating wallet with the hexless value by prefixing 0x if it is missing.

Is this possible?

Genesis3800 avatar Oct 11 '23 19:10 Genesis3800

Btw @mattsse i would be open to working on a PR for this if you guys are game.

Although you'd obviously need to decide which direction you wanna take this in.

Genesis3800 avatar Oct 11 '23 19:10 Genesis3800

When dev passes a private key using envUint, check if it is hex or not.

I think we should check this yes:

https://github.com/foundry-rs/foundry/blob/deae4f1f37a3ef081b62d7488e876d1a5bec815e/crates/cli/src/utils/mod.rs#L83-L83

mattsse avatar Oct 11 '23 22:10 mattsse

When dev passes a private key using envUint, check if it is hex or not.

I think we should check this yes:

https://github.com/foundry-rs/foundry/blob/deae4f1f37a3ef081b62d7488e876d1a5bec815e/crates/cli/src/utils/mod.rs#L83-L83

So this already exists, is that it? I don't understand. Especially because this was not an issue earlier, I used to pass my regular private keys without problem.

Genesis3800 avatar Oct 12 '23 08:10 Genesis3800

@DoTheBestToGetTheBest do you want to add the missing decoding attempt?

mattsse avatar Oct 14 '23 06:10 mattsse

@DoTheBestToGetTheBest do you want to add the missing decoding attempt?

Alright i take this, thank you

DoTheBestToGetTheBest avatar Oct 14 '23 16:10 DoTheBestToGetTheBest

Agree with @Genesis3800

Error handling would improve on current issue but prefixing it with hex indicator if not present would be a much better solution.

Reasoning is private keys are mainly exported from wallets without 0x

0xPrimata avatar Nov 27 '23 16:11 0xPrimata

requiring hex formatted uints to be prefixed with 0x makes it unambiguous

the error message should be clear now:

[FAIL. Reason: failed parsing $PRIVATE_KEY as type uint256: missing hex prefix ("0x") for hex string]

mattsse avatar Nov 27 '23 20:11 mattsse

I just added 0x to the beginning of my private key and fixed the issue.

danieliniguezv avatar Apr 23 '24 06:04 danieliniguezv

Resolved by https://github.com/foundry-rs/foundry/pull/6041#issuecomment-1792811179

zerosnacks avatar Jul 04 '24 12:07 zerosnacks