Skip to content

Sign messages

Prerequisites

Before implementing message signing:

  • Ensure Openfort's embeddedState is ready
  • Verify the user is properly authenticated
  • Have an embedded wallet configured

Message Signing

To request a signature from a user, use the SignMessage method. This implements the EIP-191 personal_sign standard.

using UnityEngine;
using System;
using System.Threading.Tasks;
using Openfort.OpenfortSDK;
using Openfort.OpenfortSDK.Model;
 
public class MessageSigningManager : MonoBehaviour 
{
    private OpenfortSDK openfort;
 
    public async Task SignBasicMessage(string message)
    {
        try 
        {
            var signMessageRequest = new SignMessageRequest(message);
            var signature = await openfort.embeddedWallet.signMessage(signMessageRequest);
            Debug.Log(quot;Message signed successfully. Signature: {signature}");
            return signature;
        }
        catch (Exception e)
        {
            Debug.LogError(quot;Error signing message: {e.Message}");
            throw;
        }
    }
}

Typed Data Signing

For signing EIP-712 typed data, use the SignTypedData method. This implements the eth_signTypedData_v4 standard.

public class MessageSigningManager : MonoBehaviour 
{
    // ... previous code ...
 
    public async Task SignTypedData(
        Dictionary<string, object> domain,
        Dictionary<string, object[]> types,
        Dictionary<string, object> value)
    {
        try 
        {
            var request = new SignTypedDataRequest(domain, types, value);
            var signature = await openfort.embeddedWallet.signTypedData(request);
            Debug.Log(quot;Typed data signed successfully. Signature: {signature}");
            return signature;
        }
        catch (Exception e)
        {
            Debug.LogError(quot;Error signing typed data: {e.Message}");
            throw;
        }
    }
}

Example of Typed Data Structure

// Example of structured data for signing
public void CreateAndSignTypedData()
{
    var domain = new Dictionary<string, object>
    {
        { "name", "My Game" },
        { "version", "1" },
        { "chainId", 1 },
        { "verifyingContract", "0x1234567890123456789012345678901234567890" }
    };
 
    var types = new Dictionary<string, object[]>
    {
        { "Person", new[] 
            {
                new { name = "name", type = "string" },
                new { name = "score", type = "uint256" }
            }
        }
    };
 
    var value = new Dictionary<string, object>
    {
        { "name", "Alice" },
        { "score", 100 }
    };
 
    SignTypedData(domain, types, value);
}

Examples

Unity Sample Android Embedded Wallet

An integration with Google Play Games using Firebase Auth as a third party auth provider to create a non-custodial embedded wallet.

Unity Sample WebGL Embedded Wallet

An integration with Openfort Auth with non-custodial embedded wallet.