Connect to Extension Wallet
Obtain wallet address

Obtain wallet address#

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

For example, in Ethereum, an Ethereum address is the unique public identifier of an account. Each account has a corresponding address, which is used for interactions and identification on the network. The account contains all state information and functions associated with that address.

If a DApp wants to request a user's signature or have the user approve a transaction, it must use the eth_requestAccounts RPC method to access the user's account.

Creating a Connection#

It is recommended to provide a button here that allows users to connect the OKX Web3 wallet to the DApp. Clicking this button will call the eth_requestAccounts method to access the user's account address.

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="connectEthereumButton">Connect Ethereum</button>
const connectEthereumButton = document.querySelector('.connectEthereumButton');

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

Detect Account Address Changes#

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

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

Whenever the return value of the eth_accounts RPC method changes, OKX will emit a corresponding event notification. eth_accounts will return an array that is either empty or contains a single account address. If an account address is present, it is the most recently used account address accessible to the caller.

Since callers are identified by their URL origin, sites with the same origin will hold the same permissions. The accountsChanged event is emitted whenever the publicly available account address changes.

Obtaining Account Addresses for More Chains#

Check out injected providers for account monitoring on other blockchains.