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 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:
Parameter | Description |
---|---|
chainId | The blockchain network identifier. See supported chains |
shieldAuth | Authentication configuration including auth type and token |
password | Optional: Recovery password for user-based recovery |
The ShieldAuthentication
configuration includes:
Parameter | Description |
---|---|
auth | Either ShieldAuthType.Openfort or ShieldAuthType.Custom |
token | The 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
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 |
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();
}
}