Connect to Extension Wallet

Obtain Wallet Address#

Wallet account addresses are used in various scenarios, including as identifiers and for signing transactions.

Creating a Connection#

It is recommended to provide a button here that allows users to connect the OKX Web3 Wallet Tron to the DApp.

In the example project code below, the JavaScript code accesses the user's account address when the user clicks the connect button, and the HTML code displays the button and the current account address:

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 Account 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.