Management API Reference

Unity SDK

The Openfort SDK for Unity

SUPPORTED PLATFORMS
  • Windows (64-bit)
  • macOS (minimum version 12.5)
  • Android (minimum version 5.1)
  • iOS (minimum version 15.2)
SUPPORTED UNITY VERSIONS
  • Unity 2021.3 or newer for Windows, macOS, Android and iOS
  • Unity 2019.4 or newer for macOS, Android, and iOS. Windows isn't supported on Unity versions from 2019.4 up through 2021.2.
TARGET PLATFORM VS UNITY EDITOR PLATFORM

We have added compilation flags to the Unity SDK to ensure that specific Unity editors can only build certain platform targets. Please note that the table below indicates which editor you can use to build a platform target, but it does not determine whether you can run the SDK in that editor.

For example, the SDK allows you to build iOS games using a macOS Unity Editor, but you cannot use the Windows Unity Editor.

Target PlatformWindows Unity EditormacOS Unity Editor
Windows
macOS
Android
iOS

Installation#

Prerequisites

The Unity SDK requires UniTask package (version 2.3.3) as specified in package.json. How to install UniTask: Follow these instructions. Please check the Unity requirements to ensure a successful integration.

Since .dll files are stored on Git Large File Storage, you must download and install git-lfs from here.

  1. Open the Package Manager
  2. Click the add + button and select "Add package from git URL..." Enter https://github.com/openfort-xyz/openfort-csharp-unity.git?path=/src/Packages/OpenfortSDK and click 'Add'

Quickstart#

Initialize OpenfortSDK#


_13
using Openfort.OpenfortSDK;
_13
using Openfort.OpenfortSDK.Model;
_13
_13
public class openfortManager: MonoBehaviour {
_13
private OpenfortSDK openfort;
_13
const string PublishableKey = "YOUR_OPENFORT_PUBLISHABLE_KEY";
_13
const string ShieldApiKey = "YOUR_SHIELD_PUBLISHABLE_KEY";
_13
const string ShieldEncryptionShare = "YOUR_SHIELD_ENC_SHARE";
_13
private async void Start()
_13
{
_13
openfort = await OpenfortSDK.Init(PublishableKey, ShieldApiKey, ShieldEncryptionShare);
_13
}
_13
}

Authentication Methods#

1. Email and Password#


_10
await openfort.LogInWithEmailPassword(email, password);

2. OAuth (e.g., Google)#


_13
OAuthInitRequest request = new OAuthInitRequest()
_13
{
_13
Provider = OAuthProvider.Google,
_13
UsePooling = true, // For WebGL or standalone Windows
_13
// For other platforms:
_13
// UsePooling = false,
_13
// Options = new OAuthInitRequestOptions()
_13
// {
_13
// RedirectTo = "mygame://callback"
_13
// },
_13
};
_13
_13
var response = await openfort.AuthenticateWithOAuth();

3. Sign-In with Ethereum (SIWE)#


_10
string address = "0x..."; // User's Ethereum address
_10
string signature = "0x..."; // Signature from the user's wallet
_10
await openfort.InitSiwe(address);
_10
var response = await openfort.AuthenticateWithSiwe(signature);

Stored Credentials#


_10
bool hasCredsSaved = await openfort.GetAccessToken() != null;
_10
if (hasCredsSaved)
_10
{
_10
await openfort.ValidateAndRefreshToken(new ValidateAndRefreshTokenRequest());
_10
// Successfully re-logged into OpenfortSDK
_10
}

Log Out#


_10
await openfort.Logout();

Advanced Features#

Sign Message#


_10
SignMessageRequest signMessageRequest = new SignMessageRequest("Hello World!");
_10
string signature = await openfort.SignMessage(signMessageRequest);

Sign Typed Data#


