Signature
This guide explains how to implement general signature functionality using the Cross Unity SDK, including EIP-712 signatures and message signing.
Basic Signature
1. Sign Message
Below is an example of requesting a signature for an arbitrary message (text) from the user's wallet. This function is used to prove user identity or to obtain consent for a specific message.
// Get account information.
var account = await CrossSdk.GetAccountAsync();
// Prepare the message and custom metadata for signing.
var message = "Hello from Unity!";
var customData = new CustomData { Metadata = "This is before message signing. This data is plain text type custom data." };
// Request message signature. (Use "0x" if address is not available)
var signature = await CrossSdk.Evm.SignMessageAsync(message, "0x", customData);
// Print signature result.
Debug.Log($"Signature: {signature}");
2. Sign EIP712 (Typed Data)
Below is an example of requesting a signature for structured data (typed data) according to the EIP-712 standard. This function is used when interacting with smart contracts or when a reliable signature for complex data structures is required.
// Get account information.
var account = await CrossSdk.GetAccountAsync();
// Define EIP-712 typed data and create a message (example: Mail struct)
var typedData = GetMailTypedDefinition(); // See GetMailTypedDefinition in Dapp.cs
var mail = new Mail
{
From = new Person { Name = "Cow", Wallets = new List<string> { "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826", "0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF" } },
To = new List<Person> { new Person { Name = "Bob", Wallets = new List<string> { "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB", "0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57", "0xB0B0b0b0b0b0B000000000000000000000000000" } } },
Contents = "Hello, Bob!"
};
// Set chain ID
var ethChainId = Utils.ExtractChainReference(account.ChainId);
typedData.Domain.ChainId = BigInteger.Parse(ethChainId);
typedData.SetMessage(mail);
// Convert to JSON message
var jsonMessage = typedData.ToJson();
// Request EIP-712 typed data signature
var signature = await CrossSdk.Evm.SignTypedDataAsync(jsonMessage);
// Verify signature
var isValid = await CrossSdk.Evm.VerifyTypedDataSignatureAsync(account.Address, jsonMessage, signature);
Debug.Log($"Signature valid: {isValid}");
3. Verify Signature
Below is an example of verifying whether a signature is valid using the message, signature, and address. This function is used to check if the signature submitted by the user was actually created by the owner of the account.
// Verify signature using message, signature, and address.
var isValid = await CrossSdk.Evm.VerifyMessageSignatureAsync(address, message, signature);
Debug.Log($"Signature valid: {isValid}");
Provider Request
1. Network Info Request
Below is an example of directly requesting network information such as chain ID, block number, and gas price from the Ethereum network. This function is used to check the network status or to refer to network information before executing a transaction.
// Request network info like chain ID, block number, and gas price.
var chainId = await CrossSdk.Evm.RequestAsync<string>("eth_chainId");
var blockNumber = await CrossSdk.Evm.RequestAsync<string>("eth_blockNumber");
var gasPrice = await CrossSdk.Evm.RequestAsync<string>("eth_gasPrice");
Debug.Log($"Chain ID: {chainId}");
Debug.Log($"Block Number: {blockNumber}");
Debug.Log($"Gas Price: {gasPrice}");
// Request network info like chain ID, block number, and gas price.
var chainId = await CrossSdk.Evm.RequestAsync<string>("eth_chainId");
var blockNumber = await CrossSdk.Evm.RequestAsync<string>("eth_blockNumber");
var gasPrice = await CrossSdk.Evm.RequestAsync<string>("eth_gasPrice");
Debug.Log($"Chain ID: {chainId}");
Debug.Log($"Block Number: {blockNumber}");
Debug.Log($"Gas Price: {gasPrice}");
Updated 10 days ago