git.delta.rocks / unique-network / refs/commits / b70e369d3c1a

difftreelog

source

pallets/common/src/account.rs3.6 KiBsourcehistory
1use crate::Config;2use codec::{Encode, EncodeLike, Decode};3use sp_core::H160;4use sp_core::crypto::AccountId32;5use core::cmp::Ordering;6use serde::{Serialize, Deserialize};7use pallet_evm::AddressMapping;8use sp_std::vec::Vec;9use sp_std::clone::Clone;1011pub trait CrossAccountId<AccountId>:12	Encode + EncodeLike + Decode + Clone + PartialEq + Ord + core::fmt::Debug + Default13// +14// Serialize + Deserialize<'static>15{16	fn as_sub(&self) -> &AccountId;17	fn as_eth(&self) -> &H160;1819	fn from_sub(account: AccountId) -> Self;20	fn from_eth(account: H160) -> Self;21}2223#[derive(Eq, Serialize, Deserialize)]24pub struct BasicCrossAccountId<T: Config> {25	/// If true - then ethereum is canonical encoding26	from_ethereum: bool,27	substrate: T::AccountId,28	ethereum: H160,29}3031impl<T: Config> Default for BasicCrossAccountId<T> {32	fn default() -> Self {33		Self::from_sub(T::AccountId::default())34	}35}3637impl<T: Config> core::fmt::Debug for BasicCrossAccountId<T> {38	fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {39		if self.from_ethereum {40			fmt.debug_tuple("CrossAccountId::Ethereum")41				.field(&self.ethereum)42				.finish()43		} else {44			fmt.debug_tuple("CrossAccountId::Substrate")45				.field(&self.substrate)46				.finish()47		}48	}49}5051impl<T: Config> PartialOrd for BasicCrossAccountId<T> {52	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {53		Some(self.substrate.cmp(&other.substrate))54	}55}5657impl<T: Config> Ord for BasicCrossAccountId<T> {58	fn cmp(&self, other: &Self) -> Ordering {59		self.partial_cmp(other)60			.expect("substrate account is total ordered")61	}62}6364impl<T: Config> PartialEq for BasicCrossAccountId<T> {65	fn eq(&self, other: &Self) -> bool {66		if self.from_ethereum == other.from_ethereum {67			self.substrate == other.substrate && self.ethereum == other.ethereum68		} else if self.from_ethereum {69			// ethereum is canonical encoding, but we need to compare derived address70			self.substrate == other.substrate71		} else {72			self.ethereum == other.ethereum73		}74	}75}76impl<T: Config> Clone for BasicCrossAccountId<T> {77	fn clone(&self) -> Self {78		Self {79			from_ethereum: self.from_ethereum,80			substrate: self.substrate.clone(),81			ethereum: self.ethereum,82		}83	}84}85impl<T: Config> Encode for BasicCrossAccountId<T> {86	fn encode(&self) -> Vec<u8> {87		let as_result = if !self.from_ethereum {88			Ok(self.substrate.clone())89		} else {90			Err(self.ethereum)91		};92		as_result.encode()93	}94}95impl<T: Config> EncodeLike for BasicCrossAccountId<T> {}96impl<T: Config> Decode for BasicCrossAccountId<T> {97	fn decode<I>(input: &mut I) -> Result<Self, codec::Error>98	where99		I: codec::Input,100	{101		Ok(match <Result<T::AccountId, H160>>::decode(input)? {102			Ok(s) => Self::from_sub(s),103			Err(e) => Self::from_eth(e),104		})105	}106}107impl<T: Config> CrossAccountId<T::AccountId> for BasicCrossAccountId<T> {108	fn as_sub(&self) -> &T::AccountId {109		&self.substrate110	}111	fn as_eth(&self) -> &H160 {112		&self.ethereum113	}114	fn from_sub(substrate: T::AccountId) -> Self {115		Self {116			ethereum: T::EvmBackwardsAddressMapping::from_account_id(substrate.clone()),117			substrate,118			from_ethereum: false,119		}120	}121	fn from_eth(ethereum: H160) -> Self {122		Self {123			ethereum,124			substrate: T::EvmAddressMapping::into_account_id(ethereum),125			from_ethereum: true,126		}127	}128}129130pub trait EvmBackwardsAddressMapping<AccountId> {131	fn from_account_id(account_id: AccountId) -> H160;132}133134/// Should have same mapping as EnsureAddressTruncated135pub struct MapBackwardsAddressTruncated;136impl EvmBackwardsAddressMapping<AccountId32> for MapBackwardsAddressTruncated {137	fn from_account_id(account_id: AccountId32) -> H160 {138		let mut out = [0; 20];139		out.copy_from_slice(&(account_id.as_ref() as &[u8])[0..20]);140		H160(out)141	}142}