连接浏览器插件钱包

Provider API#

什么是 Injected provider API?#

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

连接账户#

window.okxwallet.solana.connect()

描述

连接到欧易 Web3 钱包可以通过调用 window.okxwallet.solana.connect()

connect 调用将返回一个 Promise 对象,该 Promise 对象在用户接受连接请求时 resolve,并在用户拒绝请求或关闭弹出窗口时 reject。有关欧易 Web3 钱包可能发生错误的详细信息,请参考 错误码。 当用户接受连接请求时,window.okxwallet.solana 也会触发连接事件。

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

一旦 Web 应用程序连接到欧易 Web3 钱包,它将能够读取连接账户的公钥并提示用户进行其他交易。还公开了一个方便的 isConnected 布尔值。

例子

codeopen中打开。

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

connectSolanaButton.addEventListener('click', () => {
  try {
    const provider = window.okxwallet.solana;
    const resp = await provider.connect();
    console.log(resp.publicKey.toString());
    // 26qv4GCcx98RihuK3c4T6ozB3J7L6VwCuFVc7Ta2A3Uo
    // { address: string, publicKey: string }
  } catch (error) {
    console.log(error);
    // { code: 4001, message: "User rejected the request."}
  }
});

签名交易#

window.okxwallet.solana.signTransaction(transaction)

签名并发送交易

创建交易后,Web 应用程序可能会要求用户的欧易 Web3 钱包签署并发送交易。如果接受,欧易 Web3 钱包将使用用户的私钥签署交易并通过 Solana JSON RPC 连接提交。在 okxwallet.solana 上调用 signAndSendTransaction 方法会为已签名的交易返回 promise

const provider = window.okxwallet.solana;
const network = "<NETWORK_URL>";
const connection = new Connection(network);
const transaction = new Transaction();
const { signature } = await provider.signAndSendTransaction(transaction);
await connection.getSignatureStatus(signature);

签名交易(不发送)

创建交易后,Web 应用程序可能会要求用户的欧易 Web3 钱包签署交易,而无需将其提交到网络。调用 signTransaction 方法会为已签名的交易返回 Promise。 交易签署后,应用程序可以通过 @solana/web3.jssendRawTransaction 提交交易本身。

const provider = window.okxwallet.solana;
const network = "<NETWORK_URL>";
const connection = new Connection(network);
const transaction = new Transaction();
const signedTransaction = await provider.signTransaction(transaction);
const signature = await connection.sendRawTransaction(signedTransaction.serialize());

批量签署交易

通过 provider 上的 signAllTransactions 方法也可以一次签署和发送多个交易。

const provider = window.okxwallet.solana;
const transactions = [new Transaction()];
const signedTransactions = await provider.signAllTransactions(transactions);

例子

codeopen中打开。

HTML
JavaScript
<button class="connectSolanaButton btn">Connect Solana</button>
<button class="signTransactionButton btn">Sign Transaction</button>
// import { Connection, Transaction } from "@solana/web3.js";
const connectSolanaButton = document.querySelector('.connectSolanaButton');
const signTransactionButton = document.querySelector('.signTransactionButton');

signTransactionButton.addEventListener('click', async() => {
  try {
   const provider = window.okxwallet.solana;
    const network = "<NETWORK_URL>";
    const connection = new Connection(network);
    const transaction = new Transaction();
    const signedTransaction = await provider.signTransaction(transaction);
    const signature = await connection.sendRawTransaction(signedTransaction.serialize());
    console.log(signature);
  } catch (error) {
    console.log(error)
  }
});

connectSolanaButton.addEventListener('click', async() => {
   try {
    const provider = window.okxwallet.solana;
    const resp = await provider.connect();
    console.log(resp.publicKey.toString());
    // 26qv4GCcx98RihuK3c4T6ozB3J7L6VwCuFVc7Ta2A3Uo
    // { address: string, publicKey: string }
  } catch (error) {
    console.log(error);
    // { code: 4001, message: "User rejected the request."}
  }
});

签名信息#

window.okxwallet.solana.signMessage(args)

描述

当 Web 应用程序连接到欧易 Web3 钱包时,它还可以请求用户签署给定消息。应用可以自由编写自己的消息,这些消息将在欧易 Web3 钱包的签名提示中显示给用户。消息签名不涉及网络费用,是应用程序验证地址所有权的便捷方式。

为了发送消息供用户签名,Web 应用程序必须:

提供一个十六进制或 UTF-8 编码的字符串作为 Uint8Array。 请求通过用户的欧易 Web3 钱包钱包对编码消息进行签名。

const message = `To avoid digital dognappers, sign below to authenticate with CryptoCorgis`;
const encodedMessage = new TextEncoder().encode(message);
const signedMessage = await window.okxwallet.solana.signMessage(encodedMessage, "utf8");

例子

codeopen中打开。

HTML
JavaScript
<button class="connectSolanaButton btn">Connect Solana</button>
<button class="signButton btn">Sign Message</button>
const connectSolanaButton = document.querySelector('.connectSolanaButton');
const signButton = document.querySelector('.signButton');

signButton.addEventListener('click', async() => {
  try {
    const message = `To avoid digital dognappers, sign below to authenticate with CryptoCorgis`;
    const encodedMessage = new TextEncoder().encode(message);
    const signedMessage = await window.okxwallet.solana.signMessage(encodedMessage, "utf8");
    console.log(signedMessage);
  } catch (error) {
    // see "Errors"
  }
});

connectSolanaButton.addEventListener('click', () => {
  connetAccount();
});

async function connetAccount() {
  try {
    const provider = window.okxwallet.solana;
    const resp = await provider.connect();
    console.log(resp.publicKey.toString());
    // 26qv4GCcx98RihuK3c4T6ozB3J7L6VwCuFVc7Ta2A3Uo
    // { address: string, publicKey: string }
  } catch (error) {
    console.log(error);
    // { code: 4001, message: "User rejected the request."}
  }
}

事件#

成功连接

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

用法

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

断开连接#

断开连接与连接过程相同。但是,钱包也有可能发起断开连接,而不是应用程序本身。

用法

window.okxwallet.solana.on("disconnect", () => console.log("disconnected!")
);

账户变更

欧易 Web3 钱包允许用户从单个扩展程序或移动应用程序中无缝管理多个账户。每当用户切换账户时,欧易 Web3 钱包都会发出一个 accountChanged 事件。

如果用户在已连接到应用程序时更改账户,并且新账户已经将该应用程序列入白名单,那么用户将保持连接状态并且欧易 Web3 钱包将传递新账户的公钥:

用法

window.okxwallet.solana.on('accountChanged', (publicKey) => {
  if (publicKey) {
      // Set new public key and continue as usual
      console.log(`Switched to account ${publicKey.toBase58()}`);
  }
});

如果欧易 Web3 钱包没有传递新账户的公钥,应用程序可以不做任何事情或尝试重新连接:

window.okxwallet.solana.on('accountChanged', (publicKey) => {
  if (publicKey) {
    // Set new public key and continue as usual
    console.log(`Switched to account ${publicKey.toBase58()}`);
  } else {
    // Attempt to reconnect to OKX wallet
    window.okxwallet.solana.connect().catch((error) => {
      // Handle connection failure
    });
  }
});

例子

codeopen中打开。

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

window.okxwallet.solana.on('connect',()=>{
  console.log('connected');
})

connectSolanaButton.addEventListener('click', async() => {
  try {
    const res = await window.okxwallet.aptos.connect();
    console.log(res);
    // { address: string, publicKey: string }
  } catch (error) {
    console.log(error);
    // { code: 4001, message: "User rejected the request."}
  }
});