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(evm/cheatcodes): Use alloy-signer, rm ethers-signers/ethers-core #6911

Merged
merged 5 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ toml = "0.8"
tracing = "0.1"
tracing-subscriber = "0.3"
evm-disassembler = "0.4"
# needed as documented in https://github.com/foundry-rs/foundry/pull/6358
k256 = "=0.13.1"

axum = "0.6"
hyper = "0.14"
Expand Down
3 changes: 1 addition & 2 deletions crates/anvil/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ foundry-evm.workspace = true

# evm support
bytes = "1.4.0"
# needed as documented in https://github.com/foundry-rs/foundry/pull/6358
k256 = "=0.13.1"
k256.workspace = true
ethers = { workspace = true, features = ["rustls", "ws", "ipc", "optimism"] }
trie-db = "0.23"
hash-db = "0.15"
Expand Down
4 changes: 2 additions & 2 deletions crates/cheatcodes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ alloy-genesis.workspace = true
alloy-sol-types.workspace = true
alloy-providers.workspace = true
alloy-rpc-types.workspace = true
alloy-signer = { workspace = true, features = ["mnemonic", "keystore"] }

ethers-core.workspace = true
ethers-signers.workspace = true

eyre.workspace = true
hex.workspace = true
Expand All @@ -37,6 +36,7 @@ revm.workspace = true
serde_json.workspace = true
base64.workspace = true
tracing.workspace = true
k256.workspace = true
walkdir = "2"
p256 = "0.13.2"
thiserror = "1"
7 changes: 4 additions & 3 deletions crates/cheatcodes/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::Vm;
use alloy_primitives::{Address, Bytes};
use alloy_signer::{Error as SignerError, WalletError};
use alloy_sol_types::SolError;
use ethers_core::k256::ecdsa::signature::Error as SignatureError;
use ethers_signers::WalletError;
use foundry_common::errors::FsPathError;
use foundry_config::UnresolvedEnvVarError;
use foundry_evm_core::backend::DatabaseError;
use k256::ecdsa::signature::Error as SignatureError;
use std::{borrow::Cow, fmt};

