anvil_core/eth/
wallet.rs

1use alloy_primitives::{map::HashMap, Address, ChainId, U64};
2use serde::{Deserialize, Serialize};
3
4/// The capability to perform [EIP-7702][eip-7702] delegations, sponsored by the sequencer.
5///
6/// The sequencer will only perform delegations, and act on behalf of delegated accounts, if the
7/// account delegates to one of the addresses specified within this capability.
8///
9/// [eip-7702]: https://eips.ethereum.org/EIPS/eip-7702
10#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, Default)]
11pub struct DelegationCapability {
12    /// A list of valid delegation contracts.
13    pub addresses: Vec<Address>,
14}
15
16/// Wallet capabilities for a specific chain.
17#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, Default)]
18pub struct Capabilities {
19    /// The capability to delegate.
20    pub delegation: DelegationCapability,
21}
22
23/// A map of wallet capabilities per chain ID.
24#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, Default)]
25pub struct WalletCapabilities(HashMap<U64, Capabilities>);
26
27impl WalletCapabilities {
28    /// Get the capabilities of the wallet API for the specified chain ID.
29    pub fn get(&self, chain_id: ChainId) -> Option<&Capabilities> {
30        self.0.get(&U64::from(chain_id))
31    }
32
33    pub fn insert(&mut self, chain_id: ChainId, capabilities: Capabilities) {
34        self.0.insert(U64::from(chain_id), capabilities);
35    }
36}
37
38#[derive(Debug, thiserror::Error)]
39pub enum WalletError {
40    /// The transaction value is not 0.
41    ///
42    /// The value should be 0 to prevent draining the sequencer.
43    #[error("tx value not zero")]
44    ValueNotZero,
45    /// The from field is set on the transaction.
46    ///
47    /// Requests with the from field are rejected, since it is implied that it will always be the
48    /// sequencer.
49    #[error("tx from field is set")]
50    FromSet,
51    /// The nonce field is set on the transaction.
52    ///
53    /// Requests with the nonce field set are rejected, as this is managed by the sequencer.
54    #[error("tx nonce is set")]
55    NonceSet,
56    /// An authorization item was invalid.
57    ///
58    /// The item is invalid if it tries to delegate an account to a contract that is not
59    /// whitelisted.
60    #[error("invalid authorization address")]
61    InvalidAuthorization,
62    /// The to field of the transaction was invalid.
63    ///
64    /// The destination is invalid if:
65    ///
66    /// - There is no bytecode at the destination, or
67    /// - The bytecode is not an EIP-7702 delegation designator, or
68    /// - The delegation designator points to a contract that is not whitelisted
69    #[error("the destination of the transaction is not a delegated account")]
70    IllegalDestination,
71    /// The transaction request was invalid.
72    ///
73    /// This is likely an internal error, as most of the request is built by the sequencer.
74    #[error("invalid tx request")]
75    InvalidTransactionRequest,
76    /// An internal error occurred.
77    #[error("internal error")]
78    InternalError,
79}