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 Ethereum 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 eth_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 eth_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 Ethereum" button, and the HTML code displays the button and the current account:

<button class="connectEthereumButton">Connect Ethereum</button>
const connectEthereumButton = document.querySelector('.connectEthereumButton');

connectEthereumButton.addEventListener('click', () => {
  //Will Start the OKX extension
  okxwallet.request({ method: 'eth_requestAccounts' });

Detect address changes#

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

okxwallet.on('accountsChanged', handler: (accounts: Array<string>) => void);

The OKX provider will emit this event whenever the return value of the eth_accounts RPC changes. eth_accounts will return an array that either is empty or contains a single account address. The returned address, if any, would be the address of the most recently used account that the caller is allowed to access.

Callers are identified by their URL origin, which means that all sites with the same origin share the same permissions.

This also means that accountsChanged will be emitted whenever the user's exposed account address changes.

Monitor user accounts on other blockchains#

Check out injected providers for account monitoring on other blockchains.