External Wallet Authentication
Connect wallets via the Sign in With Ethereum (SIWE) standard. This authentication method is designed for users who prefer to authenticate using their external wallets. Openfort's Unity integration facilitates a secure and direct authentication process using these wallets.
Setting up wallet authentication
First, create a manager class to handle Openfort wallet authentication:
using UnityEngine;
using Openfort.OpenfortSDK;
using Openfort.OpenfortSDK.Model;
public class OpenfortWalletManager : MonoBehaviour
{
private OpenfortSDK openfort;
private async void Start()
{
openfort = await OpenfortSDK.Init("YOUR_OPENFORT_PUBLISHABLE_KEY");
}
}
Initialize SIWE authentication
To start the SIWE (Sign in With Ethereum) process:
public class OpenfortWalletManager : MonoBehaviour
{
// ... previous code ...
public async Task InitializeSIWE(string walletAddress)
{
try
{
await openfort.auth.initSIWE(new InitSiweRequest(walletAddress));
Debug.Log("SIWE initialization successful");
}
catch (Exception e)
{
Debug.LogError(quot;Error initializing SIWE: {e.Message}");
}
}
}
Verify SIWE signature
After getting the signature from the wallet, verify it to authenticate the user:
public class OpenfortWalletManager : MonoBehaviour
{
// ... previous code ...
public async Task AuthenticateWithSIWE(
string signature,
string message,
string walletClientType, // e.g., "metamask", "coinbaseWallet"
string connectorType) // e.g., "wallet_connect_v2", "injected", "coinbase_wallet"
{
try
{
var request = new AuthenticateWithSiweRequest(
signature,
message,
walletClientType,
connectorType
);
var response = await openfort.auth.authenticateWithSIWE(request);
Debug.Log("SIWE authentication successful");
// The response contains:
// - response.player: Player information
// - response.token: Authentication token
// - response.refreshToken: Token for refreshing authentication
// Store these tokens as needed for your game
}
catch (Exception e)
{
Debug.LogError(quot;Error authenticating with SIWE: {e.Message}");
}
}
}
Authentication response
Upon successful authentication, you'll receive a response containing:
{
"player": {
"id": "pla_cc9ed2b7-c5f5-4c43-8dca-c4b104ba1762",
"object": "player",
"createdAt": 1710976453,
"linkedAccounts": [
{
"provider": "wallet",
"address": "0x1234567890abcdef",
"disabled": false,
}
]
},
"token": "eyJhbGci...",
"refreshToken": "eyJhbGci..."
}
UI integration example
Here's a basic example of how to integrate wallet authentication with Unity UI:
public class WalletAuthUI : MonoBehaviour
{
[SerializeField] private Button connectWalletButton;
[SerializeField] private TMP_Text statusText;
private OpenfortWalletManager walletManager;
private string userWalletAddress;
private void Start()
{
walletManager = GetComponent<OpenfortWalletManager>();
connectWalletButton.onClick.AddListener(HandleWalletConnection);
}
private async void HandleWalletConnection()
{
statusText.text = "Connecting wallet...";
try
{
// First initialize SIWE
await walletManager.InitializeSIWE(userWalletAddress);
// After getting signature from wallet (implementation depends on your wallet integration)
string signature = await GetWalletSignature();
string message = await GetSIWEMessage();
// Authenticate
await walletManager.AuthenticateWithSIWE(
signature,
message,
"metamask", // or your chosen wallet
"injected" // or your chosen connector type
);
statusText.text = "Wallet connected!";
}
catch (Exception e)
{
statusText.text = "Connection failed: " + e.Message;
}
}
}