git.delta.rocks / unique-network / refs/commits / d561386d7f33

difftreelog

source

primitives/data-structs/src/mapping.rs3.1 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//! This module contains mapping between different addresses.1819use core::marker::PhantomData;2021use pallet_evm::account::CrossAccountId;22use sp_core::H160;2324use crate::{CollectionId, TokenId};2526/// Trait for mapping between token id and some `Address`.27pub trait TokenAddressMapping<Address> {28	/// Map token id to `Address`.29	fn token_to_address(collection: CollectionId, token: TokenId) -> Address;3031	/// Map `Address` to token id.32	fn address_to_token(address: &Address) -> Option<(CollectionId, TokenId)>;3334	/// Check is address for token.35	fn is_token_address(address: &Address) -> bool;36}3738/// Unit struct for mapping token id to/from *Evm address* represented by [`H160`].39pub struct EvmTokenAddressMapping;4041/// 0xf8238ccfff8ed887463fd5e00000000100000002  - collection 1, token 242const ETH_COLLECTION_TOKEN_PREFIX: [u8; 12] = [43	0xf8, 0x23, 0x8c, 0xcf, 0xff, 0x8e, 0xd8, 0x87, 0x46, 0x3f, 0xd5, 0xe0,44];4546impl TokenAddressMapping<H160> for EvmTokenAddressMapping {47	fn token_to_address(collection: CollectionId, token: TokenId) -> H160 {48		let mut out = [0; 20];49		out[0..12].copy_from_slice(&ETH_COLLECTION_TOKEN_PREFIX);50		out[12..16].copy_from_slice(&u32::to_be_bytes(collection.0));51		out[16..20].copy_from_slice(&u32::to_be_bytes(token.0));52		H160(out)53	}5455	fn address_to_token(eth: &H160) -> Option<(CollectionId, TokenId)> {56		if eth[0..12] != ETH_COLLECTION_TOKEN_PREFIX {57			return None;58		}59		let mut id_bytes = [0; 4];60		let mut token_id_bytes = [0; 4];61		id_bytes.copy_from_slice(&eth[12..16]);62		token_id_bytes.copy_from_slice(&eth[16..20]);63		Some((64			CollectionId(u32::from_be_bytes(id_bytes)),65			TokenId(u32::from_be_bytes(token_id_bytes)),66		))67	}6869	fn is_token_address(address: &H160) -> bool {70		address[0..12] == ETH_COLLECTION_TOKEN_PREFIX71	}72}7374/// Unit struct for mapping token id to/from [`CrossAccountId`].75pub struct CrossTokenAddressMapping<A>(PhantomData<A>);7677impl<A, C: CrossAccountId<A>> TokenAddressMapping<C> for CrossTokenAddressMapping<A> {78	fn token_to_address(collection: CollectionId, token: TokenId) -> C {79		C::from_eth(EvmTokenAddressMapping::token_to_address(collection, token))80	}8182	fn address_to_token(address: &C) -> Option<(CollectionId, TokenId)> {83		EvmTokenAddressMapping::address_to_token(address.as_eth())84	}8586	fn is_token_address(address: &C) -> bool {87		EvmTokenAddressMapping::is_token_address(address.as_eth())88	}89}