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 States

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

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

Methods

GetEmbeddedState

Gets the current state of the embedded wallet.

Method Signature:
public async UniTask<EmbeddedState> GetEmbeddedState()
Parameters:
  • None
Returns:
  • UniTask<EmbeddedState> - The current embedded wallet state
Example:
using System;
using UnityEngine;
using Cysharp.Threading.Tasks;
using Openfort.OpenfortSDK;
using Openfort.OpenfortSDK.Model;
 
public class WalletStateChecker : MonoBehaviour
{
    private OpenfortSDK openfort;
    
    private async void Start()
    {
        // Initialize SDK
        openfort = await OpenfortSDK.Init(
            "YOUR_OPENFORT_PUBLISHABLE_KEY",
            "YOUR_SHIELD_PUBLISHABLE_KEY"
        );
        
        // Check current state
        await CheckWalletState();
    }
    
    private async UniTask CheckWalletState()
    {
        try
        {
            var state = await openfort.GetEmbeddedState();
            Debug.Log(quot;Current embedded state: {state}");
            
            switch (state)
            {
                case EmbeddedState.NONE:
                    Debug.Log("SDK not initialized");
                    break;
                case EmbeddedState.UNAUTHENTICATED:
                    Debug.Log("User needs to authenticate");
                    break;
                case EmbeddedState.EMBEDDED_SIGNER_NOT_CONFIGURED:
                    Debug.Log("Wallet needs configuration");
                    break;
                case EmbeddedState.CREATING_ACCOUNT:
                    Debug.Log("Account being created...");
                    break;
                case EmbeddedState.READY:
                    Debug.Log("Wallet ready for use!");
                    break;
            }
        }
        catch (Exception e)
        {
            Debug.LogError(quot;Error getting embedded state: {e.Message}");
        }
    }
}

MonitorEmbeddedState

Continuously monitors the embedded wallet state until it's ready.

Method Signature:
public async UniTaskVoid MonitorEmbeddedState()
Parameters:
  • None
Returns:
  • UniTaskVoid - Async operation that completes when wallet is ready
Example:
using System;
using UnityEngine;
using Cysharp.Threading.Tasks;
using Openfort.OpenfortSDK;
using Openfort.OpenfortSDK.Model;
 
public class WalletStateMonitor : MonoBehaviour
{
    private OpenfortSDK openfort;
    
    [Header("UI References")]
    [SerializeField] private TMPro.TextMeshProUGUI stateText;
    [SerializeField] private GameObject loadingIndicator;
    
    private async void Start()
    {
        openfort = await OpenfortSDK.Init(
            "YOUR_OPENFORT_PUBLISHABLE_KEY",
            "YOUR_SHIELD_PUBLISHABLE_KEY"
        );
        
        // Start monitoring
        MonitorEmbeddedState().Forget();
    }
    
    private async UniTaskVoid MonitorEmbeddedState()
    {
        while (true)
        {
            try
            {
                var state = await openfort.GetEmbeddedState();
                UpdateUI(state);
                
                if (state == EmbeddedState.READY)
                {
                    Debug.Log("Wallet ready!");
                    OnWalletReady();
                    break;
                }
                
                await UniTask.Delay(1000); // Check every second
            }
            catch (Exception e)
            {
                Debug.LogError(quot;Monitor error: {e.Message}");
                await UniTask.Delay(2000);
            }
        }
    }
    
    private void UpdateUI(EmbeddedState state)
    {
        string stateDescription = state switch
        {
            EmbeddedState.NONE => "Initializing...",
            EmbeddedState.UNAUTHENTICATED => "Please log in",
            EmbeddedState.EMBEDDED_SIGNER_NOT_CONFIGURED => "Setting up wallet...",
            EmbeddedState.CREATING_ACCOUNT => "Creating wallet...",
            EmbeddedState.READY => "Ready!",
            _ => "Unknown state"
        };
        
        if (stateText != null)
            stateText.text = stateDescription;
            
        if (loadingIndicator != null)
            loadingIndicator.SetActive(state != EmbeddedState.READY);
    }
    
    private void OnWalletReady()
    {
        Debug.Log("Wallet is ready for blockchain operations!");
        // Enable wallet-dependent features here
    }
}

WaitForEmbeddedWalletReady

Waits until the embedded wallet reaches the READY state.

Method Signature:
public async UniTask WaitForEmbeddedWalletReady()
Parameters:
  • None
Returns:
  • UniTask - Completes when wallet is ready
Example:
using System;
using UnityEngine;
using Cysharp.Threading.Tasks;
using Openfort.OpenfortSDK;
using Openfort.OpenfortSDK.Model;
 
public class WalletOperations : MonoBehaviour
{
    private OpenfortSDK openfort;
    
    private async void Start()
    {
        openfort = await OpenfortSDK.Init(
            "YOUR_OPENFORT_PUBLISHABLE_KEY",
            "YOUR_SHIELD_PUBLISHABLE_KEY"
        );
    }
    
    public async UniTask WaitForEmbeddedWalletReady()
    {
        Debug.Log("Waiting for embedded wallet to be ready...");
        
        while (true)
        {
            var state = await openfort.GetEmbeddedState();
            
            if (state == EmbeddedState.READY)
            {
                Debug.Log("Embedded wallet is ready!");
                return;
            }
            
            // Log current state for debugging
            Debug.Log(quot;Current state: {state}");
            await UniTask.Delay(1000);
        }
    }
    
    // Example usage: Sign message only when wallet is ready
    public async UniTaskVoid SignMessageSafe(string message)
    {
        try
        {
            // Wait for wallet to be ready
            await WaitForEmbeddedWalletReady();
            
            // Now safe to perform wallet operations
            var request = new SignMessageRequest(message);
            var signature = await openfort.SignMessage(request);
            Debug.Log(quot;Message signed: {signature}");
        }
        catch (Exception e)
        {
            Debug.LogError(quot;Failed to sign message: {e.Message}");
        }
    }
}