git.delta.rocks / unique-network / refs/commits / 8298cf829479

difftreelog

source

pallets/common/src/eth.rs8.7 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617//! The module contains a number of functions for converting and checking ethereum identifiers.1819use sp_std::{vec, vec::Vec};20use evm_coder::{21	AbiCoder,22	types::{uint256, address},23};24pub use pallet_evm::{Config, account::CrossAccountId};25use sp_core::H160;26use up_data_structs::CollectionId;2728// 0x17c4e6453Cc49AAAaEACA894e6D9683e00000001 - collection 129// TODO: Unhardcode prefix30const ETH_COLLECTION_PREFIX: [u8; 16] = [31	0x17, 0xc4, 0xe6, 0x45, 0x3c, 0xc4, 0x9a, 0xaa, 0xae, 0xac, 0xa8, 0x94, 0xe6, 0xd9, 0x68, 0x3e,32];3334/// Maps the ethereum address of the collection in substrate.35pub fn map_eth_to_id(eth: &H160) -> Option<CollectionId> {36	if eth[0..16] != ETH_COLLECTION_PREFIX {37		return None;38	}39	let mut id_bytes = [0; 4];40	id_bytes.copy_from_slice(&eth[16..20]);41	Some(CollectionId(u32::from_be_bytes(id_bytes)))42}4344/// Maps the substrate collection id in ethereum.45pub fn collection_id_to_address(id: CollectionId) -> H160 {46	let mut out = [0; 20];47	out[0..16].copy_from_slice(&ETH_COLLECTION_PREFIX);48	out[16..20].copy_from_slice(&u32::to_be_bytes(id.0));49	H160(out)50}5152/// Check if the ethereum address is a collection.53pub fn is_collection(address: &H160) -> bool {54	address[0..16] == ETH_COLLECTION_PREFIX55}5657/// Convert `uint256` to `CrossAccountId`.58pub fn convert_uint256_to_cross_account<T: Config>(from: uint256) -> T::CrossAccountId59where60	T::AccountId: From<[u8; 32]>,61{62	let mut new_admin_arr = [0_u8; 32];63	from.to_big_endian(&mut new_admin_arr);64	let account_id = T::AccountId::from(new_admin_arr);65	T::CrossAccountId::from_sub(account_id)66}6768/// Cross account struct69#[derive(Debug, Default, AbiCoder)]70pub struct EthCrossAccount {71	pub(crate) eth: address,72	pub(crate) sub: uint256,73}7475impl EthCrossAccount {76	/// Converts `CrossAccountId` to `EthCrossAccountId`77	pub fn from_sub_cross_account<T>(cross_account_id: &T::CrossAccountId) -> Self78	where79		T: pallet_evm::Config,80		T::AccountId: AsRef<[u8; 32]>,81	{82		if cross_account_id.is_canonical_substrate() {83			Self::from_sub::<T>(cross_account_id.as_sub())84		} else {85			Self {86				eth: *cross_account_id.as_eth(),87				sub: Default::default(),88			}89		}90	}91	/// Creates `EthCrossAccount` from substrate account92	pub fn from_sub<T>(account_id: &T::AccountId) -> Self93	where94		T: pallet_evm::Config,95		T::AccountId: AsRef<[u8; 32]>,96	{97		Self {98			eth: Default::default(),99			sub: uint256::from_big_endian(account_id.as_ref()),100		}101	}102	/// Converts `EthCrossAccount` to `CrossAccountId`103	pub fn into_sub_cross_account<T>(&self) -> evm_coder::execution::Result<T::CrossAccountId>104	where105		T: pallet_evm::Config,106		T::AccountId: From<[u8; 32]>,107	{108		if self.eth == Default::default() && self.sub == Default::default() {109			Err("All fields of cross account is zeroed".into())110		} else if self.eth == Default::default() {111			Ok(convert_uint256_to_cross_account::<T>(self.sub))112		} else if self.sub == Default::default() {113			Ok(T::CrossAccountId::from_eth(self.eth))114		} else {115			Err("All fields of cross account is non zeroed".into())116		}117	}118}119120/// Ethereum representation of collection [`PropertyKey`](up_data_structs::PropertyKey) and [`PropertyValue`](up_data_structs::PropertyValue).121#[derive(Debug, Default, AbiCoder)]122pub struct Property {123	/// Property key.124	pub key: evm_coder::types::string,125	/// Property value.126	pub value: evm_coder::types::bytes,127}128129/// [`CollectionLimits`](up_data_structs::CollectionLimits) representation for EVM.130#[derive(Debug, Default, Clone, Copy, AbiCoder)]131#[repr(u8)]132pub enum CollectionLimits {133	/// How many tokens can a user have on one account.134	#[default]135	AccountTokenOwnership,136137	/// How many bytes of data are available for sponsorship.138	SponsoredDataSize,139140	/// In any case, chain default: [`SponsoringRateLimit::SponsoringDisabled`]141	SponsoredDataRateLimit,142143	/// How many tokens can be mined into this collection.144	TokenLimit,145146	/// Timeouts for transfer sponsoring.147	SponsorTransferTimeout,148149	/// Timeout for sponsoring an approval in passed blocks.150	SponsorApproveTimeout,151152	/// Whether the collection owner of the collection can send tokens (which belong to other users).153	OwnerCanTransfer,154155	/// Can the collection owner burn other people's tokens.156	OwnerCanDestroy,157158	/// Is it possible to send tokens from this collection between users.159	TransferEnabled,160}161/// Ethereum representation of `NestingPermissions` (see [`up_data_structs::NestingPermissions`]) fields as an enumeration.162#[derive(Default, Debug, Clone, Copy, AbiCoder)]163#[repr(u8)]164pub enum CollectionPermissions {165	/// Owner of token can nest tokens under it.166	#[default]167	TokenOwner,168169	/// Admin of token collection can nest tokens under token.170	CollectionAdmin,171}172173/// Ethereum representation of TokenPermissions (see [`up_data_structs::PropertyPermission`]) fields as an enumeration.174#[derive(AbiCoder, Copy, Clone, Default, Debug)]175#[repr(u8)]176pub enum EthTokenPermissions {177	/// Permission to change the property and property permission. See [`up_data_structs::PropertyPermission::mutable`]178	#[default]179	Mutable,180181	/// Change permission for the collection administrator. See [`up_data_structs::PropertyPermission::token_owner`]182	TokenOwner,183184	/// Permission to change the property for the owner of the token. See [`up_data_structs::PropertyPermission::collection_admin`]185	CollectionAdmin,186}187188/// Ethereum representation of TokenPermissions (see [`up_data_structs::PropertyPermission`]) as an key and value.189#[derive(Debug, Default, AbiCoder)]190pub struct PropertyPermission {191	/// TokenPermission field.192	code: EthTokenPermissions,193	/// TokenPermission value.194	value: bool,195}196197impl PropertyPermission {198	pub fn into_vec(pp: up_data_structs::PropertyPermission) -> Vec<Self> {199		vec![200			PropertyPermission {201				code: EthTokenPermissions::Mutable,202				value: pp.mutable,203			},204			PropertyPermission {205				code: EthTokenPermissions::TokenOwner,206				value: pp.token_owner,207			},208			PropertyPermission {209				code: EthTokenPermissions::CollectionAdmin,210				value: pp.collection_admin,211			},212		]213	}214215	pub fn from_vec(permission: Vec<Self>) -> up_data_structs::PropertyPermission {216		let mut token_permission = up_data_structs::PropertyPermission::default();217218		for PropertyPermission { code, value } in permission {219			match code {220				EthTokenPermissions::Mutable => token_permission.mutable = value,221				EthTokenPermissions::TokenOwner => token_permission.token_owner = value,222				EthTokenPermissions::CollectionAdmin => token_permission.collection_admin = value,223			}224		}225		token_permission226	}227}228229/// Ethereum representation of Token Property Permissions.230#[derive(Debug, Default, AbiCoder)]231pub struct TokenPropertyPermission {232	/// Token property key.233	key: evm_coder::types::string,234	/// Token property permissions.235	permissions: Vec<PropertyPermission>,236}237238impl239	From<(240		up_data_structs::PropertyKey,241		up_data_structs::PropertyPermission,242	)> for TokenPropertyPermission243{244	fn from(245		value: (246			up_data_structs::PropertyKey,247			up_data_structs::PropertyPermission,248		),249	) -> Self {250		let (key, permission) = value;251		let key = evm_coder::types::string::from_utf8(key.into_inner())252			.expect("Stored key must be valid");253		let permissions = PropertyPermission::into_vec(permission);254		Self { key, permissions }255	}256}257258impl TokenPropertyPermission {259	pub fn into_property_key_permissions(260		permissions: Vec<TokenPropertyPermission>,261	) -> evm_coder::execution::Result<Vec<up_data_structs::PropertyKeyPermission>> {262		let mut perms = Vec::new();263264		for TokenPropertyPermission { key, permissions } in permissions {265			if permissions.len() > <EthTokenPermissions as evm_coder::abi::AbiType>::FIELDS_COUNT {266				return Err(alloc::format!(267					"Actual number of fields {} for {}, which exceeds the maximum value of {}",268					permissions.len(),269					stringify!(EthTokenPermissions),270					<EthTokenPermissions as evm_coder::abi::AbiType>::FIELDS_COUNT271				)272				.as_str()273				.into());274			}275276			let token_permission = PropertyPermission::from_vec(permissions);277278			perms.push(up_data_structs::PropertyKeyPermission {279				key: key.into_bytes().try_into().map_err(|_| "too long key")?,280				permission: token_permission,281			});282		}283		Ok(perms)284	}285}