Metamask: Setting Up WalletConnect with React Native App
In this article, we will look at how to set up a Metamask wallet in your React Native app using WalletConnect. We will focus on integrating it with 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 any wallet. It enables seamless interaction between dApps (decentralized applications) and wallet providers like Metamask in a secure and scalable way.
Prerequisites
To use WalletConnect with Metamask, you will need:
- Metamask wallet
- Expo or Xcode
- React Native CLI installed
Step 1: Configure WalletConnect
Create a new file called metamask.json
in your project directory:
{
"wallet": {
"id": "YOUR_METAMASK_ID",
"secretKey": "YOUR_METAMASK_SECRET_KEY"
}
}
Replace YOUR_METAMASK_ID
and YOUR_METAMASK_SECRET_KEY
with the actual ID and secret key of your Metamask wallet.
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…
- Select “React Native” and select “Application”
- Set the name of your app to “Metamask Wallet”
Step 3. Configure Metamask in your React Native Expo app
Open the metamask.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: WalletConnect Integration
In your React Native Expo app, add the following code to handle WalletConnect:
import { useWalletConnect } from '@metamask-connect/react-native';
const Application = () => {
const { account, connection, error } = useWalletConnect({
id: 'YOUR_METAMASK_ID',
secretKey: 'YOUR_METAMASK_SECRET_KEY'
});
if (error) {
console. error(error);
return null;
}
// Processing wallet changes
const onAccountChange = async () => {
console.log('Wallet account changed');
};
return (
{account && (
Connected to Metamask: {account}
)}
);
};
Step 5: Display the QRCode modal window (optional)
If you want to display a QRCode modal window when the app starts, add the following code:
import { useWalletConnect } from '@metamask-connect/react-native';
const Application = () => {
const { account, connection, error } = useWalletConnect({
id: 'YOUR_METAMASK_ID',
secretKey: 'YOUR_METAMASK_SECRET_KEY'
});
if (error) {
console. error(error);
return null;
}
// Show QRCode modal window when starting the application
const showModal = () => {
connection.request({ account });
};
return (
{account && (
Connected to Metamask: {account}
)}
connection.request({ account })) />
);
};
That’s it! You now have a Metamask wallet set up in your React Native Expo app using WalletConnect. When the app launches, a QRCode modal window will appear, allowing users to connect their wallets.