|
| 1 | +use alloy::{ |
| 2 | + primitives::{Address, FixedBytes}, |
| 3 | + providers::Provider, |
| 4 | +}; |
| 5 | +use engine_core::{ |
| 6 | + chain::Chain, |
| 7 | + credentials::SigningCredential, |
| 8 | + error::{AlloyRpcErrorToEngineError, EngineError}, |
| 9 | + signer::{AccountSigner, EoaSigner, EoaSigningOptions}, |
| 10 | +}; |
| 11 | +use rand::Rng; |
| 12 | + |
| 13 | +use crate::constants::{ |
| 14 | + EIP_7702_DELEGATION_CODE_LENGTH, EIP_7702_DELEGATION_PREFIX, |
| 15 | + MINIMAL_ACCOUNT_IMPLEMENTATION_ADDRESS, |
| 16 | +}; |
| 17 | + |
| 18 | +/// Represents an EOA address that can have EIP-7702 delegation, associated with a specific chain |
| 19 | +#[derive(Clone, Debug)] |
| 20 | +pub struct DelegatedAccount<C: Chain> { |
| 21 | + /// The EOA address that may have delegation |
| 22 | + pub eoa_address: Address, |
| 23 | + /// The chain this account operates on |
| 24 | + pub chain: C, |
| 25 | +} |
| 26 | + |
| 27 | +impl<C: Chain> DelegatedAccount<C> { |
| 28 | + /// Create a new delegated account from an EOA address and chain |
| 29 | + pub fn new(eoa_address: Address, chain: C) -> Self { |
| 30 | + Self { eoa_address, chain } |
| 31 | + } |
| 32 | + |
| 33 | + /// Check if the EOA has EIP-7702 delegation to the minimal account implementation |
| 34 | + pub async fn is_minimal_account(&self) -> Result<bool, EngineError> { |
| 35 | + // Get the bytecode at the EOA address using eth_getCode |
| 36 | + let code = self |
| 37 | + .chain |
| 38 | + .provider() |
| 39 | + .get_code_at(self.eoa_address) |
| 40 | + .await |
| 41 | + .map_err(|e| e.to_engine_error(self.chain()))?; |
| 42 | + |
| 43 | + tracing::debug!( |
| 44 | + eoa_address = ?self.eoa_address, |
| 45 | + code_length = code.len(), |
| 46 | + code_hex = ?alloy::hex::encode(&code), |
| 47 | + "Checking EIP-7702 delegation" |
| 48 | + ); |
| 49 | + |
| 50 | + // Check if code exists and starts with EIP-7702 delegation prefix "0xef0100" |
| 51 | + if code.len() < EIP_7702_DELEGATION_CODE_LENGTH |
| 52 | + || !code.starts_with(&EIP_7702_DELEGATION_PREFIX) |
| 53 | + { |
| 54 | + tracing::debug!( |
| 55 | + eoa_address = ?self.eoa_address, |
| 56 | + has_delegation = false, |
| 57 | + reason = "Code too short or doesn't start with EIP-7702 prefix", |
| 58 | + "EIP-7702 delegation check result" |
| 59 | + ); |
| 60 | + return Ok(false); |
| 61 | + } |
| 62 | + |
| 63 | + // Extract the target address from bytes 3-23 (20 bytes for address) |
| 64 | + // EIP-7702 format: 0xef0100 + 20 bytes address |
| 65 | + let target_bytes = &code[3..23]; |
| 66 | + let target_address = Address::from_slice(target_bytes); |
| 67 | + |
| 68 | + // Compare with the minimal account implementation address |
| 69 | + let is_delegated = target_address == MINIMAL_ACCOUNT_IMPLEMENTATION_ADDRESS; |
| 70 | + |
| 71 | + tracing::debug!( |
| 72 | + eoa_address = ?self.eoa_address, |
| 73 | + target_address = ?target_address, |
| 74 | + minimal_account_address = ?MINIMAL_ACCOUNT_IMPLEMENTATION_ADDRESS, |
| 75 | + has_delegation = is_delegated, |
| 76 | + "EIP-7702 delegation check result" |
| 77 | + ); |
| 78 | + |
| 79 | + Ok(is_delegated) |
| 80 | + } |
| 81 | + |
| 82 | + /// Get the EOA address |
| 83 | + pub fn address(&self) -> Address { |
| 84 | + self.eoa_address |
| 85 | + } |
| 86 | + |
| 87 | + /// Get the current nonce for the EOA |
| 88 | + pub async fn get_nonce(&self) -> Result<u64, EngineError> { |
| 89 | + self.chain |
| 90 | + .provider() |
| 91 | + .get_transaction_count(self.eoa_address) |
| 92 | + .await |
| 93 | + .map_err(|e| e.to_engine_error(self.chain())) |
| 94 | + } |
| 95 | + |
| 96 | + /// Get a reference to the chain |
| 97 | + pub fn chain(&self) -> &C { |
| 98 | + &self.chain |
| 99 | + } |
| 100 | + |
| 101 | + /// Sign authorization for EIP-7702 delegation (automatically fetches nonce) |
| 102 | + pub async fn sign_authorization( |
| 103 | + &self, |
| 104 | + eoa_signer: &EoaSigner, |
| 105 | + credentials: &SigningCredential, |
| 106 | + ) -> Result<alloy::eips::eip7702::SignedAuthorization, EngineError> { |
| 107 | + let nonce = self.get_nonce().await?; |
| 108 | + |
| 109 | + let signing_options = EoaSigningOptions { |
| 110 | + from: self.eoa_address, |
| 111 | + chain_id: Some(self.chain.chain_id()), |
| 112 | + }; |
| 113 | + |
| 114 | + eoa_signer |
| 115 | + .sign_authorization( |
| 116 | + signing_options, |
| 117 | + self.chain.chain_id(), |
| 118 | + MINIMAL_ACCOUNT_IMPLEMENTATION_ADDRESS, |
| 119 | + nonce, |
| 120 | + credentials, |
| 121 | + ) |
| 122 | + .await |
| 123 | + } |
| 124 | + |
| 125 | + /// Generate a random UID for wrapped calls |
| 126 | + pub fn generate_random_uid() -> FixedBytes<32> { |
| 127 | + let mut rng = rand::rng(); |
| 128 | + let mut bytes = [0u8; 32]; |
| 129 | + rng.fill(&mut bytes); |
| 130 | + FixedBytes::from(bytes) |
| 131 | + } |
| 132 | +} |
0 commit comments