Skip to Content
dApps
On this page

Accessing Accounts

Detect an Ethereum provider before requesting account access; see Provider API. Call eth_requestAccounts from a user action such as a Connect button. Use eth_accounts when you only need the accounts already exposed to your site.

const provider = window.$onekey?.ethereum; if (!provider) throw new Error('OneKey Ethereum provider is unavailable.'); try { const accounts = await provider.request({ method: 'eth_requestAccounts' }); const address = accounts[0]; if (!address) throw new Error('No account was selected.'); console.log('Connected address:', address); } catch (error) { if (error.code === 4001) { console.log('The user declined the connection request.'); } else { console.error(error); } }

Disable the Connect button while the request is pending. A connected account does not pre-approve transactions or signatures. Pass the selected address to eth_sendTransaction, personal_sign, or eth_signTypedData_v4, as required by each method.

Listen for accountsChanged and handle an empty array by clearing the connected account in your application.

Demo

Open example 

Last updated on