DEV Community

Maakai123
Maakai123

Posted on • Edited on

Aptos Move Tip #6 – Move Abilities

In Aptos Move, abilities are like permission tags for data structures, controlling what can be done with them (e.g., copying, discarding, or storing). Think of them as security settings on a file—assign the wrong permissions, and you could expose sensitive data or break your smart contract. This tip explains Move’s abilities, their risks, and how to use them securely to build robust dApps on Aptos.

Why It Matters

Move’s abilities (copy, drop, store, key) define how data structures behave. Misusing them can lead to serious vulnerabilities:Unauthorized copying of sensitive data (e.g., duplicating tokens).
Resource leaks by not allowing data to be discarded.
Storage mishandling, causing data to be inaccessible or misused.By assigning abilities carefully, you ensure your smart contract is secure, efficient, and behaves as intended.

Real-World Example

Imagine a dApp for digital concert tickets on Aptos. Each ticket is a unique Token struct.
If you allow tickets to be copied freely, someone could duplicate their ticket and sell it multiple times, like photocopying a concert pass! Similarly, if a flash loan system lets borrowers “throw away” their debt without repaying, the system collapses. Proper ability management is like setting strict rules for ticket use—only the right actions are allowed.

Move Abilities Explained

Here’s what each ability does and why it matters:
Copy: Allows duplicating a value. Useful for simple data like numbers, but dangerous for assets like tokens or NFTs, as it could lead to double-spending.
Drop: Permits discarding a value from memory. Necessary for cleanup, but risky for assets that must persist (e.g., loans).

Store: Enables saving data in global storage (on-chain). Critical for persistent data, but must be restricted to prevent unauthorized access.

Key: Allows data to act as a key in global storage, enabling retrieval and manipulation. Essential for resources tied to accounts.

Insecure Code Example

This code assigns abilities incorrectly, creating vulnerabilities:

module 0x42::example {
  struct Token has copy { value: u64 }
  struct FlashLoan has drop { amount: u64 }
}
Enter fullscreen mode Exit fullscreen mode

Problems:
Token has copy: The copy ability lets anyone duplicate a Token, potentially creating unlimited tokens (like printing fake money). This could inflate the token supply and crash the dApp’s economy.

FlashLoan has drop: The drop ability allows borrowers to discard a FlashLoan without repaying it, like tearing up an IOU note and walking away debt-free.

Secure Code Example
Restrict abilities to only what’s needed for the business logic:

module 0x42::example {
  struct Token has key, store { value: u64 }
  struct FlashLoan has key, store { amount: u64 }
}
Enter fullscreen mode Exit fullscreen mode

Why It’s Better:

Token: Removes copy to prevent duplication, ensuring tokens are unique. Adds key and store to allow secure storage and retrieval in global storage (e.g., tied to a user’s account).
FlashLoan: Removes drop to prevent discarding unpaid loans, ensuring borrowers must repay. Adds key and store for persistent loan tracking.

Key Takeaways
Assign abilities sparingly: Only give copy, drop, store, or key when the business logic requires it.
Avoid copy for assets: Prevent duplication of tokens, NFTs, or other sensitive resources.
Restrict drop for obligations: Ensure critical data like loans can’t be discarded without proper handling.Use store and key for persistence: Enable secure storage and retrieval of data in global storage.
Impact: Proper ability management prevents vulnerabilities like double-spending or resource leaks, keeping your dApp secure and reliable

Pro Tips for Developers

Review each struct’s purpose and assign only the necessary abilities.
Avoid copy for any resource representing value (e.g., tokens, NFTs).
Use drop only for temporary or non-critical data, not obligations like loans.Test your contract with incorrect ability assignments to catch potential exploits.

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.