AbstractWeb3Connector.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _events = _interopRequireDefault(require("events"));
  7. var _verifyChainId = _interopRequireDefault(require("../utils/verifyChainId"));
  8. var _events2 = require("./events");
  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. /**
  28. * Abstract connector to connect EIP-1193 providers to Moralis
  29. *
  30. * It should implement at least:
  31. * - activate()
  32. * - Emit ConnectorEvent.CHAIN_CHANGED when the chain has changed (if possible)
  33. * - Emit ConnectorEvent.ACCOUNT_CHANGED when the account has changed (if possible)
  34. * - type: a name to identify
  35. * - network: the network type that is used (eg. 'evm')
  36. */
  37. class AbstractWeb3Connector extends _events.default {
  38. constructor() {
  39. super();
  40. _defineProperty(this, "type", 'abstract');
  41. _defineProperty(this, "network", 'evm');
  42. _defineProperty(this, "account", null);
  43. _defineProperty(this, "chainId", null);
  44. this.handleAccountsChanged = this.handleAccountsChanged.bind(this);
  45. this.handleChainChanged = this.handleChainChanged.bind(this);
  46. this.handleConnect = this.handleConnect.bind(this);
  47. this.handleDisconnect = this.handleDisconnect.bind(this);
  48. }
  49. subscribeToEvents(provider) {
  50. if (provider && provider.on) {
  51. provider.on(_events2.EthereumEvents.CHAIN_CHANGED, this.handleChainChanged);
  52. provider.on(_events2.EthereumEvents.ACCOUNTS_CHANGED, this.handleAccountsChanged);
  53. provider.on(_events2.EthereumEvents.CONNECT, this.handleConnect);
  54. provider.on(_events2.EthereumEvents.DISCONNECT, this.handleDisconnect);
  55. }
  56. }
  57. unsubscribeToEvents(provider) {
  58. if (provider && provider.removeListener) {
  59. provider.removeListener(_events2.EthereumEvents.CHAIN_CHANGED, this.handleChainChanged);
  60. provider.removeListener(_events2.EthereumEvents.ACCOUNTS_CHANGED, this.handleAccountsChanged);
  61. provider.removeListener(_events2.EthereumEvents.CONNECT, this.handleConnect);
  62. provider.removeListener(_events2.EthereumEvents.DISCONNECT, this.handleDisconnect);
  63. }
  64. }
  65. /**
  66. * Activates the provider.
  67. * Should returns an object with:
  68. * - provider: A valid EIP-1193 provider
  69. * - chainId(optional): the chainId that has been connected to (in hex format)
  70. * - account(optional): the address that is connected to the provider
  71. */
  72. async activate() {
  73. throw new Error('Not implemented: activate()');
  74. }
  75. /**
  76. * Updates account and emit event, on EIP-1193 accountsChanged events
  77. */
  78. handleAccountsChanged(accounts) {
  79. const account = accounts && accounts[0] ? accounts[0].toLowerCase() : null;
  80. this.account = account;
  81. this.emit(_events2.ConnectorEvents.ACCOUNT_CHANGED, account);
  82. }
  83. /**
  84. * Updates chainId and emit event, on EIP-1193 accountsChanged events
  85. */
  86. handleChainChanged(chainId) {
  87. const newChainId = (0, _verifyChainId.default)(chainId);
  88. this.chainId = newChainId;
  89. this.emit(_events2.ConnectorEvents.CHAIN_CHANGED, newChainId);
  90. }
  91. handleConnect(connectInfo) {
  92. this.emit(_events2.ConnectorEvents.CONNECT, connectInfo);
  93. }
  94. handleDisconnect(error) {
  95. this.emit(_events2.ConnectorEvents.DISCONNECT, error);
  96. }
  97. /**
  98. * Cleans all active listners, connections and stale references
  99. */
  100. async deactivate() {
  101. this.unsubscribeToEvents(this.provider);
  102. this.account = null;
  103. this.chainId = null;
  104. }
  105. }
  106. var _default = AbstractWeb3Connector;
  107. exports.default = _default;