/// Cheatcode result type.
Expand Down Expand Up @@ -283,7 +283,7 @@ macro_rules! impl_from {

impl_from!(
alloy_sol_types::Error,
ethers_core::types::SignatureError,
alloy_primitives::SignatureError,
FsPathError,
hex::FromHexError,
eyre::Error,
Expand All @@ -297,6 +297,7 @@ impl_from!(
std::string::FromUtf8Error,
UnresolvedEnvVarError,
WalletError,
SignerError,
);

#[cfg(test)]
Expand Down
13 changes: 5 additions & 8 deletions crates/cheatcodes/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
use crate::{Cheatcode, Cheatcodes, CheatsCtxt, Result, Vm::*};
use alloy_genesis::{Genesis, GenesisAccount};
use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_signer::Signer;
use alloy_sol_types::SolValue;
use ethers_signers::Signer;
use foundry_common::{
fs::{read_json_file, write_json_file},
types::ToAlloy,
};
use foundry_common::fs::{read_json_file, write_json_file};
use foundry_evm_core::{
backend::{DatabaseExt, RevertSnapshotAction},
constants::{CALLER, CHEATCODE_ADDRESS, HARDHAT_CONSOLE_ADDRESS, TEST_CONTRACT_ADDRESS},
Expand Down Expand Up @@ -48,7 +45,7 @@ impl Cheatcode for addrCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { privateKey } = self;
let wallet = super::utils::parse_wallet(privateKey)?;
Ok(wallet.address().to_alloy().abi_encode())
Ok(wallet.address().abi_encode())
}
}

Expand Down Expand Up @@ -140,9 +137,9 @@ impl Cheatcode for dumpStateCall {
}

impl Cheatcode for sign_0Call {
fn apply_full<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
fn apply_full<DB: DatabaseExt>(&self, _: &mut CheatsCtxt<DB>) -> Result {
let Self { privateKey, digest } = self;
super::utils::sign(privateKey, digest, ccx.data.env.cfg.chain_id)
super::utils::sign(privateKey, digest)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::{
};
use alloy_primitives::{Address, Bytes, B256, U256, U64};
use alloy_rpc_types::request::TransactionRequest;
use alloy_signer::LocalWallet;
use alloy_sol_types::{SolInterface, SolValue};
use ethers_signers::LocalWallet;
use foundry_common::{evm::Breakpoints, provider::alloy::RpcUrl};
use foundry_evm_core::{
backend::{DatabaseError, DatabaseExt, RevertDiagnostic},
Expand Down
8 changes: 4 additions & 4 deletions crates/cheatcodes/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

use crate::{Cheatcode, CheatsCtxt, DatabaseExt, Result, Vm::*};
use alloy_primitives::{Address, U256};
use ethers_signers::Signer;
use foundry_common::types::ToAlloy;
use alloy_signer::Signer;
use foundry_config::Config;

impl Cheatcode for broadcast_0Call {
Expand Down Expand Up @@ -107,8 +106,9 @@ fn broadcast_key<DB: DatabaseExt>(
private_key: &U256,
single_call: bool,
) -> Result {
let wallet = super::utils::parse_wallet(private_key)?.with_chain_id(ccx.data.env.cfg.chain_id);
let new_origin = &wallet.address().to_alloy();
let mut wallet = super::utils::parse_wallet(private_key)?;
wallet.set_chain_id(Some(ccx.data.env.cfg.chain_id));
let new_origin = &wallet.address();

let result = broadcast(ccx, Some(new_origin), single_call);
if result.is_ok() {
Expand Down
50 changes: 24 additions & 26 deletions crates/cheatcodes/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

use crate::{Cheatcode, Cheatcodes, CheatsCtxt, DatabaseExt, Result, Vm::*};
use alloy_primitives::{keccak256, B256, U256};
use alloy_sol_types::SolValue;
use ethers_core::k256::{
ecdsa::SigningKey,
elliptic_curve::{sec1::ToEncodedPoint, Curve},
Secp256k1,
};
use ethers_signers::{
use alloy_signer::{
coins_bip39::{
ChineseSimplified, ChineseTraditional, Czech, English, French, Italian, Japanese, Korean,
Portuguese, Spanish, Wordlist,
},
LocalWallet, MnemonicBuilder, Signer,
LocalWallet, MnemonicBuilder, Signer, SignerSync,
};
use foundry_common::types::{ToAlloy, ToEthers};
use alloy_sol_types::SolValue;
use foundry_evm_core::constants::DEFAULT_CREATE2_DEPLOYER;
use k256::{
ecdsa::SigningKey,
elliptic_curve::{sec1::ToEncodedPoint, Curve},
Secp256k1,
};
use p256::ecdsa::{signature::hazmat::PrehashSigner, Signature, SigningKey as P256SigningKey};

/// The BIP32 default derivation path prefix.
Expand Down Expand Up @@ -51,9 +50,9 @@ impl Cheatcode for getNonce_1Call {
}

impl Cheatcode for sign_1Call {
fn apply_full<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
fn apply_full<DB: DatabaseExt>(&self, _: &mut CheatsCtxt<DB>) -> Result {
let Self { wallet, digest } = self;
sign(&wallet.privateKey, digest, ccx.data.env.cfg.chain_id)
sign(&wallet.privateKey, digest)
}
}

Expand Down Expand Up @@ -88,10 +87,10 @@ impl Cheatcode for deriveKey_3Call {
impl Cheatcode for rememberKeyCall {
fn apply_full<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
let Self { privateKey } = self;
let wallet = parse_wallet(privateKey)?.with_chain_id(ccx.data.env.cfg.chain_id);
let wallet = parse_wallet(privateKey)?.with_chain_id(Some(ccx.data.env.cfg.chain_id));
let address = wallet.address();
ccx.state.script_wallets.push(wallet);
Ok(address.to_alloy().abi_encode())
Ok(address.abi_encode())
}
}

Expand Down Expand Up @@ -141,7 +140,7 @@ impl Cheatcode for computeCreate2Address_1Call {
/// If 'label' is set to 'Some()', assign that label to the associated ETH address in state
fn create_wallet(private_key: &U256, label: Option<&str>, state: &mut Cheatcodes) -> Result {
let key = parse_private_key(private_key)?;
let addr = ethers_core::utils::secret_key_to_address(&key).to_alloy();
let addr = alloy_signer::utils::secret_key_to_address(&key);

let pub_key = key.verifying_key().as_affine().to_encoded_point(false);
let pub_key_x = U256::from_be_bytes((*pub_key.x().unwrap()).into());
Expand All @@ -155,21 +154,20 @@ fn create_wallet(private_key: &U256, label: Option<&str>, state: &mut Cheatcodes
.abi_encode())
}

pub(super) fn sign(private_key: &U256, digest: &B256, chain_id: u64) -> Result {
let wallet = parse_wallet(private_key)?.with_chain_id(chain_id);
pub(super) fn sign(private_key: &U256, digest: &B256) -> Result {
// The `ecrecover` precompile does not use EIP-155. No chain ID is needed.
let wallet = parse_wallet(private_key)?;

// The `ecrecover` precompile does not use EIP-155
let sig = wallet.sign_hash(digest.to_ethers())?;
let recovered = sig.recover(digest.to_ethers())?.to_alloy();
let sig = wallet.sign_hash_sync(*digest)?;
let recovered = sig.recover_address_from_prehash(digest)?;
Comment on lines +158 to +162
Copy link
Member

Choose a reason for hiding this comment

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

so the previous impl also didn't require the chain id?

Copy link
Member Author

Choose a reason for hiding this comment

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

correct


assert_eq!(recovered, wallet.address().to_alloy());
assert_eq!(recovered, wallet.address());

let mut r_bytes = [0u8; 32];
let mut s_bytes = [0u8; 32];
sig.r.to_big_endian(&mut r_bytes);
sig.s.to_big_endian(&mut s_bytes);
let v = U256::from(sig.v().y_parity_byte_non_eip155().unwrap_or(sig.v().y_parity_byte()));
let r = B256::from(sig.r());
let s = B256::from(sig.s());

Ok((sig.v, r_bytes, s_bytes).abi_encode())
Ok((v, r, s).abi_encode())
}

pub(super) fn sign_p256(private_key: &U256, digest: &B256, _state: &mut Cheatcodes) -> Result {
Expand Down Expand Up @@ -231,7 +229,7 @@ fn derive_key<W: Wordlist>(mnemonic: &str, path: &str, index: u32) -> Result {

let wallet = MnemonicBuilder::<W>::default()
.phrase(mnemonic)
.derivation_path(&derive_key_path(path, index))?
.derivation_path(derive_key_path(path, index))?
.build()?;
let private_key = U256::from_be_bytes(wallet.signer().to_bytes().into());
Ok(private_key.abi_encode())
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ alloy-json-abi.workspace = true
alloy-primitives = { workspace = true, features = ["serde", "getrandom", "arbitrary", "rlp"] }
alloy-sol-types.workspace = true
alloy-rpc-types.workspace = true
alloy-signer.workspace = true
hashbrown = { version = "0.14", features = ["serde"] }
revm = { workspace = true, default-features = false, features = [
"std",
Expand All @@ -37,7 +38,6 @@ revm = { workspace = true, default-features = false, features = [
] }
revm-inspectors.workspace = true

ethers-signers.workspace = true
itertools.workspace = true

eyre = "0.6"
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/evm/src/executors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::inspectors::{
use alloy_dyn_abi::{DynSolValue, FunctionExt, JsonAbiExt};
use alloy_json_abi::{Function, JsonAbi};
use alloy_primitives::{Address, Bytes, Log, U256};
use ethers_signers::LocalWallet;
use alloy_signer::LocalWallet;
use foundry_common::{abi::IntoFunction, evm::Breakpoints};
use foundry_evm_core::{
backend::{Backend, DatabaseError, DatabaseExt, DatabaseResult, FuzzBackendWrapper},
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/evm/src/inspectors/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{
StackSnapshotType, TracePrinter, TracingInspector, TracingInspectorConfig,
};
use alloy_primitives::{Address, Bytes, Log, B256, U256};
use ethers_signers::LocalWallet;
use alloy_signer::LocalWallet;
use foundry_evm_core::{backend::DatabaseExt, debug::DebugArena};
use foundry_evm_coverage::HitMaps;
use foundry_evm_traces::CallTraceArena;
Expand Down
5 changes: 3 additions & 2 deletions crates/forge/bin/cmd/script/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use forge::{
revm::interpreter::{return_ok, InstructionResult},
traces::{TraceKind, Traces},
};
use foundry_common::types::ToEthers;

/// Represents which simulation stage is the script execution at.
pub enum SimulationStage {
Expand Down Expand Up @@ -170,7 +171,7 @@ impl ScriptRunner {
traces,
debug,
address: None,
script_wallets,
script_wallets: script_wallets.to_ethers(),
..Default::default()
},
))
Expand Down Expand Up @@ -305,7 +306,7 @@ impl ScriptRunner {
labeled_addresses: labels,
transactions,
address: None,
script_wallets,
script_wallets: script_wallets.to_ethers(),
breakpoints,
})
}
Expand Down
Loading