@dynamic-labs-sdk/wallet-connect 0.1.0-alpha.17 → 0.1.0-alpha.19

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.
Files changed (85) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +2 -2
  3. package/core.cjs.js +501 -2
  4. package/core.esm.js +475 -1
  5. package/index.cjs.js +4 -2
  6. package/index.esm.js +2 -1
  7. package/isWalletConnectWalletProvider.cjs.js +23 -0
  8. package/isWalletConnectWalletProvider.esm.js +19 -0
  9. package/package.json +8 -4
  10. package/src/connectWalletConnectSession/connectWalletConnectSession.d.ts +27 -0
  11. package/src/connectWalletConnectSession/connectWalletConnectSession.d.ts.map +1 -0
  12. package/src/connectWalletConnectSession/index.d.ts +2 -0
  13. package/src/connectWalletConnectSession/index.d.ts.map +1 -0
  14. package/src/createWalletAccountsForWalletConnectWalletProvider/createWalletAccountsForWalletConnectWalletProvider.d.ts +8 -0
  15. package/src/createWalletAccountsForWalletConnectWalletProvider/createWalletAccountsForWalletConnectWalletProvider.d.ts.map +1 -0
  16. package/src/createWalletAccountsForWalletConnectWalletProvider/index.d.ts +2 -0
  17. package/src/createWalletAccountsForWalletConnectWalletProvider/index.d.ts.map +1 -0
  18. package/src/events.d.ts +38 -0
  19. package/src/events.d.ts.map +1 -0
  20. package/src/exports/core.d.ts +9 -1
  21. package/src/exports/core.d.ts.map +1 -1
  22. package/src/exports/index.d.ts +3 -1
  23. package/src/exports/index.d.ts.map +1 -1
  24. package/src/initializeWalletConnectExtension/index.d.ts +2 -0
  25. package/src/initializeWalletConnectExtension/index.d.ts.map +1 -0
  26. package/src/initializeWalletConnectExtension/initializeWalletConnectExtension.d.ts +3 -0
  27. package/src/initializeWalletConnectExtension/initializeWalletConnectExtension.d.ts.map +1 -0
  28. package/src/isWalletConnectWalletProvider/index.d.ts +2 -0
  29. package/src/isWalletConnectWalletProvider/index.d.ts.map +1 -0
  30. package/src/isWalletConnectWalletProvider/isWalletConnectWalletProvider.d.ts +4 -0
  31. package/src/isWalletConnectWalletProvider/isWalletConnectWalletProvider.d.ts.map +1 -0
  32. package/src/namespaceRegistry/getNamespaceRegistry/createNamespaceRegistry/createNamespaceRegistry.d.ts +3 -0
  33. package/src/namespaceRegistry/getNamespaceRegistry/createNamespaceRegistry/createNamespaceRegistry.d.ts.map +1 -0
  34. package/src/namespaceRegistry/getNamespaceRegistry/createNamespaceRegistry/index.d.ts +2 -0
  35. package/src/namespaceRegistry/getNamespaceRegistry/createNamespaceRegistry/index.d.ts.map +1 -0
  36. package/src/namespaceRegistry/getNamespaceRegistry/getNamespaceRegistry.d.ts +2 -0
  37. package/src/namespaceRegistry/getNamespaceRegistry/getNamespaceRegistry.d.ts.map +1 -0
  38. package/src/namespaceRegistry/getNamespaceRegistry/index.d.ts +2 -0
  39. package/src/namespaceRegistry/getNamespaceRegistry/index.d.ts.map +1 -0
  40. package/src/namespaceRegistry/index.d.ts +3 -0
  41. package/src/namespaceRegistry/index.d.ts.map +1 -0
  42. package/src/namespaceRegistry/namespaceRegistry.types.d.ts +34 -0
  43. package/src/namespaceRegistry/namespaceRegistry.types.d.ts.map +1 -0
  44. package/src/sessionTopicsData/clearTemporarySessions/clearTemporarySessions.d.ts +13 -0
  45. package/src/sessionTopicsData/clearTemporarySessions/clearTemporarySessions.d.ts.map +1 -0
  46. package/src/sessionTopicsData/clearTemporarySessions/index.d.ts +2 -0
  47. package/src/sessionTopicsData/clearTemporarySessions/index.d.ts.map +1 -0
  48. package/src/sessionTopicsData/getSessionTopicsData/getSessionTopicsData.d.ts +9 -0
  49. package/src/sessionTopicsData/getSessionTopicsData/getSessionTopicsData.d.ts.map +1 -0
  50. package/src/sessionTopicsData/getSessionTopicsData/index.d.ts +2 -0
  51. package/src/sessionTopicsData/getSessionTopicsData/index.d.ts.map +1 -0
  52. package/src/sessionTopicsData/schema.d.ts +2 -0
  53. package/src/sessionTopicsData/schema.d.ts.map +1 -0
  54. package/src/sessionTopicsData/sessionTopicsData.types.d.ts +14 -0
  55. package/src/sessionTopicsData/sessionTopicsData.types.d.ts.map +1 -0
  56. package/src/sessionTopicsData/setSessionTopicsData/index.d.ts +2 -0
  57. package/src/sessionTopicsData/setSessionTopicsData/index.d.ts.map +1 -0
  58. package/src/sessionTopicsData/setSessionTopicsData/setSessionTopicsData.d.ts +16 -0
  59. package/src/sessionTopicsData/setSessionTopicsData/setSessionTopicsData.d.ts.map +1 -0
  60. package/src/signClient/consts.d.ts +10 -0
  61. package/src/signClient/consts.d.ts.map +1 -0
  62. package/src/signClient/getSignClient/createSignClient/createSignClient.d.ts +4 -0
  63. package/src/signClient/getSignClient/createSignClient/createSignClient.d.ts.map +1 -0
  64. package/src/signClient/getSignClient/createSignClient/index.d.ts +2 -0
  65. package/src/signClient/getSignClient/createSignClient/index.d.ts.map +1 -0
  66. package/src/signClient/getSignClient/getSignClient.d.ts +2 -0
  67. package/src/signClient/getSignClient/getSignClient.d.ts.map +1 -0
  68. package/src/signClient/getSignClient/index.d.ts +2 -0
  69. package/src/signClient/getSignClient/index.d.ts.map +1 -0
  70. package/src/splitCaip10Token/index.d.ts +2 -0
  71. package/src/splitCaip10Token/index.d.ts.map +1 -0
  72. package/src/splitCaip10Token/splitCaip10Token.d.ts +21 -0
  73. package/src/splitCaip10Token/splitCaip10Token.d.ts.map +1 -0
  74. package/src/syncSessionsToWalletProviders/addMissingWalletProvidersForSessions/addMissingWalletProvidersForSessions.d.ts +12 -0
  75. package/src/syncSessionsToWalletProviders/addMissingWalletProvidersForSessions/addMissingWalletProvidersForSessions.d.ts.map +1 -0
  76. package/src/syncSessionsToWalletProviders/addMissingWalletProvidersForSessions/index.d.ts +2 -0
  77. package/src/syncSessionsToWalletProviders/addMissingWalletProvidersForSessions/index.d.ts.map +1 -0
  78. package/src/syncSessionsToWalletProviders/index.d.ts +2 -0
  79. package/src/syncSessionsToWalletProviders/index.d.ts.map +1 -0
  80. package/src/syncSessionsToWalletProviders/syncSessionsToWalletProviders.d.ts +8 -0
  81. package/src/syncSessionsToWalletProviders/syncSessionsToWalletProviders.d.ts.map +1 -0
  82. package/src/walletConnectWalletProvider.types.d.ts +37 -0
  83. package/src/walletConnectWalletProvider.types.d.ts.map +1 -0
  84. package/package.cjs.js +0 -7
  85. package/package.esm.js +0 -4
