Connect to Extension Wallet

Provider API#

What is injected provider API?#

The OKX injected provider API is a JavaScript API that OKX injects into websites visited by our users. Your DApp can use this API to request users' accounts, read data from blockchains users are connected to, and help users sign messages and transactions.

Connecting to OKX Wallet#

window.okxwallet.solana.connect()

Description

You can connect to OKX Wallet by calling window.okxwallet.solana.connect().

The connect call will return a Promise object, which will resolve when the user accepts the connection request andreject if you reject the request or close the pop-up window.

For more information about possible errors in OKX Wallet, see Error message . If you accept the connection request, window.okxwallet.solana will also trigger the connection event.

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

Once the web application is connected to OKX Wallet, OKX Wallet will be able to read the public key of the connecting account and prompt you to make other transactions. We'll also conveniently expose a boolean returned from the isConnected request.

Example

Open in 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());
  } catch (error) {
    console.log(error);
  }
});

Signing transactions#

window.okxwallet.solana.signTransaction(transaction)

Signing and sending the transaction

After the transaction is initiated, the web application may request the your OKX Wallet to sign and send the transaction. If accepted, OKX Wallet will use your private key to sign the transaction and submit it through the Solana JSON RPC connection. Calling the signAndSendTransaction method on okxwallet.solana will return a Promise for the signed transaction.

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);

Signing the transaction without sending

After the transaction is initiated, the web application may require your OKX Wallet to sign the transaction without submitting it to the network. Calling the signTransaction method will return a Promise for the signed transaction. After the transaction is signed, the application can go through @solana/web3.js sendRawTransaction and submit the transaction.

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());

Batch signing transactions

You can also sign and send multiple transactions at the same time using signAllTransactions on the provider.

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

Example

Open in 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());
  } catch (error) {
    console.log(error);
  }
});

Signing messages#

window.okxwallet.solana.signMessage(args)

Description

When a web application connects to OKX Wallet, it can also request you to sign the given message. The application can freely write its own messages, which will be displayed OKX Wallet's signature prompt. Message signing doesn't involve network costs and is a convenient way for applications to verify address ownership. In order to send a message for you to sign, the web application must provide a hexadecimal or UTF-8 encoded string as Uint8Array, and request the encoded message to be signed through your OKX Wallet.

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");

Example

Open in 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());
  } catch (error) {
    console.log(error);
  }
}

Events#

Connecting to OKX Wallet Call window.okxwallet.solana.connect() to connect to OKX Wallet. Once you accept the connection request, the connection event will trigge

Usage

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

Disconnecting from OKX Wallet The disconnecting method is the same as the connecting method. However, the wallet can also initiate the disconnection.

Usage

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

Switching accounts

OKX Wallet allows you to seamlessly manage multiple accounts from a single extension or mobile application. Whenever you switch accounts, OKX Wallet will send an accountChanged event.

If you switch accounts while still connected to the application, and if the new account has placed the application on the allowlist, you will remain connected and OKX Wallet will pass the public key of the new account:

Usage

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

If OKX Wallet doesn't pass the public key of the new account, the application can either do nothing or try to reconnect:

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
    });
  }
});

Example

Open in 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);
  } catch (error) {
    console.log(error);
  }
});