Install and initialize the SDK
Overview
This guide shows how to install and initialize the MPC (multi-party computation) SDK for supported clients, which include JavaScript web apps and Unity projects.
Document structure
To cater to developers of different backgrounds and experience levels, the guides in our MPC documentation have three different tabs with instructions and code examples for each client type:
- The Core tab covers the use of the
mpcCore
provider in the MPC JavaScript SDK and is meant for beginner web3 developers who wish to implement the essential MPC features. - The Providers tab describes the use of the
mpcProvider
option that inherits the methods of other providers, such as Web3.js or Ethers.js, making it ideal for experienced web3 developers. - The Unity tab contains the instructions and code examples for Unity developers integrating their blockchain-enabled games with MPC.
About provider options in MPC JavaScript SDK
To facilitate integration for developers of varying backgrounds, the MPC JavaScript SDK offers two distinct provider options:
mpcCore
is designed for developers with little web3 experience. It offers essential methods for main operations, such as creating wallets and signing transactions and messages through MPC.mpcProvider
offers familiar methods for experienced developers that have already used libraries, such as Ethers.js or Web3.js, and offers some other benefits:- Seamless integration with existing projects built on Web3Provider, Ethers.js, or custom EIP-1193 providers.
- Minimum syntax changes: Retain your current syntax as
mpcProvider
automatically handles logic conversion for default methods.
Quickstart
- Core
- Providers
- Unity
Explore the various MPC features and get a feel for how it can benefit your web app: xdemo.vercel.app.
Explore the various MPC features and get a feel for how it can benefit your web app: xdemo.vercel.app.
Explore the various MPC features and get a feel for how it can benefit your game: Unity quickstart.
Prerequisites
- Core
- Providers
- Unity
- Complete the steps in Get started.
- Install the dependencies:
- Node.js v18.16 or later
- npm or Yarn for package installation
- Complete the steps in Get started.
- Install the dependencies:
- Node.js v18.16 or later
- npm or Yarn for package installation
- Complete the steps in Get started.
- Install the dependencies:
- Unity v2020.3 or later
- Json.NET
- Dependencies in the library:
Install
- Core
- Providers
- Unity
Install the MPC JS SDK using npm:
npm install @axieinfinity/mpcjs
Or using Yarn:
yarn add @axieinfinity/mpcjs
Install the MPC JS SDK using npm:
npm install @axieinfinity/mpcjs
Or using Yarn:
yarn add @axieinfinity/mpcjs
Download the Unity package and import it into your project:
- Download the file
mpc.x.y.x.unitypackage
from the GitHub release page. - In the Unity editor, go to Assets > Import Package > Custom Package.
- Select the downloaded package, and then click Open.
Initialize
- Core
- Providers
- Unity
Import the MpcCore
package:
import { MpcCore } from "@axieinfinity/mpcjs";
Initialize MpcCore
:
import { MpcCore } from "@axieinfinity/mpcjs";
const mpcCore = MpcCore.init({
domain: `${YOUR_SERVER_DOMAIN}`, // "project-x.skymavis.one",
rpcURL: `${NETWORK_RPC}`, // "https://saigon-testnet.roninchain.com/rpc"
secure: `${boolean}`, // true (default)
});
//...
Parameters:
Name | Type | Description |
---|---|---|
domain | String | Domain to interact with the backend and keystore. |
rpcURL | String | RPC (remote procedure call) URL of the blockchain network. |
secure | Bool | Preferred protocol: true for secure wss (use in the production environment) or false for insecure wss (use in the development environment only). Defaults to true . |
Example:
import { MpcCore } from "@axieinfinity/mpcjs";
const mpcCore = MpcCore.init({
domain: "project-x.skymavis.one",
rpcURL: "https://saigon-testnet.roninchain.com/rpc",
secure: true,
});
///...
Import the MpcProviders
package:
import { MpcProviders } from "@axieinfinity/mpcjs";
Initialize MpcProviders
:
import { MpcProviders } from "@axieinfinity/mpcjs";
const mpcProvider = MpcProviders.create(
domain: `${YOUR_SERVER_DOMAIN}`, // "project-x.skymavis.one"
rpcURL: `${NETWORK_RPC}`, // "https://saigon-testnet.roninchain.com/rpc"
chainID: `${YOUR_CHAIN_ID}` // 2021
);
//...
Parameters:
Name | Type | Description |
---|---|---|
domain | String | Domain to interact with the backend and keystore. |
rpcURL | String | RPC (remote procedure call) URL of the blockchain network. |
chainID | Int | Identitifier or numeric code associated with the blockchain network. |
Example:
import { MpcProviders } from "@axieinfinity/mpcjs";
const mpcProvider = MpcProviders.create(
domain: "project-x.skymavis.one",
rpcURL: "https://saigon-testnet.roninchain.com/rpc",
chainID: "2021"
);
//...
After initializing the MPC provider, connect it to the MPC service:
connect(accessToken, password): Promise<string>
Parameters:
Name | Type | Description |
---|---|---|
accessToken | String | Valid access token returned from the OAuth 2.0 provider (Sky Mavis Account). |
password | String | Recovery password set by the user. It's used to protect the private key shard through AES encryption to ensure that only users with the correct password can access the key. |
With mpcProvider
, you can import other providers that comply with EIP-1193, external providers such as Web3Provider, and providers that inherit from JsonRpcProvider.
Migrating from Ethers.js to mpcProvider
Sending a transaction with Ethers.js:
import { ethers } from "ethers";
const etherProvider = new ethers.providers.JsonRpcProvider(
"https://arbitrum-goerli.infura.io/v3/${YOUR_API_KEY}",
);
const rawTx = {
// Receiver address
to: `${RECEIVER_ADDRESS}`,
value: ethers.utils.parseEther('1.0'), // 1 ETH
};
// Send transaction
const txData = await etherProvider.getSigner().sendTransaction(rawTx);
//...
Sending a transaction with mpcProvider
:
...
const receiver = "0xcd3cf91e7f0601ab98c95dd18b4f99221bcf0b20";
const amount = "0x8ac7230489e80000";
// Send transaction
const txData = await mpcProvider.getSigner().sendTransaction({to: receiver, value: amount });
//...
This example shows how to sign a message with Ethers.js:
import { ethers } from "ethers";
...
const infuraUrl = "https://arbitrum-goerli.infura.io/v3/${YOUR_API_KEY}";
const etherProvider = new ethers.providers.JsonRpcProvider(infuraUrl);
const sign = await etherProvider.getSigner().signMessage("Sign this message to breed Axie #223495 with Axie #123232");
...
The following example demonstrates how to sign a message with mpcProvider
:
import { ethers } from "ethers";
const etherProvider = new ethers.providers.JsonRpcProvider(
"https://arbitrum-goerli.infura.io/v3/${YOUR_API_KEY}",
);
const signMessage = "Sign this message to breed Axie #223495 with Axie #123232";
const result = await mpcProvider.getSigner().signMessage(signMessage);
//...
This example shows how to call the .approve()
method in an ERC20 contract with Ethers.js:
import { Contract, ethers } from "ethers";
...
const etherProvider = new ethers.providers.JsonRpcProvider(
"https://arbitrum-goerli.infura.io/v3/${YOUR_API_KEY}",
);
const amount = value: ethers.utils.parseEther('1.0'); // 1 ETH
const contract = new Contract(${contractAddress}, ${ABI}, etherProvider.getSigner());
const approveTx = await contract.approve(${RECEIVER_ADDRESS}, amount);
...
The following example demonstrates how to call the .approve()
method in an ERC20 contract with mpcProvider
:
const contract = new Contract(${contractAddress}, ${ABI}, mpcProvider.getSigner());
const approveTx = await contract.approve(${RECEIVER_ADDRESS}, amount);
//...
Add an external provider:
// Create Infura as external provider
const externalProvider = new ethers.providers.JsonRpcProvider(
"https://arbitrum-goerli.infura.io/v3/${YOUR_API_KEY}",
);
// Add Infura to mpcProvider
mpcProvider.addProvider(externalProvider);
...
Example:
const networkInfo = await mpcProvider.externalProvider.getNetwork();
console.log(networkInfo);
//...
To initialize the MPC SDK, call the Initialize
method first, before using any other SDK methods. This process automatically checks for and retrieves a user's backup private key shard, if available, from the Sky Mavis backup server.
MPC.Initialize(accessToken : string, env : Environment);
Parameters:
Name | Type | Description |
---|---|---|
AccessToken | String | Valid access token returned from an OAuth 2.0 provider. |
env | Environment | Defines the environment. Use Staging for the staging or testing environment. Use Production when the game is running in a live, production environment, serving real users. The default value is Staging. |
Example:
await MPC.Initialize(CreateAccessToken(userId), Environment.Staging);
await MPC.Initialize(CreateAccessToken(userId), Environment.Production);
If the user's access token changes during the game session, call the UpdateAccessToken
function to update it.
MPC.UpdateAccessToken(AccessToken);