eth_requestAccounts
请求用户授权当前 DApp 访问账户。它是连接钱包的主要入口,首次调用会打开 UKey Wallet 的连接确认界面。
入参
无
结果
string[] - 用户授权后的账户地址数组。
示范
const accounts = await window.$ukey.ethereum.request({
method: "eth_requestAccounts",
});
console.log("当前已授权账户:", accounts[0]);
连接按钮例
async function requestWalletAccess() {
try {
const accounts = await window.$ukey.ethereum.request({
method: "eth_requestAccounts",
});
console.log("连接结果:", accounts[0]);
return accounts[0];
} catch (error) {
if (error.code === 4001) {
console.log("用户拒绝了连接操作");
} else {
console.error("连接未成功:", error);
}
throw error;
}
}
错误码
| 错误码 | 消息 | 说明 |
|---|---|---|
| 4001 | 用户取消了本次请求 | 用户取消了连接授权 |
| -32002 | Request already pending | 已有待处理的连接请求 |
说明
- 应在用户点击连接按钮后调用,不要在页面加载时自动弹窗。
- 如果用户已经授权,方法会直接返回账户地址。
- 当已有连接请求未完成时,再次调用可能返回
-32002。 - 连接状态可能在页面刷新后仍然有效,静默检查请使用
eth_accounts。 - 遵循 EIP-1102 规范。