Skip to content

Creating a new embedded wallet

To create a new embedded wallet, you can use the useWallets hook. This hook provides methods to create and manage wallets securely.

import { useWallets } from "@openfort/react"
 
function SampleComponent() {
  const {
    createWallet, 
  } = useWallets()
 
  const handleCreateWallet = async () => {
    const newWallet = await createWallet();
    console.log("New wallet created:", newWallet);
  };
 
  // ...
}

This will create a new embedded wallet for the user with Automatic recovery and set it as the active wallet.

Recovery Method

Whenever you're creating a new embedded wallet, you'll have to decide on the method of recovery. This can be one of those options:

  • RecoveryMethod.AUTOMATIC
  • RecoveryMethod.PASSWORD
  • RecoveryMethod.PASSKEY

Automatic Recovery

First, set up the configuration for automatic recovery in the OpenfortProvider by providing the createEncryptedSessionEndpoint prop. Learn more about it in the Wallet recovery section.

Then, when creating a wallet, specify the recovery method as RecoveryMethod.AUTOMATIC, or leave it out, as it will default to automatic.

import { useWallets, RecoveryMethod } from "@openfort/react"
 
function SampleComponent() {
  const {
    createWallet,
  } = useWallets()
 
  const handleCreateWallet = async () => {
    const newWallet = await createWallet({
      recovery: { 
        recoveryMethod: RecoveryMethod.AUTOMATIC, // (default) 
      } 
    });
    console.log("New wallet created:", newWallet);
  };
 
  // ...
}

Password Recovery

Password recovery doesn't require a backend from your end, but your users will have to create and use a password to recover their wallet.

import { useWallets, RecoveryMethod } from "@openfort/react"
 
function SampleComponent() {
  const {
    createWallet,
  } = useWallets()
 
  const handleCreateWallet = async (password: string) => {
    const newWallet = await createWallet({
      recovery: { 
        recoveryMethod: RecoveryMethod.PASSWORD, 
        password, 
      } 
    });
    console.log("New wallet created:", newWallet);
  };
 
  // ...
}

Passkey Recovery

Passkey recovery uses WebAuthn to provide a secure and user-friendly way to recover wallets. This method doesn't require a backend, but users will need to set up a passkey on their device.

import { useWallets, RecoveryMethod } from "@openfort/react"
 
function SampleComponent() {
  const {
    createWallet,
  } = useWallets()
 
  const handleCreateWallet = async () => {
    const newWallet = await createWallet({
      recovery: { 
        recoveryMethod: RecoveryMethod.PASSKEY, 
      } 
    });
    console.log("New wallet created:", newWallet);
  };
 
  // ...
}