Paraโ€™s Wallet Pregeneration feature allows you to create wallets for users before they authenticate, giving you control over when and how users claim ownership of their wallets. This is particularly powerful in mobile applications, where you can leverage device-specific storage capabilities for enhanced user experiences.

Mobile-Specific Benefits

While pregeneration works the same across all Para SDKs, Flutter applications offer unique advantages:

Pregeneration is especially valuable for devices that may not have full WebAuthn support for passkeys. It allows you to create Para wallets for users on any device while managing the security of the wallet yourself.

Creating Pregenerated Wallets

In Flutter, you can create pregenerated wallets of multiple types with a single method call:

import 'package:para/para.dart';
import 'package:para_flutter/client/para.dart';

Future<List<Wallet>> createPregenWallets() async {
  final pregenWallets = await para.createPregenWalletPerType(
    pregenIdentifier: "user@example.com",
    pregenIdentifierType: PregenIdentifierType.email,
    types: [WalletType.evm], // Optionally specify wallet types
  );
  
  // Get the user share
  final userShare = await para.getUserShare();
  
  // Store user share securely (see storage options below)
  
  return pregenWallets;
}

Mobile Storage Options

In Flutter applications, you have several options for securely storing the user share:

import 'package:flutter_secure_storage/flutter_secure_storage.dart';

final storage = FlutterSecureStorage();

// Store the user share
Future<void> storeUserShare(String userShare) async {
  try {
    await storage.write(
      key: 'para_user_share',
      value: userShare,
    );
  } catch (e) {
    // Handle error
  }
}

// Retrieve the user share
Future<String?> retrieveUserShare() async {
  try {
    return await storage.read(key: 'para_user_share');
  } catch (e) {
    // Handle error
    return null;
  }
}

Whichever storage method you choose, ensure you implement proper security measures. The user share is critical for wallet access, and if lost, the wallet becomes permanently inaccessible.

Using Pregenerated Wallets in Flutter Apps

Once you have created a pregenerated wallet and stored the user share, you can use it for signing operations:

import 'dart:convert';
import 'package:para_flutter/client/para.dart';

Future<String> usePregenWallet(String walletId) async {
  // Retrieve the user share from your secure storage
  final userShare = await retrieveUserShare();
  
  if (userShare == null) {
    throw Exception("User share not found");
  }
  
  // Load the user share into the Para client
  await para.setUserShare(userShare);
  
  // Now you can perform signing operations
  final messageBase64 = base64Encode(utf8.encode("Hello, World!"));
  final signature = await para.signMessage(
    walletId: walletId,
    messageBase64: messageBase64,
  );
  
  return signature;
}

Mobile-Specific Use Cases

Claiming Pregenerated Wallets

When a user is ready to take ownership of their pregenerated wallet, they can claim it once theyโ€™ve authenticated with Para:

import 'package:para_flutter/client/para.dart';

Future<String> claimWallet() async {
  // Ensure user is authenticated
  if (!(await para.isFullyLoggedIn())) {
    throw Exception("User must be authenticated to claim wallets");
  }
  
  // Retrieve and load the user share
  final userShare = await retrieveUserShare();
  if (userShare != null) {
    await para.setUserShare(userShare);
  }
  
  // Claim the wallet
  final recoverySecret = await para.claimPregenWallets();
  
  // Optionally, clear the locally stored user share after claiming
  // since Para now manages it through the user's authentication
  await clearUserShare();
  
  return recoverySecret;
}

After claiming, Para will manage the user share through the userโ€™s authentication methods. You can safely remove the user share from your local storage if you no longer need to access the wallet directly.

Best Practices for Mobile

  1. Utilize Device Security: Leverage biometric authentication (TouchID/FaceID) to protect access to locally stored user shares.

  2. Implement Device Sync: For users with multiple devices, consider implementing your own synchronization mechanism for user shares across devices.

  3. Handle Offline States: Mobile applications often work offline. Design your pregenerated wallet system to function properly even when connectivity is limited.

  4. Backup Strategies: Provide users with options to back up their wallet data, especially for device-specific wallets that might not be associated with their Para account.

  5. Clear Security Boundaries: Clearly communicate to users when theyโ€™re using an app-managed wallet versus a personally-owned wallet.