Web3Auth

Integrate Web3Auth into a dApp

Introduction

Web3Auth is a wallet infrastructure that is plugged into dApps or wallets. It serves as a pluggable authentication infrastructure for web3 wallets and applications. With Web3Auth's excellent user excellence, both mainstream and crypto natives may be onboarded in a matter of minutes.

As a wallet infrastructure, it provides out-of-the-box support for all social logins, web and mobile native platforms, wallets, and other key management methods. By the end of this guide, you will have integrated Web3Auth into your decentralized web application built on the Klaytn Network. To integrate Web3Auth into other platforms (Android, iOS, React Native, Flutter, & Unity), kindly refer to this guide.

Prerequisite

Installation

To make use of Web3Auth in your dApp, you must install the required libraries and SDK first. Hence, you'll need to set up ethers.js, and the Web3Auth Web SDK. You can use Web3Auth together with either ethers.js or web3.js libraries to communicate with the Klaytn blockchain. We'll be using ethers.js throughout this guide.

Initializing Web3Auth and Provider Instance

After successfully installing the needed libraries, next is to initialize the Web3Auth instance, set the Web3Auth provider instance in a useState() hook and also the init() function in a useEffect() hook.

Connecting Wallet

Inside your App function in your App.js file, call the connect() method on the web3Auth instance to initiate the connection of your wallet.

Setting up Utils function

In this guide, we will be making use of utils function: truncateAddress(). The truncateAddress() function takes in a valid address and returns a more readable format of the address passed in. The following steps below show how to set up and use the utils function in your project.

Step 1: Create a utils.js file in the src root folder.

Paste the following code in the newly created utils.js file.

Step 2: Import the function in your App.js file.

Getting Account and balance

Having connected your wallet successfully by calling the connect() method on the Web3Auth instance, you can get the user account and its balance by using the provider and signer object.

Disconnecting Wallet

Disconnecting from the wallet is achieved by using the logout() method on the Web3Auth instance. Also, one good practice is to refresh the state to clear any previously stored connection data.

Switching Chains

To switch chains using Web3Auth, you must firstly add the desired chain config to a connected adapter by calling the addChain() method, then proceed to calling the switchChain() method.

Getting User Info

A unique feature of Web3Auth is social logins. Once a user login using their social platforms, Web3Auth instance returns some information about the logged in user. Getting the logged in user information is as simple as calling the getUserInfo() method on the Web3Auth instance.

Signing Messages

Having initialised the provider and signer object, users can sign an arbitrary string.

Sending Native Transaction

You can perform native transactions, like sending KLAY from one user to another.

Working with a Smart Contract

  1. Deploying a Contract

You can deploy a smart contract given its Application Binary Interface(ABI) and its contract byte code.

With the Web3Auth provider and signer object, you can make contract interactions such as writing to and reading from a smart contract deployed on the blockchain.

  1. Writing to a Contract

  1. Reading from a Contract

TroubleShooting

Polyfill node core module error

This error occurs when you use webpack version 5. In this version, NodeJS polyfills is no longer supported by default. To solve this issue, refer to this guide.

Next Step

For more in-depth guides on Web3Auth, please refer to the Web3Auth Docs and Web3Auth Github repository. Also, you can find the full implementation of the code for this guide on GitHub.

Last updated