Integrate Web3Auth with the Polymesh Blockchain
While using the Web3Auth Web SDK for a non-EVM chain like Polymesh
you get a standard provider from which you can get the private key of the user. Using this private
key, you can use the corresponding libraries of the blockchain to make blockchain calls like getting
the user's public signing key
, fetching balance
, and sign & send transaction
. We have
highlighted a few methods here to get you started quickly on that.
Installation
For Polymesh, we will use the libraries @polymeshassociation/polymesh-sdk
and
@polymeshassociation/local-signing-manager
to create the Polymesh address, query the chain and
submit transactions.
- npm
- Yarn
- pnpm
npm install --save @polymeshassociation/polymesh-sdk @polymeshassociation/local-signing-manager
yarn add @polymeshassociation/polymesh-sdk @polymeshassociation/local-signing-manager
pnpm add @polymeshassociation/polymesh-sdk @polymeshassociation/local-signing-manager
Initializing Provider
Getting the chainConfig
- Mainnet
- Testnet
const chainConfig = {
chainNamespace: "other",
chainId: "0x1",
rpcTarget: "https://mainnet-rpc.polymesh.network",
// Avoid using public rpcTarget in production.
displayName: "Polymesh Mainnet",
blockExplorerUrl: "https://polymesh.subscan.io",
ticker: "POLYX",
tickerName: "Polymesh",
};
const chainConfig = {
chainNamespace: "other",
chainId: "0x5", //
rpcTarget: "https://testnet-rpc.polymesh.live",
// Avoid using public rpcTarget in production.
displayName: "Polymesh Testnet",
blockExplorerUrl: "https://polymesh-testnet.subscan.io",
ticker: "POLYX",
tickerName: "Polymesh",
};
Initializing and instantiating the Web3Auth SDK
- PnP Modal SDK
- PnP NoModal SDK
- CoreKit SFA Web SDK
import { Web3Auth } from "@web3auth/modal";
import { CommonPrivateKeyProvider } from "@web3auth/base-provider";
import { WEB3AUTH_NETWORK, CHAIN_NAMESPACES } from "@web3auth/base";
const privateKeyProvider = new CommonPrivateKeyProvider({
config: { chainConfig: chainConfig },
});
const web3auth = new Web3Auth({
// Get it from Web3Auth Dashboard
clientId,
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
privateKeyProvider: privateKeyProvider,
});
import { Web3AuthNoModal } from "@web3auth/no-modal";
import { AuthAdapter } from "@web3auth/auth-adapter";
import { CommonPrivateKeyProvider } from "@web3auth/base-provider";
import { WEB3AUTH_NETWORK, CHAIN_NAMESPACES } from "@web3auth/base";
const privateKeyProvider = new CommonPrivateKeyProvider({
config: { chainConfig },
});
const web3auth = new Web3AuthNoModal({
clientId, // Get it from Web3Auth Dashboard
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
});
const authAdapter = new AuthAdapter();
web3auth.configureAdapter(authAdapter);
import { Web3Auth } from "@web3auth/single-factor-auth";
import { CommonPrivateKeyProvider } from "@web3auth/base-provider";
import { WEB3AUTH_NETWORK, CHAIN_NAMESPACES } from "@web3auth/base";
const web3auth = new Web3Auth({
clientId, // Get your Client ID from Web3Auth Dashboard
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
});
const privateKeyProvider = new CommonPrivateKeyProvider({
config: { chainConfig },
});
Getting the Web3Auth provider
After initializing Web3Auth, the next step is to initialize the provider and use it for your operations.
- PnP Modal SDK
- PnP NoModal SDK
- CoreKit SFA Web SDK
// Initialize for PnP Modal SDK
await web3auth.initModal();
// Trigger the login
await web3auth.connect();
// Get the provider
const provider = web3auth.provider;
// Continue using the `provider`
// Initialize for PnP NoModal SDK
await web3auth.init();
// Trigger the login
await web3auth.connectTo("auth");
// Get the provider
const provider = web3auth.provider;
// Continue using the `provider`
// Initialize for CoreKit SFA Web SDK
await web3auth.init();
// Trigger the login
await web3auth.connect();
// Get the provider
const provider = web3auth.provider;
// Continue using the `provider`
After logging in, the Web3Auth instance will provide you with information regarding the user that is logged in. This information is obtained directly from the JWT token and is not stored by Web3Auth. Therefore, this information can only be accessed through social logins after the user has logged into your application.
const user = await web3auth.getUserInfo(); // web3auth instance