Connect DApp to OKX Wallet
Access user accounts

Access user accounts#

User accounts are utilized in a variety of contexts in blockchain, including as identifiers and for signing transactions.

Take Tron as an example; to request a signature from a user or have a user approve a transaction, your DApp must access the user's accounts using the tron_requestAccounts RPC.

Create a connect button#

We recommend creating a button to allow users to connect OKX Wallet to your DApp. Clicking on this button should call tron_requestAccounts to access the user's accounts.

In the example below, the JavaScript code accesses the user's accounts when they click on the "Connect Tron" button, and the HTML code displays the button and the current account:

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

connectTronButton.addEventListener('click', () => {
  //Will Start the OKX extension
  window.okxwallet.tronLink.request({ method: 'tron_requestAccounts'})
});

Detect address changes#

You can also listen to the emitted events to get updates:

window.addEventListener('message', function (e) {
  if (e.data.message && e.data.message.action === "accountsChanged") {
    // handler logic
    console.log('got accountsChanged event', e.data, e.data.message.address)
  }
})

The OKX provider will emit this event whenever the return value of the tron_requestAccounts RPC changes.