Skip to content

Commit

Permalink
fix(forge): new error message for vm.addr() when passing invalid priv…
Browse files Browse the repository at this point in the history
…ate keys
  • Loading branch information
0xvv committed Jun 29, 2022
1 parent 49f35c7 commit fb7813a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
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 {
return Err("Private key is greater than secp256k1 curve order.".to_string().encode().into())
}

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

0 comments on commit fb7813a

Please sign in to comment.