Rise In Logo

Learn everything about Aptos

Move Walkthrough Part-2

Move Walkthrough Part-2

Understanding the Signer in Aptos Move Smart Contracts

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.

What is a signer?

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.

Example 1: Creating a User Profile

Let’s consider a simple contract that stores user profiles. Each profile has:

  • A name (string)
  • A balance (initially zero)

The contract includes a public enter function to initialize the profile:

public fun enter(user: &signer, name: string)

Here:

  • user is the signer (the caller of the function)
  • name is the name input from the user

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.

What is move_to Doing?

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.

Example 2: Updating the User's Balance

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.

Example 3: Transferring Tokens

For token transfers, you can use:

coin::transfer<TokenType>(from: &signer, to: address, amount: u64)

  • The sender must be a signer for security reasons (to ensure they initiated the transaction).
  • The receiver is a standard address.
  • The function will automatically extract the signer's address and perform the transfer.

This pattern ensures only the wallet owner can initiate token transfers from their own account.

Recap: Key Concepts

  • signer is the authenticated user who sends a transaction.
  • Use move_to(signer, value) to store data under the signer's address.
  • Use signer::address_of(&signer) to extract the raw address from a signer object.
  • Use borrow_global_mut<T>(address) to access and modify stored data.
  • Use coin::transfer for token transfers, where the sender must be a signer.

Final Notes

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.

Comments

You need to enroll in the course to be able to comment!

Stay in the know

Never miss updates on new programs and opportunities.

Rise In Logo

Rise together in web3!

Disclaimer: The information /programs / events provided on https://patika.dev and https://risein.com are strictly for upskilling and networking purposes related to the technical infrastructure of blockchain platforms. We do not provide financial or investment advice and do not make any representations regarding the value, profitability, or future price of any blockchain or cryptocurrency. Users are encouraged to conduct their own research and consult with licensed financial professionals before engaging in any investment activities. https://patika.dev and https://risein.com disclaim any responsibility for financial decisions made by users based on information provided here.