Skip to content

Recovering the wallet on a new device

When a user uses their embedded wallet on a new device for the first time, the wallet must first be recovered on that device

By default, wallets created for your users will be automatically recovered and should never need recovery. For these wallets, you can skip these steps and directly request a signature or transaction.

INFO

For wallets where the recovery_method is 'privy' the wallet will be automatically recovered and should never need recovery. For these wallets, you can skip these steps and directly request a signature or transaction.

Setting up your recovery listener

The wallet must be recovered before you can request signatures or transact with the embedded wallet. The Expo SDK will try to recover the user's wallet once you start interacting with it, such as when you create an EIP-1193 provider. We recommend that you set up a global listener on your application to handle manual recovery in a single place.

To set up this configuration, use Privy's useOnNeedsRecovery hook. AS a parameter, you must pass in an object defining the the onNeedsRecovery callback.

onNeedsRecovery will receive two parameters to drive what recovery looks like:

ParameterTypeDescription
recoveryMethod"privy" | "user-passcode" | "google-drive" | "icloud" | "recovery-encryption-key"An enum representing the recovery method used for this user.
onRecovered() => voidThis is a callback function you should call once recovery has been completed. It takes no arguments.
ts
// This could live in a different screen,
// e.g. if you navigate to a recovery screen when the listener is triggered
const {recover} = useRecoverEmbeddedWallet();

useOnNeedsRecovery({ 
  onNeedsRecovery: async ({recoveryMethod, onRecovered}) => { 
    if (recoveryMethod === 'user-passcode') { 
      // Get the passcode from the user, e.g. by prompting some UI
      const password = 'user-provided-passcode';
      await recover({recoveryMethod, password});
      // Remember to call this after recovery has been completed.
      onRecovered(); 
    } 
  }, 
}); 

Recovering the wallet

To recover an embedded wallet for your user, use the recover function returned by the Privy Expo SDK's useRecoverEmbeddedWallet hook.

You can determine the proper arguments for recover via the account.recovery_method property on the embedded wallet account:

  • user-passcode, call recover with recoveryMethod: 'user-passcode' and the user input password.
  • google-drive, call recover with recoveryMethod: 'google-drive'.
  • icloud, call recover with recoveryMethod: 'icloud'. Note: this is only supported on iOS devices.

TIP

When the wallet needs to be recovered, you might consider popping up a modal with a button where the user can recover their wallet from their cloud account.

tsx
import {useRecoverEmbeddedWallet, usePrivy, getUserEmbeddedWallet} from '@privy-io/expo';

const CreateWalletButton = () => {
  const [password, setPassword] = useState('');
  const {recover} = useRecoverEmbeddedWallet();
  const {user} = usePrivy();
  const account = getUserEmbeddedWallet(user);

  if (account.recovery_method === 'user-passcode') {
    return (
      <View>
        {/* Make sure to handle sensitive information appropriately */}
        <TextInput value={password} onChangeText={setPassword} />
        <Button
          onPress={async () => {
            await recover({
              recoveryMethod: 'user-passcode',
              password,
            });
          }}
        >
          Recover Wallet
        </Button>
      </View>
    );
  }

  return null;
};