Connect to Extension Wallet
Provider API

Provider API#

Aptos-AIP-62#

The AIP-62 standard, introduced by Aptos for wallet connectivity, is already supported by the OKX wallet.

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.aptos.connect()

Description

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

When window.okxwallet.aptos.connect() has been successfully called, the OKX Wallet connection page will be displayed. You can decide whether to connect to the current DApp or not. If you agree to connect, the address and publicKey key will be returned.

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

Example

Open in codeopen.

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

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

Get acount information#

window.okxwallet.aptos.account()

Description

Calling window.okxwallet.aptos.account() will retrieve the account information of the current 'Dapp' and return the address and public key`.

const account = await window.okxwallet.aptos.account();
// { address: string, publicKey: string }

Example

Open in codeopen

HTML
JavaScript
<button class="connectAptosButton">Connect Aptos</button>
<button class="accountAptosButton">Account</button>
const connectAptosButton = document.querySelector('.connectAptosButton');
const accountAptosButton = document.querySelector('.accountAptosButton');

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


accountAptosButton.addEventListener('click', async () => {
  const account = await window.okxwallet.aptos.account();
  console.log(account);
  // { address: string, publicKey: string }
});

Get Current Network#

window.okxwallet.aptos.network()

Description

Calling window.okxwallet.aptos.network() will retrieve the network information of the current 'Dapp' and return the network name.

const network = await window.okxwallet.aptos.network();
// 'Mainnet'
// We support network:`Mainnet` | `Movement Testnet`
enum Network {
  Mainnet = 'Mainnet'
  MovementTestnet = 'Movement Testnet'
}

Example

Open in codeopen

HTML
JavaScript
<button class="connectAptosButton">Connect Aptos</button>
<button class="networkAptosButton">Network</button>
const connectAptosButton = document.querySelector('.connectAptosButton');
const networkAptosButton = document.querySelector('.networkAptosButton');

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


networkAptosButton.addEventListener('click', async () => {
  const network = await window.okxwallet.aptos.network();
  console.log(network);
  // 'Mainnet'
});

Signing transactions#

window.okxwallet.aptos.signAndSubmitTransaction(transaction)

Description

In OKX Wallet, you can use window.okxwallet.aptos.signAndSubmitTransaction(transaction) to trigger a transaction on the Aptos chain. This function will return a pendingTransaction for DApps.

const transaction = {
  arguments: [address, '717'],
  function: '0x1::coin::transfer',
  type: 'entry_function_payload',
  type_arguments: ['0x1::aptos_coin::AptosCoin'],
};

try {
  const pendingTransaction = await window.okxwallet.aptos.signAndSubmitTransaction(transaction);

  const client = new AptosClient('https://fullnode.mainnet.aptoslabs.com/');
  const txn = await client.waitForTransactionWithResult(
      pendingTransaction.hash,
  );
} catch (error) {
  // see "Errors"
}

Of course, it is also possible to simply sign the transaction without initiating an on-chain operation using window.okxwallet.aptos.signTransaction(transaction). This method will return a signed buffer.

Caution
This method is uncommon and unsafe for users, and isn't recommended.
const transaction = {
  arguments: [address, '717'],
  function: '0x1::coin::transfer',
  type: 'entry_function_payload',
  type_arguments: ['0x1::aptos_coin::AptosCoin'],
};

try {
  const signTransaction = await window.okxwallet.aptos.signTransaction(transaction);
} catch (error) {
  // see "Errors"
}

Example

Open in codeopen.

HTML
JavaScript
<button class="connectAptosButton btn">Connect Aptos</button>
<button class="signTransactionButton btn">Sign Transaction</button>
const connectAptosButton = document.querySelector('.connectAptosButton');
const signTransactionButton = document.querySelector('.signTransactionButton');
let address='';

signTransactionButton.addEventListener('click', async() => {
  try {
    const transaction = {
      arguments: [address, '717'],
      function: '0x1::coin::transfer',
      type: 'entry_function_payload',
      type_arguments: ['0x1::aptos_coin::AptosCoin'],
    };

    const pendingTransaction = await window.okxwallet.aptos.signAndSubmitTransaction(transaction);
    console.log(pendingTransaction);
  } catch (error) {
    console.log(error)
  }
});

connectAptosButton.addEventListener('click', async() => {
   console.log(res);
   const res = await window.okxwallet.aptos.connect();
   address = res.address;
});

Signing messages#

window.okxwallet.aptos.signMessage(message)

Description

DApps can call window.okxwallet.aptos.signMessage(message) to sign messages. If you agree to sign, OKX Wallet will return the successfully signed message, signature, input parameters, and return message. The data structure is shown below.

Parameters

interface SignMessagePayload {
  address?: boolean; // Should we include the address of the account in the message
  application?: boolean; // Should we include the domain of the DApp
  chainId?: boolean; // Should we include the current chain id the wallet is connected to
  message: string; // The message to be signed and displayed to the user
  nonce: string; // A nonce the DApp should generate
}

Return value

interface SignMessageResponse {
  address: string;
  application: string;
  chainId: number;
  fullMessage: string; // The message that was generated to sign
  message: string; // The message passed in by the user
  nonce: string;
  prefix: string; // Should always be APTOS
  signature: string; // The signed full message
}

Example

Open in codeopen.

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

const signMessagePayload = {
  message: 'hello okx',
  nonce: 'okx'
};

signButton.addEventListener('click', async() => {
  try {
    const signMessage = await window.okxwallet.aptos.signMessage(signMessagePayload)
    console.log(signMessage);
    // {"signature": string, "prefix": "APTOS", "fullMessage": "APTOS nonce: okx message: hello okx", "message": "hello okx", "nonce": "okx" }
  } catch (error) {
    // see "Errors"
  }
});

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

async function connetAccount() {
  const res = await window.okxwallet.aptos.connect();
  console.log(res);
}

Signing Message Verification#

import nacl from 'tweetnacl';

const message = 'hello';
const nonce = 'random_string';

try {
    const response = await window.okxwallet.aptos.signMessage({
        message,
        nonce,
    });
    const { publicKey } = await window.okxwallet.aptos.account();
    // Remove the 0x prefix
    const key = publicKey!.slice(2, 66);
    const verified = nacl.sign.detached.verify(
        Buffer.from(response.fullMessage),
        Buffer.from(response.signature, 'hex'),
        Buffer.from(key, 'hex'),
);
    console.log(verified);
} catch (error) {
    console.error(error);
}

Events#

Switching accounts

When you switch OKX Wallet accounts, it's necessary to listen to the wallet switching event: onAccountChange

Caution
When you switch wallet accounts, your current account must have an Aptos address to trigger this event.
let currentAccount = await window.okxwallet.aptos.account();

// event listener for disconnecting
window.okxwallet.aptos.onAccountChange((newAccount) => {
  // If the new account has already connected to your app then the newAccount will be returned
  if (newAccount) {
    currentAccount = newAccount;
  } else {
    // Otherwise you will need to ask to connect to the new account
    currentAccount = window.okxwallet.aptos.connect();
  }
});

Disconnecting from OKX Wallet

When OKX Wallet disconnects (OKX Wallet is a multi-chain wallet, so this event will also be triggered when you switch to an account that doesn't have an Aptos address):

// get current connection status
let connectionStatus = await window.okxwallet.aptos.isConnected();

// event listener for disconnecting
window.okxwallet.aptos.onDisconnect(() => {
  connectionStatus = false;
});

onNetworkChange()

The DApp needs to ensure that the user is connected to the target network, so it needs to get the current network, switch networks, and listen for network changes.

// Current network
let network = await window.okxwallet.aptos.network();

// event listener for network changing
window.bitkeep.aptos.onNetworkChange((newNetwork) => {
  network = newNetwork; // { networkName: 'Mainnet' }
});

Example

open in codeopen.

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

window.okxwallet.aptos.on('connect',()=>{
  console.log('got connect event');
})

connectAptosButton.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."}
  }
});