Welcome! In this lesson, we’ll explore a key concept in Aptos Move programming: the signer. We'll walk through how it's used in smart contracts, how it differs from addresses, and how to perform operations like storing user data, updating balances, and transferring tokens securely.
In Move, a signer represents the entity (usually a wallet) that initiated the transaction. Unlike a regular address, a signer has privileges and permissions because it's tied to transaction authentication.
Think of the signer as the user actively interacting with your contract.
Let’s consider a simple contract that stores user profiles. Each profile has:
The contract includes a public enter function to initialize the profile:
public fun enter(user: &signer, name: string)
Here:
To create and store the profile on the blockchain:
move_to(user, UserProfile { name, balance: 0 });
This stores the UserProfile in global storage, associated with the signer's address.
You can create the profile object inline, as shown above, to keep the code clean and avoid unnecessary variable declarations.
move_to takes a signer and stores the given object under that signer's address. It's not storing to an arbitrary address—it's tightly coupled to the authenticated signer.
Now suppose you want to update the balance in the profile. Here’s how it works:
public fun update_balance(user: &signer) acquires UserProfile
Why acquires?
The acquires keyword tells the compiler that this function will access the UserProfile resource from global storage.
To retrieve the user's address from the signer:
let user_address = signer::address_of(user);
Now, to borrow and modify the UserProfile:
let profile = borrow_global_mut<UserProfile>(user_address);
If you just needed to read (not modify) the data, you'd use borrow_global instead.
You can then update the balance field using another helper function, such as retrieving a token balance from global state.
For token transfers, you can use:
coin::transfer<TokenType>(from: &signer, to: address, amount: u64)
This pattern ensures only the wallet owner can initiate token transfers from their own account.
The examples in this lesson are for educational purposes and don’t yet include best practices like error handling or checking for sufficient balances. These should be added in production code to ensure robustness.
Swap insights and ask questions about “Learn everything about Aptos”.
Ask a question or share your thoughts about this lesson.