Skip to Content
dApp 接入
本页目录

eth_accounts

返回钱包向调用方开放、且调用方有权访问的账户地址列表。与 eth_requestAccounts 不同,该方法不会触发连接弹窗。


参数

无


返回值

string[] — Ethereum 地址数组;如果没有向本站开放的账户,则返回空数组。


示例

const accounts = await window.$onekey.ethereum.request({ method: 'eth_accounts' }) if (accounts.length > 0) { console.log('Connected:', accounts[0]) } else { console.log('Not connected') }

说明

  • 用户未连接时,返回空数组 []。
  • 使用 eth_requestAccounts 发起连接。
  • 监听 accountsChanged 事件以处理账户变化。
window.$onekey.ethereum.on('accountsChanged', (accounts) => { if (accounts.length === 0) { console.log('Disconnected') } else { console.log('Account changed:', accounts[0]) } })

检查连接状态

页面加载时可以使用此只读检查恢复界面状态。没有已授权账户时显示连接按钮,由用户点击后调用 eth_requestAccounts。

async function checkConnection() { const provider = window.$onekey?.ethereum; if (!provider) return null; const accounts = await provider.request({ method: 'eth_accounts' }); return accounts.length > 0 ? accounts[0] : null; }

错误码

错误码消息说明
-32603Internal errorProvider 内部错误
最后更新于