20 metamask已经连接了本地网络,为什么web3还是无法使用默认地址呢?

请先 登录 后评论

最佳答案 2019-12-27 10:29

新版本的MetaMask 出于隐私目的,需求请求用户授权,代码如下, 关键代码如下:

        if (window.ethereum) {
            // 请求用户授权
            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"));
        }
请先 登录 后评论

其它 1 个回答

RRR
请先 登录 后评论