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

carvin carvin 提出于 2021-07-12 11:20 5230 浏览

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

0 条评论 20 学分

最佳答案

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]
    })
  },
Tiny熊

擅长:智能合约,以太坊 · 采纳率 33% · 回答于 2021-07-12 15:26

其它 1 个回答

写回答

你需要登录后才可以回答问题,登录