@dynamic-labs/ethereum 0.17.0-RC.9 → 0.17.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.
@@ -1,26 +1,11 @@
1
1
  import { __awaiter } from '../../_virtual/_tslib.js';
2
2
  import Provider from '@walletconnect/universal-provider';
3
3
  import { ethers } from 'ethers';
4
- import { logger, utf8ToHex } from '@dynamic-labs/wallet-connector-core';
4
+ import { logger, getDeepLink, performPlatformSpecificConnectionMethod, utf8ToHex } from '@dynamic-labs/wallet-connector-core';
5
5
  import { getWalletBookWallet } from '@dynamic-labs/wallet-book';
6
- import { isMobile, DynamicError } from '@dynamic-labs/utils';
6
+ import { DynamicError, isMobile } from '@dynamic-labs/utils';
7
7
  import { EthWalletConnector } from '../EthWalletConnector.js';
8
- import { getDeepLink } from './client/client.js';
9
8
 
10
- const performPlatformSpecificConnectionMethod = (uri, metadata, opts) => {
11
- var _a, _b, _c;
12
- if (isMobile()) {
13
- const deepLink = getDeepLink(uri, metadata);
14
- window.location.href = deepLink;
15
- }
16
- else {
17
- if ((_a = metadata === null || metadata === void 0 ? void 0 : metadata.desktop) === null || _a === void 0 ? void 0 : _a.native) {
18
- const desktopUri = getDeepLink(uri, metadata);
19
- (_b = opts === null || opts === void 0 ? void 0 : opts.onDesktopUri) === null || _b === void 0 ? void 0 : _b.call(opts, desktopUri);
20
- }
21
- (_c = opts === null || opts === void 0 ? void 0 : opts.onDisplayUri) === null || _c === void 0 ? void 0 : _c.call(opts, uri);
22
- }
23
- };
24
9
  const sessionTopicKey = (walletName) => `dynamic-wc2-session-topic-${walletName}`;
