difftreelog
NFTPAR-231 Smart Contract White List. Anyone can call contract with disabled white list.
in: master
2 files changed
pallets/nft/src/lib.rsdiffbeforeafterboth--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -2513,10 +2513,10 @@
let owned_contract = <ContractOwner<T>>::contains_key(called_contract.clone())
&& <ContractOwner<T>>::get(called_contract.clone()) == *who;
+ let white_list_enabled = <ContractWhiteListEnabled<T>>::contains_key(called_contract.clone()) && <ContractWhiteListEnabled<T>>::get(called_contract.clone());
- if !owned_contract {
- let white_list_enabled = <ContractWhiteListEnabled<T>>::contains_key(called_contract.clone()) && <ContractWhiteListEnabled<T>>::get(called_contract.clone());
- if !white_list_enabled || !<ContractWhiteList<T>>::contains_key(called_contract.clone(), who) {
+ if !owned_contract && white_list_enabled {
+ if !<ContractWhiteList<T>>::contains_key(called_contract.clone(), who) {
return Err(InvalidTransaction::Call.into());
}
}
tests/src/contracts.test.tsdiffbeforeafterboth115 let expectedFlipValue = await getFlipValue(contract, deployer);115 let expectedFlipValue = await getFlipValue(contract, deployer);116116117 const flip = contract.exec('flip', value, gasLimit);117 const flip = contract.exec('flip', value, gasLimit);118 await expect(submitTransactionExpectFailAsync(bob, flip)).to.be.rejected;118 await submitTransactionAsync(bob, flip);119 expectedFlipValue = !expectedFlipValue;119 const firstFailResponse = await getFlipValue(contract, deployer);120 const afterFlip = await getFlipValue(contract,deployer);120 expect(firstFailResponse).to.be.eq(expectedFlipValue, `Only account who deployed contract can flip value.`);121 expect(afterFlip).to.be.eq(expectedFlipValue, `Anyone can call new contract.`);121122122 const deployerCanFlip = async () => {123 const deployerCanFlip = async () => {123 expectedFlipValue = !expectedFlipValue;124 expectedFlipValue = !expectedFlipValue;145 const flipAfterWhiteListed = await getFlipValue(contract,deployer);146 const flipAfterWhiteListed = await getFlipValue(contract,deployer);146 expect(flipAfterWhiteListed).to.be.eq(expectedFlipValue, `Bob was whitelisted, now he can flip.`);147 expect(flipAfterWhiteListed).to.be.eq(expectedFlipValue, `Bob was whitelisted, now he can flip.`);147148 await deployerCanFlip();149150 const disableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, false);151 const disableeResult = await submitTransactionAsync(deployer, disableWhiteListTx);152 const flipWithDisabledWhitelist = contract.exec('flip', value, gasLimit);153 await expect(submitTransactionExpectFailAsync(bob, flipWithDisabledWhitelist)).to.be.rejected;154 const flipWithDisabledWhiteList = await getFlipValue(contract, deployer);155 expect(flipWithDisabledWhiteList).to.be.eq(expectedFlipValue, `Bob can't flip when whitelist is disabled, even tho he is in whitelist.`);156157 await deployerCanFlip();158159 const enableWhiteListOneMoreTimeTx = api.tx.nft.toggleContractWhiteList(contract.address, true);160 const enableOneMoreTimeResult = await submitTransactionAsync(deployer, enableWhiteListOneMoreTimeTx);161148162 await deployerCanFlip();149 await deployerCanFlip();163150166 const bobRemoved = contract.exec('flip', value, gasLimit);153 const bobRemoved = contract.exec('flip', value, gasLimit);167 await expect(submitTransactionExpectFailAsync(bob, bobRemoved)).to.be.rejected;154 await expect(submitTransactionExpectFailAsync(bob, bobRemoved)).to.be.rejected;168 const afterBobRemoved = await getFlipValue(contract, deployer);155 const afterBobRemoved = await getFlipValue(contract, deployer);169 expect(afterBobRemoved).to.be.eq(expectedFlipValue, `Enabling whitelist doesn't make it possible to call contract for everyone.`);156 expect(afterBobRemoved).to.be.eq(expectedFlipValue, `Bob can't call contract, now when he is removeed from white list.`);170157171 await deployerCanFlip();158 await deployerCanFlip();172159173 const cleanupTx = api.tx.nft.toggleContractWhiteList(contract.address, false);160 const disableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, false);174 const cleanupResult = await submitTransactionAsync(deployer, cleanupTx);161 const disableWhiteListResult = await submitTransactionAsync(deployer, disableWhiteListTx);162 const whiteListDisabledFlip = contract.exec('flip', value, gasLimit);163 await submitTransactionAsync(bob, whiteListDisabledFlip);164 expectedFlipValue = !expectedFlipValue;165 const afterWhiteListDisabled = await getFlipValue(contract,deployer);166 expect(afterWhiteListDisabled).to.be.eq(expectedFlipValue, `Anyone can call contract with disabled whitelist.`);175167176 console.error = consoleError;168 console.error = consoleError;177 });169 });