difftreelog
CORE-345 Add check that contract is unique collection
in: master
3 files changed
pallets/common/src/eth.rsdiffbeforeafterboth--- a/pallets/common/src/eth.rs
+++ b/pallets/common/src/eth.rs
@@ -37,3 +37,7 @@
out[16..20].copy_from_slice(&u32::to_be_bytes(id.0));
H160(out)
}
+
+pub fn is_collection(address: &H160) -> bool {
+ address[0..16] == ETH_COLLECTION_PREFIX
+}
pallets/unique/src/eth/mod.rsdiffbeforeafterboth--- a/pallets/unique/src/eth/mod.rs
+++ b/pallets/unique/src/eth/mod.rs
@@ -318,6 +318,10 @@
input: &[u8],
value: sp_core::U256,
) -> Option<PrecompileResult> {
+ if !pallet_common::eth::is_collection(target) {
+ return None;
+ }
+
let helpers = EvmCollection::<T>(Rc::new(SubstrateRecorder::<T>::new(*target, gas_left)));
pallet_evm_coder_substrate::call(*source, helpers, value, input)
}
tests/src/eth/createCollection.test.tsdiffbeforeafterboth1// 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.8//9// 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/>.1617import nonFungibleAbi from './nonFungibleAbi.json';18import {ApiPromise} from '@polkadot/api';19import {evmToAddress} from '@polkadot/util-crypto';20import {expect} from 'chai';21import {getCreatedCollectionCount, getDetailedCollectionInfo} from '../util/helpers';22import {23 evmCollectionHelper,24 collectionIdFromAddress,25 collectionIdToAddress,26 createEthAccount,27 createEthAccountWithBalance,28 evmCollection,29 GAS_ARGS,30 itWeb3,31 normalizeAddress,32 normalizeEvents,33} from './util/helpers';3435async function getCollectionAddressFromResult(api: ApiPromise, result: any) {36 const collectionIdAddress = normalizeAddress(result.events[0].raw.topics[2]);37 const collectionId = collectionIdFromAddress(collectionIdAddress); 38 const collection = (await getDetailedCollectionInfo(api, collectionId))!;39 return {collectionIdAddress, collectionId, collection};40}4142describe('Create collection from EVM', () => {43 itWeb3('Create collection', async ({api, web3}) => {44 const owner = await createEthAccountWithBalance(api, web3);45 const helper = evmCollectionHelper(web3, owner);46 const collectionName = 'CollectionEVM';47 const description = 'Some description';48 const tokenPrefix = 'token prefix';49 50 const collectionCountBefore = await getCreatedCollectionCount(api);51 const result = await helper.methods52 .create721Collection(collectionName, description, tokenPrefix)53 .send();54 const collectionCountAfter = await getCreatedCollectionCount(api);55 56 const {collectionId, collection} = await getCollectionAddressFromResult(api, result);57 expect(collectionCountAfter - collectionCountBefore).to.be.eq(1);58 expect(collectionId).to.be.eq(collectionCountAfter);59 expect(collection.name.map(v => String.fromCharCode(v.toNumber())).join('')).to.be.eq(collectionName);60 expect(collection.description.map(v => String.fromCharCode(v.toNumber())).join('')).to.be.eq(description);61 expect(collection.tokenPrefix.toHuman()).to.be.eq(tokenPrefix);62 expect(collection.schemaVersion.type).to.be.eq('ImageURL');63 });64 65 itWeb3('Set sponsorship', async ({api, web3}) => {66 const owner = await createEthAccountWithBalance(api, web3);67 const collectionHelper = evmCollectionHelper(web3, owner);68 let result = await collectionHelper.methods.create721Collection('Sponsor collection', '1', '1').send();69 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);70 const sponsor = await createEthAccountWithBalance(api, web3);71 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);72 result = await collectionEvm.methods.setSponsor(sponsor).send();73 let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;74 expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;75 expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));76 await expect(collectionEvm.methods.confirmSponsorship().call()).to.be.rejectedWith('Caller is not set as sponsor');77 const sponsorCollection = evmCollection(web3, sponsor, collectionIdAddress);78 await sponsorCollection.methods.confirmSponsorship().send();79 collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;80 expect(collectionSub.sponsorship.isConfirmed).to.be.true;81 expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));82 });8384 itWeb3('Set limits', async ({api, web3}) => {85 const owner = await createEthAccountWithBalance(api, web3);86 const collectionHelper = evmCollectionHelper(web3, owner);87 const result = await collectionHelper.methods.create721Collection('Const collection', '5', '5').send();88 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);89 const limits = {90 accountTokenOwnershipLimit: 1000,91 sponsoredDataSize: 1024,92 sponsoredDataRateLimit: {Blocks: 30},93 tokenLimit: 1000000,94 sponsorTransferTimeout: 6,95 sponsorApproveTimeout: 6,96 ownerCanTransfer: false,97 ownerCanDestroy: false,98 transfersEnabled: false,99 };100101 const limitsJson = JSON.stringify(limits, null, 1);102 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);103 await collectionEvm.methods.setLimits(limitsJson).send();104 105 const collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;106 expect(collectionSub.limits.accountTokenOwnershipLimit.unwrap().toNumber()).to.be.eq(limits.accountTokenOwnershipLimit);107 expect(collectionSub.limits.sponsoredDataSize.unwrap().toNumber()).to.be.eq(limits.sponsoredDataSize);108 expect(collectionSub.limits.sponsoredDataRateLimit.unwrap().asBlocks.toNumber()).to.be.eq(limits.sponsoredDataRateLimit.Blocks);109 expect(collectionSub.limits.tokenLimit.unwrap().toNumber()).to.be.eq(limits.tokenLimit);110 expect(collectionSub.limits.sponsorTransferTimeout.unwrap().toNumber()).to.be.eq(limits.sponsorTransferTimeout);111 expect(collectionSub.limits.sponsorApproveTimeout.unwrap().toNumber()).to.be.eq(limits.sponsorApproveTimeout);112 expect(collectionSub.limits.ownerCanTransfer.toHuman()).to.be.eq(limits.ownerCanTransfer);113 expect(collectionSub.limits.ownerCanDestroy.toHuman()).to.be.eq(limits.ownerCanDestroy);114 expect(collectionSub.limits.transfersEnabled.toHuman()).to.be.eq(limits.transfersEnabled);115 });116117 itWeb3('Check tokenURI', async ({web3, api}) => {118 const owner = await createEthAccountWithBalance(api, web3);119 const helper = evmCollectionHelper(web3, owner);120 let result = await helper.methods.create721Collection('Mint collection', '6', '6').send();121 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);122 const receiver = createEthAccount(web3);123 const contract = new web3.eth.Contract(nonFungibleAbi as any, collectionIdAddress, {from: owner, ...GAS_ARGS});124 const nextTokenId = await contract.methods.nextTokenId().call();125126 expect(nextTokenId).to.be.equal('1');127 result = await contract.methods.mintWithTokenURI(128 receiver,129 nextTokenId,130 'Test URI',131 ).send();132133 const events = normalizeEvents(result.events);134 const address = collectionIdToAddress(collectionId);135136 expect(events).to.be.deep.equal([137 {138 address,139 event: 'Transfer',140 args: {141 from: '0x0000000000000000000000000000000000000000',142 to: receiver,143 tokenId: nextTokenId,144 },145 },146 ]);147148 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');149150 // TODO: this wont work right now, need release 919000 first151 // await helper.methods.setOffchainSchema(collectionIdAddress, 'https://offchain-service.local/token-info/{id}').send();152 // const tokenUri = await contract.methods.tokenURI(nextTokenId).call();153 // expect(tokenUri).to.be.equal(`https://offchain-service.local/token-info/${nextTokenId}`);154 });155});156157describe('(!negative tests!) Create collection from EVM', () => {158 itWeb3('(!negative test!) Create collection (bad lengths)', async ({api, web3}) => {159 const owner = await createEthAccountWithBalance(api, web3);160 const helper = evmCollectionHelper(web3, owner);161 {162 const MAX_NAME_LENGHT = 64;163 const collectionName = 'A'.repeat(MAX_NAME_LENGHT + 1);164 const description = 'A';165 const tokenPrefix = 'A';166 167 await expect(helper.methods168 .create721Collection(collectionName, description, tokenPrefix)169 .call()).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGHT);170 171 }172 { 173 const MAX_DESCRIPTION_LENGHT = 256;174 const collectionName = 'A';175 const description = 'A'.repeat(MAX_DESCRIPTION_LENGHT + 1);176 const tokenPrefix = 'A';177 await expect(helper.methods178 .create721Collection(collectionName, description, tokenPrefix)179 .call()).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGHT);180 }181 { 182 const MAX_TOKEN_PREFIX_LENGHT = 16;183 const collectionName = 'A';184 const description = 'A';185 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGHT + 1);186 await expect(helper.methods187 .create721Collection(collectionName, description, tokenPrefix)188 .call()).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGHT);189 }190 });191 192 itWeb3('(!negative test!) Create collection (no funds)', async ({web3}) => {193 const owner = await createEthAccount(web3);194 const helper = evmCollectionHelper(web3, owner);195 const collectionName = 'A';196 const description = 'A';197 const tokenPrefix = 'A';198 199 await expect(helper.methods200 .create721Collection(collectionName, description, tokenPrefix)201 .call()).to.be.rejectedWith('NotSufficientFounds');202 });203204 itWeb3('(!negative test!) Collection address (Contract is not an unique collection)', async ({api, web3}) => {205 const owner = await createEthAccountWithBalance(api, web3);206 const collectionAddressWithBadPrefix = '0x00112233445566778899AABBCCDDEEFF00112233';207 const collectionEvm = evmCollection(web3, owner, collectionAddressWithBadPrefix);208 const EXPECTED_ERROR = 'Contract is not an unique collection';209 {210 const sponsor = await createEthAccountWithBalance(api, web3);211 await expect(collectionEvm.methods212 .setSponsor(sponsor)213 .call()).to.be.rejectedWith(EXPECTED_ERROR);214 215 const sponsorCollection = evmCollection(web3, sponsor, collectionAddressWithBadPrefix);216 await expect(sponsorCollection.methods217 .confirmSponsorship()218 .call()).to.be.rejectedWith(EXPECTED_ERROR);219 }220 {221 const limits = '{"account_token_ownership_limit":1000}';222 await expect(collectionEvm.methods223 .setLimits(limits)224 .call()).to.be.rejectedWith(EXPECTED_ERROR);225 }226 });227228 itWeb3('(!negative test!) Check owner', async ({api, web3}) => {229 const owner = await createEthAccountWithBalance(api, web3);230 const notOwner = await createEthAccount(web3);231 const collectionHelper = evmCollectionHelper(web3, owner);232 const result = await collectionHelper.methods.create721Collection('A', 'A', 'A').send();233 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);234 const contractEvmFromNotOwner = evmCollection(web3, notOwner, collectionIdAddress);235 const EXPECTED_ERROR = 'NoPermission';236 {237 const sponsor = await createEthAccountWithBalance(api, web3);238 await expect(contractEvmFromNotOwner.methods239 .setSponsor(sponsor)240 .call()).to.be.rejectedWith(EXPECTED_ERROR);241 242 const sponsorCollection = evmCollection(web3, sponsor, collectionIdAddress);243 await expect(sponsorCollection.methods244 .confirmSponsorship()245 .call()).to.be.rejectedWith('Caller is not set as sponsor');246 }247 {248 const limits = '{"account_token_ownership_limit":1000}';249 await expect(contractEvmFromNotOwner.methods250 .setLimits(limits)251 .call()).to.be.rejectedWith(EXPECTED_ERROR);252 }253 });254255 itWeb3('(!negative test!) Set limits', async ({api, web3}) => {256 const owner = await createEthAccountWithBalance(api, web3);257 const collectionHelper = evmCollectionHelper(web3, owner);258 const result = await collectionHelper.methods.create721Collection('Schema collection', 'A', 'A').send();259 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);260 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);261 const badJson = '{accountTokenOwnershipLimit: 1000}';262 await expect(collectionEvm.methods263 .setLimits(badJson)264 .call()).to.be.rejectedWith('Parse JSON error:');265 });266});