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

difftreelog

source

pallets/common/src/eth.rs1.8 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 up_data_structs::CollectionId;20use sp_core::H160;2122// 0x17c4e6453Cc49AAAaEACA894e6D9683e00000001 - collection 123// TODO: Unhardcode prefix24const ETH_COLLECTION_PREFIX: [u8; 16] = [25	0x17, 0xc4, 0xe6, 0x45, 0x3c, 0xc4, 0x9a, 0xaa, 0xae, 0xac, 0xa8, 0x94, 0xe6, 0xd9, 0x68, 0x3e,26];2728/// Maps the ethereum address of the collection in substrate.29pub fn map_eth_to_id(eth: &H160) -> Option<CollectionId> {30	if eth[0..16] != ETH_COLLECTION_PREFIX {31		return None;32	}33	let mut id_bytes = [0; 4];34	id_bytes.copy_from_slice(&eth[16..20]);35	Some(CollectionId(u32::from_be_bytes(id_bytes)))36}3738/// Maps the substrate collection id in ethereum.39pub fn collection_id_to_address(id: CollectionId) -> H160 {40	let mut out = [0; 20];41	out[0..16].copy_from_slice(&ETH_COLLECTION_PREFIX);42	out[16..20].copy_from_slice(&u32::to_be_bytes(id.0));43	H160(out)44}4546/// Check if the ethereum address is a collection.47pub fn is_collection(address: &H160) -> bool {48	address[0..16] == ETH_COLLECTION_PREFIX49}