difftreelog
refactor extract EvmAddressMapping primitive
in: master
10 files changed
pallets/common/Cargo.tomldiffbeforeafterboth--- a/pallets/common/Cargo.toml
+++ b/pallets/common/Cargo.toml
@@ -15,10 +15,10 @@
sp-runtime = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
sp-std = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
sp-core = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
+up-evm-mapping = { default-features = false, path = '../../primitives/evm-mapping' }
nft-data-structs = { default-features = false, path = '../../primitives/nft' }
pallet-evm-coder-substrate = { default-features = false, path = '../../pallets/evm-coder-substrate' }
evm-coder = { default-features = false, path = '../../crates/evm-coder' }
-
pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
serde = { version = "1.0.130", default-features = false }
scale-info = { version = "1.0.0", default-features = false, features = [
@@ -32,6 +32,7 @@
"frame-system/std",
"sp-runtime/std",
"sp-std/std",
+ "up-evm-mapping/std",
"nft-data-structs/std",
"pallet-evm/std",
]
pallets/common/src/account.rsdiffbeforeafterboth1use crate::Config;2use codec::{Encode, EncodeLike, Decode};3use sp_core::H160;4use scale_info::{Type, TypeInfo};5use sp_core::crypto::AccountId32;6use core::cmp::Ordering;7use serde::{Serialize, Deserialize};8use pallet_evm::AddressMapping;9use sp_std::vec::Vec;10use sp_std::clone::Clone;1112pub trait CrossAccountId<AccountId>:13 Encode + EncodeLike + Decode + TypeInfo + Clone + PartialEq + Ord + core::fmt::Debug + Default14// +15// Serialize + Deserialize<'static>16{17 fn as_sub(&self) -> &AccountId;18 fn as_eth(&self) -> &H160;1920 fn from_sub(account: AccountId) -> Self;21 fn from_eth(account: H160) -> Self;2223 fn conv_eq(&self, other: &Self) -> bool;24}2526#[derive(Encode, Decode, Serialize, Deserialize, TypeInfo)]27#[serde(rename_all = "camelCase")]28enum BasicCrossAccountIdRepr<AccountId> {29 Substrate(AccountId),30 Ethereum(H160),31}3233#[derive(PartialEq, Eq)]34pub struct BasicCrossAccountId<T: Config> {35 /// If true - then ethereum is canonical encoding36 from_ethereum: bool,37 substrate: T::AccountId,38 ethereum: H160,39}4041impl<T: Config> TypeInfo for BasicCrossAccountId<T> {42 type Identity = Self;4344 fn type_info() -> Type {45 <BasicCrossAccountIdRepr<T::AccountId>>::type_info()46 }47}4849impl<T: Config> Default for BasicCrossAccountId<T> {50 fn default() -> Self {51 Self::from_sub(T::AccountId::default())52 }53}5455impl<T: Config> core::fmt::Debug for BasicCrossAccountId<T> {56 fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {57 if self.from_ethereum {58 fmt.debug_tuple("CrossAccountId::Ethereum")59 .field(&self.ethereum)60 .finish()61 } else {62 fmt.debug_tuple("CrossAccountId::Substrate")63 .field(&self.substrate)64 .finish()65 }66 }67}6869impl<T: Config> PartialOrd for BasicCrossAccountId<T> {70 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {71 Some(self.substrate.cmp(&other.substrate))72 }73}7475impl<T: Config> Ord for BasicCrossAccountId<T> {76 fn cmp(&self, other: &Self) -> Ordering {77 self.partial_cmp(other)78 .expect("substrate account is total ordered")79 }80}8182impl<T: Config> Clone for BasicCrossAccountId<T> {83 fn clone(&self) -> Self {84 Self {85 from_ethereum: self.from_ethereum,86 substrate: self.substrate.clone(),87 ethereum: self.ethereum,88 }89 }90}91impl<T: Config> Encode for BasicCrossAccountId<T> {92 fn encode(&self) -> Vec<u8> {93 BasicCrossAccountIdRepr::from(self.clone()).encode()94 }95}96impl<T: Config> EncodeLike for BasicCrossAccountId<T> {}97impl<T: Config> Decode for BasicCrossAccountId<T> {98 fn decode<I>(input: &mut I) -> Result<Self, codec::Error>99 where100 I: codec::Input,101 {102 Ok(BasicCrossAccountIdRepr::decode(input)?.into())103 }104}105impl<T> Serialize for BasicCrossAccountId<T>106where107 T: Config,108 T::AccountId: Serialize,109{110 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>111 where112 S: serde::Serializer,113 {114 let repr = BasicCrossAccountIdRepr::from(self.clone());115 (&repr).serialize(serializer)116 }117}118impl<'de, T> Deserialize<'de> for BasicCrossAccountId<T>119where120 T: Config,121 T::AccountId: Deserialize<'de>,122{123 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>124 where125 D: serde::Deserializer<'de>,126 {127 Ok(BasicCrossAccountIdRepr::deserialize(deserializer)?.into())128 }129}130impl<T: Config> CrossAccountId<T::AccountId> for BasicCrossAccountId<T> {131 fn as_sub(&self) -> &T::AccountId {132 &self.substrate133 }134 fn as_eth(&self) -> &H160 {135 &self.ethereum136 }137 fn from_sub(substrate: T::AccountId) -> Self {138 Self {139 ethereum: T::EvmBackwardsAddressMapping::from_account_id(substrate.clone()),140 substrate,141 from_ethereum: false,142 }143 }144 fn from_eth(ethereum: H160) -> Self {145 Self {146 ethereum,147 substrate: T::EvmAddressMapping::into_account_id(ethereum),148 from_ethereum: true,149 }150 }151 fn conv_eq(&self, other: &Self) -> bool {152 if self.from_ethereum == other.from_ethereum {153 self.substrate == other.substrate && self.ethereum == other.ethereum154 } else if self.from_ethereum {155 // ethereum is canonical encoding, but we need to compare derived address156 self.substrate == other.substrate157 } else {158 self.ethereum == other.ethereum159 }160 }161}162impl<T: Config> From<BasicCrossAccountIdRepr<T::AccountId>> for BasicCrossAccountId<T> {163 fn from(repr: BasicCrossAccountIdRepr<T::AccountId>) -> Self {164 match repr {165 BasicCrossAccountIdRepr::Substrate(s) => Self::from_sub(s),166 BasicCrossAccountIdRepr::Ethereum(e) => Self::from_eth(e),167 }168 }169}170impl<T: Config> From<BasicCrossAccountId<T>> for BasicCrossAccountIdRepr<T::AccountId> {171 fn from(v: BasicCrossAccountId<T>) -> Self {172 if v.from_ethereum {173 BasicCrossAccountIdRepr::Ethereum(*v.as_eth())174 } else {175 BasicCrossAccountIdRepr::Substrate(v.as_sub().clone())176 }177 }178}179180pub trait EvmBackwardsAddressMapping<AccountId> {181 fn from_account_id(account_id: AccountId) -> H160;182}183184/// Should have same mapping as EnsureAddressTruncated185pub struct MapBackwardsAddressTruncated;186impl EvmBackwardsAddressMapping<AccountId32> for MapBackwardsAddressTruncated {187 fn from_account_id(account_id: AccountId32) -> H160 {188 let mut out = [0; 20];189 out.copy_from_slice(&(account_id.as_ref() as &[u8])[0..20]);190 H160(out)191 }192}1use crate::Config;2use codec::{Encode, EncodeLike, Decode};3use sp_core::H160;4use scale_info::{Type, TypeInfo};5use core::cmp::Ordering;6use serde::{Serialize, Deserialize};7use pallet_evm::AddressMapping;8use sp_std::vec::Vec;9use sp_std::clone::Clone;10use up_evm_mapping::EvmBackwardsAddressMapping;1112pub trait CrossAccountId<AccountId>:13 Encode + EncodeLike + Decode + TypeInfo + Clone + PartialEq + Ord + core::fmt::Debug + Default14// +15// Serialize + Deserialize<'static>16{17 fn as_sub(&self) -> &AccountId;18 fn as_eth(&self) -> &H160;1920 fn from_sub(account: AccountId) -> Self;21 fn from_eth(account: H160) -> Self;2223 fn conv_eq(&self, other: &Self) -> bool;24}2526#[derive(Encode, Decode, Serialize, Deserialize, TypeInfo)]27#[serde(rename_all = "camelCase")]28enum BasicCrossAccountIdRepr<AccountId> {29 Substrate(AccountId),30 Ethereum(H160),31}3233#[derive(PartialEq, Eq)]34pub struct BasicCrossAccountId<T: Config> {35 /// If true - then ethereum is canonical encoding36 from_ethereum: bool,37 substrate: T::AccountId,38 ethereum: H160,39}4041impl<T: Config> TypeInfo for BasicCrossAccountId<T> {42 type Identity = Self;4344 fn type_info() -> Type {45 <BasicCrossAccountIdRepr<T::AccountId>>::type_info()46 }47}4849impl<T: Config> Default for BasicCrossAccountId<T> {50 fn default() -> Self {51 Self::from_sub(T::AccountId::default())52 }53}5455impl<T: Config> core::fmt::Debug for BasicCrossAccountId<T> {56 fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {57 if self.from_ethereum {58 fmt.debug_tuple("CrossAccountId::Ethereum")59 .field(&self.ethereum)60 .finish()61 } else {62 fmt.debug_tuple("CrossAccountId::Substrate")63 .field(&self.substrate)64 .finish()65 }66 }67}6869impl<T: Config> PartialOrd for BasicCrossAccountId<T> {70 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {71 Some(self.substrate.cmp(&other.substrate))72 }73}7475impl<T: Config> Ord for BasicCrossAccountId<T> {76 fn cmp(&self, other: &Self) -> Ordering {77 self.partial_cmp(other)78 .expect("substrate account is total ordered")79 }80}8182impl<T: Config> Clone for BasicCrossAccountId<T> {83 fn clone(&self) -> Self {84 Self {85 from_ethereum: self.from_ethereum,86 substrate: self.substrate.clone(),87 ethereum: self.ethereum,88 }89 }90}91impl<T: Config> Encode for BasicCrossAccountId<T> {92 fn encode(&self) -> Vec<u8> {93 BasicCrossAccountIdRepr::from(self.clone()).encode()94 }95}96impl<T: Config> EncodeLike for BasicCrossAccountId<T> {}97impl<T: Config> Decode for BasicCrossAccountId<T> {98 fn decode<I>(input: &mut I) -> Result<Self, codec::Error>99 where100 I: codec::Input,101 {102 Ok(BasicCrossAccountIdRepr::decode(input)?.into())103 }104}105impl<T> Serialize for BasicCrossAccountId<T>106where107 T: Config,108 T::AccountId: Serialize,109{110 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>111 where112 S: serde::Serializer,113 {114 let repr = BasicCrossAccountIdRepr::from(self.clone());115 (&repr).serialize(serializer)116 }117}118impl<'de, T> Deserialize<'de> for BasicCrossAccountId<T>119where120 T: Config,121 T::AccountId: Deserialize<'de>,122{123 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>124 where125 D: serde::Deserializer<'de>,126 {127 Ok(BasicCrossAccountIdRepr::deserialize(deserializer)?.into())128 }129}130impl<T: Config> CrossAccountId<T::AccountId> for BasicCrossAccountId<T> {131 fn as_sub(&self) -> &T::AccountId {132 &self.substrate133 }134 fn as_eth(&self) -> &H160 {135 &self.ethereum136 }137 fn from_sub(substrate: T::AccountId) -> Self {138 Self {139 ethereum: T::EvmBackwardsAddressMapping::from_account_id(substrate.clone()),140 substrate,141 from_ethereum: false,142 }143 }144 fn from_eth(ethereum: H160) -> Self {145 Self {146 ethereum,147 substrate: T::EvmAddressMapping::into_account_id(ethereum),148 from_ethereum: true,149 }150 }151 fn conv_eq(&self, other: &Self) -> bool {152 if self.from_ethereum == other.from_ethereum {153 self.substrate == other.substrate && self.ethereum == other.ethereum154 } else if self.from_ethereum {155 // ethereum is canonical encoding, but we need to compare derived address156 self.substrate == other.substrate157 } else {158 self.ethereum == other.ethereum159 }160 }161}162impl<T: Config> From<BasicCrossAccountIdRepr<T::AccountId>> for BasicCrossAccountId<T> {163 fn from(repr: BasicCrossAccountIdRepr<T::AccountId>) -> Self {164 match repr {165 BasicCrossAccountIdRepr::Substrate(s) => Self::from_sub(s),166 BasicCrossAccountIdRepr::Ethereum(e) => Self::from_eth(e),167 }168 }169}170impl<T: Config> From<BasicCrossAccountId<T>> for BasicCrossAccountIdRepr<T::AccountId> {171 fn from(v: BasicCrossAccountId<T>) -> Self {172 if v.from_ethereum {173 BasicCrossAccountIdRepr::Ethereum(*v.as_eth())174 } else {175 BasicCrossAccountIdRepr::Substrate(v.as_sub().clone())176 }177 }178}pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -141,7 +141,7 @@
pub mod pallet {
use super::*;
use frame_support::{Blake2_128Concat, pallet_prelude::*, storage::Key};
- use account::{EvmBackwardsAddressMapping, CrossAccountId};
+ use account::CrossAccountId;
use frame_support::traits::Currency;
use nft_data_structs::TokenId;
use scale_info::TypeInfo;
@@ -153,7 +153,7 @@
type CrossAccountId: CrossAccountId<Self::AccountId>;
type EvmAddressMapping: pallet_evm::AddressMapping<Self::AccountId>;
- type EvmBackwardsAddressMapping: EvmBackwardsAddressMapping<Self::AccountId>;
+ type EvmBackwardsAddressMapping: up_evm_mapping::EvmBackwardsAddressMapping<Self::AccountId>;
type Currency: Currency<Self::AccountId>;
type CollectionCreationPrice: Get<
pallets/evm-transaction-payment/Cargo.tomldiffbeforeafterboth--- a/pallets/evm-transaction-payment/Cargo.toml
+++ b/pallets/evm-transaction-payment/Cargo.toml
@@ -15,6 +15,7 @@
fp-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
pallet-ethereum = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
up-sponsorship = { version = "0.1.0", default-features = false, git = "https://github.com/UniqueNetwork/pallet-sponsoring" }
+up-evm-mapping = { default-features = false, path = "../../primitives/evm-mapping" }
[dependencies.codec]
default-features = false
@@ -35,4 +36,5 @@
"pallet-ethereum/std",
"fp-evm/std",
"up-sponsorship/std",
+ "up-evm-mapping/std",
]
pallets/evm-transaction-payment/src/lib.rsdiffbeforeafterboth--- a/pallets/evm-transaction-payment/src/lib.rs
+++ b/pallets/evm-transaction-payment/src/lib.rs
@@ -1,6 +1,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
pub use pallet::*;
+use up_evm_mapping::EvmBackwardsAddressMapping;
#[frame_support::pallet]
pub mod pallet {
@@ -20,6 +21,7 @@
pub trait Config: frame_system::Config {
type SponsorshipHandler: SponsorshipHandler<H160, (H160, Vec<u8>)>;
type Currency: Currency<Self::AccountId>;
+ type EvmBackwardsAddressMapping: EvmBackwardsAddressMapping<Self::AccountId>;
}
#[pallet::pallet]
pallets/nft/Cargo.tomldiffbeforeafterboth--- a/pallets/nft/Cargo.toml
+++ b/pallets/nft/Cargo.toml
@@ -34,6 +34,7 @@
'fp-evm/std',
'nft-data-structs/std',
'up-sponsorship/std',
+ 'up-evm-mapping/std',
'sp-std/std',
'sp-api/std',
'sp-runtime/std',
@@ -135,7 +136,7 @@
sp-api = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.12" }
up-sponsorship = { version = "0.1.0", default-features = false, git = "https://github.com/UniqueNetwork/pallet-sponsoring" }
-
+up-evm-mapping = { default-features = false, path = "../../primitives/evm-mapping" }
evm-coder = { default-features = false, path = "../../crates/evm-coder" }
pallet-evm-coder-substrate = { default-features = false, path = "../../pallets/evm-coder-substrate" }
primitive-types = { version = "0.10.1", default-features = false, features = [
primitives/evm-mapping/Cargo.tomldiffbeforeafterboth--- /dev/null
+++ b/primitives/evm-mapping/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "up-evm-mapping"
+version = "0.1.0"
+edition = "2018"
+
+[dependencies]
+sp-core = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
+frame-support = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
+
+[features]
+default = ["std"]
+std = [
+ "sp-core/std",
+ "frame-support/std",
+]
primitives/evm-mapping/src/lib.rsdiffbeforeafterboth--- /dev/null
+++ b/primitives/evm-mapping/src/lib.rs
@@ -0,0 +1,23 @@
+#![cfg_attr(not(feature = "std"), no_std)]
+
+use frame_support::sp_runtime::AccountId32;
+use sp_core::H160;
+
+/// Transforms substrate addresses to ethereum (Reverse of `EvmAddressMapping`)
+/// pallet_evm doesn't have this, as it only checks if eth address
+/// is owned by substrate via `EnsureAddressOrigin` trait
+///
+/// This trait implementations shouldn't conflict with used `EnsureAddressOrigin`
+pub trait EvmBackwardsAddressMapping<AccountId> {
+ fn from_account_id(account_id: AccountId) -> H160;
+}
+
+/// Should have same mapping as EnsureAddressTruncated
+pub struct MapBackwardsAddressTruncated;
+impl EvmBackwardsAddressMapping<AccountId32> for MapBackwardsAddressTruncated {
+ fn from_account_id(account_id: AccountId32) -> H160 {
+ let mut out = [0; 20];
+ out.copy_from_slice(&(account_id.as_ref() as &[u8])[0..20]);
+ H160(out)
+ }
+}
runtime/Cargo.tomldiffbeforeafterboth--- a/runtime/Cargo.toml
+++ b/runtime/Cargo.toml
@@ -69,6 +69,7 @@
'pallet-ethereum/std',
'fp-rpc/std',
'up-rpc/std',
+ 'up-evm-mapping/std',
'fp-self-contained/std',
'parachain-info/std',
'serde',
@@ -362,7 +363,7 @@
[dependencies.orml-vesting]
git = "https://github.com/open-web3-stack/open-runtime-module-library"
-version = "0.4.1-dev"
+version = "0.4.1-dev"
default-features = false
################################################################################
@@ -375,6 +376,7 @@
derivative = "2.2.0"
pallet-nft = { path = '../pallets/nft', default-features = false, version = '3.0.0' }
up-rpc = { path = "../primitives/rpc", default-features = false }
+up-evm-mapping = { path = "../primitives/evm-mapping", default-features = false }
pallet-inflation = { path = '../pallets/inflation', default-features = false, version = '3.0.0' }
nft-data-structs = { path = '../primitives/nft', default-features = false, version = '0.9.0' }
pallet-common = { default-features = false, path = "../pallets/common" }
runtime/src/lib.rsdiffbeforeafterboth--- a/runtime/src/lib.rs
+++ b/runtime/src/lib.rs
@@ -736,7 +736,7 @@
impl pallet_common::Config for Runtime {
type Event = Event;
- type EvmBackwardsAddressMapping = pallet_common::account::MapBackwardsAddressTruncated;
+ type EvmBackwardsAddressMapping = up_evm_mapping::MapBackwardsAddressTruncated;
type EvmAddressMapping = HashedAddressMapping<Self::Hashing>;
type CrossAccountId = pallet_common::account::BasicCrossAccountId<Self>;
@@ -811,6 +811,7 @@
pallet_evm_contract_helpers::HelpersContractSponsoring<Self>,
);
type Currency = Balances;
+ type EvmBackwardsAddressMapping = up_evm_mapping::MapBackwardsAddressTruncated;
}
impl pallet_nft_charge_transaction::Config for Runtime {