Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to access the ethernet Fong account normally by Metamask

Shulou Source: shulou.com Published: 2022-06-01 04:31:58 09月21日 Update

Today, I would like to talk to you about how Metamask can normally access the Ethernet Square account. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.

When you use Metamask to test Ethernet Square DApp, if there is an inexplicable problem, check to see if web3.eth.accounts can get the account. If not, then the most likely reason is that you are using the new version of Metamask and the privacy mode is enabled by default.

There are two ways for your DApp to properly access an account managed by Metamask: turn off privacy mode, or modify the code to make it compatible with privacy mode.

First, turn off privacy mode

Enter the settings in metamask first, then click security & privacy. On the Privacy Mode menu, select disable Privacy Mode:

Second, compatible with privacy mode

When Metamask first introduced privacy mode in November 2018, this option was turned off by default. But in the latest version, privacy mode has been turned on by default. It is unrealistic to require each user to turn off privacy mode manually, so a better solution is to modify our JavaScript code to be compatible with privacy mode:

Window.addEventListener ('load', async ()) = > {/ / Modern dapp browsers... If (window.ethereum) {window.web3 = new Web3 (ethereum); try {/ / Request account access if needed await ethereum.enable (); / / Acccounts now exposed web3.eth.sendTransaction ({/ *... * /});} catch (error) {/ / User denied account access... }} / / Legacy dapp browsers... Else if (window.web3) {window.web3 = new Web3 (web3.currentProvider); / / Acccounts always exposed web3.eth.sendTransaction ({/ *... * /});} / / Non-dapp browsers... Else {console.log ('Non-Ethereum browser detected. You should consider trying MetaMaskies');}})

Use window.ethereum to determine whether the new metamask is new, and if so, call the ethereum.enable () method to request the user's authorization, which will pop up an authorization dialog in the user's web page, similar to the following:

After reading the above, do you have any further understanding of how Metamask can normally access the ethernet Fong account? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Mode privacy account Ethernet content user code maximum inexplicable method dialog box manual scheme method is in yes more version reality knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Apple MySQL Linux Xiaomi