12345678910111213141516171819use 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;26272829const ETH_COLLECTION_PREFIX: [u8; 16] = [30 0x17, 0xc4, 0xe6, 0x45, 0x3c, 0xc4, 0x9a, 0xaa, 0xae, 0xac, 0xa8, 0x94, 0xe6, 0xd9, 0x68, 0x3e,31];323334pub 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}424344pub 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}505152pub fn is_collection(address: &H160) -> bool {53 address[0..16] == ETH_COLLECTION_PREFIX54}555657pub 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}666768#[derive(Debug, Default, AbiCoder)]69pub struct EthCrossAccount {70 pub(crate) eth: address,71 pub(crate) sub: uint256,72}7374impl EthCrossAccount {75 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 91 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 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}118119120#[derive(Debug, Default, Clone, Copy, AbiCoder)]121#[repr(u8)]122pub enum CollectionLimits {123 124 #[default]125 AccountTokenOwnership,126127 128 SponsoredDataSize,129130 131 SponsoredDataRateLimit,132133 134 TokenLimit,135136 137 SponsorTransferTimeout,138139 140 SponsorApproveTimeout,141142 143 OwnerCanTransfer,144145 146 OwnerCanDestroy,147148 149 TransferEnabled,150}151152#[derive(Default, Debug, Clone, Copy, AbiCoder)]153#[repr(u8)]154pub enum CollectionPermissions {155 156 #[default]157 TokenOwner,158159 160 CollectionAdmin,161}162163164#[derive(AbiCoder, Copy, Clone, Default, Debug)]165#[repr(u8)]166pub enum EthTokenPermissions {167 168 #[default]169 Mutable,170171 172 TokenOwner,173174 175 CollectionAdmin,176}