UNPKG

@thirdweb-dev/wallets

Version:

<p align="center"> <br /> <a href="https://thirdweb.com"><img src="https://github.com/thirdweb-dev/js/blob/main/legacy_packages/sdk/logo.svg?raw=true" width="200" alt=""/></a> <br /> </p> <h1 align="center">thirdweb Wallet SDK</h1> <p align="center"> <a h

129 lines (119 loc) 4.71 kB
import { _ as _defineProperty } from '../../../../dist/defineProperty-350fc508.browser.esm.js'; import { a as ConnectorNotFoundError, U as UserRejectedRequestError, R as ResourceUnavailableError } from '../../../../dist/errors-9edc08c8.browser.esm.js'; import { w as walletIds } from '../../../../dist/walletIds-dff6dced.browser.esm.js'; import { InjectedConnector } from '../../injected/dist/thirdweb-dev-wallets-evm-connectors-injected.browser.esm.js'; import { utils } from 'ethers'; import { g as getInjectedOneKeyProvider } from '../../../../dist/getInjectedOneKeyProvider-b8c89caf.browser.esm.js'; import '../../../../dist/WagmiConnector-2f14002d.browser.esm.js'; import '@thirdweb-dev/chains'; import 'eventemitter3'; import '../../../../dist/assertWindowEthereum-88295886.browser.esm.js'; import '../../../../dist/url-a45219bd.browser.esm.js'; import '../../../../dist/normalizeChainId-1fb9aedf.browser.esm.js'; class OneKeyConnector extends InjectedConnector { constructor(arg) { const defaultOptions = { name: "OneKey Wallet", shimDisconnect: true, shimChainChangedDisconnect: true, getProvider: getInjectedOneKeyProvider }; const options = { ...defaultOptions, ...arg.options }; super({ chains: arg.chains, options, connectorStorage: arg.connectorStorage }); _defineProperty(this, "id", walletIds.oneKey); } /** * Connect to injected OneKeyWallet provider */ async connect() { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; try { const provider = await this.getProvider(); if (!provider) { throw new ConnectorNotFoundError(); } this.setupListeners(); // emit "connecting" event this.emit("message", { type: "connecting" }); // Attempt to show wallet select prompt with `wallet_requestPermissions` when // `shimDisconnect` is active and account is in disconnected state (flag in storage) let account = null; if (this.options?.shimDisconnect && !Boolean(this.connectorStorage.getItem(this.shimDisconnectKey))) { account = await this.getAccount().catch(() => null); const isConnected = !!account; if (isConnected) { // Attempt to show another prompt for selecting wallet if already connected try { await provider.request({ method: "wallet_requestPermissions", params: [{ eth_accounts: {} }] }); } catch (error) { // Not all injected providers support `wallet_requestPermissions` (e.g. iOS). // Only bubble up error if user rejects request if (this.isUserRejectedRequestError(error)) { throw new UserRejectedRequestError(error); } } } } // if account is not already set, request accounts and use the first account if (!account) { const accounts = await provider.request({ method: "eth_requestAccounts" }); account = utils.getAddress(accounts[0]); } // get currently connected chainId let connectedChainId = await this.getChainId(); // check if connected chain is unsupported let isUnsupported = this.isChainUnsupported(connectedChainId); // if chainId is given, but does not match the currently connected chainId, switch to the given chainId if (options.chainId && connectedChainId !== options.chainId) { try { await this.switchChain(options.chainId); // recalculate the chainId and isUnsupported connectedChainId = options.chainId; isUnsupported = this.isChainUnsupported(options.chainId); } catch (e) { console.error(`Could not switch to chain id : ${options.chainId}`, e); } } // if shimDisconnect is enabled if (this.options?.shimDisconnect) { // add shimDisconnectKey in storage - this signals that connector is "connected" await this.connectorStorage.setItem(this.shimDisconnectKey, "true"); } const connectionInfo = { chain: { id: connectedChainId, unsupported: isUnsupported }, provider: provider, account }; this.emit("connect", connectionInfo); return connectionInfo; } catch (error) { if (this.isUserRejectedRequestError(error)) { throw new UserRejectedRequestError(error); } if (error.code === -32002) { throw new ResourceUnavailableError(error); } throw error; } } } export { OneKeyConnector };