Create a wallet
Overview
This guide describes how to create a keyless MPC-powered wallet using the Lockbox SDK.
This process involves generating a client-side private key shard, encrypting it with a passphrase, and backing up the encrypted shard. The user's wallet address is then retrieved for use in transactions.
Prerequisites
Complete the steps in Get started.
Steps
Step 1. Get the user's access token
Get the user's access token after authenticating the user with Sky Mavis Account or Ronin Waypoint.
Step 2. Collect a passphrase
A passphrase is a crucial part of the wallet creation process. It's used to encrypt the user's private key shard. The passphrase is used to decrypt the private key shard during wallet recovery.
The passphrase must meet the following requirements:
- Contain at least 8 characters
- Include at least one number
- Include at least one special character
Step 3. Create a wallet
- Web
- Unity
- Create a client-side private key shard:
Function signature:
async lockbox.genMpc() : KeygenResult ;
Result:
{
key: string;
meta: {
requestId: string;
uuid: string;
}
}
Example:
const result = await lockbox.genMpc();
console.log("Client's key shard : ", result.key);
//...
- Encrypt the client key shard with the passphrase:
Function signature:
encryptClientShard = async (passphrase: string, recoveryPhraseLength?: number)
Parameters:
Name | Type | Required | Description |
---|---|---|---|
passphrase | String | Required | The user's passphrase for encrypting the client shard. |
recoveryPhraseLength | Number | Optional | The number of words in the recovery kit used to decrypt the client shard. Default is 8 words. |
Result:
Name | Type | Description |
---|---|---|
encryptedKey | String | The client shard encrypted with the passphrase. |
recoveryPhrase | String | A set of words used to decrypt the encrypted client shard if the user loses their passphrase. |
The user must remember their passphrase and recovery kit. If the user loses the passphrase and recovery kit, they lose access to their wallet, and neither Sky Mavis nor you as a developer can recover it.
Example:
const passphrase: string = `${YOUR_PASSPHRASE}`;
const recoveryPhraseLength: number = 8; // Default
// Return encrypted client shard
const result = await lockbox.encryptClientShard(
passphrase,
recoveryPhraseLength
);
//..
- Back up the client shard:
Function signature:
backupClientShard = async (encryptedClientShard: string)
Parameters:
Name | Type | Required | Description |
---|---|---|---|
encryptedClientShard | String | Required | The encrypted client shard. |
Result:
Name | Type | Description |
---|---|---|
encryptedKey | String | The client shard encrypted with the passphrase. |
Example:
const result = await lockbox.backupClientShard(encryptedClientShard);
console.log("Encrypted client shard: ", result);
//..
Sky Mavis supports only MPC_CMP_ECDSA_SECP256K1
, compatible with most EVM (Ethereum Virtual Machine) chains.
Here's a complete example:
// Initialize lockbox
// Create a handler to create a wallet
const handleCreateWallet = async () => {
try {
// Setup password
const passphrase = `${PASSPHRASE}`;
// Generate MPC key for new wallet
const response = await lockbox.genMpc();
// Set client shard
lockbox.setClientShard(response.key);
// Encrypt client shard
const encryptedResult = await lockbox.encryptClientShard(passphrase, 8);
// Back up key to server
await lockbox.backupClientShard(encryptedResult.encryptedKey);
} catch (error) {
console.error(error);
}
};
Create an MPC key:
Task<(bool success, MPCError error)> MPC.Keygen(string password);
Example:
string password = $"{RECOVERY_PASSWORD}";
var privateKeyShard = await MPC.Keygen(password);
if (privateKeyShard.error != null) {
Debug.LogError($"Error when generating MPC private key: {privateKeyShard.error.message}");
} else {
Debug.Log($"Result of MPC key generation: {privateKeyShard}");
}
Step 4. Retrieve wallet address
- Web
- Unity
Retrieve the wallet's public key to use for transactions.
Function signature:
async function getUserProfile()
Result:
Name | Type | Description |
---|---|---|
address | String | MPC address. |
updatedAt | Number | Latest updated profile time. |
uuid | String | User ID . |
Example:
const result = await lockbox.getUserProfile();
console.log("Address : ", result.address);
After the wallet is generated, you can retrieve its public address as follows:
// Initialize the MPC environment
await MPC.Initialize(CreateAccessToken(userId), Environment.Production);
Debug.Log($"Wallet address: {MPC.WalletAddress}");
string address = MPC.WalletAddress;
Step 5. Verify wallet creation
Form a request to the Sky Mavis Account service to verify the wallet creation.
Request:
GET https://api-gateway.skymavis.com/account/userinfo
X-API-Key: {YOUR_API_KEY} // API key from the Developer Console
Authorization: Bearer {ACCESS_TOKEN} // User's access token
Response:
{
"ronin_address": "<string>",
"wallet_type": "mpc"
}
The ronin_address
field must match the wallet public key. The wallet_type
field must be equal to mpc
.