Create and manage pregenerated wallets for users in React Native and Expo applications
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.
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.
import { para } from '../your-para-client';async function getUserShare() { const userShare = await para.getUserShare(); // Store this share securely return userShare;}
In mobile applications, you have several options for securely storing the user share:
Copy
Ask AI
import * as Keychain from 'react-native-keychain';// Store the user shareasync function storeUserShare(userShare) { try { await Keychain.setGenericPassword( 'para_user_share', userShare, { service: 'com.yourapp.wallet', accessible: Keychain.ACCESSIBLE.WHEN_UNLOCKED_THIS_DEVICE_ONLY } ); } catch (error) { console.error("Error storing user share:", error); }}// Retrieve the user shareasync function retrieveUserShare() { try { const credentials = await Keychain.getGenericPassword({ service: 'com.yourapp.wallet' }); if (credentials) { return credentials.password; } return null; } catch (error) { console.error("Error retrieving user share:", 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.
Once you have created a pregenerated wallet and stored the user share, you can use it for signing operations:
Copy
Ask AI
import { para } from '../your-para-client';async function usePregenWallet() { // Retrieve the user share from your secure storage const userShare = await retrieveUserShare(); if (!userShare) { console.error("User share not found"); return; } // Load the user share into the Para client await para.setUserShare(userShare); // Now you can perform signing operations const messageBase64 = btoa("Hello, World!"); const signature = await para.signMessage({ walletId: "your-wallet-id", messageBase64, }); return signature;}
Create wallets that are bound to a specific device by using device-specific identifiers combined with secure local storage. This approach is ideal for multi-device users who need different wallets for different devices.
Copy
Ask AI
import DeviceInfo from 'react-native-device-info';async function createDeviceWallet() { const deviceId = await DeviceInfo.getUniqueId(); const pregenWallet = await para.createPregenWallet({ type: WalletType.EVM, pregenIdentifier: `device-${deviceId}`, pregenIdentifierType: "CUSTOM_ID", }); // Store the user share in device-specific secure storage const userShare = await para.getUserShare(); await storeUserShare(userShare); return pregenWallet;}
App Clips and Instant Experiences
For iOS App Clips or Android Instant Apps, create temporary wallets that enable limited blockchain functionality without requiring full app installation or user authentication.
Copy
Ask AI
async function createTemporaryWallet() { // Generate a random identifier for this session const sessionId = Math.random().toString(36).substring(2, 15); const pregenWallet = await para.createPregenWallet({ type: WalletType.EVM, pregenIdentifier: `temp-${sessionId}`, pregenIdentifierType: "CUSTOM_ID", }); const userShare = await para.getUserShare(); // Store in memory for this session only // (could also use temporary secure storage) sessionStorage.userShare = userShare; return pregenWallet;}
Transparent Web3 Integration
Seamlessly introduce blockchain functionality to your existing app users without requiring them to understand wallets or crypto.
Copy
Ask AI
async function createWalletForExistingUser(userId) { // Check if we already created a wallet for this user const hasWallet = await para.hasPregenWallet({ pregenIdentifier: `user-${userId}`, pregenIdentifierType: "CUSTOM_ID", }); if (!hasWallet) { const pregenWallet = await para.createPregenWallet({ type: WalletType.EVM, pregenIdentifier: `user-${userId}`, pregenIdentifierType: "CUSTOM_ID", }); const userShare = await para.getUserShare(); await storeUserShare(userShare); return pregenWallet; } else { // Retrieve existing wallet info const wallets = await para.getPregenWallets({ pregenIdentifier: `user-${userId}`, pregenIdentifierType: "CUSTOM_ID", }); return wallets[0]; }}
When a user is ready to take ownership of their pregenerated wallet, they can claim it once they’ve authenticated with Para:
Copy
Ask AI
import { para } from '../your-para-client';async function claimWallet() { // Ensure user is authenticated if (!(await para.isFullyLoggedIn())) { console.error("User must be authenticated to claim wallets"); return; } // Retrieve and load the user share const userShare = await retrieveUserShare(); await para.setUserShare(userShare); // Claim the wallet const 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.
Utilize Device Security: Leverage biometric authentication (TouchID/FaceID) to protect access to locally stored user shares.
Implement Device Sync: For users with multiple devices, consider implementing your own synchronization mechanism for user shares across devices.
Handle Offline States: Mobile applications often work offline. Design your pregenerated wallet system to function properly even when connectivity is limited.
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.
Clear Security Boundaries: Clearly communicate to users when they’re using an app-managed wallet versus a personally-owned wallet.