Site icon LD Talent Blog

How to create a persistent connect wallet button using Cardano Mesh SDK

How to create a persistent connect wallet button using Cardano Mesh SDK

Hire the author: Adetayo L

Introduction

Since the public availability of Web3, developers actively create software to facilitate transactions and identification in various scenarios. Wallets, like the Cardano Mesh connect wallet button, enable users to connect to decentralized applications (dApps).

Furthermore, Cardano, a blockchain technology, is gaining popularity due to its versatility and numerous use cases. Mesh, an open-source library that provides tools for building dApps on the Cardano blockchain, is one such tool. Additionally, it includes a ‘Cardano wallet’ button that allows seamless connection to users’ Cardano wallets. However, this issue can be frustrating for users who need to reconnect each time the page reloads.

To address this issue, developers seeking to integrate Cardano wallet functionality into their React app can create a persistent wallet button that remains connected across all pages and after users reload. This tutorial demonstrates the process using React.

Prerequisite

It is mandatory to have a Cardano wallet before continuing, To create a wallet you can follow these steps here.

Glossary

React: According to Wikipedia, “React is a free and open-source front-end JavaScript library for building user interfaces based on reusable UI components. It’s used for handling the view layer for web and mobile apps. The main purpose of React is to be fast, scalable, and simple.”

Mesh: Mesh is an open-source library providing numerous tools to easily build powerful dApps on the Cardano blockchain.

Cardano: According to Wikipedia, “Cardano is a public blockchain platform. It is open-source and decentralized, with consensus achieved using proof of stake. It can facilitate peer-to-peer transactions with its internal cryptocurrency, ADA”.

Step-by-step Procedure

Step 1: Setting up the project

To begin, we first need to set up the project. We’re going to be using Next.js to do this. To create a Next.js project, run:

npx create-next-app persistent-cardano-button

After creating the project, navigate to the project folder and run:

cd persistent-cardano-button

// to run the project

npm run dev

When you run the project you should come to the page below:

Let’s remove the placeholder content provided by Next.js since we won’t be doing much styling. This will leave us with a blank page. Now, it’s time to add a simple background colour to our body.

body {
   background: rgb(86, 154, 255);
}

Step 2: Setting up Mesh

To utilize the Mesh library, let’s install it into our project by running the following commands in our console.

npm install @meshsdk/core @meshsdk/react


After the installation is complete, let’s configure our project by adding webpack to the next.config.js file. Now, open the next.config.js file and ensure that it reflects the following configuration:

next.config.js

Next, add the MeshProvider to the _app.js file to ensure that the wallet is available to all pages in the project:

./pages/_app.js

Now, we are good to go.

Step 3: Testing Cardano Component


Import the Cardano wallet button provided by Mesh and observe its functionality as the first step. In the index.js file, add the button by following these instructions:

./pages/index.js

Our result will be:

We can see the four different states for users where they:

Step 4: Creating the new ConnectWallet Button

After reloading the page, we notice that the button automatically disconnects. Now, let’s take the initiative and create our custom button. To do this, we’ll create a ConnectWallet component and copy the provided code.

./components/ConnectWallet/ConnectWallet.js

Here’s a breakdown of what is happening

Step 5: Styling the component

With these steps, we have everything required to obtain a functional ConnectWallet Button. Now, let’s take an active role and style our component to achieve a similar appearance to the button component provided by Mesh. Feel free to style it according to your design system.

styles/global.css

Now, let’s import the connect wallet button to the home page and test it out.

Learning Tools

Some resources that helped me with the project:

Learning Strategy

By actively engaging with the Cardano community, including Gimbalabs, and seeking their valuable insights and suggestions, I devised a thoughtful solution. Additionally, I believe in the importance of asking questions, as diverse perspectives and ideas from various sources can lead to innovative and effective solutions.

Reflective Analysis

If you’re looking to enhance the security of your wallet button and safeguard your users’ valuable data, one possible solution is to explore the use of session storage to store sensitive information securely. Moreover, this approach can provide an additional layer of protection for your users. Additionally, it can be easily implemented through a variety of state management tools and techniques.

By utilizing session storage, you can ensure the safety and security of your users’ data while delivering a streamlined user experience. If you aim to strengthen the security of your wallet button and safeguard your users’ valuable data, implementing session storage is an excellent option to consider.

Conclusions and Future Directions

Despite the lack of tutorials or articles as a reference, I chose this topic out of a desire to solve a pressing problem. Through insightful discussions with friends and fellow developers and a series of experiments, I arrived at this solution.

For future reference, it is crucial for developers to prioritize security measures when implementing this solution, in order to safeguard users’ information. I appreciate you taking the time to read this blog post. It is my sincere hope that the information provided here has been truly beneficial and informative to you in your specific use case.

It is always a great pleasure to be able to assist others in their pursuit of knowledge and understanding, and I am truly grateful for the opportunity to do so. If you have any other questions or concerns, please feel free to contact me for assistance.

Check out the GitHub repository for this project.

Additionally, you can explore some of our other React articles here.

Hire the author: Adetayo L

Exit mobile version