git.delta.rocks / unique-network / refs/commits / 8a829ea38883

difftreelog

source

runtime/common/ethereum/precompiles/sr25519.rs3.2 KiBsourcehistory
1// Copyright 2019-2022 PureStake Inc.2// Copyright 2022      Stake Technologies34// Astar Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Astar Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Astar Network. If not, see <http://www.gnu.org/licenses/>.1617use fp_evm::{Context, ExitSucceed, PrecompileHandle, PrecompileOutput};18use pallet_evm::Precompile;19use sp_core::{crypto::UncheckedFrom, sr25519, H256};20use sp_std::marker::PhantomData;21use sp_std::prelude::*;2223use super::utils::{Bytes, EvmDataReader, EvmDataWriter, EvmResult, FunctionModifier, Gasometer};2425#[precompile_utils_macro::generate_function_selector]26#[derive(Debug, PartialEq)]27pub enum Action {28	Verify = "verify(bytes32,bytes,bytes)",29}3031/// A precompile to wrap substrate sr25519 functions.32pub struct Sr25519Precompile<Runtime>(PhantomData<Runtime>);3334impl<Runtime: pallet_evm::Config> Precompile for Sr25519Precompile<Runtime> {35	fn execute(handle: &mut impl PrecompileHandle) -> EvmResult<PrecompileOutput> {36		log::trace!(target: "sr25519-precompile", "In sr25519 precompile");3738		let gasometer = Gasometer::new();3940		let (mut input, selector) = EvmDataReader::new_with_selector(&gasometer, handle.input())?;41		let input = &mut input;4243		gasometer.check_function_modifier(44			handle.context(),45			handle.is_static(),46			FunctionModifier::View,47		)?;4849		match selector {50			// Dispatchables51			Action::Verify => Self::verify(input, &gasometer, handle.context()),52		}53	}54}5556impl<Runtime: pallet_evm::Config> Sr25519Precompile<Runtime> {57	fn verify(58		input: &mut EvmDataReader,59		gasometer: &Gasometer,60		_: &Context,61	) -> EvmResult<PrecompileOutput> {62		// Bound check63		input.expect_arguments(gasometer, 3)?;6465		// Parse arguments66		let public: sr25519::Public =67			sr25519::Public::unchecked_from(input.read::<H256>(gasometer)?).into();68		let signature_bytes: Vec<u8> = input.read::<Bytes>(gasometer)?.into();69		let message: Vec<u8> = input.read::<Bytes>(gasometer)?.into();7071		// Parse signature72		let signature_opt = sr25519::Signature::from_slice(&signature_bytes[..]);7374		let signature = if let Some(sig) = signature_opt {75			sig76		} else {77			// Return `false` if signature length is wrong78			return Ok(PrecompileOutput {79				exit_status: ExitSucceed::Returned,80				output: EvmDataWriter::new().write(false).build(),81			});82		};8384		log::trace!(85			target: "sr25519-precompile",86			"Verify signature {:?} for public {:?} and message {:?}",87			signature, public, message,88		);8990		let is_confirmed = sp_io::crypto::sr25519_verify(&signature, &message[..], &public);9192		log::trace!(93			target: "sr25519-precompile",94			"Verified signature {:?} is {:?}",95			signature, is_confirmed,96		);9798		Ok(PrecompileOutput {99			exit_status: ExitSucceed::Returned,100			output: EvmDataWriter::new().write(is_confirmed).build(),101		})102	}103}