difftreelog
refactor remove unused fn `convert_tuple_to_cross_account`
in: master
1 file changed
pallets/common/src/eth.rsdiffbeforeafterboth1// 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(ð[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(Ð_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/// Convert tuple `(address, uint256)` to `CrossAccountId`.68///69/// If `address` in the tuple has *default* value, then the canonical form is substrate,70/// if `uint256` has *default* value, then the ethereum form is canonical,71/// if both values are *default* or *non default*, then this is considered an invalid address and `Error` is returned.72pub fn convert_tuple_to_cross_account<T: Config>(73 eth_cross_account_id: (address, uint256),74) -> evm_coder::execution::Result<T::CrossAccountId>75where76 T::AccountId: From<[u8; 32]>,77{78 if eth_cross_account_id == Default::default() {79 Err("All fields of cross account is zeroed".into())80 } else if eth_cross_account_id.0 == Default::default() {81 Ok(convert_uint256_to_cross_account::<T>(82 eth_cross_account_id.1,83 ))84 } else if eth_cross_account_id.1 == Default::default() {85 Ok(T::CrossAccountId::from_eth(eth_cross_account_id.0))86 } else {87 Err("All fields of cross account is non zeroed".into())88 }89}9091/// Cross account struct92#[derive(Debug, Default, AbiCoder)]93pub struct EthCrossAccount {94 pub(crate) eth: address,95 pub(crate) sub: uint256,96}9798impl EthCrossAccount {99 /// Converts `CrossAccountId` to `EthCrossAccountId`100 pub fn from_sub_cross_account<T>(cross_account_id: &T::CrossAccountId) -> Self101 where102 T: pallet_evm::Config,103 T::AccountId: AsRef<[u8; 32]>,104 {105 if cross_account_id.is_canonical_substrate() {106 Self::from_sub::<T>(cross_account_id.as_sub())107 } else {108 Self {109 eth: *cross_account_id.as_eth(),110 sub: Default::default(),111 }112 }113 }114 /// Creates `EthCrossAccount` from substrate account115 pub fn from_sub<T>(account_id: &T::AccountId) -> Self116 where117 T: pallet_evm::Config,118 T::AccountId: AsRef<[u8; 32]>,119 {120 Self {121 eth: Default::default(),122 sub: uint256::from_big_endian(account_id.as_ref()),123 }124 }125 /// Converts `EthCrossAccount` to `CrossAccountId`126 pub fn into_sub_cross_account<T>(&self) -> evm_coder::execution::Result<T::CrossAccountId>127 where128 T: pallet_evm::Config,129 T::AccountId: From<[u8; 32]>,130 {131 if self.eth == Default::default() && self.sub == Default::default() {132 Err("All fields of cross account is zeroed".into())133 } else if self.eth == Default::default() {134 Ok(convert_uint256_to_cross_account::<T>(self.sub))135 } else if self.sub == Default::default() {136 Ok(T::CrossAccountId::from_eth(self.eth))137 } else {138 Err("All fields of cross account is non zeroed".into())139 }140 }141}142143/// [`CollectionLimits`](up_data_structs::CollectionLimits) representation for EVM.144#[derive(Debug, Default, Clone, Copy, AbiCoder)]145#[repr(u8)]146pub enum CollectionLimits {147 /// How many tokens can a user have on one account.148 #[default]149 AccountTokenOwnership,150151 /// How many bytes of data are available for sponsorship.152 SponsoredDataSize,153154 /// In any case, chain default: [`SponsoringRateLimit::SponsoringDisabled`]155 SponsoredDataRateLimit,156157 /// How many tokens can be mined into this collection.158 TokenLimit,159160 /// Timeouts for transfer sponsoring.161 SponsorTransferTimeout,162163 /// Timeout for sponsoring an approval in passed blocks.164 SponsorApproveTimeout,165166 /// Whether the collection owner of the collection can send tokens (which belong to other users).167 OwnerCanTransfer,168169 /// Can the collection owner burn other people's tokens.170 OwnerCanDestroy,171172 /// Is it possible to send tokens from this collection between users.173 TransferEnabled,174}175/// Ethereum representation of `NestingPermissions` (see [`up_data_structs::NestingPermissions`]) fields as an enumeration.176#[derive(Default, Debug, Clone, Copy, AbiCoder)]177#[repr(u8)]178pub enum CollectionPermissions {179 /// Owner of token can nest tokens under it.180 #[default]181 TokenOwner,182183 /// Admin of token collection can nest tokens under token.184 CollectionAdmin,185}186187/// Ethereum representation of TokenPermissions (see [`up_data_structs::PropertyPermission`]) fields as an enumeration.188#[derive(AbiCoder, Copy, Clone, Default, Debug)]189#[repr(u8)]190pub enum EthTokenPermissions {191 /// Permission to change the property and property permission. See [`up_data_structs::PropertyPermission::mutable`]192 #[default]193 Mutable,194195 /// Change permission for the collection administrator. See [`up_data_structs::PropertyPermission::token_owner`]196 TokenOwner,197198 /// Permission to change the property for the owner of the token. See [`up_data_structs::PropertyPermission::collection_admin`]199 CollectionAdmin,200}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(ð[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(Ð_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}