_19
var domain = new TypedDataDomain(
_19
name: "Openfort",
_19
version: "0.5",
_19
chainId: 80002,
_19
verifyingContract: "0x9b5AB198e042fCF795E4a0Fa4269764A4E8037D2"
_19
);
_19
_19
var types = new Dictionary<string, List<TypedDataField>>
_19
{
_19
// Define your types here
_19
};
_19
_19
var message = new Dictionary<string, object>
_19
{
_19
// Define your message here
_19
};
_19
_19
var signTypedDataRequest = new SignTypedDataRequest(domain, types, message);
_19
string signature = await openfort.SignTypedData(signTypedDataRequest);

Send Transaction#


_23
// 1. Request the encoded transaction from your backend
_23
var webRequest = UnityWebRequest.PostWwwForm("https://your-backend-url.com/mint", "");
_23
webRequest.SetRequestHeader("Authorization", "Bearer " + accessToken);
_23
webRequest.SetRequestHeader("Content-Type", "application/json");
_23
webRequest.SetRequestHeader("Accept", "application/json");
_23
await SendWebRequestAsync(webRequest);
_23
_23
if (webRequest.result != UnityWebRequest.Result.Success)
_23
{
_23
Debug.Log("Mint Failed: " + webRequest.error);
_23
return;
_23
}
_23
_23
// 2. Parse the response from your backend
_23
var responseText = webRequest.downloadHandler.text;
_23
var responseJson = JsonConvert.DeserializeObject<RootObject>(responseText);
_23
_23
// 3. Create and send the SignatureTransactionIntentRequest
_23
SignatureTransactionIntentRequest request = new SignatureTransactionIntentRequest(responseJson.transactionIntentId, responseJson.userOperationHash);
_23
TransactionIntentResponse intentResponse = await openfort.SendSignatureTransactionIntentRequest(request);
_23
_23
// 4. Get the transaction hash from the response
_23
string transactionHash = intentResponse.Response.TransactionHash;

Note: Obtaining the transaction hash does not guarantee a successful transaction. Use an appropriate method to check the transaction receipt for the final status.

Supported Functionality#

The SDK supports a wide range of methods, including user authentication, wallet management, transaction signing, and more.

MethodFunctionality
InitInitializes OpenfortSDK with various parameters
LogInWithEmailPasswordLogs the user in using email and password
SignUpWithEmailPasswordSigns up a new user with email and password
LinkEmailPasswordLinks an email and password to the user account
UnlinkEmailPasswordUnlinks an email and password from the user account
RequestResetPasswordRequests a password reset
ResetPasswordResets the user's password
RequestEmailVerificationRequests an email verification
VerifyEmailVerifies the user's email
InitOAuthInitializes OAuth for the user
AuthenticateWithOAuthAuthenticates the user with OAuth
UnlinkOAuthUnlinks OAuth from the user account
PoolOAuthPools OAuth for the user account
InitLinkOAuthInitializes the link OAuth process
AuthenticateWithThirdPartyProviderAuthenticates the user with a third-party provider
InitSiweInitializes Sign-In with Ethereum (SIWE)
AuthenticateWithSiweAuthenticates the user with SIWE
LinkWalletLinks a wallet to the user account
UnlinkWalletUnlinks a wallet from the user account
StoreCredentialsStores authentication credentials
GetUserGets the user information
LogoutLogs the user out and removes stored credentials
GetAccessTokenGets the currently saved access token
ValidateAndRefreshTokenValidates and refreshes the access token
SendSignatureTransactionIntentRequestSends a signature transaction intent request
SignMessageSigns a message
SignTypedDataSigns typed data
SendSignatureSessionRequestSends a transaction signed by a session
GetEmbeddedStateGets the embedded state
GetEthereumProviderGets the Ethereum provider
ConfigureEmbeddedSignerConfigures the embedded signer

Examples#

  • Sample App: Demonstrates how to use the Openfort Unity SDK.
  • Sample Game: A tutorial on integrating the Openfort Unity SDK into a game.