Programmatically Generating Metamask Mobile Deep Links
As a developer, you are probably familiar with the popular MetaMask wallet and its ability to create deep links in your dApps. However, sometimes you may want to generate these links programmatically, rather than relying on manual input from users. In this article, we will explore how to do this programmatically.
Overview
To programmatically generate a Metamask mobile deep link, you can use the “web3” library to create a wallet URL and then replace the placeholders in the resulting string with your own values.
Step 1. Create a new wallet URL
First, create a new wallet URL using the MetaMask Web3 API. You can do this by running the following JavaScript code:
const web3 = require("web3");
const WalletUrlProvider = require("metamask-deeplinks-provider");
// Replace with your MetaMask provider information
const providerAddress = "0xYourWalletAddress";
const providerSecret = "YourWalletSecret";
const walletUrlProvider = new WalletUrlProvider({
provider: {
address: provider address,
secret: providerSecret,
},
});
// Generate a random token for the dApp
const tokenId = Math.floor(Math.random() * 1000).toString();
// Create a new wallet URL with placeholders for your values
const walletUrl =
2nd step. Replacing placeholders in the wallet URL
Now that you have generated the wallet URL, you need to replace all placeholders (e.g. {walletUrl}) with your values. You can do this by iterating over each placeholder and replacing it with a value.
Below is an example of replacing placeholders.
const walletUrl =
// Replace ${walletUrlProvider.getWalletUrl()} with your provider address
const walletUrl = walletUrl.replace("${walletUrlProvider.getWalletUrl()}",
${providerAddress}
);// Replace ${tokenId} with your dApp ID (e.g. "tipme.cash")
const tokenId = "type.cash";
const walletUrl = walletUrl.replace("${tokenId}", token ID);
3. Step 2: Get the URL
After replacing all the containers, you need to retrieve the resulting URL using the MetaMask Web3 API. This can be done by calling the getWalletURL method on the WalletUrlProvider instance:
const walletUrlProvider = new WalletUrlProvider({
provider: {
address: provider address,
secret: providerSecret,
},
});
// Get the resulting URL using WalletUrlProvider.getWalletURL()
const walletUrl = walletUrlProvider.getWalletURL();
Putting it all together
Here is an example of how it all can be put together:
``javascript
const web3 = require("web3");
const WalletUrlProvider = require("metamask-deeplinks-provider");
// Replace with your MetaMask provider information
const providerAddress = "0xYourWalletAddress";
const providerSecret = "YourWalletSecret";
const walletUrlProvider = new WalletUrlProvider({
provider: {
address: provider address,
secret: providerSecret,
},
});
// Generate a random token for the dApp
const tokenId = Math.floor(Math.random() * 1000).toString();
// Create a new wallet URL with placeholders for your values
const walletUrl =
// Replace ${walletUrlProvider.getWalletUrl()} with your provider address
const walletUrl = walletUrl.replace("${walletUrlProvider.getWalletUrl()}", ${providerAddress}
);
// Replace ${tokenId} with your dApp ID (e.g. "tipme.cash")
const tokenId = "type.cash";
const walletUrl = walletUrl.replace("${tokenId}", token ID);
// Get the resulting URL using WalletUrlProvider.getWalletURL()
const walletUrl = walletUrlProvider.