Skip to content

Manage embedded wallet state

There are two essential steps to configure Openfort's embedded wallets in your Unity application:

  1. Configure the embedded wallet
  2. Wait for the embedded wallet to be ready

Embedded Wallet Configuration

The Openfort SDK provides methods to configure a non-custodial embedded wallet for blockchain interactions and transaction signing in your Unity game.

Here's how to implement the configuration:

using UnityEngine;
using System;
using System.Threading.Tasks;
using Openfort.OpenfortSDK;
using Openfort.OpenfortSDK.Model;
 
public class OpenfortManager : MonoBehaviour
{
    private OpenfortSDK openfort;
 
    private async void Start()
    {
        try
        {
            openfort = await OpenfortSDK.Init("YOUR_OPENFORT_PUBLISHABLE_KEY");
        }
        catch (Exception e)
        {
            Debug.LogError(quot;Initialization error: {e.Message}");
        }
    }
 
    public async Task ConfigureEmbeddedWallet(string authToken, string password = null)
    {
        try
        {
            int chainId = 80002; // Polygon Amoy testnet
 
            // Create shield authentication configuration
            var shieldAuthentication = new ShieldAuthentication(
                ShieldAuthType.Openfort, // or ShieldAuthType.Custom for third-party auth
                authToken
            );
 
            RecoveryParams recoveryParams = new RecoveryParams
            {
                password = password,
                recoveryMethod = RecoveryMethod.PASSWORD
            };
 
            // Create the embedded wallet request
            var request = new EmbeddedSignerRequest({
                chainId,
                shieldAuthentication,
                recoveryParams
            });
 
            // Configure the embedded wallet
            await openfort.embeddedWallet.configure(request);
            Debug.Log("Embedded signer configured successfully");
        }
        catch (Exception e)
        {
            Debug.LogError(quot;Configuration error: {e.Message}");
            throw;
        }
    }
}

Configuration Parameters

The EmbeddedSignerRequest takes the following parameters:

ParameterDescription
chainIdThe blockchain network identifier. See supported chains
shieldAuthAuthentication configuration including auth type and token
passwordOptional: Recovery password for user-based recovery

The ShieldAuthentication configuration includes:

ParameterDescription
authEither ShieldAuthType.Openfort or ShieldAuthType.Custom
tokenThe access or ID token for user verification

Checking Embedded wallet State

The embedded wallet goes through several states during initialization. It's crucial to wait for the proper state before using the signer.

Embedded States

StateValueDescription
NONE0Initial SDK state
UNAUTHENTICATED1Before user authentication
EMBEDDED_SIGNER_NOT_CONFIGURED2Before wallet configuration
CREATING_ACCOUNT3Creating new account for chainID
READY4Wallet ready for use

Here's how to check the embedded wallet state:

public class OpenfortManager : MonoBehaviour
{
    private OpenfortSDK openfort;
 
        public async UniTask<EmbeddedState> GetEmbeddedState()
    {
        return await GetOpenfortImpl().GetEmbeddedState();
    }
}