git.delta.rocks / unique-network / refs/commits / 8484aa77f543

difftreelog

source

pallets/common/src/account.rs4.8 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(Encode, Decode, Serialize, Deserialize)]24#[serde(rename_all = "camelCase")]25enum BasicCrossAccountIdRepr<AccountId> {26	Substrate(AccountId),27	Ethereum(H160),28}2930#[derive(Eq)]31pub struct BasicCrossAccountId<T: Config> {32	/// If true - then ethereum is canonical encoding33	from_ethereum: bool,34	substrate: T::AccountId,35	ethereum: H160,36}3738impl<T: Config> Default for BasicCrossAccountId<T> {39	fn default() -> Self {40		Self::from_sub(T::AccountId::default())41	}42}4344impl<T: Config> core::fmt::Debug for BasicCrossAccountId<T> {45	fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {46		if self.from_ethereum {47			fmt.debug_tuple("CrossAccountId::Ethereum")48				.field(&self.ethereum)49				.finish()50		} else {51			fmt.debug_tuple("CrossAccountId::Substrate")52				.field(&self.substrate)53				.finish()54		}55	}56}5758impl<T: Config> PartialOrd for BasicCrossAccountId<T> {59	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {60		Some(self.substrate.cmp(&other.substrate))61	}62}6364impl<T: Config> Ord for BasicCrossAccountId<T> {65	fn cmp(&self, other: &Self) -> Ordering {66		self.partial_cmp(other)67			.expect("substrate account is total ordered")68	}69}7071impl<T: Config> PartialEq for BasicCrossAccountId<T> {72	fn eq(&self, other: &Self) -> bool {73		if self.from_ethereum == other.from_ethereum {74			self.substrate == other.substrate && self.ethereum == other.ethereum75		} else if self.from_ethereum {76			// ethereum is canonical encoding, but we need to compare derived address77			self.substrate == other.substrate78		} else {79			self.ethereum == other.ethereum80		}81	}82}83impl<T: Config> Clone for BasicCrossAccountId<T> {84	fn clone(&self) -> Self {85		Self {86			from_ethereum: self.from_ethereum,87			substrate: self.substrate.clone(),88			ethereum: self.ethereum,89		}90	}91}92impl<T: Config> Encode for BasicCrossAccountId<T> {93	fn encode(&self) -> Vec<u8> {94		BasicCrossAccountIdRepr::from(self.clone()).encode()95	}96}97impl<T: Config> EncodeLike for BasicCrossAccountId<T> {}98impl<T: Config> Decode for BasicCrossAccountId<T> {99	fn decode<I>(input: &mut I) -> Result<Self, codec::Error>100	where101		I: codec::Input,102	{103		Ok(BasicCrossAccountIdRepr::decode(input)?.into())104	}105}106impl<T> Serialize for BasicCrossAccountId<T>107where108	T: Config,109	T::AccountId: Serialize,110{111	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>112	where113		S: serde::Serializer,114	{115		let repr = BasicCrossAccountIdRepr::from(self.clone());116		(&repr).serialize(serializer)117	}118}119impl<'de, T> Deserialize<'de> for BasicCrossAccountId<T>120where121	T: Config,122	T::AccountId: Deserialize<'de>,123{124	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>125	where126		D: serde::Deserializer<'de>,127	{128		Ok(BasicCrossAccountIdRepr::deserialize(deserializer)?.into())129	}130}131impl<T: Config> CrossAccountId<T::AccountId> for BasicCrossAccountId<T> {132	fn as_sub(&self) -> &T::AccountId {133		&self.substrate134	}135	fn as_eth(&self) -> &H160 {136		&self.ethereum137	}138	fn from_sub(substrate: T::AccountId) -> Self {139		Self {140			ethereum: T::EvmBackwardsAddressMapping::from_account_id(substrate.clone()),141			substrate,142			from_ethereum: false,143		}144	}145	fn from_eth(ethereum: H160) -> Self {146		Self {147			ethereum,148			substrate: T::EvmAddressMapping::into_account_id(ethereum),149			from_ethereum: true,150		}151	}152}153impl<T: Config> From<BasicCrossAccountIdRepr<T::AccountId>> for BasicCrossAccountId<T> {154	fn from(repr: BasicCrossAccountIdRepr<T::AccountId>) -> Self {155		match repr {156			BasicCrossAccountIdRepr::Substrate(s) => Self::from_sub(s),157			BasicCrossAccountIdRepr::Ethereum(e) => Self::from_eth(e),158		}159	}160}161impl<T: Config> From<BasicCrossAccountId<T>> for BasicCrossAccountIdRepr<T::AccountId> {162	fn from(v: BasicCrossAccountId<T>) -> Self {163		if v.from_ethereum {164			BasicCrossAccountIdRepr::Ethereum(*v.as_eth())165		} else {166			BasicCrossAccountIdRepr::Substrate(v.as_sub().clone())167		}168	}169}170171pub trait EvmBackwardsAddressMapping<AccountId> {172	fn from_account_id(account_id: AccountId) -> H160;173}174175/// Should have same mapping as EnsureAddressTruncated176pub struct MapBackwardsAddressTruncated;177impl EvmBackwardsAddressMapping<AccountId32> for MapBackwardsAddressTruncated {178	fn from_account_id(account_id: AccountId32) -> H160 {179		let mut out = [0; 20];180		out.copy_from_slice(&(account_id.as_ref() as &[u8])[0..20]);181		H160(out)182	}183}