Capability Pattern
The Capability pattern is a fundamental design pattern in Move that uses objects to represent permissions and access rights. A capability is an unforgeable token that grants the holder specific privileges.Core Concept
Capabilities are objects with thekey ability that represent permissions. Only the holder of a capability can perform certain privileged operations.
Key Properties
- Unforgeable: Can only be created by the module that defines them
- Transferable: Can be transferred to delegate authority
- Storable: Can be stored in other objects for complex access control
- Auditable: Ownership is tracked on-chain
Basic Capability
Simple Admin Capability
Treasury Capability Pattern
TheTreasuryCap from the coin module is a perfect example:
Publisher Capability
ThePublisher object from the package module uses the capability pattern:
Multi-Level Capabilities
Create hierarchical access control with multiple capability types:Parameterized Capabilities
Capabilities can be generic over types:Capability with Data
Capabilities can store additional data:Time-Limited Capability
Delegatable Capability
Revocable Capability
Best Practices
- Keep capabilities simple: Don’t add unnecessary data or complexity
-
Use meaningful names: Name capabilities after the permission they grant (e.g.,
MintCap,AdminCap) - Document authority: Clearly document what each capability allows
- Consider lifecycle: Plan for capability creation, transfer, and destruction
- Validate on use: Always validate the capability when it’s used, not just its existence
-
Use store sparingly: Only add
storeif the capability needs to be stored in other objects -
Protect initialization: Ensure capabilities are issued correctly in
initor guarded functions - Consider revocation: Decide if capabilities should be revocable and implement accordingly