git.delta.rocks / unique-network / refs/commits / 994aeb14c5bb

difftreelog

source

pallets/common/src/eth.rs5.6 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 evm_coder::{20	AbiCoder,21	types::{uint256, address},22};23pub use pallet_evm::{Config, account::CrossAccountId};24use sp_core::H160;25use up_data_structs::CollectionId;2627// 0x17c4e6453Cc49AAAaEACA894e6D9683e00000001 - collection 128// TODO: Unhardcode prefix29const ETH_COLLECTION_PREFIX: [u8; 16] = [30	0x17, 0xc4, 0xe6, 0x45, 0x3c, 0xc4, 0x9a, 0xaa, 0xae, 0xac, 0xa8, 0x94, 0xe6, 0xd9, 0x68, 0x3e,31];3233/// Maps the ethereum address of the collection in substrate.34pub fn map_eth_to_id(eth: &H160) -> Option<CollectionId> {35	if eth[0..16] != ETH_COLLECTION_PREFIX {36		return None;37	}38	let mut id_bytes = [0; 4];39	id_bytes.copy_from_slice(&eth[16..20]);40	Some(CollectionId(u32::from_be_bytes(id_bytes)))41}4243/// Maps the substrate collection id in ethereum.44pub fn collection_id_to_address(id: CollectionId) -> H160 {45	let mut out = [0; 20];46	out[0..16].copy_from_slice(&ETH_COLLECTION_PREFIX);47	out[16..20].copy_from_slice(&u32::to_be_bytes(id.0));48	H160(out)49}5051/// Check if the ethereum address is a collection.52pub fn is_collection(address: &H160) -> bool {53	address[0..16] == ETH_COLLECTION_PREFIX54}5556/// Convert `uint256` to `CrossAccountId`.57pub fn convert_uint256_to_cross_account<T: Config>(from: uint256) -> T::CrossAccountId58where59	T::AccountId: From<[u8; 32]>,60{61	let mut new_admin_arr = [0_u8; 32];62	from.to_big_endian(&mut new_admin_arr);63	let account_id = T::AccountId::from(new_admin_arr);64	T::CrossAccountId::from_sub(account_id)65}6667/// Cross account struct68#[derive(Debug, Default, AbiCoder)]69pub struct EthCrossAccount {70	pub(crate) eth: address,71	pub(crate) sub: uint256,72}7374impl EthCrossAccount {75	/// Converts `CrossAccountId` to `EthCrossAccountId`76	pub fn from_sub_cross_account<T>(cross_account_id: &T::CrossAccountId) -> Self77	where78		T: pallet_evm::Config,79		T::AccountId: AsRef<[u8; 32]>,80	{81		if cross_account_id.is_canonical_substrate() {82			Self::from_sub::<T>(cross_account_id.as_sub())83		} else {84			Self {85				eth: *cross_account_id.as_eth(),86				sub: Default::default(),87			}88		}89	}90	/// Creates `EthCrossAccount` from substrate account91	pub fn from_sub<T>(account_id: &T::AccountId) -> Self92	where93		T: pallet_evm::Config,94		T::AccountId: AsRef<[u8; 32]>,95	{96		Self {97			eth: Default::default(),98			sub: uint256::from_big_endian(account_id.as_ref()),99		}100	}101	/// Converts `EthCrossAccount` to `CrossAccountId`102	pub fn into_sub_cross_account<T>(&self) -> evm_coder::execution::Result<T::CrossAccountId>103	where104		T: pallet_evm::Config,105		T::AccountId: From<[u8; 32]>,106	{107		if self.eth == Default::default() && self.sub == Default::default() {108			Err("All fields of cross account is zeroed".into())109		} else if self.eth == Default::default() {110			Ok(convert_uint256_to_cross_account::<T>(self.sub))111		} else if self.sub == Default::default() {112			Ok(T::CrossAccountId::from_eth(self.eth))113		} else {114			Err("All fields of cross account is non zeroed".into())115		}116	}117}118119/// [`CollectionLimits`](up_data_structs::CollectionLimits) representation for EVM.120#[derive(Debug, Default, Clone, Copy, AbiCoder)]121#[repr(u8)]122pub enum CollectionLimits {123	/// How many tokens can a user have on one account.124	#[default]125	AccountTokenOwnership,126127	/// How many bytes of data are available for sponsorship.128	SponsoredDataSize,129130	/// In any case, chain default: [`SponsoringRateLimit::SponsoringDisabled`]131	SponsoredDataRateLimit,132133	/// How many tokens can be mined into this collection.134	TokenLimit,135136	/// Timeouts for transfer sponsoring.137	SponsorTransferTimeout,138139	/// Timeout for sponsoring an approval in passed blocks.140	SponsorApproveTimeout,141142	/// Whether the collection owner of the collection can send tokens (which belong to other users).143	OwnerCanTransfer,144145	/// Can the collection owner burn other people's tokens.146	OwnerCanDestroy,147148	/// Is it possible to send tokens from this collection between users.149	TransferEnabled,150}151/// Ethereum representation of `NestingPermissions` (see [`up_data_structs::NestingPermissions`]) fields as an enumeration.152#[derive(Default, Debug, Clone, Copy, AbiCoder)]153#[repr(u8)]154pub enum CollectionPermissions {155	/// Owner of token can nest tokens under it.156	#[default]157	TokenOwner,158159	/// Admin of token collection can nest tokens under token.160	CollectionAdmin,161}162163/// Ethereum representation of TokenPermissions (see [`up_data_structs::PropertyPermission`]) fields as an enumeration.164#[derive(AbiCoder, Copy, Clone, Default, Debug)]165#[repr(u8)]166pub enum EthTokenPermissions {167	/// Permission to change the property and property permission. See [`up_data_structs::PropertyPermission::mutable`]168	#[default]169	Mutable,170171	/// Change permission for the collection administrator. See [`up_data_structs::PropertyPermission::token_owner`]172	TokenOwner,173174	/// Permission to change the property for the owner of the token. See [`up_data_structs::PropertyPermission::collection_admin`]175	CollectionAdmin,176}