Connect your Blockchain
Web3Auth is the frontend authentication system for your dApp. Once the user is authenticated, the Web3Auth SDK returns a provider. Using this provider we can sign transactions and make RPC calls to any blockchain.
- EVM
- Solana
- XRPL
- Other Chains
import { Web3Auth } from "@web3auth/modal";
import Web3 from "web3";
const web3auth = new Web3Auth({
clientId, // get from https://dashboard.web3auth.io
web3AuthNetwork: "sapphire_mainnet",
chainConfig: {
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0x1", // EVM chain's Chain ID
rpcTarget: "https://rpc.ankr.com/eth", // EVM chain's RPC endpoint
// Avoid using public rpcTarget in production.
// Use services like Infura, Quicknode, Alchemy, Ankr etc.
displayName: "Ethereum Mainnet", // EVM chain's Name
blockExplorer: "https://etherscan.io/", // EVM chain's Blockexplorer
ticker: "ETH", // EVM chain's Ticker
tickerName: "Ethereum", // EVM chain's Ticker Name
},
});
await web3auth.initModal();
const web3authProvider = await web3auth.connect();
const web3 = new Web3(web3authProvider);
const userAccounts = await web3.eth.getAccounts();
console.log(userAccounts);
import { Web3Auth } from "@web3auth/modal";
import { SolanaWallet } from "@web3auth/solana-provider";
const web3auth = new Web3Auth({
clientId, // get from https://dashboard.web3auth.io
web3AuthNetwork: "sapphire_mainnet",
chainConfig: {
chainNamespace: CHAIN_NAMESPACES.SOLANA,
chainId: "0x1", // Please use 0x1 for Mainnet, 0x2 for Testnet, 0x3 for Devnet
rpcTarget: "https://rpc.ankr.com/solana",
displayName: "Solana Mainnet",
blockExplorer: "https://explorer.solana.com",
ticker: "SOL",
tickerName: "Solana",
},
});
await web3auth.initModal();
const web3authProvider = await web3auth.connect();
const solanaWallet = new SolanaWallet(web3authProvider);
const userAccounts = await solanaWallet.requestAccounts();
console.log(userAccounts);
import { Web3Auth } from "@web3auth/modal";
import { CHAIN_NAMESPACES } from "@web3auth/base";
import { OpenloginAdapter } from "@web3auth/openlogin-adapter";
import { XrplPrivateKeyProvider, getXRPLChainConfig } from "@web3auth/xrpl-provider";
const web3auth = new Web3Auth({
chainConfig: {
chainNamespace: CHAIN_NAMESPACES.OTHER,
},
clientId = "YOUR_WEB3AUTH_CLIENT_ID", // get from https://dashboard.web3auth.io
web3AuthNetwork = "sapphire_mainnet", // testnet, mainnet, cyan, aqua
});
const xrplProvider = new XrplPrivateKeyProvider({
config: {
chainId: "chainId",
chainConfig: getXRPLChainConfig("testnet"), // devnet, testnet, mainnet
rpcTarget: "rpc url",
},
});
const adapter = new OpenloginAdapter({
privateKeyProvider: xrplProvider, // <-- Injecting the XRPL provider
});
web3AuthInstance.configureAdapter(adapter);
await web3auth.initModal();
const provider = await web3auth.connect();
import { Web3Auth } from "@web3auth/modal";
import { OpenloginAdapter } from "@web3auth/openlogin-adapter";
const web3auth = new Web3Auth({
clientId, // get from https://dashboard.web3auth.io
web3AuthNetwork: "sapphire_mainnet",
chainConfig: {
/*
you can pass your own chain configs here
*/
chainId: "chain id",
rpcTarget: "rpc url",
chainNamespace: CHAIN_NAMESPACES.OTHER,
displayName: "Chain Name",
ticker: "TKR",
tickerName: "Ticker Name",
}
});
const openloginAdapter = new OpenloginAdapter({
adapterSettings: {
uxMode: "popup",
},
});
web3auth.configureAdapter(openloginAdapter);
await web3auth.initModal();
const web3authProvider = await web3auth.connect();
async getPrivateKey() {
const privateKey = await web3authProvider.request({
method: "private_key"
});
//Fetch Address with privateKey
}
Using Web3Auth Providers
A provider is how libraries like web3.js & ethers.js talk to the blockchain. Providers take JSON-RPC requests and return the response. This is normally done by submitting the request to an HTTP or IPC socket-based server. Each adapter in Web3Auth exposes a Provider on successful user authentication. This provider can be used to interact with wallet or connected chain using RPC calls. Currently, Web3Auth supports providers for both EVM and Solana chains, which means that any EVM compatible chain & Solana can directly use our native providers to interact with the dApp. You can follow our API Reference to know more about how to use these providers with their compatible adapters:
Adding JSON RPC APIs
Web3Auth providers give you a standard way of interacting with the blockchain. However, alongside that, it is recommended to use JSON RPC APIs, which help you connect to the blockchain without the need to run your own instance/ nodes. There are many services which offer a web API for accessing different blockchains:
Infura
Infura is the leading platform for Ethereum infrastructure. It provides a gateway to the Ethereum network, allowing developers to build and scale decentralized applications without having to run their own infrastructure. Most of the Web3Auth backend infrastructure runs on Infura APIs.
Quicknode
Quicknode is a managed blockchain node service that provides high-performance access to 15+ blockchains, including Ethereum, Gnosis (xDAI), Polygon, Binance Smart Chain, Avalanche, Fantom, Solana, Optimism, Arbitrum (+Nova), Algorand, Harmony, Celo, Terra and Bitcoin networks. They're the biggest node providers for the Solana Ecosystem, and our major partners for Solana infrastructure.
Alchemy
As a developer platform, Alchemy provides a suite of developer toolings and abstractions including JSON RPC APIs across all major chains, an ethers.js SDK, and a library of enhanced APIs like their NFT APIs.
Ankr
Ankr is a decentralized cloud computing platform that provides a full suite of infrastructure services for blockchain developers. Most of the examples in our documentation use Ankr's Public JSON RPC APIs, to help you kickstart.
Web3Auth has multichain support
So, we have covered our existing wallet adapters and providers available. What if you want to use a different chain or a wallet?
Here the ability to expose the user's private key comes in handy. Every SDK of Web3Auth has the functionality of exposing the user's private key, within the dApp instance locally. This private key can be utilised by the dApp to interact with the chain.
The small API surface also means Web3Auth is composable - you can combine it with meta-transaction flows, multisigs, and other cryptographic protocols. It is easy to build on top of, and generally fits in with most other technology stacks, including but not limited to; your favorite scalability solutions, meta transactions, smart contract wallets, different elliptic curve pairs and even RSA.
Example below demonstrates usage of other
chainNamespace and retrieving private key of logged in user.
const web3auth = new Web3Auth({
chainConfig: { chainNamespace: CHAIN_NAMESPACES.OTHER },
web3AuthNetwork: "sapphire_mainnet",
clientId: "example-client-id",
});
//Assuming user is already logged in.
async getPrivateKey() {
const privateKey = await web3auth.provider.request({
method: "private_key"
});
//Do something with privateKey
}
You can read more about this in our Provider SDK Reference
Currently only OpenloginAdapter
is supported with other
chainNamespace.