> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getpara.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Wallet Pregeneration

> Create and manage pregenerated wallets for users in React Native and Expo applications

<Info>
  Creating wallets from a backend? Use the [REST API](/v3/rest/overview) instead — no user share to store, and wallets
  are still claimable when the user signs up. Use this guide when your flow needs direct user-share control on the
  client.
</Info>

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. In mobile applications you can use device-specific storage for the user share.

## Mobile-Specific Benefits

While pregeneration works the same across all Para SDKs, React Native and Expo applications offer unique advantages:

<CardGroup cols={2}>
  <Card title="Seamless Web2 to Web3 Transition" description="Create wallets behind the scenes when users login to your existing mobile app" />

  <Card title="Local Secure Storage" description="Store user shares directly on the device using encrypted storage options" />

  <Card title="Device-Specific Wallets" description="Create wallets tied to specific devices for enhanced security" />

  <Card title="App Clips and Instant Experiences" description="Enable blockchain functionality in lightweight app experiences" />
</CardGroup>

<Note>
  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.
</Note>

## Creating a Pregenerated Wallet

### Check if a wallet exists

```typescript theme={null}
import { para } from '../your-para-client';

async function checkPregenWallet() {
  const hasWallet = await para.hasPregenWallet({
    pregenId: { email: "user@example.com" },
  });

  return hasWallet;
}
```

### Create a pregenerated wallet

```typescript theme={null}
import { para } from '../your-para-client';
import { WalletType } from '@getpara/react-native-wallet';

async function createPregenWallet() {
  const pregenWallet = await para.createPregenWallet({
    type: 'EVM', // or 'SOLANA', 'COSMOS', 'STELLAR'
    pregenId: {email: "user@example.com" },
  });

  console.log("Pregenerated Wallet ID:", pregenWallet.id);
  return pregenWallet;
}
```

### Retrieve the user share

```typescript theme={null}
import { para } from '../your-para-client';

async function getUserShare() {
  const userShare = await para.getUserShare();
  // Store this share securely
  return userShare;
}
```

## Mobile Storage Options

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

<Tabs>
  <Tab title="Keychain/Keystore">
    ```typescript theme={null}
    import * as Keychain from 'react-native-keychain';

    // Store the user share
    async 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 share
    async 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;
      }
    }
    ```
  </Tab>

  <Tab title="MMKV">
    ```typescript theme={null}
    import { MMKV } from 'react-native-mmkv';

    // Initialize with encryption for better security
    const storage = new MMKV({
      id: 'wallet-storage',
      encryptionKey: 'your-secure-encryption-key'
    });

    // Store the user share
    function storeUserShare(userShare) {
      storage.set('user_share', userShare);
    }

    // Retrieve the user share
    function retrieveUserShare() {
      return storage.getString('user_share');
    }
    ```
  </Tab>
</Tabs>

<Warning>
  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.
</Warning>

## Using Pregenerated Wallets in Mobile Apps

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

```typescript theme={null}
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;
}
```

### Mobile-Specific Use Cases

<AccordionGroup>
  <Accordion title="Device-Specific Wallets">
    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.

    ```typescript theme={null}
    import DeviceInfo from 'react-native-device-info';

    async function createDeviceWallet() {
      const deviceId = await DeviceInfo.getUniqueId();

      const pregenWallet = await para.createPregenWallet({
        type: 'EVM',
        pregenId: { customId: `device-${deviceId}` },
      });

      // Store the user share in device-specific secure storage
      const userShare = await para.getUserShare();
      await storeUserShare(userShare);

      return pregenWallet;
    }
    ```
  </Accordion>

  <Accordion title="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.

    ```typescript theme={null}
    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: 'EVM',
        pregenId: { customId: `temp-${sessionId}` },
      });

      const userShare = await para.getUserShare();

      // Store in memory for this session only
      // (could also use temporary secure storage)
      sessionStorage.userShare = userShare;

      return pregenWallet;
    }
    ```
  </Accordion>

  <Accordion title="Transparent Web3 Integration">
    Seamlessly introduce blockchain functionality to your existing app users without requiring them to understand wallets or crypto.

    ```typescript theme={null}
    async function createWalletForExistingUser(userId) {
      // Check if we already created a wallet for this user
      const hasWallet = await para.hasPregenWallet({
        pregenId: { customId: `user-${userId}` },
      });

      if (!hasWallet) {
        const pregenWallet = await para.createPregenWallet({
          type: 'EVM',
          pregenId: { customId: `user-${userId}` },
        });

        const userShare = await para.getUserShare();
        await storeUserShare(userShare);

        return pregenWallet;
      } else {
        // Retrieve existing wallet info
        const wallets = await para.getPregenWallets({
          pregenId: { customId: `user-${userId}` },
        });

        return wallets[0];
      }
    }
    ```
  </Accordion>
</AccordionGroup>

## 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:

```typescript theme={null}
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;
}
```

<Note>
  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.
</Note>

## 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.

## Related Resources

<CardGroup cols={2}>
  <Card horizontal title="Pregen Wallets on Web" imgUrl="/images/v3/feature-pregeneration.png" href="/v3/react/guides/pregen" description="Learn how to create and manage pregenerated wallets in web applications" />

  <Card horizontal title="Mobile Storage" imgUrl="/images/v3/general-storage.png" href="/v3/react-native/guides/custom-storage" description="Configuring the para client for custom mobile storage options" />
</CardGroup>
