@unique-nft/api
Version:
Definitely typed JS API for the Unique Network blockchain
35 lines (31 loc) • 1.05 kB
text/typescript
export const requestAccounts = async (): Promise<Array<string>> => {
if (typeof window === 'undefined') {
throw new Error(`Cannot access window. It's necessary`)
}
if (!window.ethereum) {
throw new Error('No window.ethereum found')
}
let accounts: string[] = []
try {
accounts = await window.ethereum.request({method: 'eth_requestAccounts'})
} catch (err: any) {
if (err.code === 4001) {
// EIP-1193 userRejectedRequest error
// If this happens, the user rejected the connection request.
throw new Error('User rejected')
}
throw err
}
return accounts
}
interface ISafeGetAccountsResult {
extensionFound: boolean
accounts: string[]
}
export const safeGetAccounts = async (): Promise<ISafeGetAccountsResult> => {
if (typeof window === 'undefined' || !window.ethereum) {
return {extensionFound: false, accounts: []}
}
const accounts: string[] = await window.ethereum.request({method: 'eth_accounts'})
return {extensionFound: true, accounts}
}