pallet_revive/precompiles/builtin/
ecrecover.rs1use crate::{
19 precompiles::{BuiltinAddressMatcher, Error, Ext, PrimitivePrecompile},
20 vm::RuntimeCosts,
21 Config,
22};
23use alloc::vec::Vec;
24use core::{marker::PhantomData, num::NonZero};
25
26pub struct EcRecover<T>(PhantomData<T>);
27
28impl<T: Config> PrimitivePrecompile for EcRecover<T> {
29 type T = T;
30 const MATCHER: BuiltinAddressMatcher = BuiltinAddressMatcher::Fixed(NonZero::new(1).unwrap());
31 const HAS_CONTRACT_INFO: bool = false;
32
33 fn call(
34 _address: &[u8; 20],
35 i: Vec<u8>,
36 env: &mut impl Ext<T = Self::T>,
37 ) -> Result<Vec<u8>, Error> {
38 env.gas_meter_mut().charge(RuntimeCosts::EcdsaRecovery)?;
39 let mut input = [0u8; 128];
40 let len = i.len().min(128);
41 input[..len].copy_from_slice(&i[..len]);
42
43 let mut msg = [0u8; 32];
44 let mut sig = [0u8; 65];
45
46 msg[0..32].copy_from_slice(&input[0..32]);
47 sig[0..32].copy_from_slice(&input[64..96]); sig[32..64].copy_from_slice(&input[96..128]); sig[64] = input[63]; if input[32..63] != [0u8; 31] || ![27, 28].contains(&input[63]) {
54 return Ok(Vec::new());
55 }
56
57 let data = match sp_io::crypto::secp256k1_ecdsa_recover(&sig, &msg) {
58 Ok(pubkey) => {
59 let mut address = sp_io::hashing::keccak_256(&pubkey);
60 address[0..12].copy_from_slice(&[0u8; 12]);
61 address.to_vec()
62 },
63 Err(_) => Vec::new(),
64 };
65
66 Ok(data)
67 }
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73 use crate::{precompiles::tests::run_test_vectors, tests::Test};
74
75 #[test]
76 fn test_ecrecover() {
77 run_test_vectors::<EcRecover<Test>>(include_str!("./testdata/1-ecRecover.json"));
78 }
79}