Sign a message
Overview
This guide describes how to sign messages using the MPC SDK. In the MPC's collaborative process, the user and the Sky Mavis backend both participate in signing messages, creating a unique signature prefixed with the standard \x19Ethereum Signed Message
. Message signatures serve as irrefutable proof of identity, enabling you to build secure verification systems within your game.
The MPC SDK exclusively supports signing string-type messages.
Prerequisites
Complete the steps in Create an MPC wallet.
Sign a message
- Core
- Providers
- Unity
To sign a message using mpcCore
, use the private key shard correspondent to the public address that you generated in Create an MPC wallet, as well as the user's access token after they signed in with their Sky Mavis account.
const result = await mpcCore.signMessage(accessToken, privateKeyShard, message);
Example:
const accessToken = `${ACCESS_TOKEN}`;
const privateKeyShard = `${PRIVATE_KEY}`;
// "Sign this message to breed Axie #223495 with Axie #123232"
const encodedMessage = "U2lnbiB0aGlzIG1lc3NhZ2UgdG8gYnJlZWQgQXhpZSAjMjIzNDk1IHdpdGggQXhpZSAjMTIzMjMy";
const result = await mpcCore.signMessage(accessToken, privateKeyShard, encodedMessage);
//...
Before signing a message, ensure it's encoded to the Base64 format.
To sign a message using mpcProvider
, call the signMessage
function:
const txHash = await mpcProvider.getSigner().signMessage(message);
Example:
// "Sign this message to breed Axie #223495 with Axie #123232"
const encodedMessage = "U2lnbiB0aGlzIG1lc3NhZ2UgdG8gYnJlZWQgQXhpZSAjMjIzNDk1IHdpdGggQXhpZSAjMTIzMjMy"
const txHash = await mpcProvider.getSigner().signMessage(encodedMessage);
//...
To sign a messasge, use the SignMessage
method:
Task<(string signedMessage, MPCError error)> MPC.SignMessage(string message, string password);
Example:
string password = $"{RECOVERY_PASSWORD}";
string message = "Sign this message to breed Axie #223495 with Axie #123232";
var (data, error) = await MPC.SignMessage(message, password);
if (error != null)
{
Debug.LogError($"Error when signing message: {JsonConvert.SerializeObject(error)}");
}
else
{
Debug.Log($"Signature: {JsonConvert.SerializeObject(data)}");
}