Manage embedded wallet state
There are two essential steps to configure Openfort's embedded wallets in your Unity application:
- Configure the embedded wallet
- 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.
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:public async UniTask<EmbeddedState> GetEmbeddedState()
- None
UniTask<EmbeddedState>
- The current embedded wallet state
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()
- None
UniTaskVoid
- Async operation that completes when wallet is ready
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()
- None
UniTask
- Completes when wallet is ready
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}");
}
}
}