eth_requestAccounts
Request the user to authorize the current DApp to access the account. It is the main entrance to connect to the wallet. The first call will open the UKey Wallet connection confirmation interface.
Inputs
none
return value
string[] - Array of account addresses after user authorization.
Demo
const accounts = await window.$ukey.ethereum.request({
method: "eth_requestAccounts",
});
console.log("Authorized account:", accounts[0]);
Connect button example
async function requestWalletAccess() {
try {
const accounts = await window.$ukey.ethereum.request({
method: "eth_requestAccounts",
});
console.log("Connection established:", accounts[0]);
return accounts[0];
} catch (error) {
if (error.code === 4001) {
console.log("The user declined the connection");
} else {
console.error("Unable to connect:", error);
}
throw error;
}
}
error code
| Code | Message | Details |
|---|---|---|
| 4001 | User rejected the request | Connection approval was rejected by the user |
| -32002 | Request already pending | There is a pending connection request |
illustrate
- It should be called after the user clicks the connect button, and should not automatically pop up when the page loads.
- If the user has been authorized, the method will directly return the account address.
- When an existing connection request is not completed, calling again may return
-32002. - The connection status may still be valid after page refresh, please use
eth_accountsto check silently. - Follows EIP-1102 specification.