사용자에게서 서명을 요청하거나 사용자가 트랜잭션을 승인하도록하려면 eth_requestAccounts RPC 방법을 사용하여 사용자의 계정에 액세스해야 합니다.
const accounts = await window.biport.request({ method: 'eth_requestAccounts' })
.catch((err) => {
if (err.code === 4001) {
// EIP-1193 userRejectedRequest error
// If this happens, the user rejected the connection request.
console.log('Please connect to Biport.');
} else {
console.error(err);
}
});
console.log('Connected Account:', accounts[0]);
사용자 계정 다루기
eth_accounts RPC 메소드를 사용하여 사용자 계정을 다룰 수 있습니다. provider에서 accountsChanged 이벤트를 수신하여 사용자 계정이 변경될 때 알림을 받을 수 있습니다.
let currentAccount = null;
window.biport.request({ method: 'eth_accounts' })
.then(handleAccountsChanged)
.catch((err) => {
// Some unexpected error.
// For backwards compatibility reasons, if no accounts are available,
// eth_accounts returns an empty array.
console.error(err);
});
// Note that this event is emitted on page load.
// If the array of accounts is non-empty, you're already
// connected.
window.biport.on('accountsChanged', handleAccountsChanged);
// eth_accounts always returns an array.
function handleAccountsChanged(accounts) {
if (accounts.length === 0) {
// Biport is locked or the user has not connected any accounts.
console.log('Please connect to Biport.');
} else if (accounts[0] !== currentAccount) {
// Reload your interface with accounts[0].
currentAccount = accounts[0];
// Update the account displayed (see the HTML for the connect button)
showAccount.innerHTML = currentAccount;
}
}