package/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## 0.1.0-alpha.19 (2025-10-10)
2
+
3
+ This was a version bump only, there were no code changes.
4
+
5
+ ## 0.1.0-alpha.18 (2025-10-08)
6
+
7
+ ### 🚀 Features
8
+
9
+ - Add TypeDoc for JavaScript SDK API Reference Documentation ([#512](https://github.com/dynamic-labs/dynamic-sdk/pull/512))
10
+
1
11
  ## 0.1.0-alpha.17 (2025-10-02)
2
12
 
3
13
  ### 🩹 Fixes
package/README.md CHANGED
@@ -5,8 +5,8 @@ This package contains the WalletConnect integration for the Dynamic SDK.
5
5
  ## Usage
6
6
 
7
7
  ```ts
8
- import { addWalletConnectEvmExtension } from '@dynamic-labs-sdk/wallet-connect/evm';
9
- import { addWalletConnectSolanaExtension } from '@dynamic-labs-sdk/wallet-connect/solana';
8
+ import { addWalletConnectEvmExtension } from '@dynamic-labs-sdk/evm/wallet-connect';
9
+ import { addWalletConnectSolanaExtension } from '@dynamic-labs-sdk/solana/wallet-connect';
10
10
  import { createDynamicClient } from '@dynamic-labs-sdk/client';
11
11
 
12
12
  const dynamicClient = createDynamicClient({
package/core.cjs.js CHANGED
@@ -1,6 +1,505 @@
1
1
  'use strict';
2
2
 
3
3
  var assertPackageVersion = require('@dynamic-labs-sdk/assert-package-version');
4
- var _package = require('./package.cjs.js');
4
+ var isWalletConnectWalletProvider = require('./isWalletConnectWalletProvider.cjs.js');
5
+ var client = require('@dynamic-labs-sdk/client');
6
+ var core = require('@dynamic-labs-sdk/client/core');
7
+ var z = require('zod/mini');
8
+ var SignClient = require('@walletconnect/sign-client');
9
+ var sdkApiCore = require('@dynamic-labs/sdk-api-core');
5
10
 
6
- assertPackageVersion.assertPackageVersion(_package.name, _package.version);
11
+ function _interopNamespaceDefault(e) {
12
+ var n = Object.create(null);
13
+ if (e) {
14
+ Object.keys(e).forEach(function (k) {
15
+ if (k !== 'default') {
16
+ var d = Object.getOwnPropertyDescriptor(e, k);
17
+ Object.defineProperty(n, k, d.get ? d : {
18
+ enumerable: true,
19
+ get: function () { return e[k]; }
20
+ });
21
+ }
22
+ });
23
+ }
24
+ n.default = e;
25
+ return Object.freeze(n);
26
+ }
27
+
28
+ var z__namespace = /*#__PURE__*/_interopNamespaceDefault(z);
29
+
30
+ function _extends() {
31
+ _extends = Object.assign || function assign(target) {
32
+ for(var i = 1; i < arguments.length; i++){
33
+ var source = arguments[i];
34
+ for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
35
+ }
36
+ return target;
37
+ };
38
+ return _extends.apply(this, arguments);
39
+ }
40
+
41
+ /**
42
+ * An account in WalletConnect follows the CAIP-10 standard
43
+ * and is formatted as follows:
44
+ *
45
+ * `<namespace>:<chain>:<address>`
46
+ *
47
+ * Where:
48
+ * - `<namespace>` is the namespace key (e.g. "eip155")
49
+ * - `<chain>` is what we refer to as a network ID (e.g. "1")
50
+ * - `<address>` is the address of the account (e.g. "0x1234567890")
51
+ *
52
+ * See docs: https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-10.md
53
+ *
54
+ * @returns An object with the address, namespace key, and network id
55
+ */ const splitCaip10Token = (caip10Token)=>{
56
+ const [namespaceKey, networkId, address] = caip10Token.split(':');
57
+ return {
58
+ address,
59
+ namespaceKey,
60
+ networkId
61
+ };
62
+ };
63
+
64
+ const createWalletAccountsForWalletConnectWalletProvider = ({ walletProvider })=>{
65
+ /**
66
+ * Since each session can have multiple namespaces, we need to find the namespace
67
+ * that corresponds to this specific wallet provider.
68
+ *
69
+ * A WalletConnect namespace is the equivalent of what we refer to as a chain in the SDK,
70
+ * and they have attributed to them the methods, events and networks that are expected
71
+ * to be supported by the wallet application, as well as which accounts are connected.
72
+ */ const namespace = walletProvider.session.namespaces[walletProvider.namespaceKey];
73
+ core.assertDefined(namespace, `Namespace key ${walletProvider.namespaceKey} of wallet provider ${walletProvider.key} not found in its session ${walletProvider.topic}`);
74
+ const accountAddresses = core.filterDuplicates(namespace.accounts.map((account)=>splitCaip10Token(account).address));
75
+ var _walletProvider_formatWalletAddress;
76
+ const formatAddress = (_walletProvider_formatWalletAddress = walletProvider.formatWalletAddress) != null ? _walletProvider_formatWalletAddress : ({ address })=>address;
77
+ return accountAddresses.map((address)=>({
78
+ address: formatAddress({
79
+ address
80
+ }),
81
+ chain: walletProvider.chain,
82
+ id: core.formatWalletAccountId({
83
+ address,
84
+ chain: walletProvider.chain,
85
+ walletProviderKey: walletProvider.key
86
+ }),
87
+ lastSelectedAt: null,
88
+ verifiedCredentialId: null,
89
+ walletProviderKey: walletProvider.key
90
+ }));
91
+ };
92
+
93
+ const sessionTopicsStorageKeySchema = core.createStorageKeySchema({
94
+ key: 'sessionTopics',
95
+ schema: z__namespace.record(/**
96
+ * The session topic that identifies this session.
97
+ */ z__namespace.string(), /**
98
+ * The session data.
99
+ */ z__namespace.object({
100
+ /**
101
+ * Whether this session's wallet accounts should be added to the dynamic's wallet accounts.
102
+ * When a customer connects a session with addToDynamicWalletAccounts set to false, the resulting session
103
+ * should not produce wallet accounts to be added to the dynamic's wallet accounts.
104
+ * When that happens, we cannot clear them until the page is refreshed or the wallet account is removed
105
+ * by the external owner by calling removeWalletAccount.
106
+ */ shouldAddToDynamicWalletAccounts: z__namespace.boolean()
107
+ }))
108
+ });
109
+
110
+ /**
111
+ * Sets the session topics data.
112
+ *
113
+ * Read more about it in the sessionTopicsData.types.ts file.
114
+ *
115
+ * @param update - The function to update the session topics data.
116
+ * @param client - The client instance.
117
+ */ const setSessionTopicsData = async ({ update }, client)=>{
118
+ const { storage } = core.getCore(client);
119
+ const sessionTopicsData = await storage.getItem(sessionTopicsStorageKeySchema);
120
+ const newSessionTopicsData = update(sessionTopicsData != null ? sessionTopicsData : {});
121
+ if (Object.keys(newSessionTopicsData).length === 0) {
122
+ return storage.removeItem(sessionTopicsStorageKeySchema);
123
+ }
124
+ return storage.setItem(sessionTopicsStorageKeySchema, newSessionTopicsData);
125
+ };
126
+
127
+ /**
128
+ * Gets the session topics data.
129
+ *
130
+ * Read more about it in the sessionTopicsData.types.ts file.
131
+ * @param client - The client instance.
132
+ */ const getSessionTopicsData = async (client)=>{
133
+ const { storage } = core.getCore(client);
134
+ const sessionTopicsData = await storage.getItem(sessionTopicsStorageKeySchema);
135
+ return sessionTopicsData != null ? sessionTopicsData : {};
136
+ };
137
+
138
+ /**
139
+ * Reason for disconnection when the user disconnects from a WalletConnect session.
140
+ *
141
+ * Source: https://github.com/WalletConnect/walletconnect-monorepo/blob/cc3a9d26dcbd05c92d5ded935b29dc3b6063d147/packages/utils/src/errors.ts#L87
142
+ */ const USER_DISCONNECTED_REASON = {
143
+ code: 6000,
144
+ message: 'User disconnected.'
145
+ };
146
+
147
+ const createSignClient = async (client)=>{
148
+ var _client_projectSettings_sdk_walletConnect, _core_metadata, _core_metadata1;
149
+ const core$1 = core.getCore(client);
150
+ core.assertDefined(client.projectSettings, 'Project settings not available');
151
+ const generalSettings = client.projectSettings.general;
152
+ core.assertDefined(generalSettings.displayName, 'Please configure an app name in dashboard to use WalletConnect');
153
+ const projectId = (_client_projectSettings_sdk_walletConnect = client.projectSettings.sdk.walletConnect) == null ? void 0 : _client_projectSettings_sdk_walletConnect.projectId;
154
+ core.assertDefined(projectId, 'Please configure a project ID for WalletConnect in dashboard');
155
+ const customStoragePrefix = `dynamic_${core$1.environmentId}_walletconnect`;
156
+ var _core_metadata_name;
157
+ const displayName = (_core_metadata_name = (_core_metadata = core$1.metadata) == null ? void 0 : _core_metadata.name) != null ? _core_metadata_name : generalSettings.displayName;
158
+ var _core_metadata_iconUrl;
159
+ const appLogo = (_core_metadata_iconUrl = (_core_metadata1 = core$1.metadata) == null ? void 0 : _core_metadata1.iconUrl) != null ? _core_metadata_iconUrl : generalSettings.appLogo;
160
+ return SignClient.init({
161
+ customStoragePrefix,
162
+ metadata: {
163
+ description: '',
164
+ icons: appLogo ? [
165
+ appLogo
166
+ ] : [],
167
+ name: displayName,
168
+ url: ''
169
+ },
170
+ projectId
171
+ });
172
+ };
173
+
174
+ const getSignClient = core.createRuntimeServiceAccessKey('walletConnectSignClient', (client)=>createSignClient(client));
175
+
176
+ const createNamespaceRegistry = ()=>{
177
+ const walletProviderBuilderMap = new Map();
178
+ return {
179
+ getWalletProviderBuilder: (key)=>walletProviderBuilderMap.get(key),
180
+ registerNamespace: ({ createWalletProviderFromSession, namespaceKey })=>{
181
+ walletProviderBuilderMap.set(namespaceKey, createWalletProviderFromSession);
182
+ }
183
+ };
184
+ };
185
+
186
+ const getNamespaceRegistry = core.createRuntimeServiceAccessKey('walletConnectNamespaceRegistry', createNamespaceRegistry);
187
+
188
+ const addMissingWalletProvidersForSessions = async ({ sessions, allowOverridingWalletProviders }, client$1)=>{
189
+ const namespaceRegistry = getNamespaceRegistry(client$1);
190
+ const walletProviderRegistry = core.getWalletProviderRegistry(client$1);
191
+ const sessionTopicsData = await getSessionTopicsData(client$1);
192
+ const currentWalletAccounts = client.getWalletAccounts(client$1);
193
+ // Sort so the oldest sessions are first and the newer sessions will
194
+ // override the older ones' wallet accounts (if there is any overlap).
195
+ sessions.sort((sessionA, sessionB)=>sessionA.expiry - sessionB.expiry);
196
+ /**
197
+ * All the WC wallet providers that are currently registered
198
+ */ const currentWalletProviders = walletProviderRegistry.listProviders().filter(isWalletConnectWalletProvider.isWalletConnectWalletProvider);
199
+ /**
200
+ * Keep record on which walletProviders are tied to active sessions
201
+ */ const walletProvidersWithActiveSession = [];
202
+ /**
203
+ * Maps the ID of the created wallet accounts to the wallet accounts themselves.
204
+ */ const createdWalletAccounts = {};
205
+ /**
206
+ * Keep record on which wallet account IDs were not in the current wallet accounts
207
+ * and are therefore brand new.
208
+ */ const brandNewWalletAccounts = new Set();
209
+ /**
210
+ * Keep record on which wallet account IDs already exist but will have
211
+ * their wallet provider keys overridden to use the new WC wallet provider key.
212
+ */ const walletAccountsToOverride = new Set();
213
+ for (const session of sessions){
214
+ const namespaceKeys = Object.keys(session.namespaces);
215
+ for (const namespaceKey of namespaceKeys){
216
+ const existingWalletProvider = currentWalletProviders.find((walletProvider)=>walletProvider.namespaceKey === namespaceKey && walletProvider.topic === session.topic);
217
+ if (existingWalletProvider) {
218
+ walletProvidersWithActiveSession.push(existingWalletProvider);
219
+ continue;
220
+ }
221
+ const walletProviderBuilder = namespaceRegistry.getWalletProviderBuilder(namespaceKey);
222
+ if (!walletProviderBuilder) {
223
+ continue;
224
+ }
225
+ const walletProvider = await walletProviderBuilder({
226
+ session
227
+ }, client$1);
228
+ walletProviderRegistry.register({
229
+ priority: core.WalletProviderPriority.WALLET_SDK,
230
+ walletProvider
231
+ });
232
+ walletProvidersWithActiveSession.push(walletProvider);
233
+ const sessionData = sessionTopicsData[session.topic];
234
+ if (!sessionData.shouldAddToDynamicWalletAccounts) {
235
+ continue;
236
+ }
237
+ const namespaceWalletAccounts = createWalletAccountsForWalletConnectWalletProvider({
238
+ walletProvider
239
+ });
240
+ if (namespaceWalletAccounts.length === 0) {
241
+ continue;
242
+ }
243
+ for (const createdWalletAccount of namespaceWalletAccounts){
244
+ const isBrandNew = !currentWalletAccounts.some((existingAccount)=>existingAccount.id === createdWalletAccount.id);
245
+ if (isBrandNew) {
246
+ createdWalletAccounts[createdWalletAccount.id] = createdWalletAccount;
247
+ brandNewWalletAccounts.add(createdWalletAccount.id);
248
+ continue;
249
+ }
250
+ const existingAccount = currentWalletAccounts.find((existingAccount)=>existingAccount.id === createdWalletAccount.id);
251
+ const isOverrideCandidate = existingAccount && existingAccount.walletProviderKey !== createdWalletAccount.walletProviderKey;
252
+ if (!isOverrideCandidate) {
253
+ continue;
254
+ }
255
+ // If overriding wallet providers is not allowed, we can still override
256
+ // as long as the wallet account being overridden is also a wallet connect wallet account.
257
+ const existingWalletIsWalletConnect = core.splitWalletProviderKey(existingAccount.walletProviderKey).walletProviderType === sdkApiCore.WalletProviderEnum.WalletConnect;
258
+ if (allowOverridingWalletProviders || existingWalletIsWalletConnect) {
259
+ createdWalletAccounts[createdWalletAccount.id] = createdWalletAccount;
260
+ walletAccountsToOverride.add(createdWalletAccount.id);
261
+ }
262
+ }
263
+ }
264
+ }
265
+ if (walletAccountsToOverride.size === 0 && brandNewWalletAccounts.size === 0) {
266
+ return {
267
+ walletProvidersWithActiveSession
268
+ };
269
+ }
270
+ // For now we only support connect only, so we only need to worry about unverified wallet accounts.
271
+ core.setUnverifiedWalletAccounts({
272
+ unverifiedWalletAccountsToUpdate: Object.values(createdWalletAccounts)
273
+ }, client$1);
274
+ if (brandNewWalletAccounts.size > 0) {
275
+ for (const walletAccountId of brandNewWalletAccounts){
276
+ void core.createVisit({
277
+ walletAccount: createdWalletAccounts[walletAccountId]
278
+ }, client$1);
279
+ }
280
+ }
281
+ core.emitWalletAccountsChangedEvent(client$1);
282
+ return {
283
+ walletProvidersWithActiveSession
284
+ };
285
+ };
286
+
287
+ const doSyncSessionsToWalletProviders = async ({ allowOverridingWalletProviders }, client$1)=>{
288
+ await core.waitForProjectSettings(client$1);
289
+ const signClient = await getSignClient(client$1);
290
+ const walletProviderRegistry = core.getWalletProviderRegistry(client$1);
291
+ const sessionTopicsData = await getSessionTopicsData(client$1);
292
+ const sessions = signClient.session.getAll().filter((session)=>{
293
+ if (sessionTopicsData[session.topic]) {
294
+ return true;
295
+ }
296
+ // If we don't have data for this session, it is corrupted and we need to disconnect
297
+ void signClient.disconnect({
298
+ reason: USER_DISCONNECTED_REASON,
299
+ topic: session.topic
300
+ });
301
+ return false;
302
+ });
303
+ /**
304
+ * All the WC wallet providers that are currently registered
305
+ */ const currentWalletProviders = walletProviderRegistry.listProviders().filter(isWalletConnectWalletProvider.isWalletConnectWalletProvider);
306
+ // Step 1: remove wallet providers whose sessions have been cleared
307
+ for (const walletProvider of currentWalletProviders){
308
+ const hasSession = sessions.some((session)=>session.topic === walletProvider.topic && walletProvider.namespaceKey in session.namespaces);
309
+ if (!hasSession) {
310
+ walletProviderRegistry.unregister(walletProvider.key);
311
+ }
312
+ }
313
+ // Step 2: add wallet providers that are missing for current sessions
314
+ const { walletProvidersWithActiveSession: updatedWalletProviders } = await addMissingWalletProvidersForSessions({
315
+ allowOverridingWalletProviders,
316
+ sessions
317
+ }, client$1);
318
+ // Step 3: identify which sessions have no more associated wallet accounts for clean up
319
+ const currentWalletAccounts = client.getWalletAccounts(client$1);
320
+ /**
321
+ * Maps which sessions have wallet accounts associated with them
322
+ */ const sessionTopicHasWalletAccountMap = new Map();
323
+ for (const walletProvider of updatedWalletProviders){
324
+ const sessionTopic = walletProvider.topic;
325
+ if (!sessionTopicHasWalletAccountMap.has(sessionTopic)) {
326
+ sessionTopicHasWalletAccountMap.set(sessionTopic, false);
327
+ }
328
+ // Early return if we already know this session has wallet accounts associated with it
329
+ if (sessionTopicHasWalletAccountMap.get(sessionTopic)) {
330
+ continue;
331
+ }
332
+ const walletProviderWalletAccounts = currentWalletAccounts.filter((walletAccount)=>walletAccount.walletProviderKey === walletProvider.key);
333
+ if (walletProviderWalletAccounts.length > 0) {
334
+ sessionTopicHasWalletAccountMap.set(sessionTopic, true);
335
+ }
336
+ }
337
+ // Step 4: kill sessions and their wallet providers if they have no more wallet accounts
338
+ for (const [sessionTopic, hasWalletAccount] of sessionTopicHasWalletAccountMap){
339
+ if (hasWalletAccount) {
340
+ continue;
341
+ }
342
+ const session = sessions.find((session)=>session.topic === sessionTopic);
343
+ if (session) {
344
+ void signClient.disconnect({
345
+ reason: USER_DISCONNECTED_REASON,
346
+ topic: session.topic
347
+ });
348
+ }
349
+ for (const walletProvider of updatedWalletProviders){
350
+ if (walletProvider.topic === sessionTopic) {
351
+ walletProviderRegistry.unregister(walletProvider.key);
352
+ }
353
+ }
354
+ }
355
+ // Step 5: compile which session topics are active to clean up stale session data
356
+ // We do session.getAll() again here to catch any sessions that might have been disconnected from other steps or even externally.
357
+ const activeSessionTopics = new Set(signClient.session.getAll().map((session)=>session.topic));
358
+ await setSessionTopicsData({
359
+ update: (sessionTopicsData)=>{
360
+ return Object.fromEntries(Object.entries(sessionTopicsData).filter(([topic])=>activeSessionTopics.has(topic)));
361
+ }
362
+ }, client$1);
363
+ };
364
+ const syncSessionsToWalletProviders = async ({ debounceTime = 0, allowOverridingWalletProviders }, client)=>{
365
+ const core$1 = core.getCore(client);
366
+ // This is called on adding WC extensions, which could be many in quick succession, so let's debounce
367
+ // the syncSessionsToWalletProviders call to ensure it only runs once and avoid race conditions.
368
+ return core$1.debouncedMutex({
369
+ callback: async ()=>doSyncSessionsToWalletProviders({
370
+ allowOverridingWalletProviders
371
+ }, client),
372
+ debounceTime,
373
+ lockKey: 'wallet-connect-sync-sessions-to-wallet-providers'
374
+ });
375
+ };
376
+
377
+ /**
378
+ * This is a lower level function that is used to connect a WalletConnect session to the Dynamic SDK.
379
+ * It will generate the wallet providers and wallet accounts for all accounts in the session.
380
+ *
381
+ * @param session - The session object created from the approval promise of the signClient.connect method.
382
+ * @param addToDynamicWalletAccounts - Whether to add the wallet accounts to the dynamic's wallet accounts.
383
+ * @param client - The Dynamic client instance. Only required when using multiple Dynamic clients.
384
+ * @returns The wallet accounts.
385
+ */ const connectWalletConnectSession = async ({ addToDynamicWalletAccounts = true, session }, client$1)=>{
386
+ /**
387
+ * You would normally get the session from the signClient.connect method. It takes the optionalNamespaces as an argument.
388
+ *
389
+ * Each chain should build its own "connectWithWalletConnect<chain-name>" function.
390
+ * Understand how to build the optionalNamespaces object:
391
+ *
392
+ * The namespace configurations are used at the time a wallet application receives the connection URI.
393
+ * It will use the configurations we supply to understand which methods, events and networks it should provide.
394
+ *
395
+ * The wallet application will then accept the connection request and, in the session`object, provide its own namespace object
396
+ * in return, which will contain the methods, events and networks it does in fact support, as well as
397
+ * which accounts are available.
398
+ *
399
+ * WalletConnect expects namespaces defined in the following format:
400
+ * ```typescript
401
+ * {
402
+ * <namespaceKey>: {
403
+ * "accounts": ["<account1>", "<account2>", "<account3>", ...],
404
+ * "events": ["accountsChanged", "chainChanged", "disconnected"],
405
+ * "methods": ["personal_sign", "eth_sendTransaction", "eth_accounts", ...],
406
+ * "chains": ["<chain1>", "<chain2>", "<chain3>", ...]
407
+ * },
408
+ * ...
409
+ * }
410
+ * ```
411
+ *
412
+ * Where each account is a CAIP-10 formatted string, for example: "eip155:1:0x1234567890"
413
+ * — see docs: https://chainagnostic.org/CAIPs/caip-10
414
+ *
415
+ * Where each chain is a CAIP-2 formatted string, for example: "eip155:1"
416
+ * — see docs: https://chainagnostic.org/CAIPs/caip-2
417
+ */ const sessionData = {
418
+ shouldAddToDynamicWalletAccounts: addToDynamicWalletAccounts
419
+ };
420
+ await setSessionTopicsData({
421
+ update: (sessionTopicsData)=>_extends({}, sessionTopicsData, {
422
+ [session.topic]: sessionData
423
+ })
424
+ }, client$1);
425
+ /**
426
+ * Once the connection is established, we ensure that all active connections (aka sessions)
427
+ * are synced to our WalletConnect wallet providers.
428
+ * Each session can be uniquely identified by its topic (a random string generated by WalletConnect).
429
+ *
430
+ * Each session may have multiple wallet providers, one for each namespace (aka our SDK chains)
431
+ */ await syncSessionsToWalletProviders({
432
+ allowOverridingWalletProviders: true
433
+ }, client$1);
434
+ const sessionWalletProviders = core.getWalletProviders(client$1).filter(isWalletConnectWalletProvider.isWalletConnectWalletProvider).filter((provider)=>provider.topic === session.topic);
435
+ /**
436
+ * Upon setting the session data to NOT externally owned, syncSessionsToWalletProviders will have
437
+ * already created the wallet accounts for each of this session's wallet providers — so all we need
438
+ * to do is retrieve them.
439
+ */ if (addToDynamicWalletAccounts) {
440
+ const sessionWalletProviderKeys = sessionWalletProviders.map((provider)=>provider.key);
441
+ const walletAccounts = client.getWalletAccounts(client$1).filter((walletAccount)=>sessionWalletProviderKeys.includes(walletAccount.walletProviderKey));
442
+ return {
443
+ walletAccounts
444
+ };
445
+ }
446
+ /**
447
+ * If we didn't add the wallet accounts to the dynamic's wallet accounts,
448
+ * we never created them at all, so we need to create them now and provide
449
+ * them to the caller, which will become their [external] owner.
450
+ */ const walletAccounts = sessionWalletProviders.map((walletProvider)=>createWalletAccountsForWalletConnectWalletProvider({
451
+ walletProvider
452
+ })).flat();
453
+ return {
454
+ walletAccounts
455
+ };
456
+ };
457
+
458
+ /**
459
+ * Clears all temporary sessions and disconnects from them.
460
+ *
461
+ * External sessions are for WC sessions whose accounts are not supposed to be saved in
462
+ * the current dynamic's wallet accounts.
463
+ * Since we don't own the created wallet accounts, we must keep their corresponding WC sessions alive
464
+ * until the page is refreshed or the customer removes the wallet account.
465
+ *
466
+ * @param client - The client instance.
467
+ */ const clearTemporarySessions = async (client)=>{
468
+ const signClient = await getSignClient(client);
469
+ const sessionTopicsData = await getSessionTopicsData(client);
470
+ const temporarySessionTopics = Object.keys(sessionTopicsData).filter((topic)=>!sessionTopicsData[topic].shouldAddToDynamicWalletAccounts);
471
+ if (temporarySessionTopics.length === 0) {
472
+ return;
473
+ }
474
+ for (const topic of temporarySessionTopics){
475
+ await signClient.disconnect({
476
+ reason: USER_DISCONNECTED_REASON,
477
+ topic
478
+ });
479
+ }
480
+ const newSessionTopicsData = Object.fromEntries(Object.entries(sessionTopicsData).filter(([, { shouldAddToDynamicWalletAccounts: isPermanentSession }])=>isPermanentSession));
481
+ await setSessionTopicsData({
482
+ update: ()=>newSessionTopicsData
483
+ }, client);
484
+ };
485
+
486
+ const initializeWalletConnectExtension = async (client)=>{
487
+ // Clear external session topics that were created in previous browser sessions
488
+ // Read more in the clearTemporarySessions function.
489
+ await clearTemporarySessions(client);
490
+ // Add debounce to account for multiple WC extension additions in quick succession
491
+ await syncSessionsToWalletProviders({
492
+ allowOverridingWalletProviders: false,
493
+ debounceTime: 200
494
+ }, client);
495
+ };
496
+
497
+ assertPackageVersion.assertPackageVersion(isWalletConnectWalletProvider.name, isWalletConnectWalletProvider.version);
498
+
499
+ exports.USER_DISCONNECTED_REASON = USER_DISCONNECTED_REASON;
500
+ exports.connectWalletConnectSession = connectWalletConnectSession;
501
+ exports.getNamespaceRegistry = getNamespaceRegistry;
502
+ exports.getSignClient = getSignClient;
503
+ exports.initializeWalletConnectExtension = initializeWalletConnectExtension;
504
+ exports.splitCaip10Token = splitCaip10Token;
505
+ exports.syncSessionsToWalletProviders = syncSessionsToWalletProviders;