Skip to Content
dApp 接入
本页目录

访问账户

请求账户权限前,先检测 Ethereum provider,参见 Provider API。eth_requestAccounts 应由用户点击“连接”等操作触发。仅需读取已向本站开放的账户时,使用 eth_accounts。

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); } }

请求处理期间禁用“连接”按钮。账户已连接,不代表用户预先批准了交易或签名。请按各方法的要求,将所选地址传给 eth_sendTransaction、personal_sign 或 eth_signTypedData_v4。

监听 accountsChanged;收到空数组时,清除应用中的已连接账户状态。

演示

打开示例 

最后更新于