Flutter (Mobile)
Get started with Flutter for mobile development using Capsule SDK.
The Capsule SDK for Flutter allows you to easily integrate secure and scalable wallet functionalities into your mobile applications. This guide covers the installation, setup, and usage of the Capsule SDK, including handling authentication flows.
Prerequisites
To use Capsule, you need an API key. This key authenticates your requests to Capsule services and is essential for integration.
Don’t have an API key yet? Request access to the Developer Portal to create API keys, manage billing, teams, and more.
Dependency Installation
To install the Capsule SDK and required dependencies, use the following command:
Project Setup
To set up associated domains for passkey functionality in your Flutter project, you need to configure both iOS and Android platforms:
To enable passkeys on iOS, you need to set up associated domains in your Xcode project:
- Open your Flutter project’s iOS folder in Xcode
- Select your target and go to “Signing & Capabilities”
- Click ”+ Capability” and add “Associated Domains”
- Add the following domains:
webcredentials:app.beta.usecapsule.com
webcredentials:app.usecapsule.com
For more details, see the Apple Developer documentation.
Important: You must register your teamId + bundleIdentifier
with the Capsule team to set up associated domains. For example, if your Team ID is A1B2C3D4E5
and Bundle Identifier is com.yourdomain.yourapp
, provide A1B2C3D4E5.com.yourdomain.yourapp
to Capsule. This is required by Apple for passkey security. Allow up to 24 hours for domain propagation.
Using the Capsule SDK
Much like our web SDKs, the Flutter SDK follows a similar usage pattern, handling both authentication and signing capabilities. The SDK provides comprehensive authentication flows for both creating new users and logging in existing users. Both flows utilize passkeys for secure and seamless authentication.
For authentication, we have two main paths: creating a new user or logging in an existing one. Authentication options include phone, email, OAuth, and an advanced use case with pregenerated wallets (covered later). The key concept is that once a user is created, we create an associated native passkey on their mobile device. This passkey will secure the user’s wallets when they’re created.
Beta Testing Credentials
In the BETA
Environment, you can use any email ending in @test.usecapsule.com
(like dev@test.usecapsule.com) or US phone numbers (+1) in the format (area code)-555-xxxx
(like (425)-555-1234). Any OTP code will work for verification with these test credentials.
These credentials are for beta testing only. You can delete test users anytime in the beta developer console to free up user slots.
Capsule Initialization
First things first - you’ll need a beta API key from the developer portal. Once you’ve got your API key, you can create a Capsule instance in your Flutter application. Here’s an example of how to set this up:
While we recommend utilizing environment variables for API keys, there’s no harm in the keys being exposed on the client side.
Authentication Methods
The SDK provides multiple authentication methods, each following a similar pattern but with their own specific requirements:
- Email + Passkey
- Phone + Passkey
- OAuth (Google, Apple, X/Twitter, Discord)
- Pregenerated Wallets (advanced use case)
Email Authentication Flow
The email flow demonstrates the typical pattern all authentication methods follow. Here’s a detailed walkthrough:
Check If User Exists
First, we need to check if the user already has an account. This determines whether we proceed with creation or direct them to login:
Create User and Send OTP
For new users, we initiate the account creation process. This triggers an OTP email:
The OTP email is automatically sent to the provided address. You don’t need to trigger this separately.
Verify OTP and Generate Passkey
Once the user enters the OTP, we verify it and create their passkey:
The passkey generation will trigger the device’s native biometric prompt. No additional UI is needed for this step.
Create and Secure Wallet
Finally, we create the user’s wallet:
The recoverySecret is crucial for account recovery. Ensure you have a secure way to store or display it to the user.
Phone Authentication Flow
The phone flow follows the same pattern as email but with phone-specific methods:
Check If User Exists
Create User and Send OTP
Country code requires the +
prefix. Ensure you include this in the phone number before sending it to the SDK.
Verify OTP and Generate Passkey
Create Wallet
This step is identical to the email flow’s wallet creation step.
OAuth Authentication Flow
OAuth provides a streamlined process, handling much of the verification automatically, but unlike email and phone requires a browser view for the initial authentication:
Initiate OAuth Flow
The OAuth URL should be opened in a secure browser view for better security but a WebView in your app is also an option.
Handle OAuth Result
OAuth flows automatically retrieve the user’s email. No manual email collection is needed. The flow will be the same as email authentication after the OAuth result is received.
Each flow ultimately results in:
- A verified user account
- A device-specific passkey
- A wallet with its recovery secret
The key difference between flows is the initial verification method, but they all converge at the passkey and wallet creation steps.
Login Existing User
Once the wallet is created and automatically stored and secured by the passkey, logging in becomes straightforward. The Capsule Flutter class mirrors most of the same method names as the Capsule web JS class, maintaining consistency across platforms.
Initiate User Login
The login process is simplified into a single method that handles the passkey authentication handshake with Capsule servers and securely loads the user’s wallet:
Transaction Signing
Now that we have a user with wallets, we can begin signing. Signing with Capsule provides three main methods:
signMessage
- For general-purpose signing of any datasignTransaction
- Specifically for EVM transactions (requires RLP encoding)sendTransaction
- For direct transaction submission of RLPEncoded transactions
Here’s the important part: signMessage
signs on raw bytes of a base64 string. It will sign ANY data that’s passed
into the message parameter. Remember - it signs on the raw bytes, so the data itself isn’t relevant. This means
transactions, messages, and other data types need to be properly constructed before passing them to signMessage
.
There’s no validation when signing raw bytes - we sign what you give us.
This is why it’s crucial to use libraries that handle Transaction construction correctly. You’ll want to:
- Construct the transaction
- Get the serialized byte data
- Convert it to base64
- Pass that into
signMessage
signMessage
works with raw bytes in base64 format. It will sign ANY provided data without validation, so ensure your
transaction construction is correct before signing.
Here’s an example in Solana using the solana_web3
package:
Advanced: Pregenerated Wallets
Pregenerated wallets are an advanced use case that lets you create a wallet based on an identifier like an email before the user exists. When you create a pregenerated wallet, your app receives the user’s half of the key (userShare). This keyShare must be secured, encrypted, and NOT lost as it controls the user’s half of the key.
This advanced feature allows apps to take actions on behalf of a user without needing user interaction. The methods are identical across all our SDKs, so you can use them the same way in Flutter as you would in Web or Server implementations:
For more details on pregenerated wallets check out the
Pregenerated Wallets guide.
Examples
To help you get started with the Capsule Flutter SDK, we’ve prepared a comprehensive example:
This example repository demonstrates how to handle user authentication, wallet creation, and signing messages using the Capsule SDK in a Flutter application.
Next Steps
After integrating Capsule, you can explore other features and integrations to enhance your Capsule experience. Here are some resources to help you get started:
Ecosystems
Learn how to use Capsule with popular Web3 clients and wallet connectors. We’ll cover integration with key libraries for EVM, Solana, and Cosmos ecosystems.
If you’re ready to go live with your Capsule integration, make sure to review our go-live checklist:
Troubleshooting
If you encounter issues during the integration or usage of the Capsule SDK in your Flutter application, here are some common problems and their solutions:
For more detailed troubleshooting and solutions specific to Flutter, please refer to our comprehensive troubleshooting guide:
Integration Support
If you’re experiencing issues that aren’t resolved by our troubleshooting resources, please contact our team for assistance. To help us resolve your issue quickly, please include the following information in your request:
1
A detailed description of the problem you’re encountering.
2
Any relevant error messages or logs.
3
Steps to reproduce the issue.
4
Details about your system or environment (e.g., device, operating system, software version).
Providing this information will enable our team to address your concerns more efficiently.
Was this page helpful?