Initializing PnP React Native SDK
After Installation, the next step to use Web3Auth is to Initialize the SDK. The Initialization is a two-step process,
Please note that these are the most critical steps where you need to pass on different parameters according to the preference of your project. Additionally, Whitelabeling and Custom Authentication have to be configured within this step, if you wish to customize your Web3Auth Instance.
Importing Required packages
Importing Web3Auth
You may also import additional types from the SDK to help in the development process.
import Web3Auth, { WEB3AUTH_NETWORK, LOGIN_PROVIDER } from "@web3auth/react-native-sdk";
Importing a WebBrowser
implementation
- Expo Managed Workflow
- Bare React Native Workflow
import * as WebBrowser from "expo-web-browser";
import * as WebBrowser from "@toruslabs/react-native-web-browser";
Importing a Storage
implementation
- Expo Managed Workflow
- Bare React Native Workflow
import * as SecureStore from "expo-secure-store";
import EncryptedStorage from "react-native-encrypted-storage";
Instantiating Web3Auth
Now with react-native-sdk
v4 SecureStore
or EncryptedStorage
gets passed with the Web3Auth
constructor, depending on the workflow you are using. This is due to the addition of session
management without storing the private key in the device.
- Expo Managed Workflow
- Bare React Native Workflow
const web3auth = new Web3Auth(WebBrowser, SecureStore, SdkInitParams);
const web3auth = new Web3Auth(WebBrowser, EncryptedStorage, SdkInitParams);
SdkInitParams
object
The Web3Auth
constructor in the React Native SDK takes an SdkInitParams
object respectively as
an argument. The fields of such objects are listed below.
- Table
- Interface
Parameter | Description |
---|---|
clientId | Your Web3Auth Client ID. You can get it from Web3Auth Dashboard under project details. It's a mandatory field of type string |
network | Defines the Web3Auth network. It's a mandatory field of type WEB3AUTH_NETWORK_TYPE . |
redirectUrl | URL that Web3Auth will redirect API responses upon successful authentication from browser. It's a mandatory field of type string . |
privateKeyProvider | Private key provider for your chain namespace. |
accountAbstractionProvider? | Account abstraction provider for your preferred chain and provider. |
whiteLabel? | WhiteLabel options for web3auth. It helps you define custom UI, branding, and translations for your brand app. It takes WhiteLabelData as a value. |
loginConfig? | Login config for the custom verifiers. It takes LoginConfig as a value. |
useCoreKitKey? | Use CoreKit Key to get core kit key. It's an optional field with default value as false . |
mfaSettings? | Allows developers to configure the Mfa settings for authentication. It takes MfaSettings as a value. |
sessionTime? | It allows developers to configure the session management time. Session Time is in seconds, default is 86400 seconds which is 1 day. sessionTime can be max 7 days |
storageServerUrl? | Specify a custom storage url. Default value is https://broadcast-server.tor.us . |
storageKey? | Specify the storage key. Setting to "local" will persist social login session accross browser tabs. Available options are "local" and "session". |
export type SdkInitParams = {
/**
* You can get your clientId/projectId by registering your
* dapp on {@link "https://dashboard.web3auth.io"| developer dashboard}
*/
clientId: string;
network: WEB3AUTH_NETWORK_TYPE;
/**
* This parameter will be used to change the build environment of auth sdk.
* @defaultValue production
*/
buildEnv?: BUILD_ENV_TYPE;
/**
* redirectUrl is the dapp's url where user will be redirected after login.
*
* @remarks
* Register this url at {@link "https://dashboard.web3auth.io"| developer dashboard}
* else initialization will give error.
*/
redirectUrl: string;
/**
* loginConfig enables you to pass your own login verifiers configuration for various
* loginProviders.
*
* loginConfig is key value map where each key should be a valid loginProvider and value
* should be custom configuration for that loginProvider
*
* @remarks
* You can deploy your own verifiers from {@link "https://dashboard.web3auth.io"| developer dashboard}
* to use here.
*
*/
loginConfig?: LoginConfig;
/**
* options for whitelabling default auth modal.
*/
whiteLabel?: WhiteLabelData;
/**
* Specify a custom storage server url
* @defaultValue https://broadcast-server.tor.us
* @internal
*/
storageServerUrl?: string;
/**
* setting to "local" will persist social login session accross browser tabs.
*
* @defaultValue "local"
*/
storageKey?: "session" | "local";
/**
* How long should a login session last at a minimum in seconds
*
* @defaultValue 86400 seconds
* @remarks Max value of sessionTime can be 7 * 86400 (7 days)
*/
sessionTime?: number;
/**
* This parameter will be used to enable mfa factors and set priority on UI listing.
* List of factors available
* backUpShareFactor | socialFactor | passwordFactor
* @defaultValue false
*/
mfaSettings?: MfaSettings;
/**
* This parameter will be used to use auth mpc
* @defaultValue false
*/
enableLogging?: boolean;
useCoreKitKey?: boolean;
walletSdkURL?: string;
/**
* Private key provider for your chain namespace
*/
privateKeyProvider: IBaseProvider<string>;
/**
* Account abstraction provider for your chain namespace
*/
accountAbstractionProvider?: IBaseProvider<IProvider>;
};
Adding a Custom Chain Configuration
chainConfig
const chainConfig = {
chainId: "0x1", // Please use 0x1 for Mainnet
rpcTarget: "https://rpc.ankr.com/eth",
displayName: "Ethereum Mainnet",
blockExplorerUrl: "https://etherscan.io/",
ticker: "ETH",
tickerName: "Ethereum",
logo: "https://images.toruswallet.io/eth.svg",
};
- Table
- Type Declarations
Parameter | Description |
---|---|
chainNamespace | Namespace of the chain |
chainId | Chain ID of the chain |
rpcTarget | RPC target URL for the chain |
wsTarget? | Web socket target URL for the chain |
displayName? | Display name for the chain |
blockExplorerUrl? | URL of the block explorer |
ticker? | Default currency ticker of the network |
tickerName? | Name for currency ticker |
decimals? | Number of decimals for the currency |
logo? | Logo for the token |
isTestnet? | Whether the network is testnet or not |
export declare const CHAIN_NAMESPACES: {
readonly EIP155: "eip155";
readonly SOLANA: "solana";
readonly CASPER: "casper";
readonly XRPL: "xrpl";
readonly OTHER: "other";
};
export type ChainNamespaceType = (typeof CHAIN_NAMESPACES)[keyof typeof CHAIN_NAMESPACES];
export type CustomChainConfig = {
chainNamespace: ChainNamespaceType;
/**
* The chain id of the chain
*/
chainId: string;
/**
* RPC target Url for the chain
*/
rpcTarget: string;
/**
* web socket target Url for the chain
*/
wsTarget?: string;
/**
* Display Name for the chain
*/
displayName?: string;
/**
* Url of the block explorer
*/
blockExplorerUrl?: string;
/**
* Default currency ticker of the network (e.g: ETH)
*/
ticker?: string;
/**
* Name for currency ticker (e.g: `Ethereum`)
*/
tickerName?: string;
/**
* Number of decimals for the currency ticker (e.g: 18)
*/
decimals?: number;
/**
* Logo for the token
*/
logo?: string;
/**
* Whether the network is testnet or not
*/
isTestnet?: boolean;
};
It's mandatory to pass the logo
parameter when using WalletServices.
Get the Chain Config for your preferred Blockchain from the Connect Blockchain Reference.
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.
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.
- EIP1193 Private Key Provider for EVM Compatible Chains
- Solana Private Key Provider for Solana Blockchain
- XRPL Private Key Provider for XRPL Blockchain
- Common Private Key Provider for Connecting to any Blockchain
- ETH
- Solana
- XRPL
- Other Chains
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",
},
},
});
import { SolanaPrivateKeyProvider } from "@web3auth/solana-provider";
import { CHAIN_NAMESPACES, WEB3AUTH_NETWORK } from "@web3auth/base";
const privateKeyProvider = new SolanaPrivateKeyProvider({
config: {
/*
pass the chain config that you want to connect with.
all chainConfig fields are required.
*/
chainConfig: {
chainNamespace: CHAIN_NAMESPACES.SOLANA,
chainId: "0x1",
rpcTarget: "https://rpc.ankr.com/solana",
displayName: "Solana Mainnet",
blockExplorerUrl: "https://explorer.solana.com/",
ticker: "SOL",
tickerName: "Solana",
},
},
});
import { XrplPrivateKeyProvider } from "@web3auth/xrpl-provider";
import { CHAIN_NAMESPACES, WEB3AUTH_NETWORK } from "@web3auth/base";
const privateKeyProvider = new XrplPrivateKeyProvider({
config: {
/*
pass the chain config that you want to connect with.
all chainConfig fields are required.
*/
chainConfig: {
chainNamespace: CHAIN_NAMESPACES.OTHER,
chainId: "0x1",
rpcTarget: "https://s.altnet.rippletest.net:51234",
displayName: "XRPL",
blockExplorerUrl: "https://testnet.xrpl.org",
ticker: "XRP",
tickerName: "XRP",
},
},
});
import { CommonPrivateKeyProvider } from "@web3auth/base-provider";
import { CHAIN_NAMESPACES, WEB3AUTH_NETWORK } from "@web3auth/base";
const privateKeyProvider = new CommonPrivateKeyProvider({
config: {
/*
pass the chain config that you want to connect with.
all chainConfig fields are required.
*/
chainConfig: {
chainNamespace: CHAIN_NAMESPACES.OTHER,
chainId: "0x1",
rpcTarget: `https://rpc.target.url`,
displayName: "Display Name",
blockExplorerUrl: "https://chain.explorer.link",
ticker: "TKR",
tickerName: "Ticker Name",
},
},
});
Usage
- Bare General
- Expo General
- Bare Custom Auth
import * as WebBrowser from "@toruslabs/react-native-web-browser";
import EncryptedStorage from "react-native-encrypted-storage";
import Web3Auth, {
LOGIN_PROVIDER,
WEB3AUTH_NETWORK,
ChainNamespace,
} from "@web3auth/react-native-sdk";
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";
const scheme = "web3authrnexample"; // Or your desired app redirection scheme
const redirectUrl = `${scheme}://auth`;
const clientId =
"BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ"; // get from https://dashboard.web3auth.io
const chainConfig = {
chainNamespace: ChainNamespace.EIP155,
chainId: "0xaa36a7",
rpcTarget: "https://rpc.ankr.com/eth_sepolia",
// Avoid using public rpcTarget in production.
// Use services like Infura, Quicknode etc
displayName: "Ethereum Sepolia Testnet",
blockExplorerUrl: "https://sepolia.etherscan.io",
ticker: "ETH",
tickerName: "Ethereum",
decimals: 18,
logo: "https://cryptologos.cc/logos/ethereum-eth-logo.png",
};
const privateKeyProvider = new EthereumPrivateKeyProvider({
config: {
chainConfig,
},
});
const web3auth = new Web3Auth(WebBrowser, EncryptedStorage, {
clientId,
redirectUrl,
network: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET, // or other networks
privateKeyProvider,
});
import * as WebBrowser from "expo-web-browser";
import * as SecureStore from "expo-secure-store";
import Web3Auth, {
WEB3AUTH_NETWORK,
LOGIN_PROVIDER,
ChainNamespace,
} from "@web3auth/react-native-sdk";
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";
const redirectUrl =
Constants.appOwnership == AppOwnership.Expo || Constants.appOwnership == AppOwnership.Guest
? Linking.createURL("web3auth", {})
: Linking.createURL("web3auth", { scheme: "web3authexpoexample" });
const clientId = "YOUR WEB3AUTH CLIENT ID";
const chainConfig = {
chainNamespace: ChainNamespace.EIP155,
chainId: "0xaa36a7",
rpcTarget: "https://rpc.ankr.com/eth_sepolia",
// Avoid using public rpcTarget in production.
// Use services like Infura, Quicknode etc
displayName: "Ethereum Sepolia Testnet",
blockExplorerUrl: "https://sepolia.etherscan.io",
ticker: "ETH",
tickerName: "Ethereum",
decimals: 18,
logo: "https://cryptologos.cc/logos/ethereum-eth-logo.png",
};
const privateKeyProvider = new EthereumPrivateKeyProvider({
config: {
chainConfig,
},
});
const web3auth = new Web3Auth(WebBrowser, SecureStore, {
clientId,
redirectUrl,
network: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET, // or other networks
privateKeyProvider,
});
import * as WebBrowser from "@toruslabs/react-native-web-browser";
import EncryptedStorage from "react-native-encrypted-storage";
import Web3Auth, {
LOGIN_PROVIDER,
WEB3AUTH_NETWORK,
ChainNamespace,
} from "@web3auth/react-native-sdk";
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";
const scheme = "web3authrnbareauth0example"; // Or your desired app redirection scheme
const redirectUrl = `${scheme}://auth`;
const clientId =
"BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ";
const chainConfig = {
chainNamespace: ChainNamespace.EIP155,
chainId: "0xaa36a7",
rpcTarget: "https://rpc.ankr.com/eth_sepolia",
// Avoid using public rpcTarget in production.
// Use services like Infura, Quicknode etc
displayName: "Ethereum Sepolia Testnet",
blockExplorerUrl: "https://sepolia.etherscan.io",
ticker: "ETH",
tickerName: "Ethereum",
decimals: 18,
logo: "https://cryptologos.cc/logos/ethereum-eth-logo.png",
};
const privateKeyProvider = new EthereumPrivateKeyProvider({
config: {
chainConfig,
},
});
const web3auth = new Web3Auth(WebBrowser, EncryptedStorage, {
clientId,
network: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET, // or other networks
redirectUrl,
useCoreKitKey: true,
privateKeyProvider,
loginConfig: {
jwt: {
verifier: "w3a-auth0-demo",
typeOfLogin: "jwt",
clientId: "hUVVf4SEsZT7syOiL0gLU9hFEtm2gQ6O",
},
},
});