First, let us create a wallet using Web3Auth for Social Login. For this, we have to sign up at to create an account to create a web3Auth Client ID which is available after you are signed into the dashboard and create a project under 'Plug and Play'. Copy the Client ID shown and keep it ready before continuing.
You can view a detailed steps on how to get started with Web3Auth :
import { Web3AuthNoModal } from "@web3auth/no-modal";
import {
CHAIN_NAMESPACES,
WALLET_ADAPTERS,
ADAPTER_EVENTS,
CONNECTED_EVENT_DATA,
} from "@web3auth/base";
import { OpenloginAdapter } from "@web3auth/openlogin-adapter";
Whitelisting
The Client ID is unique for each project, if we use different Client IDs the same account login will give us different private keys. So if anyone uses our client ID, it will give a private key similar to the one we give. So it is necessary to add website links before allowing it to share our web3auth login.
Initialise web3auth
const web3auth = new Web3AuthNoModal({
clientId,
web3AuthNetwork: "testnet", // mainnet, aqua, cyan or testnet
chainConfig: {
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0x13881",
rpcTarget: "https://rpc.ankr.com/polygon_mumbai", // This is the public RPC we have added, please pass on your own endpoint while creating an app
},
});
// Emits an event when connection completed
web3auth.on(ADAPTER_EVENTS.CONNECTED, () => {
if (!web3AuthInstance?.provider) {
return
}
})
// Emits an event when an error occurs during connection
web3auth.on(ADAPTER_EVENTS.ERRORED, (error) => {
console.log(error);
})
Initialize the web3Auth instance after setting up the Adapter Configuration
await web3auth.init();
Login with web3auth
Now, log in to web3Auth with valid credentials from any of the supported social platforms that web3auth supports..
For this example, Let us take Google as the social platform that we are looking to login
try {
if (!web3auth) {
console.log("web3auth not initialized yet");
return;
}
const web3authProvider = await web3auth.connectTo(
WALLET_ADAPTERS.OPENLOGIN,
{
mfaLevel: "default",
loginProvider: "google",
}
);
const privateKey = (await web3auth.provider?.request({
method: "private_key",
})) as string;
}
catch (e) {
console.log(`Failed to login! Reason: ${e instanceof Error && e?.message ? e.message : 'unknown'}.`)
return
}
if (!web3authProvider) {
console.log(`Failed to get the provider connected`)
return
}
After completing the login process, you can obtain the private key. Before initializing the Mirai SDK with it, it is necessary to format the private key.
Format privateKey
function formatPrivateKey(privateKey: any): string {
if (privateKey.startsWith("0x")) {
return privateKey;
} else {
return "0x" + privateKey;
}
}
Initialize Mirai SDK
To initialize the wallet SDK - specify the required networks and it will be initialize SDK for all networks.