Metamask: WalletConnect Setup with React Native App
In this article, we will explore how to set up a Metamask wallet in your React Native app using WalletConnect. We will focus on integrating it into your Expo or Xcode mobile app.
What is WalletConnect?
WalletConnect is an open-source protocol that allows users to send and receive cryptocurrencies without having to download wallets. It enables seamless interactions between dApps (decentralized applications) and wallet providers, such as Metamask, in a secure and scalable way.
Prerequisites
To use WalletConnect with Metamask, you will need:
- A Metamask wallet
- Expo or Xcode
- React Native CLI installed
Step 1: Configure WalletConnect configuration
Create a new file called metamask.json
in your project directory:
secretKey": "YOUR_METAMASK_SECRET_KEY"
{
wallet': {
"id": "METAMASK_ID",
}
}
Replace YOUR_METAMASK_ID
and YOUR_METAMASK_SECRET_KEY
with your actual Metamask wallet ID and secret key.
Step 2: Create a new React Native Expo project or Xcode app
If you’re using Expo, run:
expo init metamask-wallet
Otherwise, create an Xcode project (or use the iOS equivalent) with the following settings:
- File -> New -> Project...
- Choose "React Native" and select "App"
- Set your app name to "Metamask Wallet".
Step 3: Configure Metamask in your React Native Expo app
Open themetamask.json` file in your project directory and add the following code:
import { initialize } from 'metamask-wallet';
initialize({
walletId: "YOUR_METAMASK_ID",
secretKey: "YOUR_METAMASK_SECRET_KEY"
});
Step 4: Integrate WalletConnect
In your React Native Expo app, add the following code to handle WalletConnect:
import { useWalletConnect } from "@metamask-connect/react-native";
const App = () => {
const { account, connection, error } = useWalletConnect({
id: "YOUR_METAMASK_ID",
secretKey: "YOUR_METAMASK_SECRET_KEY"
});
if (error) {
console.error(error);
return null;
}
// Handle wallet changes
const onAccountChange = async () => {
console.log('Wallet account changed');
};
return (
{account && (
Connected to Metamask: {account}
)}
);
};
Step 5: Show the QRCode modal (optional)
If you want to show a QRCode modal when the app launches, add the following code:
import { useWalletConnect } from "@metamask-connect/react-native";
const App = () => {
const { account, connection, error } = useWalletConnect({
id: "YOUR_METAMASK_ID",
secretKey: "YOUR_METAMASK_SECRET_KEY"
});
if (error) {
console.error(error);
return null;
}
// Show the modal QRCode when the app is launched
const showModal = () => {
connection.request({ account });
};
return (
{account && (
Connected to Metamask: {account}
)}
connection.request({ account })) />
);
};
That’s it! You now have your Metamask wallet configured in your React Native Expo app using WalletConnect. When your app launches, the QRCode module will appear, allowing users to connect their wallet.