连接浏览器插件钱包

Provider API#

什么是 Injected provider API?#

欧易 Injected providers API 是一个 JavaScript API,欧易将其注入用户访问的网站。您的 DApp 可以使用此 API 请求用户帐户,从用户连接的区块链读取数据,帮助用户签署消息和交易。

Note: Cosmos 接入仅支持欧易 Web3 钱包插件端。

连接账户#

#window.okxwallet.keplr.enable(chainIds)

描述

如果插件当前被锁定,window.keplr.enable(chainIds)方法请求解锁插件。如果用户未授予该网页的权限,它将要求用户授予该网页访问 Keplr 的权限。 enable 方法可以接收一个或多个链id作为数组。当传递链id数组时,你可以同时请求尚未授权的所有链的权限。 如果用户取消解锁或拒绝权限,将抛出错误。

enable(chainIds: string | string[]): Promise<void>

例子

codeopen中打开。

HTML
JavaScript
<button class="connectCosmosButton">Connect Cosmos</button>
const connectCosmosButton = document.querySelector('.connectCosmosButton');

connectCosmosButton.addEventListener('click', async() => {
  try {
    const chainId = "cosmoshub-4";
    // Enabling before using the Keplr is recommended.
    // This method will ask the user whether to allow access if they haven't visited this website.
    // Also, it will request that the user unlock the wallet if the wallet is locked.
    await window.okxwallet.keplr.enable(chainId);
    console.log(res);
  } catch (error) {
    console.log(error);
  }
});

签名交易#

#window.okxwallet.keplr.signAmino(chainId, signer, signDoc)

描述

按照固定格式签名,类似 cosmjs 的 OfflineSigner 的 signAmino 方法

参数就是对象,signDoc 就是一个固定格式

window.okxwallet.keplr.signAmino(chainId: string, signer: string, signDoc: StdSignDoc, signOptions: any): Promise<AminoSignResponse>

例子

codeopen中打开。

HTML
JavaScript
<button class="connectCosmosButton btn">Connect Cosmos</button>
<button class="signTransactionButton btn">Sign Transaction</button>
const connectCosmosButton = document.querySelector('.connectCosmosButton');
const signTransactionButton = document.querySelector('.signTransactionButton');

signTransactionButton.addEventListener('click', async() => {
  try {
    const res =  const res = await window.okxwallet.keplr.signAmino(
      "osmosis-1",
      "osmo1sxqwesgp7253fdv985csvz95fwc0q53ulldggl",
      {
        account_number: "707744",
        chain_id: "osmosis-1",
        fee: {
          gas: "500000",
          amount: [
            {
              denom: "uosmo",
              amount: "12500"
            }
          ]
        },
        memo: "",
        msgs: [
          {
            type: "osmosis/gamm/swap-exact-amount-in",
            value: {
              routes: [
                {
                  pool_id: "795",
                  token_out_denom: "uosmo"
                },
                {
                  pool_id: "1",
                  token_out_denom:
                    "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2"
                }
              ],
              sender: "osmo1sxqwesgp7253fdv985csvz95fwc0q53ulldggl",
              token_in: {
                amount: "10000",
                denom:
                  "ibc/2DA9C149E9AD2BD27FEFA635458FB37093C256C1A940392634A16BEA45262604"
              },
              token_out_min_amount: "553"
            }
          }
        ],
        sequence: "54"
      }
    );
    console.log(res);
  } catch (error) {
    console.log(error)
  }
});

connectCosmosButton.addEventListener('click', async() => {
  try {
    const chainId = "cosmoshub-4";
    // Enabling before using the Keplr is recommended.
    // This method will ask the user whether to allow access if they haven't visited this website.
    // Also, it will request that the user unlock the wallet if the wallet is locked.
    const res = await window.keplr.enable(chainId);
    console.log(res);
  } catch (error) {
    console.log(error);
  }
});

签名信息#

#window.okxwallet.keplr.signArbitrary(chainId, signer, data)

描述

签名任意信息,相当于之前几个链的 signMessage(any)

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

例子

codeopen中打开。

HTML
JavaScript
<button class="connectCosmosButton btn">Connect Cosmos</button>
<button class="signMessageButton btn">Sign Message</button>
const connectCosmosButton = document.querySelector('.connectCosmosButton');
const signMessageButton = document.querySelector('.signMessageButton');

signMessageButton.addEventListener('click', async() => {
  try {
    const res =  const res = await window.okxwallet.keplr.signArbitrary(
      "osmosis-1",
      "osmo1sxqwesgp7253fdv985csvz95fwc0q53ulldggl",
      'test cosmos'
      }
    );
    console.log(res);
  } catch (error) {
    console.log(error)
  }
});

connectCosmosButton.addEventListener('click', async() => {
  try {
    const chainId = "cosmoshub-4";
    // Enabling before using the Keplr is recommended.
    // This method will ask the user whether to allow access if they haven't visited this website.
    // Also, it will request that the user unlock the wallet if the wallet is locked.
    const res = await window.keplr.enable(chainId);
    console.log(res);
  } catch (error) {
    console.log(error);
  }
});

事件#

成功连接

连接到欧易 Web3 钱包可以通过调用 window.okxwallet.keplr.enable(chainId)。 当用户接受连接请求时,会触发连接事件。

用法

window.okxwallet.keplr.on("connect", () => console.log("connected!"));

例子

codeopen中打开。

HTML
JavaScript
<button class="connectCosmosButton">Connect Cosmos</button>
const connectCosmosButton = document.querySelector('.connectCosmosButton');

window.okxwallet.keplr.on("connect", () => console.log("connected!"));

connectCosmosButton.addEventListener('click', () => {
  try {
    const chainId = "cosmoshub-4";
    // Enabling before using the Keplr is recommended.
    // This method will ask the user whether to allow access if they haven't visited this website.
    // Also, it will request that the user unlock the wallet if the wallet is locked.
    const res = await window.okxwallet.keplr.enable(chainId);
    console.log(res);
  } catch (error) {
    console.log(error);
  }
});