foundry
foundry copied to clipboard
startBroadcast suddenly does not work with privatekey from the env file being passed as a param
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:
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.
can you try with latest forge, there have been some fixes with uint parsing
is your key hex and begin with 0x?
No, I am using it without the 0x prefix. Should I add the prefix?
yeah hex values require 0x now to make this less ambiguous, however we should improve the error message
Can't this be processed within Foundry instead of giving an error.
- When dev passes a private key using
envUint
, check if it is hex or not. - Try creating wallet with the hexless value by prefixing 0x if it is missing.
Is this possible?
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.
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
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.
@DoTheBestToGetTheBest do you want to add the missing decoding attempt?
@DoTheBestToGetTheBest do you want to add the missing decoding attempt?
Alright i take this, thank you
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
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]
I just added 0x to the beginning of my private key and fixed the issue.
Resolved by https://github.com/foundry-rs/foundry/pull/6041#issuecomment-1792811179