25
10
  class WalletConnectV2 extends EthWalletConnector {
26
11
  constructor(opts) {
@@ -32,46 +17,61 @@ class WalletConnectV2 extends EthWalletConnector {
32
17
  this.name = opts.walletName;
33
18
  this.projectId = opts.projectId;
34
19
  }
20
+ initConnection() {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ const { provider } = WalletConnectV2;
23
+ if (!provider) {
24
+ throw new DynamicError('No provider found (init connection)');
25
+ }
26
+ // this means there is already a connection in progress, so don't call connect again
27
+ if (provider === null || provider === void 0 ? void 0 : provider.uri) {
28
+ return;
29
+ }
30
+ yield provider.connect({
31
+ namespaces: {
32
+ eip155: {
33
+ chains: this.evmNetworks
34
+ // filtering out Palm for now since it causes Trust to crash
35
+ .filter((network) => network.chainId !== 11297108109)
36
+ .map((network) => `eip155:${network.chainId}`),
37
+ events: ['chainChanged', 'accountsChanged'],
38
+ methods: [
39
+ 'eth_sendTransaction',
40
+ 'eth_signTransaction',
41
+ 'eth_sign',
42
+ 'personal_sign',
43
+ 'eth_signTypedData',
44
+ ],
45
+ rpcMap: this.evmNetworkRpcMap(),
46
+ },
47
+ },
48
+ });
49
+ });
50
+ }
35
51
  initProvider() {
36
52
  return __awaiter(this, void 0, void 0, function* () {
37
- if (this.projectId && !WalletConnectV2.provider) {
38
- WalletConnectV2.provider = yield Provider.init({
53
+ let { provider } = WalletConnectV2;
54
+ if (!provider) {
55
+ provider = yield Provider.init({
39
56
  projectId: this.projectId,
40
57
  });
41
- const connection = yield WalletConnectV2.provider.client.connect({
42
- requiredNamespaces: {
43
- eip155: {
44
- chains: this.evmNetworks
45
- // filtering out Palm for now since it causes Trust to crash
46
- .filter((network) => network.chainId !== 11297108109)
47
- .map((network) => `eip155:${network.chainId}`),
48
- events: ['chainChanged', 'accountsChanged'],
49
- methods: [
50
- 'eth_sendTransaction',
51
- 'eth_signTransaction',
52
- 'eth_sign',
53
- 'personal_sign',
54
- 'eth_signTypedData',
55
- ],
56
- },
57
- },
58
- });
59
- // eslint-disable-next-line prefer-destructuring
60
- WalletConnectV2.provider.uri = connection.uri;
61
- WalletConnectV2.createSession = connection.approval;
58
+ WalletConnectV2.provider = provider;
62
59
  }
63
60
  });
64
61
  }
65
- init() {
62
+ refreshSession() {
66
63
  var _a, _b, _c, _d;
64
+ if ((_b = (_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.session) === null || _b === void 0 ? void 0 : _b.topic) {
65
+ if (localStorage.getItem(this.sessionTopicKey) ===
66
+ ((_d = (_c = WalletConnectV2.provider) === null || _c === void 0 ? void 0 : _c.session) === null || _d === void 0 ? void 0 : _d.topic)) {
67
+ this.session = WalletConnectV2.provider.session;
68
+ }
69
+ }
70
+ }
71
+ init() {
67
72
  return __awaiter(this, void 0, void 0, function* () {
68
73
  yield this.initProvider();
69
- if ((_b = (_a = WalletConnectV2.provider) === null || _a === void 0 ? void 0 : _a.session) === null || _b === void 0 ? void 0 : _b.topic) {
70
- if (localStorage.getItem(this.sessionTopicKey) ===
71
- ((_d = (_c = WalletConnectV2.provider) === null || _c === void 0 ? void 0 : _c.session) === null || _d === void 0 ? void 0 : _d.topic)) {
72
- this.session = WalletConnectV2.provider.session;
73
- }
74
- }
74
+ yield this.initConnection();
75
75
  });
76
76
  }
77
77
  get sessionTopicKey() {
@@ -127,7 +127,11 @@ class WalletConnectV2 extends EthWalletConnector {
127
127
  if (!isMobile() && !((_b = wallet.desktop) === null || _b === void 0 ? void 0 : _b.native)) {
128
128
  return undefined;
129
129
  }
130
- return getDeepLink(WalletConnectV2.provider.uri, wallet);
130
+ return getDeepLink({
131
+ metadata: wallet,
132
+ mode: 'regular',
133
+ uri: WalletConnectV2.provider.uri,
134
+ });
131
135
  }
132
136
  getWeb3Provider() {
133
137
  if (!WalletConnectV2.provider) {
@@ -140,49 +144,31 @@ class WalletConnectV2 extends EthWalletConnector {
140
144
  if (this.session) {
141
145
  return this.session.namespaces.eip155.accounts[0].split(':')[2];
142
146
  }
143
- const metadata = getWalletBookWallet(this.name);
144
147
  if (!WalletConnectV2.provider) {
145
148
  throw new DynamicError('No provider found');
146
149
  }
147
- if (!WalletConnectV2.provider.uri || !WalletConnectV2.createSession) {
148
- throw new DynamicError('No uri or approval found');
150
+ if (!WalletConnectV2.provider.uri) {
151
+ throw new DynamicError('No uri found');
149
152
  }
153
+ const metadata = getWalletBookWallet(this.name);
150
154
  performPlatformSpecificConnectionMethod(WalletConnectV2.provider.uri, metadata, {
151
155
  onDesktopUri: opts === null || opts === void 0 ? void 0 : opts.onDesktopUri,
152
156
  onDisplayUri: opts === null || opts === void 0 ? void 0 : opts.onDisplayUri,
153
157
  });
154
- const session = yield WalletConnectV2.createSession();
155
- if (!session) {
156
- return undefined;
157
- }
158
- this.session = session;
159
- localStorage.setItem(this.sessionTopicKey, session.topic);
160
- // need to "reconnect" the provider so that we can tell it what the rpc urls are
161
- yield WalletConnectV2.provider.connect({
162
- namespaces: {
163
- eip155: {
164
- chains: this.evmNetworks
165
- // filtering out Palm for now since it causes Trust to crash
166
- .filter((network) => network.chainId !== 11297108109)
167
- .map((network) => `eip155:${network.chainId}`),
168
- events: ['chainChanged', 'accountsChanged'],
169
- methods: [
170
- 'eth_sendTransaction',
171
- 'eth_signTransaction',
172
- 'eth_sign',
173
- 'personal_sign',
174
- 'eth_signTypedData',
175
- ],
176
- rpcMap: this.evmNetworkRpcMap(),
177
- },
178
- },
179
- skipPairing: true,
158
+ return new Promise((resolve, reject) => {
159
+ if (!WalletConnectV2.provider) {
160
+ reject(new DynamicError('No provider found'));
161
+ return;
162
+ }
163
+ WalletConnectV2.provider.on('connect', ({ session }) => {
164
+ if (!session) {
165
+ reject(new DynamicError('No session found'));
166
+ }
167
+ this.session = session;
168
+ localStorage.setItem(this.sessionTopicKey, session.topic);
169
+ resolve(this.session.namespaces.eip155.accounts[0].split(':')[2]);
170
+ });
180
171
  });
181
- // THIS LINE IS SUPER IMPORTANT
182
- // since we are skipping pairing, (see: skipPairing: true above), we need to manually set the session
183
- // so that future provider requests work (like getNetwork)
184
- WalletConnectV2.provider.session = session;
185
- return session.namespaces.eip155.accounts[0].split(':')[2];
186
172
  });
187
173
  }
188
174
  signMessage(messageToSign) {
@@ -197,7 +183,7 @@ class WalletConnectV2 extends EthWalletConnector {
197
183
  if (isMobile()) {
198
184
  // for sign message, no uri is needed because the uri encodes connection details,
199
185
  // and at this point we are already connected
200
- const deepLink = getDeepLink('', metadata);
186
+ const deepLink = getDeepLink({ metadata, mode: 'regular' });
201
187
  window.location.href = deepLink;
202
188
  }
203
189
  // eslint-disable-next-line prefer-destructuring
@@ -219,7 +205,9 @@ class WalletConnectV2 extends EthWalletConnector {
219
205
  }
220
206
  try {
221
207
  yield WalletConnectV2.provider.disconnect();
208
+ WalletConnectV2.provider.uri = undefined;
222
209
  localStorage.removeItem(this.sessionTopicKey);
210
+ this.session = undefined;
223
211
  }
224
212
  catch (e) {
225
213
  logger.debug(e);
@@ -249,6 +237,8 @@ class WalletConnectV2 extends EthWalletConnector {
249
237
  }
250
238
  getConnectedAccounts() {
251
239
  return __awaiter(this, void 0, void 0, function* () {
240
+ yield this.initProvider();
241
+ this.refreshSession();
252
242
  if (!this.session) {
253
243
  return [];
254
244
  }