evm_domain_runtime/
precompiles.rs

1use pallet_evm::{
2    IsPrecompileResult, Precompile, PrecompileHandle, PrecompileResult, PrecompileSet,
3};
4use sp_core::H160;
5use sp_std::marker::PhantomData;
6
7use pallet_evm_precompile_modexp::Modexp;
8use pallet_evm_precompile_sha3fips::Sha3FIPS256;
9use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256};
10
11pub struct Precompiles<R>(PhantomData<R>);
12
13impl<R> Precompiles<R>
14where
15    R: pallet_evm::Config,
16{
17    pub fn used_addresses() -> [H160; 7] {
18        [
19            hash(1),
20            hash(2),
21            hash(3),
22            hash(4),
23            hash(5),
24            hash(1024),
25            hash(1025),
26        ]
27    }
28}
29
30impl<R> Default for Precompiles<R> {
31    #[inline]
32    fn default() -> Self {
33        Self(PhantomData)
34    }
35}
36
37impl<R> PrecompileSet for Precompiles<R>
38where
39    R: pallet_evm::Config,
40{
41    fn execute(&self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {
42        match handle.code_address() {
43            // Ethereum precompiles :
44            a if a == hash(1) => Some(ECRecover::execute(handle)),
45            a if a == hash(2) => Some(Sha256::execute(handle)),
46            a if a == hash(3) => Some(Ripemd160::execute(handle)),
47            a if a == hash(4) => Some(Identity::execute(handle)),
48            a if a == hash(5) => Some(Modexp::execute(handle)),
49            // Non-Frontier specific nor Ethereum precompiles :
50            a if a == hash(1024) => Some(Sha3FIPS256::execute(handle)),
51            a if a == hash(1025) => Some(ECRecoverPublicKey::execute(handle)),
52            _ => None,
53        }
54    }
55
56    fn is_precompile(&self, address: H160, _gas: u64) -> IsPrecompileResult {
57        IsPrecompileResult::Answer {
58            is_precompile: Self::used_addresses().contains(&address),
59            extra_cost: 0,
60        }
61    }
62}
63
64fn hash(a: u64) -> H160 {
65    H160::from_low_u64_be(a)
66}