Skip to main content

Initializing Core Kit SFA Node SDK

After Installation, the next step to use Web3Auth Node.js SDK is to Initialize the SDK.

However, the Initialization is a two-step process, ie.

Instantiating Web3Auth

Import the Web3Auth class from @web3auth/node-sdk

const { Web3Auth } = require("@web3auth/node-sdk");

Assign the Web3Auth class to a variable

const web3auth = new Web3Auth(Web3AuthOptions);

This Web3Auth constructor takes an object with Web3AuthOptions as input.

Arguments

Web3AuthOptions

ParameterDescription
clientIdWeb3Auth Client ID. Obtain your clientId from the Web3Auth Developer Dashboard. It's a mandatory parameter as a string.
web3AuthNetwork?Web3Auth Networks. Choose between sapphire_mainnet or sapphire_testnet. It's an optional parameter.
enableLogging?Setting it to true will enable logs. It's an optional parameter as a boolean.
usePnPKey?Setting this to true returns the same key as web sdk (i.e., plug n play key), By default, this sdk returns CoreKitKey. It's an optional parameter as a boolean.

Usage Example

const web3auth = new Web3Auth({
clientId: "WEB3AUTH_CLIENT_ID", // Get your Client ID from the Web3Auth Dashboard
web3AuthNetwork: "sapphire_mainnet",
usePnPKey: false, // By default, this SDK returns CoreKitKey
});

Instantiating a Provider

privateKeyProvider

privateKeyProvider parameter helps you to connect with various wallet SDKs. These are preconfigured RPC clients for different blockchains used to interact with the respective blockchain networks.

note

It's mandatory to provide privateKeyProvider for your corresponding chain namespace. To know more in-depth about providers, have a look at the Providers reference.

You can choose between the following providers based on your usecase.

Usage
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";
import { CHAIN_NAMESPACES, WEB3AUTH_NETWORK } from "@web3auth/base";

const privateKeyProvider = new EthereumPrivateKeyProvider({
config: {
/*
pass the chain config that you want to connect with.
all chainConfig fields are required.
*/
chainConfig: {
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0x1",
rpcTarget: "https://rpc.ankr.com/eth",
displayName: "Ethereum Mainnet",
blockExplorerUrl: "https://etherscan.io",
ticker: "ETH",
tickerName: "Ethereum",
},
},
});

Initializing Web3Auth

init()

To complete the initialization process, we need to initialize the Web3Auth instance, which we named web3auth. This is achieved by calling the init() function of the previously created web3auth instance, using a private key provider.

await web3auth.init({ provider: privateKeyProvider });

Example

const Web3Auth = require("@web3auth/node-sdk");
const EthereumPrivateKeyProvider = require("@web3auth/ethereum-provider");
const CHAIN_NAMESPACES = require("@web3auth/base");

// Swap with a different privateKeyProvider if you want to use a different blockchain
const privateKeyProvider = new EthereumPrivateKeyProvider({
config: {
chainConfig: {
chainId: "0x1",
rpcTarget: "https://rpc.ankr.com/eth",
displayName: "Ethereum Mainnet",
blockExplorer: "https://etherscan.io",
ticker: "ETH",
tickerName: "Ethereum",
},
},
});

const web3auth = new Web3Auth({
clientId: "WEB3AUTH_CLIENT_ID", // Get your Client ID from the Web3Auth Dashboard
web3AuthNetwork: "sapphire_mainnet",
usePnPKey: false, // By default, this sdk returns CoreKitKey by default.
});

web3auth.init({ provider: privateKeyProvider });
Note

One needs to re-initialize the SDK for each new user.