Sign messages with the embedded signer
Learn how to sign messages and typed data using the embedded signer in Unity
Prerequisites#
Before implementing message signing:
- Ensure Openfort's
embeddedState
isready
- Verify the user is properly authenticated
- Have an embedded signer configured
Message Signing#
To request a signature from a user, use the SignMessage
method. This implements the EIP-191 personal_sign standard.
_26using UnityEngine;_26using System;_26using System.Threading.Tasks;_26using Openfort.OpenfortSDK;_26using Openfort.OpenfortSDK.Model;_26_26public class MessageSigningManager : MonoBehaviour _26{_26 private OpenfortSDK openfort;_26_26 public async Task SignBasicMessage(string message)_26 {_26 try _26 {_26 var signMessageRequest = new SignMessageRequest(message);_26 var signature = await openfort.SignMessage(signMessageRequest);_26 Debug.Log($"Message signed successfully. Signature: {signature}");_26 return signature;_26 }_26 catch (Exception e)_26 {_26 Debug.LogError($"Error signing message: {e.Message}");_26 throw;_26 }_26 }_26}
Typed Data Signing#
For signing EIP-712 typed data, use the SignTypedData
method. This implements the eth_signTypedData_v4 standard.
_23public class MessageSigningManager : MonoBehaviour _23{_23 // ... previous code ..._23_23 public async Task SignTypedData(_23 Dictionary<string, object> domain,_23 Dictionary<string, object[]> types,_23 Dictionary<string, object> value)_23 {_23 try _23 {_23 var request = new SignTypedDataRequest(domain, types, value);_23 var signature = await openfort.SignTypedData(request);_23 Debug.Log($"Typed data signed successfully. Signature: {signature}");_23 return signature;_23 }_23 catch (Exception e)_23 {_23 Debug.LogError($"Error signing typed data: {e.Message}");_23 throw;_23 }_23 }_23}
Example of Typed Data Structure#
_29// Example of structured data for signing_29public void CreateAndSignTypedData()_29{_29 var domain = new Dictionary<string, object>_29 {_29 { "name", "My Game" },_29 { "version", "1" },_29 { "chainId", 1 },_29 { "verifyingContract", "0x1234567890123456789012345678901234567890" }_29 };_29_29 var types = new Dictionary<string, object[]>_29 {_29 { "Person", new[] _29 {_29 new { name = "name", type = "string" },_29 new { name = "score", type = "uint256" }_29 }_29 }_29 };_29_29 var value = new Dictionary<string, object>_29 {_29 { "name", "Alice" },_29 { "score", 100 }_29 };_29_29 SignTypedData(domain, types, value);_29}