20 求在浏览器中使用web3js连接钱包的具体方法和代码

求在浏览器中使用web3js连接钱包的具体方法和代码 可以加QQ私聊 133197676

请先 登录 后评论

最佳答案 2021-07-12 15:53

async initWeb3Account(callback) {
    if (window.ethereum) {
      this.provider = window.ethereum;

      // 请求连接demo.
      try {
        await window.ethereum.enable();
      } catch (error) {
        console.error("User denied account access");
      }
    } 

    this.web3 = new Web3(this.provider);
    this.web3.provider = this.provider

    this.web3.eth.getAccounts((err, accs) => {
      if (err != null) {
        console.error("无法获取账号, 是否安装了 Metamask");
        this.message = "";
        return;
      }

      if (accs.length === 0) {
        console.error("无法获取账号,Metamask 时候正确配置.");
        return;
      }
      // this.account = accs[0]
    })
  },
请先 登录 后评论

其它 1 个回答

carvin
请先 登录 后评论
  • 3 关注
  • 0 收藏,3366 浏览
  • carvin 提出于 2021-07-12 11:20