Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using MetaMask #32

Closed
Tracked by #13
manuelbarbas opened this issue Nov 24, 2023 · 1 comment
Closed
Tracked by #13

Using MetaMask #32

manuelbarbas opened this issue Nov 24, 2023 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@manuelbarbas
Copy link
Collaborator

manuelbarbas commented Nov 24, 2023

Metamask

Metamask is a Chromium (Chrome, Brave, and Edge) and Firefox browser add-on that provides a non-custodial Ethereum wallet. An end-user's private key is stored in an encrypted Vault data stored in the browser.

It can also connect to hardware based wallets such as Ledger.

https://medium.com/mycrypto/how-to-ensure-youre-running-the-legitimate-version-of-metamask-5fcd8ab32b96[Please be sure you and your end-users are running a legitimate version of Metamask!]

Metamask allows you to integrate your dApp with SKALE by setting the Network RPC endpoint for your users. For more information and support, see https://consensys.net/blog/metamask/connect-users-to-layer-2-networks-with-the-metamask-custom-networks-api

Install the NPM Packages

npm install --save web3

Example Code

import Web3 from 'web3';

let web3 = "";

let switchToSKALE = {
  chainId: "0x50877ed6", //decodes to 1351057110
  chainName: "SKALEChaos Testnet",
  rpcUrls: ["https://staging-v3.skalenodes.com/v1/staging-fast-active-bellatrix"],
  nativeCurrency: {
    name: "SKALE FUEL",
    symbol: "sFUEL",
    decimals: 18
  },
  blockExplorerUrls: [
    "https://staging-fast-active-bellatrix.explorer.staging-v3.skalenodes.com"
  ]
};

web3 = window.web3;

  if (window.ethereum) {
    window.web3 = new Web3(window.ethereum);
    try {
      // Request account access if needed
      await window.ethereum.enable();
      console.log("MetaMask - Web3 Initialized!");

      //Get user wallet accounts
      window.web3.eth.getAccounts((error, accounts) => {
        if (error) {
          console.error(error);
        }
        console.log(accounts);

        //request change to SKALE Network
        window.ethereum
          .request({
            method: "wallet_addEthereumChain",
            params: [switchToSKALE, accounts[0]]
          })
          .catch((error) => console.log(error.message));
      });
    } catch (error) {
      // User denied account access...
    }
  }
  // Legacy dapp browsers...
  else if (window.web3) {
    window.web3 = new Web3(web3.currentProvider);
    console.log("MetaMask - Web3 Initialized!");
  }
  // Non-dapp browsers...
  else {
    console.log(
      "Non-Web3 browser detected. You should consider trying MetaMask!"
    );
  }

Example Sandbox

https://codesandbox.io/s/metamask-wallet-integration-skale-dev-docs-k77zj

@manuelbarbas
Copy link
Collaborator Author

What is: Wallet
Language: Javascript, Typescript, C#, Swift
Target: Web, Mobile(Android and IOS), Unity

@manuelbarbas manuelbarbas added the documentation Improvements or additions to documentation label Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant