Skip to content

Cosmos(持续更新中...)

在 Bitget Wallet App 及安装 Chrome Extension 的 Chrome 浏览器中运行 DApp 时,可获得全局对象 window.bitkeep.keplr 并进行后续的 API 调用。

提示

  1. 只支持 Sei 主网
  2. 所有方法只支持 Sei 链接
js
const provider = window.bitkeep.keplr
const provider = window.bitkeep.keplr

支持版本

平台版本描述
Chrome Extension>=v1.4.8
App(iOS)>=v7.3.7
App(Android)>=v7.3.7

支持链

链id描述
Seipacific-1

Cosmos SDK

链接到 Bitget Wallet

js
enable(chainId: string): Promise<void>
// Only support Sei main network
enable(chainId: string): Promise<void>
// Only support Sei main network

获取地址/公钥

js
getKey(chainId: string): Promise<{
    // Name of the selected Wallet.
    name: string;
    algo: string;
    pubKey: Uint8Array;
    address: Uint8Array;
    bech32Address: string;
}>


await provider.getKey('pacific-1')
getKey(chainId: string): Promise<{
    // Name of the selected Wallet.
    name: string;
    algo: string;
    pubKey: Uint8Array;
    address: Uint8Array;
    bech32Address: string;
}>


await provider.getKey('pacific-1')

Sign Amino

js
signAmino(chainId: string, signer: string, signDoc: StdSignDoc): Promise<AminoSignResponse>
signAmino(chainId: string, signer: string, signDoc: StdSignDoc): Promise<AminoSignResponse>

与 CosmJS OfflineSigner 的 signAmino 类似,但 Bitget Wallet 的 signAmino 将 chain-id作为必需参数。标志 Amino-encoded StdSignDoc

Sign Direct/Protobuf

js
signDirect(chainId:string, signer:string, signDoc: {
    /** SignDoc bodyBytes */
    bodyBytes?: Uint8Array | null;
    /** SignDoc authInfoBytes */
    authInfoBytes?: Uint8Array | null;
    /** SignDoc chainId */
    chainId?: string | null;
    /** SignDoc accountNumber */
    accountNumber?: Long | null;
  }): Promise<DirectSignResponse>
signDirect(chainId:string, signer:string, signDoc: {
    /** SignDoc bodyBytes */
    bodyBytes?: Uint8Array | null;
    /** SignDoc authInfoBytes */
    authInfoBytes?: Uint8Array | null;
    /** SignDoc chainId */
    chainId?: string | null;
    /** SignDoc accountNumber */
    accountNumber?: Long | null;
  }): Promise<DirectSignResponse>

与 CosmJS OfflineDirectSigner 的 signDirect 类似,但 bitkeep signDirect 将 chain-id 作为必需参数。标志 Proto-encoded StdSignDoc .

请求事务广播

js
sendTx(
    chainId: string,
    tx: Uint8Array,
    mode: BroadcastMode
): Promise<Uint8Array>;
sendTx(
    chainId: string,
    tx: Uint8Array,
    mode: BroadcastMode
): Promise<Uint8Array>;

此函数请求 Bitget Wallet 将事务的广播委托给 Bitget Wallet 的 LCD 端点(而不是广播事务的网页)。此方法如果成功广播,则返回事务哈希,否则该方法将引发错误

signArbitrary

js
signArbitrary(
    chainId: string,
    signer: string,
    data: string | Uint8Array
): Promise<StdSignature>;

verifyArbitrary(
    chainId: string,
    signer: string,
    data: string | Uint8Array,
    signature: StdSignature
): Promise<boolean>;
signArbitrary(
    chainId: string,
    signer: string,
    data: string | Uint8Array
): Promise<StdSignature>;

verifyArbitrary(
    chainId: string,
    signer: string,
    data: string | Uint8Array,
    signature: StdSignature
): Promise<boolean>;

这是ADR-36的实验性实现。使用此功能的风险由您自行承担。

它的主要用途是证明链下账户的所有权,使用 signArbitrary API 请求 ADR-36 签名。

如果请求使用 Bitget Wallet 所需的 ADR-36 的 0 API 签名文档,而不是使用 signArbitary API,它将充当 signArbitary

  • 仅支持以 Amino 格式签名文档。(在 protobuf 的情况下,ADR-36 要求没有完全指定用于实现)
  • 签名文档消息应为单个,消息类型应为“签名/消息签名数据”
  • 签名文档“sign/MsgSignData”消息应具有“签名者”和“数据”作为其值。“数据”应采用 base64 编码
  • 签名文档 chain_id 应为空字符串("")
  • 签署文档备忘录应为空字符串("")
  • 签名文档 account_number 应为 "0"
  • 签名文档序列应为 "0"
  • 签名文档费用应为 ({gas: "0", amount: []})

使用 verifyArbitrary ,您可以验证 ADR-36 规范标准请求的 signArbitrary 个 API 或 signAmino 个 API 请求的结果。

verifyArbitrary 仅用于简单用法。 verifyArbitrary 返回当前所选帐户的签名文档的验证结果。如果帐户不是当前选择的帐户,则会引发错误。

