git.delta.rocks / unique-network / refs/commits / 497a13397353

difftreelog

source

pallets/common/src/eth.rs1.6 KiBsourcehistory
1use nft_data_structs::{CollectionId, TokenId};2use sp_core::H160;34// 0x17c4e6453Cc49AAAaEACA894e6D9683e00000001 - collection 15// TODO: Unhardcode prefix6const ETH_ACCOUNT_PREFIX: [u8; 16] = [7	0x17, 0xc4, 0xe6, 0x45, 0x3c, 0xc4, 0x9a, 0xaa, 0xae, 0xac, 0xa8, 0x94, 0xe6, 0xd9, 0x68, 0x3e,8];910// 0xf8238ccfff8ed887463fd5e00000000100000002  - collection 1, token 211// TODO: Unhardcode prefix12const ETH_ACCOUNT_TOKEN_PREFIX: [u8; 12] = [13	0xf8, 0x23, 0x8c, 0xcf, 0xff, 0x8e, 0xd8, 0x87, 0x46, 0x3f, 0xd5, 0xe0,14];1516pub fn map_eth_to_id(eth: &H160) -> Option<CollectionId> {17	if eth[0..16] != ETH_ACCOUNT_PREFIX {18		return None;19	}20	let mut id_bytes = [0; 4];21	id_bytes.copy_from_slice(&eth[16..20]);22	Some(CollectionId(u32::from_be_bytes(id_bytes)))23}24pub fn collection_id_to_address(id: CollectionId) -> H160 {25	let mut out = [0; 20];26	out[0..16].copy_from_slice(&ETH_ACCOUNT_PREFIX);27	out[16..20].copy_from_slice(&u32::to_be_bytes(id.0));28	H160(out)29}3031pub fn map_eth_to_token_id(eth: &H160) -> Option<(CollectionId, TokenId)> {32	if eth[0..12] != ETH_ACCOUNT_TOKEN_PREFIX {33		return None;34	}35	let mut id_bytes = [0; 4];36	let mut token_id_bytes = [0; 4];37	id_bytes.copy_from_slice(&eth[12..16]);38	token_id_bytes.copy_from_slice(&eth[16..20]);39	Some((40		CollectionId(u32::from_be_bytes(id_bytes)),41		TokenId(u32::from_be_bytes(token_id_bytes)),42	))43}44pub fn collection_token_id_to_address(id: CollectionId, token: TokenId) -> H160 {45	let mut out = [0; 20];46	out[0..12].copy_from_slice(&ETH_ACCOUNT_TOKEN_PREFIX);47	out[12..16].copy_from_slice(&u32::to_be_bytes(id.0));48	out[16..20].copy_from_slice(&u32::to_be_bytes(token.0));49	H160(out)50}