Metamask Authentication in React (Vite) Browser Extension: A Step-by-Step Guide
As a developer building web extensions with React and Vite, you are probably familiar with the importance of access control. One key aspect is authenticating your extension with MetaMask, a popular cryptocurrency wallet. However, when using Vite, you run into a problem due to the lack of window.ethereum
. In this article, we will walk you through setting up Metamask authentication in your React (Vite) browser extension.
Why can’t I use window.ethereum
?
window.ethereum
is a built-in object that provides access to Ethereum’s Web3 library. When using Vite and React, the framework does not allow direct access to this object due to security restrictions. The problem arises because Vite’s built-in Web3 support does not cover browser extensions.
Recommended solution: Metamask Extension-Provider
To solve this problem, you can use MetaMask Extension-Provider, a popular solution for authenticating your web extension with MetaMask. This library allows you to link your extension without the need for window.ethereum
.
Step 1: Install the MetaMask Extension-Provider
You will need to install the MetaMask Extension-Provider using npm or yarn:
npm install metamask-extension-provider
Or, if using yarn:
yarn add metamask-extension-provider
Step 2: Configure the Metamask Extension-Provider in your React (Vite) extension
Create a new file called metamask.js
in the src
directory of your extension. This file will contain code that establishes a connection to MetaMask.
import {connect} from 'react-vite';
import metamaskExtensionProvider from 'metamask-extension-provider';
const metamaskProvider = {
contracts: [
// List of contract addresses you want to access
{
name: 'Contract1',
address: '0x...ContractAddress...',
},
],
};
connect ({
provider: metamaskExtensionProvider,
id: 'your-extension-id',
contracts: metamaskProvider,
})
Replace Contract1
with the actual addresses of your contract, and 0x...ContractAddress...
with the address of your contract.
Step 3: Register your extension
To register your extension, you need to generate a unique ID for it. This ID will be used as an identifier in the MetaMask Extension-Provider configuration:
const extensionId = 'your-extension-id';
Update your metamask.js
file with the following code:
import {connect} from 'react-vite';
import metamaskExtensionProvider from 'metamask-extension-provider';
const metamaskProvider = {
contracts: [
// List of contract addresses you want to access
{
name: 'Contract1',
address: '0x...Contract address...',
},
],
};
connect ({
provider: metamaskExtensionProvider,
id: extensionId,
contracts: metamaskProvider,
})
Step 4: Connect your extension
You can now connect your extension to the MetaMask Extension-Provider. Update your manifest.json
file with the following code:
{
"manifest_version": 2,
"name": "The name of your extension",
"version": "1.0.0",
"description": "A short description of your extension",
"icons": {
"16": "path/to/icons16.png",
"48": "path/to/icons48.png",
},
"background": {
"scripts": ["metamask.js"],
},
}
Replace icon16.png
and icon48.png
with your extension icons.
Step 5: Add your extension to MetaMask
Finally, add your extension to the MetaMask Extension-Provider:
// In metamask-extension-provider/
export const provider = {
id: 'your-extension-id',
};
Start Vite and create a new browser extension using vite create
.
congratulations!