Application Setup
Check whether Metamask is installed(OPTIONAL)
async function isMetamaskInstalled() {
// Check if the `window.ethereum` property exists on the window object
return typeof window.ethereum !== 'undefined' && window.ethereum.isMetaMask;
}Add the Connect to Identify Snap button on your application
// Get permissions to interact with and install the Identify Snap
async function connect() {
console.log("snap id", snapId);
const isMetamaskDetected = await isMetamaskInstalled();
if (!isMetamaskDetected ) {
console.error(
"You need to install Metamask for this demo to work. You can install it at https://metamask.io"
);
alert(
"You need to install Metamask for this demo to work. You can install it at https://metamask.io"
);
}
let snaps = await window.ethereum.request({
method: "wallet_getSnaps"
});
console.log("Installed snaps...", snaps);
try {
if (!(snapId in snaps)) {
console.log("Identify Snap is not yet installed. Installing now...");
const result = await ethereum.request({
method: "wallet_requestSnaps",
params: {
[snapId]: {}
}
});
console.log("result: ", result);
snaps = await window.ethereum.request({
method: "wallet_getSnaps"
});
}
} catch (e) {
console.log(
`Failed to obtain installed snap: ${JSON.stringify(e, null, 4)}`
);
alert(`Failed to obtain installed snap: ${JSON.stringify(e, null, 4)}`);
}
if (snapId in snaps) {
console.log("Connected successfully!");
alert("Connected successfully!");
} else {
console.log("Could not connect successfully. Please try again!");
alert("Could not connect successfully. Please try again!");
}
}Last updated
Was this helpful?

