Skip to content

Commit

Permalink
Extend AccountPublic to allow retrieval of Factory and Filter capabil…
Browse files Browse the repository at this point in the history
…ities (#153)

Without this, it's very difficult to detect whether a certain capability
is allowed to be retrieved unless you run scripts manually. With helper
methods like this (I'm sure others will be needed), you can borrow and
return details of the filter/factory yourself and then process them
offline
  • Loading branch information
austinkline authored Sep 6, 2023
1 parent 5ac8e29 commit 8e4f566
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
14 changes: 14 additions & 0 deletions contracts/HybridCustody.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ pub contract HybridCustody {
pub fun getPublicCapability(path: PublicPath, type: Type): Capability?
pub fun getPublicCapFromDelegator(type: Type): Capability?
pub fun getAddress(): Address
pub fun getCapabilityFactoryManager(): &{CapabilityFactory.Getter}?
pub fun getCapabilityFilter(): &{CapabilityFilter.Filter}?
}

/// Methods accessible to the designated parent of a ChildAccount
Expand Down Expand Up @@ -716,6 +718,18 @@ pub contract HybridCustody {
self.resources <- {}
}

/// Returns a capability to this child account's CapabilityFilter
///
pub fun getCapabilityFilter(): &{CapabilityFilter.Filter}? {
return self.filter.check() ? self.filter.borrow() : nil
}

/// Returns a capability to this child account's CapabilityFactory
///
pub fun getCapabilityFactoryManager(): &{CapabilityFactory.Getter}? {
return self.factory.check() ? self.factory.borrow() : nil
}

destroy () {
destroy <- self.resources
}
Expand Down
19 changes: 19 additions & 0 deletions scripts/test/can_get_child_factory_and_filter_caps.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import "HybridCustody"

// @addr - The address of the child account
// @parent - The parent account that this child is assigned to
pub fun main(addr: Address, parent: Address): Bool {
let identifier = HybridCustody.getChildAccountIdentifier(parent)
let path = PrivatePath(identifier: identifier) ?? panic("invalid public path identifier for parent address")

let acctPublic = getAuthAccount(addr).getCapability<&HybridCustody.ChildAccount{HybridCustody.AccountPublic}>(path)
.borrow() ?? panic("account public not found")

let factory = acctPublic.getCapabilityFactoryManager()
assert(factory != nil, message: "capability factory is not valid")

let filter = acctPublic.getCapabilityFilter()
assert(filter != nil, message: "capability filter is not valid")

return true
}
8 changes: 8 additions & 0 deletions test/HybridCustody_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,14 @@ pub fun testRemoveParent() {
txExecutor("hybrid-custody/remove_parent_from_child.cdc", [child], [parent.address], nil, nil)
}

pub fun testGetChildAccountCapabilityFilterAndFactory() {
let child = blockchain.createAccount()
let parent = blockchain.createAccount()

setupChildAndParent_FilterKindAll(child: child, parent: parent)
scriptExecutor("test/can_get_child_factory_and_filter_caps.cdc", [child.address, parent.address])
}

// --------------- End Test Cases ---------------

Expand Down

0 comments on commit 8e4f566

Please sign in to comment.