From 0ddf6b8deb121c6fd83cb17ad50defc343c07ad5 Mon Sep 17 00:00:00 2001 From: Eric Ren Date: Wed, 13 Sep 2023 10:34:52 -0700 Subject: [PATCH] add Filter removeAllTypes() (#145) added method `removeAllTypes()` for `AllowListFilter` and `DenyListFilter` --------- Co-authored-by: Giovanni Sanchez <108043524+sisyphusSmiling@users.noreply.github.com> --- contracts/CapabilityFilter.cdc | 16 +++++ test/HybridCustody_tests.cdc | 67 +++++++++++++++++++ .../filter/allow/remove_all_types.cdc | 10 +++ transactions/filter/deny/remove_all_types.cdc | 10 +++ 4 files changed, 103 insertions(+) create mode 100644 transactions/filter/allow/remove_all_types.cdc create mode 100644 transactions/filter/deny/remove_all_types.cdc diff --git a/contracts/CapabilityFilter.cdc b/contracts/CapabilityFilter.cdc index 5c97dd2..7a76509 100644 --- a/contracts/CapabilityFilter.cdc +++ b/contracts/CapabilityFilter.cdc @@ -54,6 +54,14 @@ pub contract CapabilityFilter { } } + /// Removes all types from the mapping of denied types + /// + pub fun removeAllTypes() { + for type in self.deniedTypes.keys { + self.removeType(type) + } + } + /// Determines if a requested capability is allowed by this `Filter` /// /// @param cap: The capability to check @@ -112,6 +120,14 @@ pub contract CapabilityFilter { emit FilterUpdated(id: self.uuid, filterType: self.getType(), type: type, active: false) } } + + /// Removes all types from the mapping of denied types + /// + pub fun removeAllTypes() { + for type in self.allowedTypes.keys { + self.removeType(type) + } + } /// Determines if a requested capability is allowed by this `Filter` /// diff --git a/test/HybridCustody_tests.cdc b/test/HybridCustody_tests.cdc index 520f0fc..6b767a5 100644 --- a/test/HybridCustody_tests.cdc +++ b/test/HybridCustody_tests.cdc @@ -331,6 +331,57 @@ pub fun testGetCapability_ManagerFilterAllowed() { scriptExecutor("hybrid-custody/get_nft_provider_capability.cdc", [parent.address, child.address]) } +pub fun testAllowlistFilterRemoveAllTypes() { + let child = blockchain.createAccount() + let parent = blockchain.createAccount() + + setupChildAndParent_FilterKindAll(child: child, parent: parent) + + setupNFTCollection(child) + + scriptExecutor("hybrid-custody/get_nft_provider_capability.cdc", [parent.address, child.address]) + + let filter = getTestAccount(FilterKindAllowList) + setupFilter(filter, FilterKindAllowList) + + let nftIdentifier = buildTypeIdentifier(getTestAccount(exampleNFT), exampleNFT, "Collection") + setManagerFilterOnChild(child: child, parent: parent, filterAddress: filter.address) + + addTypeToFilter(filter, FilterKindAllowList, nftIdentifier) + + scriptExecutor("hybrid-custody/get_nft_provider_capability.cdc", [parent.address, child.address]) + + removeAllFilterTypes(filter, FilterKindAllowList) + + let error = expectScriptFailure("hybrid-custody/get_nft_provider_capability.cdc", [parent.address, child.address]) + assert(contains(error, "Capability is not allowed by this account's Parent"), message: "failed to find expected error message") +} + +pub fun testDenyListFilterRemoveAllTypes() { + let child = blockchain.createAccount() + let parent = blockchain.createAccount() + + setupChildAndParent_FilterKindAll(child: child, parent: parent) + + setupNFTCollection(child) + + scriptExecutor("hybrid-custody/get_nft_provider_capability.cdc", [parent.address, child.address]) + + let filter = getTestAccount(FilterKindDenyList) + setupFilter(filter, FilterKindDenyList) + + let nftIdentifier = buildTypeIdentifier(getTestAccount(exampleNFT), exampleNFT, "Collection") + addTypeToFilter(filter, FilterKindDenyList, nftIdentifier) + setManagerFilterOnChild(child: child, parent: parent, filterAddress: filter.address) + + let error = expectScriptFailure("hybrid-custody/get_nft_provider_capability.cdc", [parent.address, child.address]) + assert(contains(error, "Capability is not allowed by this account's Parent"), message: "failed to find expected error message") + + + removeAllFilterTypes(filter, FilterKindDenyList) + scriptExecutor("hybrid-custody/get_nft_provider_capability.cdc", [parent.address, child.address]) +} + pub fun testGetCapability_ManagerFilterNotAllowed() { let child = blockchain.createAccount() let parent = blockchain.createAccount() @@ -922,6 +973,22 @@ pub fun addTypeToFilter(_ acct: Test.Account, _ kind: String, _ identifier: Stri txExecutor(filePath, [acct], [identifier], nil, nil) } +pub fun removeAllFilterTypes(_ acct: Test.Account, _ kind: String) { + var filePath = "" + switch kind { + case FilterKindAllowList: + filePath = "filter/allow/remove_all_types.cdc" + break + case FilterKindDenyList: + filePath = "filter/deny/remove_all_types.cdc" + break + default: + assert(false, message: "unknown filter kind given") + } + + txExecutor(filePath, [acct], [], nil, nil) +} + pub fun addNFTCollectionToDelegator(child: Test.Account, parent: Test.Account, isPublic: Bool) { txExecutor("hybrid-custody/add_example_nft_collection_to_delegator.cdc", [child], [parent.address, isPublic], nil, nil) } diff --git a/transactions/filter/allow/remove_all_types.cdc b/transactions/filter/allow/remove_all_types.cdc new file mode 100644 index 0000000..eebce22 --- /dev/null +++ b/transactions/filter/allow/remove_all_types.cdc @@ -0,0 +1,10 @@ +import "CapabilityFilter" + +transaction() { + prepare(acct: AuthAccount) { + let filter = acct.borrow<&CapabilityFilter.AllowlistFilter>(from: CapabilityFilter.StoragePath) + ?? panic("filter does not exist") + + filter.removeAllTypes() + } +} diff --git a/transactions/filter/deny/remove_all_types.cdc b/transactions/filter/deny/remove_all_types.cdc new file mode 100644 index 0000000..eb273f3 --- /dev/null +++ b/transactions/filter/deny/remove_all_types.cdc @@ -0,0 +1,10 @@ +import "CapabilityFilter" + +transaction() { + prepare(acct: AuthAccount) { + let filter = acct.borrow<&CapabilityFilter.DenylistFilter>(from: CapabilityFilter.StoragePath) + ?? panic("filter does not exist") + + filter.removeAllTypes() + } +} \ No newline at end of file