123456import { IKeyringPair } from '@polkadot/types/types';7import chai from 'chai';8import chaiAsPromised from 'chai-as-promised';9import privateKey from './substrate/privateKey';10import usingApi, { submitTransactionExpectFailAsync } from './substrate/substrate-api';11import {12 addToWhiteListExpectSuccess,13 createCollectionExpectSuccess,14 createItemExpectSuccess,15 destroyCollectionExpectSuccess,16 enableWhiteListExpectSuccess,17 normalizeAccountId,18 addCollectionAdminExpectSuccess,19 addToWhiteListExpectFail,20 removeFromWhiteListExpectSuccess,21 removeFromWhiteListExpectFailure,22 addToWhiteListAgainExpectSuccess,23 transferExpectFailure,24 approveExpectSuccess,25 approveExpectFail,26 transferExpectSuccess,27 transferFromExpectSuccess,28 setMintPermissionExpectSuccess,29 createItemExpectFailure,30} from './util/helpers';3132chai.use(chaiAsPromised);33const expect = chai.expect;3435let Alice: IKeyringPair;36let Bob: IKeyringPair;37let Charlie: IKeyringPair;3839describe('Integration Test ext. White list tests', () => {4041 before(async () => {42 await usingApi(async () => {43 Alice = privateKey('//Alice');44 Bob = privateKey('//Bob');45 Charlie = privateKey('//Charlie');46 });47 });4849 it('Owner can add address to white list', async () => {50 const collectionId = await createCollectionExpectSuccess();51 await addToWhiteListExpectSuccess(Alice, collectionId, Bob.address);52 });5354 it('Admin can add address to white list', async () => {55 const collectionId = await createCollectionExpectSuccess();56 await addCollectionAdminExpectSuccess(Alice, collectionId, Bob);57 await addToWhiteListExpectSuccess(Bob, collectionId, Charlie.address);58 });5960 it('Non-privileged user cannot add address to white list', async () => {61 const collectionId = await createCollectionExpectSuccess();62 await addToWhiteListExpectFail(Bob, collectionId, Charlie.address);63 });6465 it('Nobody can add address to white list of non-existing collection', async () => {66 const collectionId = (1<<32) - 1;67 await addToWhiteListExpectFail(Alice, collectionId, Bob.address);68 });6970 it('Nobody can add address to white list of destroyed collection', async () => {71 const collectionId = await createCollectionExpectSuccess();72 await destroyCollectionExpectSuccess(collectionId, '//Alice');73 await addToWhiteListExpectFail(Alice, collectionId, Bob.address);74 });7576 it('If address is already added to white list, nothing happens', async () => {77 const collectionId = await createCollectionExpectSuccess();78 await addToWhiteListExpectSuccess(Alice, collectionId, Bob.address);79 await addToWhiteListAgainExpectSuccess(Alice, collectionId, Bob.address);80 });8182 it('Owner can remove address from white list', async () => {83 const collectionId = await createCollectionExpectSuccess();84 await addToWhiteListExpectSuccess(Alice, collectionId, Bob.address);85 await removeFromWhiteListExpectSuccess(Alice, collectionId, normalizeAccountId(Bob));86 }); 8788 it('Admin can remove address from white list', async () => {89 const collectionId = await createCollectionExpectSuccess();90 await addCollectionAdminExpectSuccess(Alice, collectionId, Bob);91 await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);92 await removeFromWhiteListExpectSuccess(Bob, collectionId, normalizeAccountId(Charlie));93 }); 9495 it('Non-privileged user cannot remove address from white list', async () => {96 const collectionId = await createCollectionExpectSuccess();97 await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);98 await removeFromWhiteListExpectFailure(Bob, collectionId, normalizeAccountId(Charlie));99 }); 100101 it('Nobody can remove address from white list of non-existing collection', async () => {102 const collectionId = (1<<32) - 1;103 await removeFromWhiteListExpectFailure(Alice, collectionId, normalizeAccountId(Charlie));104 }); 105106107108 it('Nobody can remove address from white list of deleted collection', async () => {109 const collectionId = await createCollectionExpectSuccess();110 await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);111 await destroyCollectionExpectSuccess(collectionId, '//Alice');112 await removeFromWhiteListExpectFailure(Alice, collectionId, normalizeAccountId(Charlie));113 }); 114115 it('If address is already removed from white list, nothing happens', async () => {116 const collectionId = await createCollectionExpectSuccess();117 await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);118 await removeFromWhiteListExpectSuccess(Alice, collectionId, normalizeAccountId(Charlie));119 await removeFromWhiteListExpectSuccess(Alice, collectionId, normalizeAccountId(Charlie));120 }); 121122 it('If Public Access mode is set to WhiteList, tokens can’t be transferred from a non-whitelisted address with transfer or transferFrom. Test1', async () => {123 const collectionId = await createCollectionExpectSuccess();124 const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);125 await enableWhiteListExpectSuccess(Alice, collectionId);126 await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);127128 await transferExpectFailure(129 collectionId,130 itemId,131 Alice,132 Charlie,133 1,134 );135 }); 136137 it('If Public Access mode is set to WhiteList, tokens can’t be transferred from a non-whitelisted address with transfer or transferFrom. Test2', async () => {138 const collectionId = await createCollectionExpectSuccess();139 const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);140 await enableWhiteListExpectSuccess(Alice, collectionId);141 await addToWhiteListExpectSuccess(Alice, collectionId, Alice.address);142 await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);143 await approveExpectSuccess(collectionId, itemId, Alice, Charlie);144 await removeFromWhiteListExpectSuccess(Alice, collectionId, normalizeAccountId(Alice));145146 await transferExpectFailure(147 collectionId,148 itemId,149 Alice,150 Charlie,151 1,152 );153 }); 154155 it('If Public Access mode is set to WhiteList, tokens can’t be transferred to a non-whitelisted address with transfer or transferFrom. Test1', async () => {156 const collectionId = await createCollectionExpectSuccess();157 const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);158 await enableWhiteListExpectSuccess(Alice, collectionId);159 await addToWhiteListExpectSuccess(Alice, collectionId, Alice.address);160161 await transferExpectFailure(162 collectionId,163 itemId,164 Alice,165 Charlie,166 1,167 );168 }); 169170 it('If Public Access mode is set to WhiteList, tokens can’t be transferred to a non-whitelisted address with transfer or transferFrom. Test2', async () => {171 const collectionId = await createCollectionExpectSuccess();172 const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);173 await enableWhiteListExpectSuccess(Alice, collectionId);174 await addToWhiteListExpectSuccess(Alice, collectionId, Alice.address);175 await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);176 await approveExpectSuccess(collectionId, itemId, Alice, Charlie);177 await removeFromWhiteListExpectSuccess(Alice, collectionId, normalizeAccountId(Alice));178179 await transferExpectFailure(180 collectionId,181 itemId,182 Alice,183 Charlie,184 1,185 );186 }); 187188 it('If Public Access mode is set to WhiteList, tokens can’t be destroyed by a non-whitelisted address (even if it owned them before enabling WhiteList mode)', async () => {189 const collectionId = await createCollectionExpectSuccess();190 const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);191 await enableWhiteListExpectSuccess(Alice, collectionId);192193 await usingApi(async (api) => {194 const tx = api.tx.nft.burnItem(collectionId, itemId, 11);195 const badTransaction = async function () { 196 await submitTransactionExpectFailAsync(Alice, tx);197 };198 await expect(badTransaction()).to.be.rejected;199 });200 }); 201 202 it('If Public Access mode is set to WhiteList, token transfers can’t be Approved by a non-whitelisted address (see Approve method)', async () => {203 const collectionId = await createCollectionExpectSuccess();204 const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);205 await enableWhiteListExpectSuccess(Alice, collectionId);206 await approveExpectFail(collectionId, itemId, Alice, Bob);207 }); 208 209 it('If Public Access mode is set to WhiteList, tokens can be transferred to a whitelisted address with transfer.', async () => {210 const collectionId = await createCollectionExpectSuccess();211 const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);212 await enableWhiteListExpectSuccess(Alice, collectionId);213 await addToWhiteListExpectSuccess(Alice, collectionId, Alice.address);214 await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);215 await transferExpectSuccess(collectionId, itemId, Alice, Charlie, 1, 'NFT');216 }); 217218 it('If Public Access mode is set to WhiteList, tokens can be transferred to a whitelisted address with transferFrom.', async () => {219 const collectionId = await createCollectionExpectSuccess();220 const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);221 await enableWhiteListExpectSuccess(Alice, collectionId);222 await addToWhiteListExpectSuccess(Alice, collectionId, Alice.address);223 await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);224 await approveExpectSuccess(collectionId, itemId, Alice, Charlie);225 await transferFromExpectSuccess(collectionId, itemId, Alice, Alice, Charlie, 1, 'NFT');226 });227228 it('If Public Access mode is set to WhiteList, tokens can be transferred from a whitelisted address with transfer', async () => {229 const collectionId = await createCollectionExpectSuccess();230 const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);231 await enableWhiteListExpectSuccess(Alice, collectionId);232 await addToWhiteListExpectSuccess(Alice, collectionId, Alice.address);233 await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);234 await transferExpectSuccess(collectionId, itemId, Alice, Charlie, 1, 'NFT');235 });236237 it('If Public Access mode is set to WhiteList, tokens can be transferred from a whitelisted address with transferFrom', async () => {238 const collectionId = await createCollectionExpectSuccess();239 const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);240 await enableWhiteListExpectSuccess(Alice, collectionId);241 await addToWhiteListExpectSuccess(Alice, collectionId, Alice.address);242 await addToWhiteListExpectSuccess(Alice, collectionId, Charlie.address);243 await approveExpectSuccess(collectionId, itemId, Alice, Charlie);244 await transferFromExpectSuccess(collectionId, itemId, Alice, Alice, Charlie, 1, 'NFT');245 });246247 it('If Public Access mode is set to WhiteList, and Mint Permission is set to false, tokens can be created by owner', async () => {248 const collectionId = await createCollectionExpectSuccess();249 await enableWhiteListExpectSuccess(Alice, collectionId);250 await setMintPermissionExpectSuccess(Alice, collectionId, false);251 await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);252 });253254 it('If Public Access mode is set to WhiteList, and Mint Permission is set to false, tokens can be created by admin', async () => {255 const collectionId = await createCollectionExpectSuccess();256 await enableWhiteListExpectSuccess(Alice, collectionId);257 await setMintPermissionExpectSuccess(Alice, collectionId, false);258 await addCollectionAdminExpectSuccess(Alice, collectionId, Bob);259 await createItemExpectSuccess(Bob, collectionId, 'NFT', Bob.address);260 });261262 it('If Public Access mode is set to WhiteList, and Mint Permission is set to false, tokens cannot be created by non-privileged and white-listed address', async () => {263 const collectionId = await createCollectionExpectSuccess();264 await enableWhiteListExpectSuccess(Alice, collectionId);265 await setMintPermissionExpectSuccess(Alice, collectionId, false);266 await addToWhiteListExpectSuccess(Alice, collectionId, Bob.address);267 await createItemExpectFailure(Bob, collectionId, 'NFT', Bob.address);268 });269270 it('If Public Access mode is set to WhiteList, and Mint Permission is set to false, tokens cannot be created by non-privileged and non-white listed address', async () => {271 const collectionId = await createCollectionExpectSuccess();272 await enableWhiteListExpectSuccess(Alice, collectionId);273 await setMintPermissionExpectSuccess(Alice, collectionId, false);274 await createItemExpectFailure(Bob, collectionId, 'NFT', Bob.address);275 });276277 it('If Public Access mode is set to WhiteList, and Mint Permission is set to true, tokens can be created by owner', async () => {278 const collectionId = await createCollectionExpectSuccess();279 await enableWhiteListExpectSuccess(Alice, collectionId);280 await setMintPermissionExpectSuccess(Alice, collectionId, true);281 await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);282 }); 283284 it('If Public Access mode is set to WhiteList, and Mint Permission is set to true, tokens can be created by admin', async () => {285 const collectionId = await createCollectionExpectSuccess();286 await enableWhiteListExpectSuccess(Alice, collectionId);287 await setMintPermissionExpectSuccess(Alice, collectionId, true);288 await addCollectionAdminExpectSuccess(Alice, collectionId, Bob);289 await createItemExpectSuccess(Bob, collectionId, 'NFT', Bob.address);290 });291292 it('If Public Access mode is set to WhiteList, and Mint Permission is set to true, tokens cannot be created by non-privileged and non-white listed address', async () => {293 const collectionId = await createCollectionExpectSuccess();294 await enableWhiteListExpectSuccess(Alice, collectionId);295 await setMintPermissionExpectSuccess(Alice, collectionId, true);296 await createItemExpectFailure(Bob, collectionId, 'NFT', Bob.address);297 });298299 it('If Public Access mode is set to WhiteList, and Mint Permission is set to true, tokens can be created by non-privileged and white listed address', async () => {300 const collectionId = await createCollectionExpectSuccess();301 await enableWhiteListExpectSuccess(Alice, collectionId);302 await setMintPermissionExpectSuccess(Alice, collectionId, true);303 await addToWhiteListExpectSuccess(Alice, collectionId, Bob.address);304 await createItemExpectSuccess(Bob, collectionId, 'NFT', Bob.address);305 });306});307