UNPKG

@web3auth/web3auth-wagmi-connector

Version:
284 lines (268 loc) 11.3 kB
/******/ (() => { // webpackBootstrap /******/ var __webpack_modules__ = ({ /***/ 497: /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { "use strict"; // EXPORTS __webpack_require__.d(__webpack_exports__, { T: () => (/* binding */ Web3AuthConnector) }); ;// CONCATENATED MODULE: external "@wagmi/core" const core_namespaceObject = require("@wagmi/core"); ;// CONCATENATED MODULE: external "@web3auth/base" const base_namespaceObject = require("@web3auth/base"); ;// CONCATENATED MODULE: external "viem" const external_viem_namespaceObject = require("viem"); ;// CONCATENATED MODULE: ./src/lib/connector.ts const { ADAPTER_STATUS, CHAIN_NAMESPACES, WALLET_ADAPTERS, log } = base_namespaceObject; function isIWeb3AuthModal(obj) { return typeof obj.initModal !== "undefined"; } function Web3AuthConnector(parameters) { let walletProvider = null; const { web3AuthInstance, loginParams, modalConfig, id, name, type } = parameters; return (0,core_namespaceObject.createConnector)(config => ({ id: id || "web3auth", name: name || "Web3Auth", type: type || "Web3Auth", async connect({ chainId } = {}) { try { config.emitter.emit("message", { type: "connecting" }); const provider = await this.getProvider(); provider.on("accountsChanged", this.onAccountsChanged); provider.on("chainChanged", this.onChainChanged); provider.on("disconnect", this.onDisconnect.bind(this)); if (!web3AuthInstance.connected) { if (isIWeb3AuthModal(web3AuthInstance)) { await web3AuthInstance.connect(); } else if (loginParams) { await web3AuthInstance.connectTo(WALLET_ADAPTERS.AUTH, loginParams); } else { log.error("please provide valid loginParams when using @web3auth/no-modal"); throw new external_viem_namespaceObject.UserRejectedRequestError("please provide valid loginParams when using @web3auth/no-modal"); } } let currentChainId = await this.getChainId(); if (chainId && currentChainId !== chainId) { var _chain$id; const chain = await this.switchChain({ chainId }).catch(error => { if (error.code === external_viem_namespaceObject.UserRejectedRequestError.code) throw error; return { id: currentChainId }; }); currentChainId = (_chain$id = chain === null || chain === void 0 ? void 0 : chain.id) !== null && _chain$id !== void 0 ? _chain$id : currentChainId; } const accounts = await this.getAccounts(); return { accounts, chainId: currentChainId }; } catch (error) { log.error("error while connecting", error); this.onDisconnect(); throw new external_viem_namespaceObject.UserRejectedRequestError("Something went wrong"); } }, async getAccounts() { const provider = await this.getProvider(); return (await provider.request({ method: "eth_accounts" })).map(x => (0,external_viem_namespaceObject.getAddress)(x)); }, async getChainId() { const provider = await this.getProvider(); const chainId = await provider.request({ method: "eth_chainId" }); return Number(chainId); }, async getProvider() { if (walletProvider) { return walletProvider; } if (web3AuthInstance.status === ADAPTER_STATUS.NOT_READY) { if (isIWeb3AuthModal(web3AuthInstance)) { await web3AuthInstance.initModal({ modalConfig }); } else if (loginParams) { await web3AuthInstance.init(); } else { log.error("please provide valid loginParams when using @web3auth/no-modal"); throw new external_viem_namespaceObject.UserRejectedRequestError("please provide valid loginParams when using @web3auth/no-modal"); } } walletProvider = web3AuthInstance.provider; return walletProvider; }, async isAuthorized() { try { const accounts = await this.getAccounts(); return !!accounts.length; } catch { return false; } }, async switchChain({ chainId }) { try { var _chain$blockExplorers, _chain$nativeCurrency, _chain$nativeCurrency2, _chain$nativeCurrency3, _chain$nativeCurrency4, _chain$nativeCurrency5; const chain = config.chains.find(x => x.id === chainId); if (!chain) throw new external_viem_namespaceObject.SwitchChainError(new core_namespaceObject.ChainNotConfiguredError()); await web3AuthInstance.addChain({ chainNamespace: CHAIN_NAMESPACES.EIP155, chainId: `0x${chain.id.toString(16)}`, rpcTarget: chain.rpcUrls.default.http[0], displayName: chain.name, blockExplorerUrl: ((_chain$blockExplorers = chain.blockExplorers) === null || _chain$blockExplorers === void 0 ? void 0 : _chain$blockExplorers.default.url) || "", ticker: ((_chain$nativeCurrency = chain.nativeCurrency) === null || _chain$nativeCurrency === void 0 ? void 0 : _chain$nativeCurrency.symbol) || "ETH", tickerName: ((_chain$nativeCurrency2 = chain.nativeCurrency) === null || _chain$nativeCurrency2 === void 0 ? void 0 : _chain$nativeCurrency2.name) || "Ethereum", decimals: ((_chain$nativeCurrency3 = chain.nativeCurrency) === null || _chain$nativeCurrency3 === void 0 ? void 0 : _chain$nativeCurrency3.decimals) || 18, logo: (_chain$nativeCurrency4 = chain.nativeCurrency) !== null && _chain$nativeCurrency4 !== void 0 && _chain$nativeCurrency4.symbol ? `https://images.toruswallet.io/${(_chain$nativeCurrency5 = chain.nativeCurrency) === null || _chain$nativeCurrency5 === void 0 ? void 0 : _chain$nativeCurrency5.symbol.toLowerCase()}.svg` : "https://images.toruswallet.io/eth.svg" }); log.info("Chain Added: ", chain.name); await web3AuthInstance.switchChain({ chainId: `0x${chain.id.toString(16)}` }); log.info("Chain Switched to ", chain.name); config.emitter.emit("change", { chainId }); return chain; } catch (error) { log.error("Error: Cannot change chain", error); throw new external_viem_namespaceObject.SwitchChainError(error); } }, async disconnect() { await web3AuthInstance.logout(); const provider = await this.getProvider(); provider.removeListener("accountsChanged", this.onAccountsChanged); provider.removeListener("chainChanged", this.onChainChanged); }, onAccountsChanged(accounts) { if (accounts.length === 0) config.emitter.emit("disconnect");else config.emitter.emit("change", { accounts: accounts.map(x => (0,external_viem_namespaceObject.getAddress)(x)) }); }, onChainChanged(chain) { const chainId = Number(chain); config.emitter.emit("change", { chainId }); }, onDisconnect() { config.emitter.emit("disconnect"); } })); } /***/ }), /***/ 282: /***/ (() => { /***/ }) /******/ }); /************************************************************************/ /******/ // The module cache /******/ var __webpack_module_cache__ = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ var cachedModule = __webpack_module_cache__[moduleId]; /******/ if (cachedModule !== undefined) { /******/ return cachedModule.exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = __webpack_module_cache__[moduleId] = { /******/ // no module.id needed /******/ // no module.loaded needed /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /************************************************************************/ /******/ /* webpack/runtime/compat get default export */ /******/ (() => { /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = (module) => { /******/ var getter = module && module.__esModule ? /******/ () => (module['default']) : /******/ () => (module); /******/ __webpack_require__.d(getter, { a: getter }); /******/ return getter; /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/define property getters */ /******/ (() => { /******/ // define getter functions for harmony exports /******/ __webpack_require__.d = (exports, definition) => { /******/ for(var key in definition) { /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); /******/ } /******/ } /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/hasOwnProperty shorthand */ /******/ (() => { /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) /******/ })(); /******/ /******/ /* webpack/runtime/make namespace object */ /******/ (() => { /******/ // define __esModule on exports /******/ __webpack_require__.r = (exports) => { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ })(); /******/ /************************************************************************/ var __webpack_exports__ = {}; // This entry need to be wrapped in an IIFE because it need to be in strict mode. (() => { "use strict"; __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ Web3AuthConnector: () => (/* reexport safe */ _lib_connector__WEBPACK_IMPORTED_MODULE_0__.T) /* harmony export */ }); /* harmony import */ var _lib_connector__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(497); /* harmony import */ var _lib_interfaces__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(282); /* harmony import */ var _lib_interfaces__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_lib_interfaces__WEBPACK_IMPORTED_MODULE_1__); /* harmony reexport (unknown) */ var __WEBPACK_REEXPORT_OBJECT__ = {}; /* harmony reexport (unknown) */ for(const __WEBPACK_IMPORT_KEY__ in _lib_interfaces__WEBPACK_IMPORTED_MODULE_1__) if(["default","Web3AuthConnector"].indexOf(__WEBPACK_IMPORT_KEY__) < 0) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = () => _lib_interfaces__WEBPACK_IMPORTED_MODULE_1__[__WEBPACK_IMPORT_KEY__] /* harmony reexport (unknown) */ __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__); })(); module.exports = __webpack_exports__; /******/ })() ;