Skip to content

Commit 15adbe4

Browse files
committed
feat(anvil): bypass authorization signatures for testing
1 parent 575bf62 commit 15adbe4

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

crates/anvil/src/eth/backend/cheats.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
//! Support for "cheat codes" / bypass functions
22
3+
use alloy_eips::eip7702::SignedAuthorization;
34
use alloy_evm::precompiles::{Precompile, PrecompileInput};
45
use alloy_primitives::{
5-
Address, Bytes,
6+
Address, Bytes, U256,
67
map::{AddressHashSet, foldhash::HashMap},
78
};
9+
use alloy_rpc_types::Authorization;
810
use parking_lot::RwLock;
911
use revm::precompile::{
1012
PrecompileError, PrecompileOutput, PrecompileResult, secp256k1::ec_recover_run,
@@ -84,6 +86,38 @@ impl CheatsManager {
8486
pub fn has_recover_overrides(&self) -> bool {
8587
!self.state.read().signature_overrides.is_empty()
8688
}
89+
90+
/// Creates authorization entries for impersonated accounts with signature overrides.
91+
/// This allows impersonated accounts to be used in EIP-7702 transactions.
92+
pub fn create_impersonated_authorizations(
93+
&self,
94+
authorizations: &[SignedAuthorization],
95+
chain_id: u64,
96+
) -> Vec<SignedAuthorization> {
97+
let mut authorization_list = authorizations.to_vec();
98+
for addr in self.impersonated_accounts() {
99+
let auth = Authorization { chain_id: U256::from(chain_id), address: addr, nonce: 0 };
100+
101+
let signed_auth = SignedAuthorization::new_unchecked(
102+
auth,
103+
0, // y_parity
104+
U256::from(1), // r
105+
U256::from(1), // s
106+
);
107+
108+
let mut sig_bytes = [0u8; 65];
109+
let r_bytes = signed_auth.r().to_be_bytes::<32>();
110+
let s_bytes = signed_auth.s().to_be_bytes::<32>();
111+
sig_bytes[..32].copy_from_slice(&r_bytes);
112+
sig_bytes[32..64].copy_from_slice(&s_bytes);
113+
sig_bytes[64] = signed_auth.y_parity();
114+
let sig = Bytes::copy_from_slice(&sig_bytes);
115+
116+
self.add_recover_override(sig, addr);
117+
authorization_list.push(signed_auth);
118+
}
119+
authorization_list
120+
}
87121
}
88122

89123
/// Container type for all the state variables

crates/anvil/src/eth/backend/executor.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,14 @@ impl<DB: Db + ?Sized, V: TransactionValidator> TransactionExecutor<'_, DB, V> {
268268
fn env_for(&self, tx: &PendingTransaction) -> Env {
269269
let mut tx_env = tx.to_revm_tx_env();
270270

271+
if let TypedTransaction::EIP7702(tx_7702) = &tx.transaction.transaction {
272+
let authorization_list = self.cheats.create_impersonated_authorizations(
273+
&tx_7702.tx().authorization_list,
274+
self.cfg_env.chain_id,
275+
);
276+
tx_env.base.set_signed_authorization(authorization_list);
277+
}
278+
271279
if self.optimism {
272280
tx_env.enveloped_tx = Some(alloy_rlp::encode(&tx.transaction.transaction).into());
273281
}

0 commit comments

Comments
 (0)