External Wallet Authentication
Learn how to connect the external wallets to authenticate users.
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:
_13using UnityEngine;_13using Openfort.OpenfortSDK;_13using Openfort.OpenfortSDK.Model;_13_13public class OpenfortWalletManager : MonoBehaviour _13{_13 private OpenfortSDK openfort;_13 _13 private async void Start()_13 {_13 openfort = await OpenfortSDK.Init("YOUR_OPENFORT_PUBLISHABLE_KEY");_13 }_13}
Initialize SIWE authentication#
To start the SIWE (Sign in With Ethereum) process:
_17public class OpenfortWalletManager : MonoBehaviour _17{_17 // ... previous code ..._17_17 public async Task InitializeSIWE(string walletAddress)_17 {_17 try _17 {_17 await openfort.InitSIWE(new InitSiweRequest(walletAddress));_17 Debug.Log("SIWE initialization successful");_17 }_17 catch (Exception e)_17 {_17 Debug.LogError($"Error initializing SIWE: {e.Message}");_17 }_17 }_17}
Verify SIWE signature#
After getting the signature from the wallet, verify it to authenticate the user:
_35public class OpenfortWalletManager : MonoBehaviour _35{_35 // ... previous code ..._35_35 public async Task AuthenticateWithSIWE(_35 string signature, _35 string message, _35 string walletClientType, // e.g., "metamask", "coinbaseWallet"_35 string connectorType) // e.g., "wallet_connect_v2", "injected", "coinbase_wallet"_35 {_35 try _35 {_35 var request = new AuthenticateWithSiweRequest(_35 signature,_35 message,_35 walletClientType,_35 connectorType_35 );_35 _35 var response = await openfort.AuthenticateWithSIWE(request);_35 Debug.Log("SIWE authentication successful");_35 _35 // The response contains:_35 // - response.player: Player information_35 // - response.token: Authentication token_35 // - response.refreshToken: Token for refreshing authentication_35 _35 // Store these tokens as needed for your game_35 }_35 catch (Exception e)_35 {_35 Debug.LogError($"Error authenticating with SIWE: {e.Message}");_35 }_35 }_35}
Authentication response#
Upon successful authentication, you'll receive a response containing:
_16{_16 "player": {_16 "id": "pla_cc9ed2b7-c5f5-4c43-8dca-c4b104ba1762",_16 "object": "player",_16 "createdAt": 1710976453,_16 "linkedAccounts": [_16 {_16 "provider": "wallet",_16 "address": "0x1234567890abcdef",_16 "disabled": false,_16 }_16 ]_16 },_16 "token": "eyJhbGci...",_16 "refreshToken": "eyJhbGci..."_16}
UI integration example#
Here's a basic example of how to integrate wallet authentication with Unity UI:
_43public class WalletAuthUI : MonoBehaviour_43{_43 [SerializeField] private Button connectWalletButton;_43 [SerializeField] private TMP_Text statusText;_43 _43 private OpenfortWalletManager walletManager;_43 private string userWalletAddress;_43_43 private void Start()_43 {_43 walletManager = GetComponent<OpenfortWalletManager>();_43 connectWalletButton.onClick.AddListener(HandleWalletConnection);_43 }_43_43 private async void HandleWalletConnection()_43 {_43 statusText.text = "Connecting wallet...";_43 _43 try _43 {_43 // First initialize SIWE_43 await walletManager.InitializeSIWE(userWalletAddress);_43 _43 // After getting signature from wallet (implementation depends on your wallet integration)_43 string signature = await GetWalletSignature();_43 string message = await GetSIWEMessage();_43 _43 // Authenticate_43 await walletManager.AuthenticateWithSIWE(_43 signature,_43 message,_43 "metamask", // or your chosen wallet_43 "injected" // or your chosen connector type_43 );_43 _43 statusText.text = "Wallet connected!";_43 }_43 catch (Exception e)_43 {_43 statusText.text = "Connection failed: " + e.Message;_43 }_43 }_43}