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

134 lines (122 loc) 5.04 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var defineProperty = require('../../../../dist/defineProperty-b749763b.cjs.prod.js'); var errors = require('../../../../dist/errors-8b161b04.cjs.prod.js'); var walletIds = require('../../../../dist/walletIds-6ed32bf4.cjs.prod.js'); var evm_connectors_injected_dist_thirdwebDevWalletsEvmConnectorsInjected = require('../../injected/dist/thirdweb-dev-wallets-evm-connectors-injected.cjs.prod.js'); var ethers = require('ethers'); var getInjectedRainbowProvider = require('../../../../dist/getInjectedRainbowProvider-62df0752.cjs.prod.js'); require('../../../../dist/WagmiConnector-17577fd3.cjs.prod.js'); require('@thirdweb-dev/chains'); require('eventemitter3'); require('../../../../dist/assertWindowEthereum-941010de.cjs.prod.js'); require('../../../../dist/url-8b7f19db.cjs.prod.js'); require('../../../../dist/normalizeChainId-dc975fa1.cjs.prod.js'); class RainbowConnector extends evm_connectors_injected_dist_thirdwebDevWalletsEvmConnectorsInjected.InjectedConnector { constructor(arg) { const defaultOptions = { name: "Rainbow Wallet", shimDisconnect: true, shimChainChangedDisconnect: true, getProvider: getInjectedRainbowProvider.getInjectedRainbowProvider }; const options = { ...defaultOptions, ...arg.options }; super({ chains: arg.chains, options, connectorStorage: arg.connectorStorage }); defineProperty._defineProperty(this, "id", walletIds.walletIds.rainbow); this._UNSTABLE_shimOnConnectSelectAccount = options.UNSTABLE_shimOnConnectSelectAccount; } /** * Connect to injected Rainbow provider */ async connect() { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; try { const provider = await this.getProvider(); if (!provider) { throw new errors.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._UNSTABLE_shimOnConnectSelectAccount && 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 Rainbow injected providers support `wallet_requestPermissions` (e.g. Rainbow iOS). // Only bubble up error if user rejects request if (this.isUserRejectedRequestError(error)) { throw new errors.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 = ethers.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 errors.UserRejectedRequestError(error); } if (error.code === -32002) { throw new errors.ResourceUnavailableError(error); } throw error; } } } exports.RainbowConnector = RainbowConnector;