Recover a wallet from backup
Overview
This guide describes how to recover an MPC wallet from a backup on the Sky Mavis server. The recovery process helps the player re-access their wallet on a new device or new client. The implementation steps described here only apply to the standard Sky Mavis backup flow.
When performing recovery, the system fetches the user's encrypted private key shard from the Sky Mavis keystore, and assuming that the user provides the correct recovery password, the recovery is successful.
Prerequisites
- Complete the steps in Create an MPC wallet
- Complete the steps in Create a transaction
Step 1. Verify that user has MPC wallet
To verify that the wallet is created and that the user can recover it when needed, send a request to the user info endpoint of the Sky Mavis OAuth 2.0 API to retrieve the user's profile.
The use of the user info endpoint requires an API key for authentication.
The request must be made from your backend server instead of the game client.
GET https://api-gateway.skymavis.com/account/userinfo
X-API-Key: {YOUR_API_KEY}
Authorization: Bearer {ACCESS_TOKEN}
In the response, the ronin_address
field must not be empty, and the wallet_type
field must be equal to mpc
.
{
//...
"ronin_address": "0x6fda69632c0393c6f740323bd402993d933a7b2b",
//...
"wallet_type": "mpc",
//...
}
Step 2. Pull encrypted key from Sky Mavis server
- Core
- Providers
- Unity
const encryptedKey = await mpcCore.getBackupKey(accessToken: string);
Example:
// OAuth 2.0 access token
const accessToken = `${ACCESS_TOKEN}`;
const encryptedKey = await mpcCore.getBackupKey(accessToken);
//...
const encryptedKey = await mpcProviders.getBackupKey(accessToken: string);
Example:
// OAuth 2.0 access token
const accessToken = `${ACCESS_TOKEN}`;
const encryptedKey = await mpcProviders.getBackupKey(accessToken);
//...
Task<(string, MPCError)> MPC.GetBackupKey()
Example:
var (encryptedKey, error) = await MPC.GetBackupKey();
if (error != null) {
Debug.LogError($"Error when getting backup of private key shard: {error.message}");
}
else {
Debug.Log($"Key backup retrieved from server: {encryptedKey}");
}
Step 3. Decrypt private key shard
During the wallet creation process, you asked the user to create a password to encrypt the private key shard. Your client needs to collect this password to decrypt the private key.
- Core
- Providers
- Unity
const privateKeyShard = await mpcCore.decryptKey(accessToken, encryptedKey, password);
Example:
const accessToken = `${ACCESS_TOKEN}`;
const encryptedKey = `${ENCRYPTED_KEY}`;
const password = `${RECOVERY_PASSWORD}`;
const privateKeyShard = await mpcCore.decryptKey(accessToken, encryptedKey, password);
console.log(decryptKey.data);
const privateKeyShard = await mpcProviders.decryptKey(accessToken, encryptedKey, password);
Example:
const accessToken = `${ACCESS_TOKEN}`;
const encryptedKey = `${ENCRYPTED_KEY}`;
const password = `${RECOVERY_PASSWORD}`;
const privateKeyShard = await mpcProviders.decryptKey(accessToken, encryptedKey, password);
Task<(string, MPCError)> MPC.DecryptData(string encryptedKey, string password);
Example:
string encryptedKey = $"{ENCRYPTED_KEY}";
string password = $"{RECOVERY_PASSWORD}";
var (privateKeyShard, error) = await MPC.DecryptData(encryptedKey, password);
if (error != null) {
Debug.LogError($"Error when getting decrypted data: {error.message}");
}
else {
Debug.Log($"Decrypted private key shard: {privateKeyShard}");
}
If the user enters the password correctly, the decryption is successful.