建议在自己的实现中使用 verifyADR36Amino 函数,而不是使用 verifyArbitrary API。

Change Key Store Event

js
    keplr_keystorechange
    keplr_keystorechange

当用户在网页收到密钥存储/帐户上的信息后切换其密钥存储/帐户时,网页知道的密钥可能与 Bitget Wallet 中的所选密钥不匹配,这可能会导致交互出现问题。

为了防止这种情况发生,当密钥存储/帐户发生更改时,Bitget Wallet 会向网页窗口发出 keplr_keystorechange 事件。您可以基于此事件侦听器请求新的密钥/帐户。

js
window.addEventListener("keplr_keystorechange", () => {
    console.log("Key store in BitKeep is changed. You may need to refetch the account info.")
})
window.addEventListener("keplr_keystorechange", () => {
    console.log("Key store in BitKeep is changed. You may need to refetch the account info.")
})

keplr_keystorechange 是 keplr 钱包标准中的统一方法,可以同时监听 keplr 及 Bitget 钱包的对应变化,开发者可以通过如下代码区分 event 来源:

js
var currentWalllet = ''; //DApp should connect with just one wallet at same time.
try{
    await provider.enable(chainId);
    currentWalllet = 'bitget'; //rewrite when switch the wallet.
    console.log('bitget connect:', {chainId, result});
}catch(error){
    //connect error
}

/* 
address changed, 
chainId changed
*/
window.addEventListener("keplr_keystorechange", (event) => {
    if(currentWalllet == 'bitget'){
        console.log("bitget keplr_keystorechange:", event);
    }else{
        console.log("other keplr_keystorechange:", event);
    }
});
var currentWalllet = ''; //DApp should connect with just one wallet at same time.
try{
    await provider.enable(chainId);
    currentWalllet = 'bitget'; //rewrite when switch the wallet.
    console.log('bitget connect:', {chainId, result});
}catch(error){
    //connect error
}

/* 
address changed, 
chainId changed
*/
window.addEventListener("keplr_keystorechange", (event) => {
    if(currentWalllet == 'bitget'){
        console.log("bitget keplr_keystorechange:", event);
    }else{
        console.log("other keplr_keystorechange:", event);
    }
});

与CosmosJs使用

与Cosmosjs 链接

@cosmjs/launchpad@cosmjs/stargate

您可以使用 OfflineSigner 将 Bitget Wallet 链接到 CosmJS。

js
// Enabling before using the bitkeep.keplr is recommended.
// This method will ask the user whether or not to allow access if they haven't visited this website.
// Also, it will request user to unlock the wallet if the wallet is locked.
await window.bitkeep.keplr.enable(chainId);

const offlineSigner = window.bitkeep.getOfflineSigner(chainId);

// You can get the address/public keys by `getAccounts` method.
// It can return the array of address/public key.
// But, currently, BitKeep extension manages only one address/public key pair.
// XXX: This line is needed to set the sender address for SigningCosmosClient.
const accounts = await offlineSigner.getAccounts();

// Initialize the api with the offline signer that is injected by BitKeep extension.
const cosmJS = new SigningCosmosClient(
    "https://sei-rpc.polkachu.com",
    accounts[0].address,
    offlineSigner,
);
// Enabling before using the bitkeep.keplr is recommended.
// This method will ask the user whether or not to allow access if they haven't visited this website.
// Also, it will request user to unlock the wallet if the wallet is locked.
await window.bitkeep.keplr.enable(chainId);

const offlineSigner = window.bitkeep.getOfflineSigner(chainId);

// You can get the address/public keys by `getAccounts` method.
// It can return the array of address/public key.
// But, currently, BitKeep extension manages only one address/public key pair.
// XXX: This line is needed to set the sender address for SigningCosmosClient.
const accounts = await offlineSigner.getAccounts();

// Initialize the api with the offline signer that is injected by BitKeep extension.
const cosmJS = new SigningCosmosClient(
    "https://sei-rpc.polkachu.com",
    accounts[0].address,
    offlineSigner,
);
  • 要获得 OfflineSigner ,您可以使用 bitkeep.keplr.getOfflineSigner(chainId) 或 window.bitkeep.getOfflineSigner(chainId) 。(window.bitkeep.getOfflineSigner 是运行 bitkeep.keplr.getOfflineSigner 并返回值的别名)
  • 如果 BitKeep 插件已锁定,则 window.bitkeep.keplr.enable(chainId) 方法将请求用户解锁其插件。如果用户未授予将其插件链接到网站的权限,它将首先要求链接网站。
  • 如果用户取消解锁或拒绝链接权限,则会引发错误。
  • 如果插件已解锁并且网站具有链接权限,则不会执行任何操作。

