# 常见问题解答

# Bitget Chrome 插件

提示

请全局使用 window.bitkeep.ethereum 作为接入 Bitget Wallet 的规范,全部的操作都应该使用该对象。

# 1. 检测不到 window?.bitkeep?.ethereum 程序

检测不到 window?.bitkeep?.ethereum 程序项目方自行可引导用户跳转到 Bitget Wallet (opens new window) 官网下载插件

示例代码如下:


 







function getProvider() {
    const provider = window.bitkeep && window.bitkeep.ethereum;
    if (!provider) {
        window.open('https://web3.bitget.com/zh-CN/wallet-download?type=2');
        throw "Please go to our official website to download!!"
    }
    return provider;
}
1
2
3
4
5
6
7
8

# 2. 覆盖及其他钱包调用冲突的问题

许多 Dapp 接入 BitKeep Extension 时使用 window.ethereum 从而导致出现覆盖问题 冲突问题,这给用户带来了很多困惑。解决方案如下:

提示

请使用 window.bitkeep.ethereum 接入 Bitget Extension,它和 web3.currentProvider window.ethereum 程序提供功能是相同的。可参考下面多钱包示例代码

# 3. 多个钱包切换链或者地址冲突问题

提示

在切换钱包连接之前,应该清空上一次钱包监听的事件。

示例代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- <script src="./js/web3.min.js"></script> -->
    <script src="https://cdn.bootcdn.net/ajax/libs/web3/1.8.0/web3.min.js"></script>
  </head>
  <style>
    * {
      margin: 0;
      padding: 0;
      list-style: none;
    }
  </style>
  <body>
    <div>
      <button id="bitKeepConnect">BitKeep</button>
      <button id="injectedConnect">injected</button>
      <div>
        <span id="address"></span>
        <span id="chainId"></span>
      </div>
    </div>
    <div>
      <button id="eth_signTypedData_v4">eth_signTypedData_v4</button>
    </div>
    <script>
      let bitKeepBtn = document.querySelector("#bitKeepConnect"),
        injectedBtn = document.querySelector("#injectedConnect"),
        ethSignTypedDataV4Btn = document.querySelector("#eth_signTypedData_v4"),
        addressSpan = document.querySelector("#address"),
        chainIdSpan = document.querySelector("#chainId");
      let provider = null,
        web3 = null;
      function getProvider(type) {
        let provider = null;
        if (type == "bitkeep") {
          provider = window.bitkeep && window.bitkeep.ethereum;
          if (!provider) {
            window.open("https://web3.bitget.com/zh-CN/wallet-download?type=0&theme=light");
            throw "please install BitKeep";
          }
        } else {
          provider = window.ethereum;
          if (!provider) {
            throw "please install injected wallet";
          }
        }
        return provider;
      }
      //address  and chanId changed event
      async function accountsChanged(accounts) {
        console.log("address, isBitKeep:", !!provider.isBitKeep, accounts);
        const [address] = await web3.eth.getAccounts();
        addressSpan.innerText = address;
      }
      async function chainChanged(chainId) {
        console.log("chainChanged,isBitKeep", !!provider.isBitKeep, chainId);
        chainIdSpan.innerText = await web3.eth.getChainId();
      }
      async function connect(type = "bitkeep") {
        const lastProvider = provider;
        const newProvider = getProvider(type);
        await newProvider.request({ method: "eth_requestAccounts" });
        //1. Prevent logical conflicts. clear the last wallet listening event.
        if (lastProvider) {
          provider.removeAllListeners();
        }
        //2. The authorized link successfully replaces the wallet's providers
        provider = newProvider;
        web3 = new Web3(provider);
        // 3. event listener  address and chainId changed.  If the address does not exist, it is disconnected
        provider.on("accountsChanged", async (accounts) => {
          accountsChanged(accounts);
        });
        provider.on("chainChanged", async (chainId) => {
          chainChanged(chainId);
        });
        //4. Get the current address and chainId
        accountsChanged();
        chainChanged();
        //5.  Cache the default link and go to the wallet
        localStorage.setItem("injected", type);
      }
      //connect
      async function bitKeepBtnClick() {
        await connect("bitkeep");
        injectedBtn.style.backgroundColor = "transparent";
        bitKeepBtn.style.backgroundColor = "blue";
      }
      async function injectedBtnClick() {
        await connect("metaMask");
        bitKeepBtn.style.backgroundColor = "transparent";
        injectedBtn.style.backgroundColor = "blue";
      }
      async function ethSignTypedDataV4BtnClick() {
        const [address] = await web3.eth.getAccounts();
        // provider.request({method:"personal_sign",params:[address  ,"Hello world"]})
        const sign = await web3.eth.personal.sign(web3.utils.utf8ToHex("Hello world"), address);
      }
      bitKeepBtn.onclick = bitKeepBtnClick;
      injectedBtn.onclick = injectedBtnClick;
      ethSignTypedDataV4Btn.onclick = ethSignTypedDataV4BtnClick; // Call up signatur
      localStorage.injected &&  itKeepBtnClick(localStorage.injected);
      //
    </script>
  </body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

# 4. 使用第三方 npm 包接入问题

提示

使用第三方 npm 包,应该使用 window.bitkeep.ethereum 作为程序的提供者注入。

该包的最新版本默认走MetaMask的统一方式,区分多钱包的方式最新代码还没发版。Bitget Extension提供如下两种解决方案示例代码:

  1. 打开 Bitget Wallet,但无法不能使用问题的解决方案: ssues/574 (opens new window)
  2. 使用 bitkeep-web3modal (opens new window) 示例代码来支持 Bitget Extension

下面这种方式 fork 的 web3modal 支持多钱包存在的情况

import web3modal from 'bitkeep-web3modal';
const web3Modal = new Web3Modal({
    network: 'mainnet', // optional
    cacheProvider: true, // optional
    providerOptions: {
        bitkeep: {
            package: true,
        },
        walletconnect: {
        display: {
            logo: 'data:image/gif;base64,INSERT_BASE64_STRING',
            name: 'Mobile',
            description: 'Scan qrcode with your mobile wallet',
        },
        package: WalletConnectProvider,
        options: {
            infuraId: 'INFURA_ID', // required
        },
        },
    }, // required
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

该包最新支持 Bitget Wallet 版本处于待发布状态,如需快速支持可参考我们提供的示例演示 (opens new window)实现。

提示

ethers.js 默认在 window 上挂载 _ethers 对象, 不建议使用 window._ethers Bitget Wallet 默认也注入了 _ethers 对象,避免使用 _ethers 因为加载顺序造成使用冲突参考以下方式引入使用。

//import
import ethers from "ethers"
const ethers = rquire("ethers")
//cdn
window.ethers
1
2
3
4
5
6

# 5. Aptos已经切换成Mainnet

网络

我们现在支持的网络:Mainnet | Devnet

正式Mainnet已经上线,Testnet已经修改成Mainnet

可以使用DevTest进行测试

最后更新: 2023/8/31 19:45:11