@dynamic-labs/ethereum 4.9.1 → 4.9.2-preview.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,189 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../../_virtual/_tslib.js';
3
+ import EthereumProvider from '@walletconnect/ethereum-provider';
4
+ import { performPlatformSpecificConnectionMethod } from '@dynamic-labs/wallet-connector-core';
5
+ import { parseIntSafe, DynamicError } from '@dynamic-labs/utils';
6
+ import { logger } from '../../utils/logger.js';
7
+
8
+ var _a;
9
+ class WalletConnectProvider {
10
+ constructor() {
11
+ throw new Error('WalletConnectProvider is not instantiable');
12
+ }
13
+ static getMappedChainsByPreferredOrder() {
14
+ const allChains = _a.enabledNetworks.map((network) => `eip155:${network.chainId}`);
15
+ const reorderedChains = _a.preferredChains.filter((chain) => allChains.includes(chain));
16
+ const remainingChains = allChains.filter((chain) => !_a.preferredChains.includes(chain));
17
+ return [...reorderedChains, ...remainingChains].map((chain) => Number(chain.split(':')[1]));
18
+ }
19
+ }
20
+ _a = WalletConnectProvider;
21
+ WalletConnectProvider.isInitialized = false;
22
+ WalletConnectProvider.enabledNetworks = [];
23
+ WalletConnectProvider.preferredChains = [];
24
+ WalletConnectProvider.evmNetworkRpcMap = {};
25
+ WalletConnectProvider.eventListenersSetup = false;
26
+ WalletConnectProvider.accountChangedHandler = () => { };
27
+ WalletConnectProvider.chainChangedHandler = () => { };
28
+ WalletConnectProvider.disconnectHandler = () => { };
29
+ /**
30
+ * Initializes the provider. This method should only be called once.
31
+ * Does not start a connection.
32
+ */
33
+ WalletConnectProvider.init = (...args_1) => __awaiter(void 0, [...args_1], void 0, function* ({ storePrefix = 'dynamic-wc2', } = {}) {
34
+ logger.debug('[WalletConnectProvider] init', {
35
+ isInitialized: _a.isInitialized,
36
+ });
37
+ if (_a.isInitialized) {
38
+ return;
39
+ }
40
+ _a.isInitialized = true;
41
+ logger.debug('[WalletConnectProvider] initializing');
42
+ _a.provider = yield EthereumProvider.init({
43
+ customStoragePrefix: storePrefix,
44
+ disableProviderPing: true,
45
+ optionalChains: _a.getMappedChainsByPreferredOrder(),
46
+ optionalEvents: ['chainChanged', 'accountsChanged'],
47
+ optionalMethods: [
48
+ 'eth_chainId',
49
+ 'eth_signTypedData',
50
+ 'eth_signTransaction',
51
+ 'eth_sign',
52
+ 'personal_sign',
53
+ 'eth_sendTransaction',
54
+ 'eth_signTypedData_v4',
55
+ 'wallet_switchEthereumChain',
56
+ 'wallet_addEthereumChain',
57
+ ],
58
+ projectId: _a.projectId,
59
+ rpcMap: _a.evmNetworkRpcMap,
60
+ showQrModal: false,
61
+ });
62
+ logger.debug('[WalletConnectProvider] initialized');
63
+ });
64
+ /**
65
+ * Connects to a wallet. This method should be called whenever a new wallet connection is needed.
66
+ * If the wallet is already connected when the page is refreshed, this method does not need to be called.
67
+ */
68
+ WalletConnectProvider.connect = (_b) => __awaiter(void 0, [_b], void 0, function* ({ deepLinks, deepLinkPreference, connectionOpts, }) {
69
+ const handleDisplayURI = (uri) => {
70
+ var _b;
71
+ logger.debug('[WalletConnectProvider] handleDisplayURI', uri);
72
+ _a.connectionUri = uri;
73
+ performPlatformSpecificConnectionMethod(_a.connectionUri, deepLinks, {
74
+ onDesktopUri: connectionOpts === null || connectionOpts === void 0 ? void 0 : connectionOpts.onDesktopUri,
75
+ onDisplayUri: connectionOpts === null || connectionOpts === void 0 ? void 0 : connectionOpts.onDisplayUri,
76
+ }, deepLinkPreference);
77
+ logger.debug('[WalletConnectProvider] removing display_uri event listener');
78
+ (_b = _a.provider) === null || _b === void 0 ? void 0 : _b.off('display_uri', handleDisplayURI);
79
+ };
80
+ if (!_a.provider) {
81
+ throw new DynamicError('WalletConnectProvider is not initialized');
82
+ }
83
+ // this is in case the user just cancels the deeplink prompt (i.e. in mobile/Safari)
84
+ // in this case, the connection is not rejected, so the "enable" promise is just pending
85
+ // so on retry, we should just use the same uri to handle that promise
86
+ if (_a.connectionUri) {
87
+ handleDisplayURI(_a.connectionUri);
88
+ return;
89
+ }
90
+ logger.debug('[WalletConnectProvider] adding display_uri event listener');
91
+ _a.provider.on('display_uri', handleDisplayURI);
92
+ try {
93
+ const result = yield _a.provider.enable();
94
+ logger.debug('[WalletConnectProvider] connected to WalletConnect', result);
95
+ return result;
96
+ }
97
+ catch (error) {
98
+ logger.error('[WalletConnectProvider] Failed to connect to WalletConnect', error);
99
+ const customError = new DynamicError('Connection rejected. Please try again.');
100
+ customError.code = 'connection_rejected';
101
+ throw customError;
102
+ }
103
+ finally {
104
+ // Reset the connection URI after it's been consumed
105
+ _a.connectionUri = undefined;
106
+ }
107
+ });
108
+ /**
109
+ * Disconnects from a wallet. This method should be called whenever we need to disconnect from a wallet.
110
+ * It will kill the connection, but not the provider.
111
+ */
112
+ WalletConnectProvider.disconnect = () => __awaiter(void 0, void 0, void 0, function* () {
113
+ if (!_a.provider) {
114
+ logger.debug('[WalletConnectProvider] disconnect - provider is not initialized');
115
+ return;
116
+ }
117
+ _a.connectionUri = undefined;
118
+ logger.debug('[WalletConnectProvider] disconnecting from WalletConnect');
119
+ try {
120
+ yield _a.provider.disconnect();
121
+ }
122
+ catch (error) {
123
+ logger.error('[WalletConnectProvider] Failed to disconnect from WalletConnect', error);
124
+ }
125
+ });
126
+ /**
127
+ * Returns the EthereumProvider instance.
128
+ */
129
+ WalletConnectProvider.getProvider = () => _a.provider;
130
+ WalletConnectProvider.getConnectionUri = () => _a.connectionUri;
131
+ WalletConnectProvider.handleChainChangedEvent = (chain, onChainChanged) => {
132
+ logger.debug('[WalletConnectProvider] handling chain change event', {
133
+ chain,
134
+ });
135
+ const chainId = parseIntSafe(chain);
136
+ if (!chainId) {
137
+ return;
138
+ }
139
+ onChainChanged === null || onChainChanged === void 0 ? void 0 : onChainChanged(chainId);
140
+ };
141
+ WalletConnectProvider.handleAccountChangedEvent = (accounts, onAccountChanged) => {
142
+ logger.debug('[WalletConnectProvider] handling account change event', {
143
+ accounts,
144
+ });
145
+ const [account] = accounts;
146
+ const address = account.includes(':') ? account.split(':').pop() : account;
147
+ if (!address) {
148
+ return;
149
+ }
150
+ onAccountChanged === null || onAccountChanged === void 0 ? void 0 : onAccountChanged(address);
151
+ };
152
+ /**
153
+ * Sets up event listeners for the provider.
154
+ */
155
+ WalletConnectProvider.setupEventListeners = ({ onChainChanged, onAccountChanged, onDisconnect, }) => {
156
+ if (!_a.provider ||
157
+ _a.eventListenersSetup) {
158
+ return;
159
+ }
160
+ _a.chainChangedHandler = (chainId) => {
161
+ _a.handleChainChangedEvent(chainId, onChainChanged);
162
+ };
163
+ _a.accountChangedHandler = (account) => {
164
+ _a.handleAccountChangedEvent(account, onAccountChanged);
165
+ };
166
+ _a.disconnectHandler = () => {
167
+ logger.debug('[WalletConnectProvider] handling disconnect event');
168
+ onDisconnect === null || onDisconnect === void 0 ? void 0 : onDisconnect();
169
+ };
170
+ _a.provider.on('accountsChanged', _a.accountChangedHandler);
171
+ _a.provider.on('chainChanged', _a.chainChangedHandler);
172
+ _a.provider.on('disconnect', _a.disconnectHandler);
173
+ _a.eventListenersSetup = true;
174
+ };
175
+ /**
176
+ * Tears down event listeners for the provider.
177
+ */
178
+ WalletConnectProvider.teardownEventListeners = () => {
179
+ if (!_a.provider ||
180
+ !_a.eventListenersSetup) {
181
+ return;
182
+ }
183
+ _a.provider.off('accountsChanged', _a.accountChangedHandler);
184
+ _a.provider.off('chainChanged', _a.chainChangedHandler);
185
+ _a.provider.off('disconnect', _a.disconnectHandler);
186
+ _a.eventListenersSetup = false;
187
+ };
188
+
189
+ export { WalletConnectProvider };
@@ -1,2 +1,2 @@
1
- export { WalletConnect } from './walletConnect';
1
+ export { WalletConnectConnector } from './WalletConnectConnector';
2
2
  export { fetchWalletConnectWallets, getWalletConnectConnector } from './utils';