Offline Signers 签名类型

  • 在 CosmJS 中,有两种类型的签名者:OfflineSigner 和 OfflineDirectSigner。OfflineSigner 用于在 Cosmos SDK Launchpad(Cosmos SDK v0.39.x 或更低版本)中对使用 Amino 序列化的 SignDoc 进行签名。OfflineDirectSigner 用于对 Protobuf 编码的 SignDoc 进行签名。
  • Bitget Wallet 支持这两种类型的签名者。bitkeep.keplr.getOfflineSigner(chainId) 或 window.bitkeep.getOfflineSigner(chainId) 返回同时满足 OfflineSigner 和 OfflineDirectSigner 的 Signer。因此,当将 CosmJS 与这个签名者一起使用时,Amino 用于 Launchpad 链,Protobuf 用于 Stargate 链。
  • 但是,如果要发送的消息能够使用 Amino 编解码器进行序列化/反序列化,则可以使用 Amino 的签名器。此外,由于 protobuf 型符号文档存在一些限制,因此在某些情况下可能需要 Amino。例如,Ledger Nano 的 Cosmos 应用程序目前不支持 Protobuf 格式的符号文档。此外,由于 protobuf 符号文档是二进制格式的,因此 Bitget Wallet 本身不支持的 msg 可能无法读取。
  • 如果要强制使用 Amino,可以使用以下 API: bitkeep.keplr.getOfflineSignerOnlyAmino(chainId) 或 window.bitkeep.getOfflineSignerOnlyAmino(chainId: string) 。因为这将始终返回一个与 Amino 兼容的签名者,所以任何与 Amino 兼容的 CosmJS 请求的 msg 都将向 Bitget Wallet 请求一个 Amino SignDoc。
  • 此外,还支持 bitkeep.keplr.getOfflineSignerAuto(chainId: string): Promise<OfflineSigner | OfflineDirectSigner> 或 window.bitkeep.getOfflineSignerAuto(chainId: string): Promise<OfflineSigner | OfflineDirectSigner> 个 API。请注意,返回的值是异步的。如果帐户是基于账本的帐户,则此 API 会自动返回仅支持 Amino 的签名者,如果帐户是基于助记符/私钥的帐户,则返回与 Amino 和 Protobuf 兼容的签名者。由于此 API 受链接的 Bitget Wallet 帐户类型的影响,因此如果使用keplr_keystorechange事件来检测帐户更改,则必须在触发此事件时使用 API 更改签名者。

与Stargate一起使用

Bitget Wallet 的 OfflineSigner 实现了 OfflineDirectSigner 接口。将 SigningStargateClient 与 Bitget Wallet 的 OfflineSigner Bitget Wallet 将以 Proto 签名文档格式对交易进行签名

js
import { SigningStargateClient } from "@cosmjs/stargate"

const denom = 'usei'
const toAddress = 'sei1jr0269wfm6ldfdtg6u9vl4hdmk8le0r46x6jkx'
const rpcUrl = 'https://xxx'

// Detect BitKeep Keplr
const keplr = window.bitkeep?.keplr
if (!keplr) {
    return alert("You need to install Bitget Wallet")
}
// Get the current state and amount of tokens that we want to transfer

// Create the signing client
const offlineSigner = window.bitkeep?.getOfflineSigner("atlantic-2")
const signingClient = await SigningStargateClient.connectWithSigner(
    rpcUrl,
    offlineSigner,
)
// Get the address and balance of your user
const account = (await offlineSigner.getAccounts())[0]

// Submit the transaction to send tokens to the faucet
const sendResult = await signingClient.sendTokens(
    account.address,
    toAddress,
    [
        {
            denom: denom,
            amount: '1',
        },
    ],
    {
        amount: [{ denom: denom, amount: "5000" }],
        gas: "200000",
    },
    ''
)

if (sendResult.code !== undefined && sendResult.code !== 0) {
    alert("Failed to send tx: " + sendResult.log || sendResult.rawLog);
} else {
    alert("Succeed to send tx:" + sendResult.transactionHash);
}
import { SigningStargateClient } from "@cosmjs/stargate"

const denom = 'usei'
const toAddress = 'sei1jr0269wfm6ldfdtg6u9vl4hdmk8le0r46x6jkx'
const rpcUrl = 'https://xxx'

// Detect BitKeep Keplr
const keplr = window.bitkeep?.keplr
if (!keplr) {
    return alert("You need to install Bitget Wallet")
}
// Get the current state and amount of tokens that we want to transfer

// Create the signing client
const offlineSigner = window.bitkeep?.getOfflineSigner("atlantic-2")
const signingClient = await SigningStargateClient.connectWithSigner(
    rpcUrl,
    offlineSigner,
)
// Get the address and balance of your user
const account = (await offlineSigner.getAccounts())[0]

// Submit the transaction to send tokens to the faucet
const sendResult = await signingClient.sendTokens(
    account.address,
    toAddress,
    [
        {
            denom: denom,
            amount: '1',
        },
    ],
    {
        amount: [{ denom: denom, amount: "5000" }],
        gas: "200000",
    },
    ''
)

if (sendResult.code !== undefined && sendResult.code !== 0) {
    alert("Failed to send tx: " + sendResult.log || sendResult.rawLog);
} else {
    alert("Succeed to send tx:" + sendResult.transactionHash);
}