current position:Home>Metamask is connected to the local network. Why can't Web3 use the default address?

Metamask is connected to the local network. Why can't Web3 use the default address?

2022-02-03 20:50:52 Q & A of Denglian community




Take the answer 1:

The new version of the MetaMask For privacy purposes , Request user authorization , The code is as follows , The key codes are as follows :

        if (window.ethereum) {            //  Request user authorization             window.ethereum.enable(function(){                web3.eth.getAccounts((error, accounts) => {                    web3.eth.defaultAccount = accounts[0];                    console.log(accounts)                })            })        } else  if (typeof web3 !== 'undefined') {            web3 = new Web3(web3.currentProvider);            web3.eth.defaultAccount = web3.eth.accounts[0];            console.log(web3.eth.defaultAccount);        } else {            // set the provider you want from Web3.providers            web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));        }





Other answers 1:

copyright notice
author[Q & A of Denglian community],Please bring the original link to reprint, thank you.
https://en.netfreeman.com/2022/02/202202032050492714.html

Random recommended