# 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.

:::info
For a full description of wallet states and lifecycle, see [Wallet lifecycle](/docs/products/embedded-wallet/wallet-lifecycle).
:::

| State                          | Value | Description                      |
| ------------------------------ | ----- | -------------------------------- |
| NONE                           | 0     | Initial SDK state                |
| UNAUTHENTICATED                | 1     | Before user authentication       |
| EMBEDDED\_SIGNER\_NOT\_CONFIGURED | 2     | Before wallet configuration      |
| CREATING\_ACCOUNT               | 3     | Creating new account for chainID |
| READY                          | 4     | Wallet ready for use             |

## Methods

### GetEmbeddedState

Gets the current state of the embedded wallet.

**Method Signature:**

```csharp
public async UniTask<EmbeddedState> GetEmbeddedState()
```

**Parameters:**

* None

**Returns:**

* `UniTask<EmbeddedState>` - The current embedded wallet state

**Example:**

```csharp
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($"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($"Error getting embedded state: {e.Message}");
        }
    }
}
```

### Monitoring Embedded State (Helper Pattern)

:::info
`MonitorEmbeddedState` is **not** an SDK method — it is a helper pattern you implement in your own `MonoBehaviour` using `GetEmbeddedState()`.
:::

**Example:**

```csharp
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($"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
    }
}
```

### Waiting for Wallet Ready (Helper Pattern)

:::info
`WaitForEmbeddedWalletReady` is **not** an SDK method — it is a helper pattern you implement in your own `MonoBehaviour` using `GetEmbeddedState()`.
:::

**Example:**

```csharp
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($"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($"Message signed: {signature}");
        }
        catch (Exception e)
        {
            Debug.LogError($"Failed to sign message: {e.Message}");
        }
    }
}
```
