Web3AuthConnector.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Web3Auth = void 0;
  6. var _ethers = require("ethers");
  7. var _verifyChainId = _interopRequireDefault(require("../utils/verifyChainId"));
  8. var _AbstractWeb3Connector = _interopRequireDefault(require("./AbstractWeb3Connector"));
  9. function _interopRequireDefault(obj) {
  10. return obj && obj.__esModule ? obj : {
  11. default: obj
  12. };
  13. }
  14. function _defineProperty(obj, key, value) {
  15. if (key in obj) {
  16. Object.defineProperty(obj, key, {
  17. value: value,
  18. enumerable: true,
  19. configurable: true,
  20. writable: true
  21. });
  22. } else {
  23. obj[key] = value;
  24. }
  25. return obj;
  26. }
  27. class Web3Auth extends _AbstractWeb3Connector.default {
  28. constructor(...args) {
  29. super(...args);
  30. _defineProperty(this, "type", 'web3Auth');
  31. _defineProperty(this, "activate", async ({
  32. chainId = '0x1',
  33. clientId,
  34. theme,
  35. appLogo,
  36. loginMethodsOrder
  37. } = {}) => {
  38. // Checking that all params are given
  39. if (!clientId) {
  40. throw new Error('"clientId" not provided, please provide clientId');
  41. } // Initalizing Web3Auth and getting constants
  42. let Web3Auth;
  43. try {
  44. var _require;
  45. Web3Auth = (_require = require('@web3auth/web3auth')) === null || _require === void 0 ? void 0 : _require.Web3Auth;
  46. } catch (_unused) {// Do Nothing Individual Checks are done below
  47. } // Check if user is using CDN to import
  48. if (!Web3Auth) {
  49. var _window, _window$Web3auth;
  50. Web3Auth = (_window = window) === null || _window === void 0 ? void 0 : (_window$Web3auth = _window.Web3auth) === null || _window$Web3auth === void 0 ? void 0 : _window$Web3auth.Web3Auth;
  51. } // Error checking for if library is not installed
  52. if (!Web3Auth) {
  53. throw new Error('"@web3auth/web3auth" not installed, please install');
  54. } // Build config
  55. const ethChainConfig = {
  56. chainNamespace: 'eip155',
  57. chainId: (0, _verifyChainId.default)(chainId)
  58. }; // Build Web3Auth
  59. let web3auth;
  60. try {
  61. web3auth = new Web3Auth({
  62. chainConfig: ethChainConfig,
  63. uiConfig: {
  64. theme: theme !== null && theme !== void 0 ? theme : 'dark',
  65. appLogo: appLogo !== null && appLogo !== void 0 ? appLogo : 'https://moralis.io/wp-content/uploads/2021/05/moralisWhiteLogo.svg',
  66. loginMethodsOrder
  67. },
  68. clientId: clientId
  69. });
  70. } catch (_unused2) {// Do Nothing error checked below
  71. }
  72. if (!web3auth) {
  73. throw new Error('Could not connect via Web3Auth, error during initializing Web3Auth');
  74. } // Authenticate
  75. await web3auth.initModal();
  76. let provider = null;
  77. try {
  78. provider = await web3auth.connect();
  79. } catch (_unused3) {// Do nothing, check next line down
  80. }
  81. if (!provider) {
  82. throw new Error('Could not connect via Web3Auth, error in connecting to provider');
  83. } // Gather User data
  84. try {
  85. var _web3auth, _web3auth2, _web3auth3;
  86. const isSocialLogin = (_web3auth = web3auth) !== null && _web3auth !== void 0 && _web3auth.provider ? false : true;
  87. const ether = new _ethers.ethers.providers.Web3Provider((_web3auth2 = web3auth) !== null && _web3auth2 !== void 0 && _web3auth2.provider ? web3auth.provider : web3auth);
  88. const signer = ether.getSigner();
  89. const values = await Promise.all([ether.getNetwork(), signer.getAddress()]);
  90. const providerChainId = values[0].chainId;
  91. this.account = values[1].toLocaleLowerCase();
  92. this.chainId = `0x${providerChainId.toString(16)}`;
  93. this.provider = isSocialLogin ? ether : (_web3auth3 = web3auth) === null || _web3auth3 === void 0 ? void 0 : _web3auth3.provider;
  94. this.web3Instance = web3auth;
  95. this.subscribeToEvents(this.provider);
  96. return {
  97. chainId: this.chainId,
  98. account: this.account,
  99. provider: this.provider
  100. };
  101. } catch (_unused4) {
  102. throw new Error('Could not connect via Web3Auth, error while authenticating');
  103. }
  104. });
  105. _defineProperty(this, "deactivate", async () => {
  106. this.unsubscribeToEvents(this.provider);
  107. if (this.web3Instance) {
  108. await this.web3Instance.logout();
  109. }
  110. this.account = null;
  111. this.chainId = null;
  112. this.provider = null;
  113. });
  114. }
  115. }
  116. exports.Web3Auth = Web3Auth;