@@ -3,7 +3,7 @@
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
5
5
 
6
- var walletConnect = require('../walletConnect.cjs');
6
+ var WalletConnectConnector = require('../WalletConnectConnector/WalletConnectConnector.cjs');
7
7
 
8
8
  const fetchWalletConnectWallets = ({ walletBook, }) => {
9
9
  var _a;
@@ -12,7 +12,7 @@ const fetchWalletConnectWallets = ({ walletBook, }) => {
12
12
  .map(([key, wallet]) => {
13
13
  const { shortName } = wallet;
14
14
  const name = shortName || wallet.name;
15
- return class extends walletConnect.WalletConnect {
15
+ return class extends WalletConnectConnector.WalletConnectConnector {
16
16
  constructor(props) {
17
17
  super(Object.assign(Object.assign({}, props), { walletName: name }));
18
18
  this.overrideKey = key;
@@ -1,5 +1,5 @@
1
1
  'use client'
2
- import { WalletConnect } from '../walletConnect.js';
2
+ import { WalletConnectConnector } from '../WalletConnectConnector/WalletConnectConnector.js';
3
3
 
4
4
  const fetchWalletConnectWallets = ({ walletBook, }) => {
5
5
  var _a;
@@ -8,7 +8,7 @@ const fetchWalletConnectWallets = ({ walletBook, }) => {
8
8
  .map(([key, wallet]) => {
9
9
  const { shortName } = wallet;
10
10
  const name = shortName || wallet.name;
11
- return class extends WalletConnect {
11
+ return class extends WalletConnectConnector {
12
12
  constructor(props) {
13
13
  super(Object.assign(Object.assign({}, props), { walletName: name }));
14
14
  this.overrideKey = key;
@@ -3,9 +3,9 @@
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
5
5
 
6
- var walletConnect = require('../walletConnect.cjs');
6
+ var WalletConnectConnector = require('../WalletConnectConnector/WalletConnectConnector.cjs');
7
7
 
8
- const getWalletConnectConnector = () => class extends walletConnect.WalletConnect {
8
+ const getWalletConnectConnector = () => class extends WalletConnectConnector.WalletConnectConnector {
9
9
  constructor(props) {
10
10
  super(Object.assign(Object.assign({}, props), { walletName: 'WalletConnect' }));
11
11
  }
@@ -1,7 +1,7 @@
1
1
  'use client'
2
- import { WalletConnect } from '../walletConnect.js';
2
+ import { WalletConnectConnector } from '../WalletConnectConnector/WalletConnectConnector.js';
3
3
 
4
- const getWalletConnectConnector = () => class extends WalletConnect {
4
+ const getWalletConnectConnector = () => class extends WalletConnectConnector {
5
5
  constructor(props) {
6
6
  super(Object.assign(Object.assign({}, props), { walletName: 'WalletConnect' }));
7
7
  }