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

fix(forge): new error message for vm.addr() when passing invalid private keys #2164

Merged
merged 3 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions evm/src/executor/inspector/cheatcodes/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ethers::{
};
use foundry_common::fmt::*;
use revm::{CreateInputs, Database, EVMData};
use std::str::FromStr;

pub const DEFAULT_CREATE2_DEPLOYER: H160 = H160([
78, 89, 180, 72, 71, 179, 121, 87, 133, 136, 146, 12, 167, 143, 191, 38, 192, 180, 149, 108,
Expand All @@ -26,6 +27,13 @@ fn addr(private_key: U256) -> Result<Bytes, Bytes> {
return Err("Private key cannot be 0.".to_string().encode().into())
}

let secp256k1_order =
U256::from_str("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141")
.unwrap();
if private_key > secp256k1_order {
0xvv marked this conversation as resolved.
Show resolved Hide resolved
return Err("Private key is greater than secp256k1 curve order.".to_string().encode().into())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect most user's won't know what "curve order" means or what that value is. What do you think about:

Suggested change
return Err("Private key is greater than secp256k1 curve order.".to_string().encode().into())
return Err("Private key must be less than 115792089237316195423570985008687907852837564279074904382605163141518161494337.".to_string().encode().into())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it is already better but maybe there is a more usable way to print the value

}

let mut bytes: [u8; 32] = [0; 32];
private_key.to_big_endian(&mut bytes);

Expand Down
3 changes: 2 additions & 1 deletion forge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ which implements the following methods:

- `function addr(uint sk) public returns (address addr)` Derives an ethereum
address from the private key `sk`. Note that `hevm.addr(0)` will fail with
`BadCheatCode` as `0` is an invalid ECDSA private key.
`BadCheatCode` as `0` is an invalid ECDSA private key. `sk` values above the
secp256k1 curve order, near the max uint256 value will also fail.

- `function ffi(string[] calldata) external returns (bytes memory)` Executes the
arguments as a command in the system shell and returns stdout. Note that this
Expand Down