LogoLogo
  • Basics
    • Introduction
    • Roadmap
    • FAQs
  • Hedera Wallet Snap
    • Snap Architecture
    • Snap Account
    • Snap RPC APIs
      • Basic APIs
        • hello
      • Snap State APIs
        • getCurrentAccount
        • showAccountPrivateKey
      • Account APIs
        • getAccountInfo
        • getAccountBalance
        • stakeHbar
        • unstakeHbar
        • approveAllowance
        • deleteAllowance
        • deleteAccount
      • Transactions APIs
        • transferCrypto
        • getTransactions
      • Hedera Token Service APIs
        • hts/createToken
        • hts/mintToken
        • hts/burnToken
        • hts/pauseToken
        • hts/unpauseToken
        • hts/associateTokens
        • hts/dissociateTokens
        • hts/freezeAccount
        • hts/unfreezeAccount
        • hts/enableKYCFlag
        • hts/disableKYCFlag
        • hts/wipeToken
        • hts/deleteToken
        • hts/updateToken
        • hts/updateTokenFeeSchedule
        • hts/initiateSwap
        • hts/completeSwap
      • Hedera Smart Contract Service APIs
        • hscs/createSmartContract
        • hscs/updateSmartContract
        • hscs/deleteSmartContract
        • hscs/callSmartContractFunction
        • hscs/getSmartContractFunction
        • hscs/getSmartContractBytecode
        • hscs/getSmartContractInfo
      • Hedera Consensus Service APIs
        • hcs/createTopic
        • hcs/updateTopic
        • hcs/submitMessage
        • hcs/getTopicInfo
        • hcs/getTopicMessages
        • hcs/deleteTopic
      • Miscellaneous APIs
        • signMessage
    • Snap Permissions
  • Getting Started
    • Introduction
    • Application Setup
    • Hello World
Powered by GitBook
On this page
  • Check whether Metamask is installed(OPTIONAL)
  • Add the Connect to Hedera Wallet Snap button on your application

Was this helpful?

Export as PDF
  1. Getting Started

Application Setup

PreviousIntroductionNextHello World

Last updated 1 year ago

Was this helpful?

Check whether Metamask is installed(OPTIONAL)

It's optional but recommended to verify whether MetaMask is installed by the users because Snaps are only available within Metamask. The choice is up to developers on how they want to handle this aspect of the integration.

async function isMetamaskInstalled() {
  // Check if the `window.ethereum` property exists on the window object
  return typeof window.ethereum !== 'undefined' && window.ethereum.isMetaMask;
}

Check out to learn about how to interact with the Hedera Wallet Snap from your website.

Add the Connect to Hedera Wallet Snap button on your application

Note that before users can interact with the Hedera Wallet Snap, it needs to first be installed on the Metamask itself so you can have a button on your website that lets users do exactly this.

// Get permissions to interact with and install the Hedera Wallet 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("Hedera Wallet 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!");
  }
}
Some things to keep in mind while interacting with the above demo

If you're getting any errors with the live demo, make sure you go through the section to learn about what you may be missing. You need to install in your browser for the live demo to work

Snap RPC APIs
FAQs
Metamask