-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial config setup * implement other functions * add tests * fmt * update .sol file * refactor name * updated mock * change interface name, refactor * remove calldata from interface * fmt * update fp-evm * feat: minor improvements * feat: apply code suggestions --------- Co-authored-by: Ashutosh Varma <ashutoshvarma11@live.com>
- Loading branch information
1 parent
1cff317
commit 2a8df8d
Showing
11 changed files
with
661 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
[package] | ||
name = "pallet-evm-precompile-unified-accounts" | ||
description = "Evm Precompile for AU" | ||
version = "0.1.0" | ||
authors.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
|
||
[dependencies] | ||
hex = { workspace = true } | ||
log = { workspace = true } | ||
num_enum = { workspace = true } | ||
precompile-utils = { workspace = true } | ||
|
||
# Substrate | ||
frame-support = { workspace = true } | ||
frame-system = { workspace = true } | ||
parity-scale-codec = { workspace = true, features = ["max-encoded-len"] } | ||
sp-core = { workspace = true } | ||
sp-io = { workspace = true } | ||
sp-runtime = { workspace = true } | ||
sp-std = { workspace = true } | ||
|
||
# Frontier | ||
fp-evm = { workspace = true } | ||
pallet-evm = { workspace = true } | ||
pallet-unified-accounts = { workspace = true } | ||
|
||
# Astar | ||
astar-primitives = { workspace = true } | ||
|
||
[dev-dependencies] | ||
derive_more = { workspace = true } | ||
hex-literal = { workspace = true } | ||
scale-info = { workspace = true } | ||
serde = { workspace = true } | ||
|
||
precompile-utils = { workspace = true, features = ["testing"] } | ||
|
||
ethers = { workspace = true } | ||
libsecp256k1 = { workspace = true, features = ["hmac", "static-context"] } | ||
pallet-balances = { workspace = true, features = ["std"] } | ||
pallet-timestamp = { workspace = true } | ||
sp-runtime = { workspace = true } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"parity-scale-codec/std", | ||
"pallet-unified-accounts/std", | ||
"fp-evm/std", | ||
"frame-support/std", | ||
"frame-system/std", | ||
"pallet-evm/std", | ||
"precompile-utils/std", | ||
"sp-core/std", | ||
"sp-std/std", | ||
"sp-io/std", | ||
"sp-runtime/std", | ||
"astar-primitives/std", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @title UA interface. | ||
*/ | ||
|
||
/// Interface to the precompiled contract | ||
/// Predeployed at the address 0x0000000000000000000000000000000000005006 | ||
/// For better understanding check the source code: | ||
/// repo: https://github.com/AstarNetwork/astar | ||
/// code: pallets/unified-accounts/src/lib.rs | ||
interface UnifiedAccounts { | ||
/// Gets the evm address associated with given account id. If no mapping exists, | ||
/// then return the default account id. | ||
/// @param accountId: The account id for which you want the evm address for. | ||
/// @return (mapped_address, true) if there is a mapping found otherwise (default_address, false) | ||
function get_evm_address_or_default( | ||
bytes32 accountId | ||
) external view returns (address, bool); | ||
|
||
/// Gets the account id associated with given evm address. If no mapping exists, | ||
/// then return the default evm address. | ||
/// @param evmAddress: The evm address for which you want the account id for. | ||
/// @return (mapped_account, true) if there is a mapping found otherwise (default_account, false) | ||
function get_native_address_or_default( | ||
address evmAddress | ||
) external view returns (bytes32, bool); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// This file is part of Astar. | ||
|
||
// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
// Astar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Astar is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Astar. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
use astar_primitives::evm::{UnifiedAddress, UnifiedAddressMapper}; | ||
use core::marker::PhantomData; | ||
use fp_evm::Precompile; | ||
use fp_evm::{PrecompileHandle, PrecompileOutput}; | ||
use frame_support::dispatch::Dispatchable; | ||
use frame_support::traits::IsType; | ||
use precompile_utils::{ | ||
succeed, Address, EvmDataWriter, EvmResult, FunctionModifier, PrecompileHandleExt, | ||
}; | ||
use sp_core::{crypto::AccountId32, H256}; | ||
use sp_std::prelude::*; | ||
|
||
#[cfg(test)] | ||
mod mock; | ||
#[cfg(test)] | ||
mod tests; | ||
|
||
#[precompile_utils::generate_function_selector] | ||
#[derive(Debug, PartialEq)] | ||
pub enum Action { | ||
GetEvmAddressOrDefault = "get_evm_address_or_default(bytes32)", | ||
GetNativeAddressOrDefault = "get_native_address_or_default(address)", | ||
} | ||
|
||
/// A precompile that expose AU related functions. | ||
pub struct UnifiedAccountsPrecompile<T, UA>(PhantomData<(T, UA)>); | ||
|
||
impl<R, UA> Precompile for UnifiedAccountsPrecompile<R, UA> | ||
where | ||
R: pallet_evm::Config + pallet_unified_accounts::Config, | ||
<<R as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin: | ||
From<Option<R::AccountId>>, | ||
<R as frame_system::Config>::AccountId: IsType<AccountId32>, | ||
UA: UnifiedAddressMapper<R::AccountId>, | ||
{ | ||
fn execute(handle: &mut impl PrecompileHandle) -> EvmResult<PrecompileOutput> { | ||
log::trace!(target: "au-precompile", "Execute input = {:?}", handle.input()); | ||
|
||
let selector = handle.read_selector()?; | ||
|
||
handle.check_function_modifier(FunctionModifier::View)?; | ||
|
||
match selector { | ||
// Dispatchables | ||
Action::GetEvmAddressOrDefault => Self::get_evm_address_or_default(handle), | ||
Action::GetNativeAddressOrDefault => Self::get_native_address_or_default(handle), | ||
} | ||
} | ||
} | ||
|
||
impl<R, UA> UnifiedAccountsPrecompile<R, UA> | ||
where | ||
R: pallet_evm::Config + pallet_unified_accounts::Config, | ||
<<R as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin: | ||
From<Option<R::AccountId>>, | ||
<R as frame_system::Config>::AccountId: IsType<AccountId32>, | ||
UA: UnifiedAddressMapper<R::AccountId>, | ||
{ | ||
fn get_evm_address_or_default( | ||
handle: &mut impl PrecompileHandle, | ||
) -> EvmResult<PrecompileOutput> { | ||
let mut input = handle.read_input()?; | ||
input.expect_arguments(1)?; | ||
let account_id = AccountId32::new(input.read::<H256>()?.into()).into(); | ||
|
||
let output: (Address, bool) = match UA::to_h160_or_default(&account_id) { | ||
UnifiedAddress::Mapped(address) => (address.into(), true), | ||
UnifiedAddress::Default(address) => (address.into(), false), | ||
}; | ||
Ok(succeed(EvmDataWriter::new().write(output).build())) | ||
} | ||
|
||
fn get_native_address_or_default( | ||
handle: &mut impl PrecompileHandle, | ||
) -> EvmResult<PrecompileOutput> { | ||
let mut input = handle.read_input()?; | ||
input.expect_arguments(1)?; | ||
let evm_address = input.read::<Address>()?; | ||
|
||
let output: (H256, bool) = match UA::to_account_id_or_default(&evm_address.into()) { | ||
UnifiedAddress::Mapped(account_id) => (H256::from(account_id.into().as_ref()), true), | ||
UnifiedAddress::Default(account_id) => (H256::from(account_id.into().as_ref()), false), | ||
}; | ||
Ok(succeed(EvmDataWriter::new().write(output).build())) | ||
} | ||
} |
Oops, something went wrong.