WalletConnectWeb3Connector.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.WalletConnectEvent = void 0;
  6. var _verifyChainId = _interopRequireDefault(require("../utils/verifyChainId"));
  7. var _AbstractWeb3Connector = _interopRequireDefault(require("./AbstractWeb3Connector"));
  8. var _events = require("./events");
  9. var _MoralisRpcs = require("./MoralisRpcs");
  10. function _interopRequireDefault(obj) {
  11. return obj && obj.__esModule ? obj : {
  12. default: obj
  13. };
  14. }
  15. function _defineProperty(obj, key, value) {
  16. if (key in obj) {
  17. Object.defineProperty(obj, key, {
  18. value: value,
  19. enumerable: true,
  20. configurable: true,
  21. writable: true
  22. });
  23. } else {
  24. obj[key] = value;
  25. }
  26. return obj;
  27. }
  28. const WalletConnectEvent = Object.freeze({
  29. ACCOUNTS_CHANGED: 'accountsChanged',
  30. CHAIN_CHANGED: 'chainChanged',
  31. DISCONNECT: 'disconnect'
  32. });
  33. /**
  34. * Connector to connect an WalletConenct provider to Moralis
  35. * Note: this assumes using WalletConnect v1
  36. * // TODO: support WalletConnect v2
  37. */
  38. exports.WalletConnectEvent = WalletConnectEvent;
  39. class WalletConnectWeb3Connector extends _AbstractWeb3Connector.default {
  40. constructor(...args) {
  41. super(...args);
  42. _defineProperty(this, "type", 'WalletConnect');
  43. }
  44. async activate({
  45. chainId: providedChainId,
  46. mobileLinks,
  47. newSession
  48. } = {}) {
  49. // Log out of any previous sessions
  50. if (newSession) {
  51. this.cleanup();
  52. }
  53. if (!this.provider) {
  54. let WalletConnectProvider;
  55. const config = {
  56. rpc: (0, _MoralisRpcs.getMoralisRpcs)('WalletConnect'),
  57. chainId: providedChainId,
  58. qrcodeModalOptions: {
  59. mobileLinks
  60. }
  61. };
  62. try {
  63. var _require;
  64. WalletConnectProvider = (_require = require('@walletconnect/web3-provider')) === null || _require === void 0 ? void 0 : _require.default;
  65. } catch (error) {// Do nothing. User might not need walletconnect
  66. }
  67. if (!WalletConnectProvider) {
  68. var _window, _window$WalletConnect;
  69. WalletConnectProvider = (_window = window) === null || _window === void 0 ? void 0 : (_window$WalletConnect = _window.WalletConnectProvider) === null || _window$WalletConnect === void 0 ? void 0 : _window$WalletConnect.default;
  70. }
  71. if (!WalletConnectProvider) {
  72. throw new Error('Cannot enable via WalletConnect: dependency "@walletconnect/web3-provider" is missing');
  73. }
  74. if (typeof WalletConnectProvider === 'function') {
  75. this.provider = new WalletConnectProvider(config);
  76. } else {
  77. this.provider = new window.WalletConnectProvider(config);
  78. }
  79. }
  80. if (!this.provider) {
  81. throw new Error('Could not connect via WalletConnect, error in connecting to provider');
  82. }
  83. const accounts = await this.provider.enable();
  84. const account = accounts[0].toLowerCase();
  85. const {
  86. chainId
  87. } = this.provider;
  88. const verifiedChainId = (0, _verifyChainId.default)(chainId);
  89. this.account = account;
  90. this.chainId = verifiedChainId;
  91. this.subscribeToEvents(this.provider);
  92. return {
  93. provider: this.provider,
  94. account,
  95. chainId: verifiedChainId
  96. };
  97. } // Cleanup old sessions
  98. cleanup() {
  99. try {
  100. if (window) {
  101. window.localStorage.removeItem('walletconnect');
  102. }
  103. } catch (error) {// Do nothing
  104. }
  105. }
  106. async deactivate() {
  107. this.unsubscribeToEvents(this.provider);
  108. if (this.provider) {
  109. try {
  110. await this.provider.close();
  111. } catch (_unused) {// Do nothing
  112. }
  113. }
  114. this.account = null;
  115. this.chainId = null;
  116. this.provider = null;
  117. }
  118. }
  119. var _default = WalletConnectWeb3Connector;
  120. exports.default = _default;