Skip to content

Commit

Permalink
add Filter removeAllTypes() (onflow#145)
Browse files Browse the repository at this point in the history
added method `removeAllTypes()` for `AllowListFilter` and
`DenyListFilter`

---------

Co-authored-by: Giovanni Sanchez <[email protected]>
  • Loading branch information
rrrkren and sisyphusSmiling authored Sep 13, 2023
1 parent 8e4f566 commit 0ddf6b8
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
16 changes: 16 additions & 0 deletions contracts/CapabilityFilter.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`
///
Expand Down
67 changes: 67 additions & 0 deletions test/HybridCustody_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 10 additions & 0 deletions transactions/filter/allow/remove_all_types.cdc
Original file line number Diff line number Diff line change
@@ -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()
}
}
10 changes: 10 additions & 0 deletions transactions/filter/deny/remove_all_types.cdc
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 0ddf6b8

Please sign in to comment.