Skip to content

Commit

Permalink
feat: apply code review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshvarma committed Sep 21, 2023
1 parent 1de5d16 commit 2dbc09b
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 235 deletions.
56 changes: 28 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ pallet-xc-asset-config = { path = "./pallets/xc-asset-config", default-features
pallet-xvm = { path = "./pallets/xvm", default-features = false }
pallet-xcm = { path = "./pallets/pallet-xcm", default-features = false }
pallet-ethereum-checked = { path = "./pallets/ethereum-checked", default-features = false }
pallet-account = { path = "./pallets/account", default-features = false }
pallet-unified-accounts = { path = "./pallets/account", default-features = false }

astar-primitives = { path = "./primitives", default-features = false }

Expand Down
2 changes: 1 addition & 1 deletion pallets/account/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "pallet-account"
name = "pallet-unified-accounts"
version = "0.1.0"
description = "Pallet for mapping VM accounts with native accounts"
authors.workspace = true
Expand Down
8 changes: 4 additions & 4 deletions pallets/account/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
}

#[benchmarks(
where <<T as Config>::ClaimSignature as ClaimSignature>::Signature: IsType<[u8;65]>
where <<T as Config>::SignatureHelper as SignatureHelper>::Signature: IsType<[u8;65]>
)]
mod benchmarks {
use super::*;

#[benchmark]
fn claim_evm_account() {
fn claim_evm_address() {
let caller: T::AccountId = whitelisted_caller();
let eth_secret_key = libsecp256k1::SecretKey::parse(&[0xff; 32]).unwrap();
let evm_address = Pallet::<T>::eth_address(&eth_secret_key);
let signature = Pallet::<T>::eth_sign_prehash(
&T::ClaimSignature::build_signing_payload(&caller),
&T::SignatureHelper::build_signing_payload(&caller),
&eth_secret_key,
)
.into();
Expand All @@ -59,7 +59,7 @@ mod benchmarks {
}

#[benchmark]
fn claim_default_evm_account() {
fn claim_default_evm_address() {
let caller: T::AccountId = whitelisted_caller();
let caller_clone = caller.clone();
let evm_address = T::DefaultAccountMapping::into_h160(caller.clone());
Expand Down
51 changes: 26 additions & 25 deletions pallets/account/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,43 @@ use sp_std::marker::PhantomData;

use crate::*;

/// AddressManager implementation
impl<T: Config> AddressManager<T::AccountId, EvmAddress> for Pallet<T> {
fn to_account_id(address: &EvmAddress) -> Option<T::AccountId> {
NativeAccounts::<T>::get(address)
/// UnifiedAddressMapper implementation
impl<T: Config> UnifiedAddressMapper<T::AccountId> for Pallet<T> {
fn to_account_id(evm_address: &EvmAddress) -> Option<T::AccountId> {
NativeToEvm::<T>::get(evm_address)
}

fn to_account_id_or_default(address: &EvmAddress) -> T::AccountId {
NativeAccounts::<T>::get(address).unwrap_or_else(|| {
fn to_account_id_or_default(evm_address: &EvmAddress) -> T::AccountId {
NativeToEvm::<T>::get(evm_address).unwrap_or_else(|| {
// fallback to default account_id
T::DefaultAddressMapping::into_account_id(address.clone())
T::DefaultAddressMapping::into_account_id(evm_address.clone())
})
}

fn to_default_account_id(address: &EvmAddress) -> T::AccountId {
T::DefaultAddressMapping::into_account_id(address.clone())
fn to_default_account_id(evm_address: &EvmAddress) -> T::AccountId {
T::DefaultAddressMapping::into_account_id(evm_address.clone())
}

fn to_address(account_id: &T::AccountId) -> Option<EvmAddress> {
EvmAccounts::<T>::get(account_id)
fn to_h160(account_id: &T::AccountId) -> Option<EvmAddress> {
EvmToNative::<T>::get(account_id)
}

fn to_address_or_default(account_id: &T::AccountId) -> EvmAddress {
EvmAccounts::<T>::get(account_id).unwrap_or_else(|| {
fn to_h160_or_default(account_id: &T::AccountId) -> EvmAddress {
EvmToNative::<T>::get(account_id).unwrap_or_else(|| {
// fallback to default account_id
T::DefaultAccountMapping::into_h160(account_id.clone())
})
}

fn to_default_address(account_id: &T::AccountId) -> EvmAddress {
fn to_default_h160(account_id: &T::AccountId) -> EvmAddress {
T::DefaultAccountMapping::into_h160(account_id.clone())
}
}

/// AccountMapping wrapper implementation over AddressManager
/// AccountMapping wrapper implementation
impl<T: Config> AccountMapping<T::AccountId> for Pallet<T> {
fn into_h160(account: T::AccountId) -> H160 {
<Self as AddressManager<T::AccountId, EvmAddress>>::to_address_or_default(&account)
<Self as UnifiedAddressMapper<T::AccountId>>::to_h160_or_default(&account)
}
}

Expand All @@ -80,10 +80,10 @@ impl<H: Hasher<Out = H256>> AccountMapping<AccountId> for HashedAccountMapping<H
}
}

/// AddresstMapping wrapper implementation over AddressManager
/// AddressMapping wrapper implementation
impl<T: Config> AddressMapping<T::AccountId> for Pallet<T> {
fn into_account_id(address: H160) -> T::AccountId {
<Self as AddressManager<T::AccountId, EvmAddress>>::to_account_id_or_default(&address)
fn into_account_id(evm_address: H160) -> T::AccountId {
<Self as UnifiedAddressMapper<T::AccountId>>::to_account_id_or_default(&evm_address)
}
}

Expand All @@ -93,9 +93,9 @@ pub struct KillAccountMapping<T>(PhantomData<T>);
impl<T: Config> OnKilledAccount<T::AccountId> for KillAccountMapping<T> {
fn on_killed_account(who: &T::AccountId) {
// remove mapping created by `claim_account` or `get_or_create_evm_address`
if let Some(evm_addr) = EvmAccounts::<T>::get(who) {
NativeAccounts::<T>::remove(evm_addr);
EvmAccounts::<T>::remove(who);
if let Some(evm_addr) = EvmToNative::<T>::take(who) {
NativeToEvm::<T>::remove(evm_addr);
EvmToNative::<T>::remove(who);
}
}
}
Expand All @@ -108,7 +108,7 @@ impl<T: Config> StaticLookup for Pallet<T> {
fn lookup(a: Self::Source) -> Result<Self::Target, LookupError> {
match a {
MultiAddress::Address20(i) => Ok(
<Self as AddressManager<T::AccountId, EvmAddress>>::to_account_id_or_default(
<Self as UnifiedAddressMapper<T::AccountId>>::to_account_id_or_default(
&EvmAddress::from_slice(&i),
),
),
Expand All @@ -122,12 +122,13 @@ impl<T: Config> StaticLookup for Pallet<T> {
}

/// EIP-712 compatible signature scheme for verifying ownership of EVM Address
/// https://eips.ethereum.org/EIPS/eip-712
///
/// Raw Data = Domain Separator + Type Hash + keccak256(AccountId)
pub struct EIP712Signature<T: Config, ChainId: Get<u64>>(PhantomData<(T, ChainId)>);
impl<T: Config, ChainId: Get<u64>> ClaimSignature for EIP712Signature<T, ChainId> {
impl<T: Config, ChainId: Get<u64>> SignatureHelper for EIP712Signature<T, ChainId> {
type AccountId = T::AccountId;
/// EVM address type
/// evm address type
type Address = EvmAddress;
/// A signature (a 512-bit value, plus 8 bits for recovery ID).
type Signature = [u8; 65];
Expand Down
Loading

0 comments on commit 2dbc09b

Please sign in to comment.