Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.privacyboost.io/llms.txt

Use this file to discover all available pages before exploring further.

Session Storage

This guide covers securely storing and restoring Privacy Boost sessions in React Native, expanding on the persistence options in Key Management.

Storage Options

React Native offers several storage options with different security trade-offs:
StorageSecurityUse when
AsyncStorageLow (unencrypted)Development only
react-native-keychainHigh (OS keychain)Production apps

Basic: AsyncStorage

The simplest approach, but AsyncStorage is unencrypted. Only suitable for development.
import AsyncStorage from '@react-native-async-storage/async-storage';
import { PrivacyBoost } from '@sunnyside-io/privacy-boost-react-native';

const SESSION_KEY = 'privacy_boost_session';

// Save session
async function saveSession(sdk: PrivacyBoost) {
  const session = sdk.exportSession();
  if (session) {
    await AsyncStorage.setItem(SESSION_KEY, JSON.stringify(session));
  }
}

// Restore session
async function restoreSession(sdk: PrivacyBoost): Promise<boolean> {
  const stored = await AsyncStorage.getItem(SESSION_KEY);
  if (!stored) return false;

  try {
    const session = JSON.parse(stored);
    return sdk.importSession(session);
  } catch {
    await AsyncStorage.removeItem(SESSION_KEY);
    return false;
  }
}

// Clear session
async function clearSession(sdk: PrivacyBoost) {
  sdk.logout();
  await AsyncStorage.removeItem(SESSION_KEY);
}
For production apps, use react-native-keychain to store sessions in the platform’s secure storage (iOS Keychain / Android Keystore).

Install

npm install react-native-keychain
cd ios && pod install

Session Manager

import * as Keychain from 'react-native-keychain';
import { PrivacyBoost } from '@sunnyside-io/privacy-boost-react-native';

const SERVICE_NAME = 'com.yourapp.privacyboost';

async function saveSession(sdk: PrivacyBoost): Promise<void> {
  const session = sdk.exportSession();
  if (!session) return;

  await Keychain.setGenericPassword(
    'privacy-boost-session',
    JSON.stringify(session),
    { service: SERVICE_NAME }
  );
}

async function restoreSession(sdk: PrivacyBoost): Promise<boolean> {
  const credentials = await Keychain.getGenericPassword({ service: SERVICE_NAME });
  if (!credentials) return false;

  try {
    const session = JSON.parse(credentials.password);
    return sdk.importSession(session);
  } catch {
    await Keychain.resetGenericPassword({ service: SERVICE_NAME });
    return false;
  }
}

async function clearSession(sdk: PrivacyBoost): Promise<void> {
  sdk.logout();
  await Keychain.resetGenericPassword({ service: SERVICE_NAME });
}

With Biometric Protection

Add Face ID / fingerprint requirement to access stored sessions:
import * as Keychain from 'react-native-keychain';

async function saveSessionWithBiometrics(sdk: PrivacyBoost): Promise<void> {
  const session = sdk.exportSession();
  if (!session) return;

  await Keychain.setGenericPassword(
    'privacy-boost-session',
    JSON.stringify(session),
    {
      service: SERVICE_NAME,
      accessControl: Keychain.ACCESS_CONTROL.BIOMETRY_CURRENT_SET,
      accessible: Keychain.ACCESSIBLE.WHEN_UNLOCKED_THIS_DEVICE_ONLY,
    }
  );
}

async function restoreSessionWithBiometrics(sdk: PrivacyBoost): Promise<boolean> {
  try {
    const credentials = await Keychain.getGenericPassword({
      service: SERVICE_NAME,
      authenticationPrompt: {
        title: 'Unlock Privacy Boost',
        subtitle: 'Authenticate to access your private wallet',
      },
    });

    if (!credentials) return false;

    const session = JSON.parse(credentials.password);
    return sdk.importSession(session);
  } catch {
    // Biometric auth failed or was cancelled
    return false;
  }
}

Check Biometric Availability

import * as Keychain from 'react-native-keychain';

async function checkBiometrics(): Promise<string | null> {
  const biometryType = await Keychain.getSupportedBiometryType();
  // Returns: 'TouchID' | 'FaceID' | 'Fingerprint' | 'Iris' | null
  return biometryType;
}

Usage in a React Native Component

import React, { useEffect, useState } from 'react';
import { View, Text, Button, ActivityIndicator } from 'react-native';
import { KeySource } from '@sunnyside-io/privacy-boost-react-native';

function PrivacyWallet({ sdk }: { sdk: PrivacyBoost }) {
  const [loading, setLoading] = useState(true);
  const [authenticated, setAuthenticated] = useState(false);

  useEffect(() => {
    // Try restoring session on mount
    restoreSessionWithBiometrics(sdk)
      .then((restored) => {
        setAuthenticated(restored);
        setLoading(false);
      })
      .catch(() => setLoading(false));
  }, []);

  if (loading) return <ActivityIndicator />;

  if (!authenticated) {
    return (
      <View>
        <Text>Not logged in</Text>
        <Button
          title="Connect Wallet"
          onPress={async () => {
            const result = await sdk.authenticate(
              walletDelegate,
              new KeySource.WalletDerived(),
              undefined,
            );
            if (result.tag === 'Authenticated') {
              await saveSessionWithBiometrics(sdk);
              setAuthenticated(true);
            } else if (result.tag === 'MnemonicGenerated') {
              // First-time user: show the recovery phrase, then continue.
              await sdk.proceedAfterMnemonic(undefined);
              await saveSessionWithBiometrics(sdk);
              setAuthenticated(true);
            }
            // CredentialRequired / RecoveryRequired handled elsewhere.
          }}
        />
      </View>
    );
  }

  return (
    <View>
      <Text>Privacy Address: {sdk.getPrivacyAddress()}</Text>
      <Button
        title="Logout"
        onPress={async () => {
          await clearSession(sdk);
          setAuthenticated(false);
        }}
      />
    </View>
  );
}

iOS Configuration

Add Face ID usage description to ios/YourApp/Info.plist:
<key>NSFaceIDUsageDescription</key>
<string>Authenticate to access your Privacy Boost wallet</string>

Next Steps