Web3AuthConnector.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Web3Auth = void 0;
  6. var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  7. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  8. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  9. var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
  10. var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
  11. var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
  12. var _ethers = require("ethers");
  13. var _verifyChainId = _interopRequireDefault(require("../utils/verifyChainId"));
  14. var _AbstractWeb3Connector = _interopRequireDefault(require("./AbstractWeb3Connector"));
  15. function _createSuper(Derived) {
  16. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  17. return function () {
  18. var Super = (0, _getPrototypeOf2.default)(Derived),
  19. result;
  20. if (hasNativeReflectConstruct) {
  21. var NewTarget = (0, _getPrototypeOf2.default)(this).constructor;
  22. result = Reflect.construct(Super, arguments, NewTarget);
  23. } else {
  24. result = Super.apply(this, arguments);
  25. }
  26. return (0, _possibleConstructorReturn2.default)(this, result);
  27. };
  28. }
  29. function _isNativeReflectConstruct() {
  30. if (typeof Reflect === "undefined" || !Reflect.construct) return false;
  31. if (Reflect.construct.sham) return false;
  32. if (typeof Proxy === "function") return true;
  33. try {
  34. Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
  35. return true;
  36. } catch (e) {
  37. return false;
  38. }
  39. }
  40. var Web3Auth = function (_AbstractWeb3Connecto) {
  41. (0, _inherits2.default)(Web3Auth, _AbstractWeb3Connecto);
  42. var _super = _createSuper(Web3Auth);
  43. function Web3Auth() {
  44. var _this;
  45. (0, _classCallCheck2.default)(this, Web3Auth);
  46. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  47. args[_key] = arguments[_key];
  48. }
  49. _this = _super.call.apply(_super, [this].concat(args));
  50. _this.type = 'web3Auth';
  51. _this.activate = function () {
  52. var _ref,
  53. _ref$chainId,
  54. chainId,
  55. clientId,
  56. theme,
  57. appLogo,
  58. loginMethodsOrder,
  59. _Web3Auth,
  60. _require,
  61. _window,
  62. _window$Web3auth,
  63. ethChainConfig,
  64. web3auth,
  65. provider,
  66. _web3auth,
  67. _web3auth2,
  68. _web3auth3,
  69. isSocialLogin,
  70. ether,
  71. signer,
  72. values,
  73. providerChainId,
  74. _args = arguments;
  75. return _regenerator.default.async(function (_context) {
  76. while (1) {
  77. switch (_context.prev = _context.next) {
  78. case 0:
  79. _ref = _args.length > 0 && _args[0] !== undefined ? _args[0] : {}, _ref$chainId = _ref.chainId, chainId = _ref$chainId === void 0 ? '0x1' : _ref$chainId, clientId = _ref.clientId, theme = _ref.theme, appLogo = _ref.appLogo, loginMethodsOrder = _ref.loginMethodsOrder;
  80. if (clientId) {
  81. _context.next = 3;
  82. break;
  83. }
  84. throw new Error('"clientId" not provided, please provide clientId');
  85. case 3:
  86. try {
  87. _Web3Auth = (_require = require('@web3auth/web3auth')) == null ? void 0 : _require.Web3Auth;
  88. } catch (_unused) {}
  89. if (!_Web3Auth) {
  90. _Web3Auth = (_window = window) == null ? void 0 : (_window$Web3auth = _window.Web3auth) == null ? void 0 : _window$Web3auth.Web3Auth;
  91. }
  92. if (_Web3Auth) {
  93. _context.next = 7;
  94. break;
  95. }
  96. throw new Error('"@web3auth/web3auth" not installed, please install');
  97. case 7:
  98. ethChainConfig = {
  99. chainNamespace: 'eip155',
  100. chainId: (0, _verifyChainId.default)(chainId)
  101. };
  102. try {
  103. web3auth = new _Web3Auth({
  104. chainConfig: ethChainConfig,
  105. uiConfig: {
  106. theme: theme != null ? theme : 'dark',
  107. appLogo: appLogo != null ? appLogo : 'https://moralis.io/wp-content/uploads/2021/05/moralisWhiteLogo.svg',
  108. loginMethodsOrder: loginMethodsOrder
  109. },
  110. clientId: clientId
  111. });
  112. } catch (_unused2) {}
  113. if (web3auth) {
  114. _context.next = 11;
  115. break;
  116. }
  117. throw new Error('Could not connect via Web3Auth, error during initializing Web3Auth');
  118. case 11:
  119. _context.next = 13;
  120. return _regenerator.default.awrap(web3auth.initModal());
  121. case 13:
  122. provider = null;
  123. _context.prev = 14;
  124. _context.next = 17;
  125. return _regenerator.default.awrap(web3auth.connect());
  126. case 17:
  127. provider = _context.sent;
  128. _context.next = 22;
  129. break;
  130. case 20:
  131. _context.prev = 20;
  132. _context.t0 = _context["catch"](14);
  133. case 22:
  134. if (provider) {
  135. _context.next = 24;
  136. break;
  137. }
  138. throw new Error('Could not connect via Web3Auth, error in connecting to provider');
  139. case 24:
  140. _context.prev = 24;
  141. isSocialLogin = (_web3auth = web3auth) != null && _web3auth.provider ? false : true;
  142. ether = new _ethers.ethers.providers.Web3Provider((_web3auth2 = web3auth) != null && _web3auth2.provider ? web3auth.provider : web3auth);
  143. signer = ether.getSigner();
  144. _context.next = 30;
  145. return _regenerator.default.awrap(Promise.all([ether.getNetwork(), signer.getAddress()]));
  146. case 30:
  147. values = _context.sent;
  148. providerChainId = values[0].chainId;
  149. _this.account = values[1].toLocaleLowerCase();
  150. _this.chainId = "0x" + providerChainId.toString(16);
  151. _this.provider = isSocialLogin ? ether : (_web3auth3 = web3auth) == null ? void 0 : _web3auth3.provider;
  152. _this.web3Instance = web3auth;
  153. _this.subscribeToEvents(_this.provider);
  154. return _context.abrupt("return", {
  155. chainId: _this.chainId,
  156. account: _this.account,
  157. provider: _this.provider
  158. });
  159. case 40:
  160. _context.prev = 40;
  161. _context.t1 = _context["catch"](24);
  162. throw new Error('Could not connect via Web3Auth, error while authenticating');
  163. case 43:
  164. case "end":
  165. return _context.stop();
  166. }
  167. }
  168. }, null, null, [[14, 20], [24, 40]], Promise);
  169. };
  170. _this.deactivate = function () {
  171. return _regenerator.default.async(function (_context2) {
  172. while (1) {
  173. switch (_context2.prev = _context2.next) {
  174. case 0:
  175. _this.unsubscribeToEvents(_this.provider);
  176. if (!_this.web3Instance) {
  177. _context2.next = 4;
  178. break;
  179. }
  180. _context2.next = 4;
  181. return _regenerator.default.awrap(_this.web3Instance.logout());
  182. case 4:
  183. _this.account = null;
  184. _this.chainId = null;
  185. _this.provider = null;
  186. case 7:
  187. case "end":
  188. return _context2.stop();
  189. }
  190. }
  191. }, null, null, null, Promise);
  192. };
  193. return _this;
  194. }
  195. return (0, _createClass2.default)(Web3Auth);
  196. }(_AbstractWeb3Connector.default);
  197. exports.Web3Auth = Web3Auth;