@dynamic-labs-sdk/wallet-connect 0.3.0 → 0.5.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.
Files changed (82) hide show
  1. package/dist/SessionClosedUnexpectedlyError-DiLzuxNr.cjs.js +41 -0
  2. package/dist/SessionClosedUnexpectedlyError-DiLzuxNr.cjs.js.map +1 -0
  3. package/dist/SessionClosedUnexpectedlyError-DtBVy8Mt.esm.js +23 -0
  4. package/dist/SessionClosedUnexpectedlyError-DtBVy8Mt.esm.js.map +1 -0
  5. package/dist/appendWalletConnectUriToDeepLink/appendWalletConnectUriToDeepLink.d.ts +26 -0
  6. package/dist/appendWalletConnectUriToDeepLink/appendWalletConnectUriToDeepLink.d.ts.map +1 -0
  7. package/dist/appendWalletConnectUriToDeepLink/index.d.ts +2 -0
  8. package/dist/appendWalletConnectUriToDeepLink/index.d.ts.map +1 -0
  9. package/dist/core.cjs.js +127 -312
  10. package/dist/core.cjs.js.map +1 -1
  11. package/dist/core.esm.js +127 -310
  12. package/dist/core.esm.js.map +1 -1
  13. package/dist/createWalletAccountsForWalletConnectWalletProvider/createWalletAccountsForWalletConnectWalletProvider.d.ts.map +1 -1
  14. package/dist/createWalletConnectSessionEventEmitter/createWalletConnectSessionEventEmitter.d.ts +30 -0
  15. package/dist/createWalletConnectSessionEventEmitter/createWalletConnectSessionEventEmitter.d.ts.map +1 -0
  16. package/dist/createWalletConnectSessionEventEmitter/index.d.ts +2 -0
  17. package/dist/createWalletConnectSessionEventEmitter/index.d.ts.map +1 -0
  18. package/dist/errors/SessionClosedUnexpectedlyError.d.ts +16 -0
  19. package/dist/errors/SessionClosedUnexpectedlyError.d.ts.map +1 -0
  20. package/dist/exports/core.d.ts +2 -4
  21. package/dist/exports/core.d.ts.map +1 -1
  22. package/dist/exports/index.d.ts +2 -0
  23. package/dist/exports/index.d.ts.map +1 -1
  24. package/dist/importWalletConnectAuthResponse/importWalletConnectAuthResponse.d.ts +6 -1
  25. package/dist/importWalletConnectAuthResponse/importWalletConnectAuthResponse.d.ts.map +1 -1
  26. package/dist/importWalletConnectAuthResponse/verifyWalletConnectAuths/verifyWalletConnectAuths.d.ts.map +1 -1
  27. package/dist/importWalletConnectSession/importWalletConnectSession.d.ts +9 -5
  28. package/dist/importWalletConnectSession/importWalletConnectSession.d.ts.map +1 -1
  29. package/dist/index.cjs.js +39 -3
  30. package/dist/index.cjs.js.map +1 -1
  31. package/dist/index.esm.js +36 -2
  32. package/dist/index.esm.js.map +1 -1
  33. package/dist/isWalletConnectWalletProvider/isWalletConnectWalletProvider.d.ts.map +1 -1
  34. package/dist/signClient/getSignClient/createSignClient/createSignClient.d.ts.map +1 -1
  35. package/dist/tsconfig.lib.tsbuildinfo +1 -1
  36. package/dist/walletConnectWalletProvider.types.d.ts +5 -10
  37. package/dist/walletConnectWalletProvider.types.d.ts.map +1 -1
  38. package/package.json +3 -3
  39. package/dist/initializeWalletConnectExtension/index.d.ts +0 -2
  40. package/dist/initializeWalletConnectExtension/index.d.ts.map +0 -1
  41. package/dist/initializeWalletConnectExtension/initializeWalletConnectExtension.d.ts +0 -3
  42. package/dist/initializeWalletConnectExtension/initializeWalletConnectExtension.d.ts.map +0 -1
  43. package/dist/isWalletConnectWalletProvider-CWTccSvR.esm.js +0 -18
  44. package/dist/isWalletConnectWalletProvider-CWTccSvR.esm.js.map +0 -1
  45. package/dist/isWalletConnectWalletProvider-D4PnL5el.cjs.js +0 -36
  46. package/dist/isWalletConnectWalletProvider-D4PnL5el.cjs.js.map +0 -1
  47. package/dist/namespaceRegistry/getNamespaceRegistry/createNamespaceRegistry/createNamespaceRegistry.d.ts +0 -3
  48. package/dist/namespaceRegistry/getNamespaceRegistry/createNamespaceRegistry/createNamespaceRegistry.d.ts.map +0 -1
  49. package/dist/namespaceRegistry/getNamespaceRegistry/createNamespaceRegistry/index.d.ts +0 -2
  50. package/dist/namespaceRegistry/getNamespaceRegistry/createNamespaceRegistry/index.d.ts.map +0 -1
  51. package/dist/namespaceRegistry/getNamespaceRegistry/getNamespaceRegistry.d.ts +0 -2
  52. package/dist/namespaceRegistry/getNamespaceRegistry/getNamespaceRegistry.d.ts.map +0 -1
  53. package/dist/namespaceRegistry/getNamespaceRegistry/index.d.ts +0 -2
  54. package/dist/namespaceRegistry/getNamespaceRegistry/index.d.ts.map +0 -1
  55. package/dist/namespaceRegistry/index.d.ts +0 -3
  56. package/dist/namespaceRegistry/index.d.ts.map +0 -1
  57. package/dist/namespaceRegistry/namespaceRegistry.types.d.ts +0 -34
  58. package/dist/namespaceRegistry/namespaceRegistry.types.d.ts.map +0 -1
  59. package/dist/sessionTopicsData/clearTemporarySessions/clearTemporarySessions.d.ts +0 -13
  60. package/dist/sessionTopicsData/clearTemporarySessions/clearTemporarySessions.d.ts.map +0 -1
  61. package/dist/sessionTopicsData/clearTemporarySessions/index.d.ts +0 -2
  62. package/dist/sessionTopicsData/clearTemporarySessions/index.d.ts.map +0 -1
  63. package/dist/sessionTopicsData/getSessionTopicsData/getSessionTopicsData.d.ts +0 -11
  64. package/dist/sessionTopicsData/getSessionTopicsData/getSessionTopicsData.d.ts.map +0 -1
  65. package/dist/sessionTopicsData/getSessionTopicsData/index.d.ts +0 -2
  66. package/dist/sessionTopicsData/getSessionTopicsData/index.d.ts.map +0 -1
  67. package/dist/sessionTopicsData/schema.d.ts +0 -4
  68. package/dist/sessionTopicsData/schema.d.ts.map +0 -1
  69. package/dist/sessionTopicsData/sessionTopicsData.types.d.ts +0 -14
  70. package/dist/sessionTopicsData/sessionTopicsData.types.d.ts.map +0 -1
  71. package/dist/sessionTopicsData/setSessionTopicsData/index.d.ts +0 -2
  72. package/dist/sessionTopicsData/setSessionTopicsData/index.d.ts.map +0 -1
  73. package/dist/sessionTopicsData/setSessionTopicsData/setSessionTopicsData.d.ts +0 -16
  74. package/dist/sessionTopicsData/setSessionTopicsData/setSessionTopicsData.d.ts.map +0 -1
  75. package/dist/syncSessionsToWalletProviders/addMissingWalletProvidersForSessions/addMissingWalletProvidersForSessions.d.ts +0 -12
  76. package/dist/syncSessionsToWalletProviders/addMissingWalletProvidersForSessions/addMissingWalletProvidersForSessions.d.ts.map +0 -1
  77. package/dist/syncSessionsToWalletProviders/addMissingWalletProvidersForSessions/index.d.ts +0 -2
  78. package/dist/syncSessionsToWalletProviders/addMissingWalletProvidersForSessions/index.d.ts.map +0 -1
  79. package/dist/syncSessionsToWalletProviders/index.d.ts +0 -2
  80. package/dist/syncSessionsToWalletProviders/index.d.ts.map +0 -1
  81. package/dist/syncSessionsToWalletProviders/syncSessionsToWalletProviders.d.ts +0 -8
  82. package/dist/syncSessionsToWalletProviders/syncSessionsToWalletProviders.d.ts.map +0 -1
package/dist/core.cjs.js CHANGED
@@ -25,16 +25,14 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
25
25
  }) : target, mod));
26
26
 
27
27
  //#endregion
28
- const require_isWalletConnectWalletProvider = require('./isWalletConnectWalletProvider-D4PnL5el.cjs.js');
28
+ const require_SessionClosedUnexpectedlyError = require('./SessionClosedUnexpectedlyError-DiLzuxNr.cjs.js');
29
29
  let _dynamic_labs_sdk_assert_package_version = require("@dynamic-labs-sdk/assert-package-version");
30
30
  let _walletconnect_utils = require("@walletconnect/utils");
31
31
  let _dynamic_labs_sdk_client = require("@dynamic-labs-sdk/client");
32
+ let _dynamic_labs_sdk_api_core = require("@dynamic-labs/sdk-api-core");
32
33
  let _dynamic_labs_sdk_client_core = require("@dynamic-labs-sdk/client/core");
33
- let zod_mini = require("zod/mini");
34
- zod_mini = __toESM(zod_mini);
35
34
  let _walletconnect_sign_client = require("@walletconnect/sign-client");
36
35
  _walletconnect_sign_client = __toESM(_walletconnect_sign_client);
37
- let _dynamic_labs_sdk_api_core = require("@dynamic-labs/sdk-api-core");
38
36
 
39
37
  //#region src/splitCaip10Token/splitCaip10Token.ts
40
38
  /**
@@ -61,6 +59,53 @@ const splitCaip10Token = (caip10Token) => {
61
59
  };
62
60
  };
63
61
 
62
+ //#endregion
63
+ //#region src/createWalletConnectSessionEventEmitter/createWalletConnectSessionEventEmitter.ts
64
+ /**
65
+ * Creates a WalletConnect session event emitter with common event handling logic.
66
+ *
67
+ * Handles chainChanged, accountsChanged, and disconnected events from WalletConnect sessions.
68
+ *
69
+ * @param params.getSession - Function to get the current session
70
+ * @param params.namespaceKey - The WalletConnect namespace key (e.g., 'eip155', 'solana')
71
+ * @param params.sessionTopic - The session topic to listen for events
72
+ * @param params.signClient - The WalletConnect sign client instance
73
+ * @param params.formatAddress - Optional function to format addresses (e.g., EIP-55 for EVM)
74
+ * @returns An object with cleanupEventEmitter and getEventEmitter functions
75
+ */
76
+ const createWalletConnectSessionEventEmitter = ({ formatAddress, getSession, namespaceKey, sessionTopic, signClient }) => {
77
+ return (0, _dynamic_labs_sdk_client_core.createWalletProviderEventEmitter)({
78
+ removeEventListeners: ({ setupReturnValue: handleSessionEvent }) => {
79
+ signClient.off("session_event", handleSessionEvent);
80
+ },
81
+ setupEventListeners: ({ handleAccountsChanged, handleDisconnected, handleNetworkChanged }) => {
82
+ const handleSessionEvent = ({ params: { event }, topic }) => {
83
+ if (topic !== sessionTopic) return;
84
+ if (event.name === "chainChanged") {
85
+ const networkParam = event.data;
86
+ handleNetworkChanged({ networkId: networkParam.startsWith(`${namespaceKey}:`) ? networkParam.split(":")[1] : networkParam });
87
+ return;
88
+ }
89
+ if (event.name === "accountsChanged") {
90
+ const accountsParam = event.data;
91
+ handleAccountsChanged({ addresses: (0, _dynamic_labs_sdk_client_core.filterDuplicates)(accountsParam.map((account) => {
92
+ if (account.startsWith(`${namespaceKey}:`)) return splitCaip10Token(account).address;
93
+ return account;
94
+ })).map((address) => formatAddress ? formatAddress(address) : address) });
95
+ return;
96
+ }
97
+ if (event.name === "disconnected") {
98
+ handleDisconnected();
99
+ return;
100
+ }
101
+ };
102
+ getSession();
103
+ signClient.on("session_event", handleSessionEvent);
104
+ return handleSessionEvent;
105
+ }
106
+ });
107
+ };
108
+
64
109
  //#endregion
65
110
  //#region src/createWalletAccountsForWalletConnectWalletProvider/createWalletAccountsForWalletConnectWalletProvider.ts
66
111
  const createWalletAccountsForWalletConnectWalletProvider = ({ walletProvider }) => {
@@ -72,8 +117,9 @@ const createWalletAccountsForWalletConnectWalletProvider = ({ walletProvider })
72
117
  * and they have attributed to them the methods, events and networks that are expected
73
118
  * to be supported by the wallet application, as well as which accounts are connected.
74
119
  */
75
- const namespace = walletProvider.session.namespaces[walletProvider.namespaceKey];
76
- (0, _dynamic_labs_sdk_client_core.assertDefined)(namespace, `Namespace key ${walletProvider.namespaceKey} of wallet provider ${walletProvider.key} not found in its session ${walletProvider.topic}`);
120
+ const session = walletProvider.getSession();
121
+ const namespace = session.namespaces[walletProvider.namespaceKey];
122
+ (0, _dynamic_labs_sdk_client_core.assertDefined)(namespace, `Namespace key ${walletProvider.namespaceKey} of wallet provider ${walletProvider.key} not found in its session ${session.topic}`);
77
123
  const accountAddresses = (0, _dynamic_labs_sdk_client_core.filterDuplicates)(namespace.accounts.map((account) => splitCaip10Token(account).address));
78
124
  const formatAddress = walletProvider.formatWalletAddress ?? (({ address }) => address);
79
125
  return accountAddresses.map((address) => ({
@@ -90,253 +136,18 @@ const createWalletAccountsForWalletConnectWalletProvider = ({ walletProvider })
90
136
  }));
91
137
  };
92
138
 
93
- //#endregion
94
- //#region src/sessionTopicsData/schema.ts
95
- const sessionTopicsStorageKeySchema = (0, _dynamic_labs_sdk_client_core.createStorageKeySchema)({
96
- key: "sessionTopics",
97
- schema: zod_mini.record(
98
- /**
99
- * The session topic that identifies this session.
100
- */
101
- zod_mini.string(),
102
- /**
103
- * The session data.
104
- */
105
- zod_mini.object({ shouldAddToDynamicWalletAccounts: zod_mini.boolean() })
106
- )
107
- });
108
-
109
- //#endregion
110
- //#region src/sessionTopicsData/setSessionTopicsData/setSessionTopicsData.ts
111
- /**
112
- * Sets the session topics data.
113
- *
114
- * Read more about it in the sessionTopicsData.types.ts file.
115
- *
116
- * @param update - The function to update the session topics data.
117
- * @param client - The client instance.
118
- */
119
- const setSessionTopicsData = async ({ update }, client) => {
120
- const { storage } = (0, _dynamic_labs_sdk_client_core.getCore)(client);
121
- const newSessionTopicsData = update(await storage.getItem(sessionTopicsStorageKeySchema) ?? {});
122
- if (Object.keys(newSessionTopicsData).length === 0) return storage.removeItem(sessionTopicsStorageKeySchema);
123
- return storage.setItem(sessionTopicsStorageKeySchema, newSessionTopicsData);
124
- };
125
-
126
- //#endregion
127
- //#region src/sessionTopicsData/getSessionTopicsData/getSessionTopicsData.ts
128
- /**
129
- * Gets the session topics data.
130
- *
131
- * Read more about it in the sessionTopicsData.types.ts file.
132
- * @param client - The client instance.
133
- */
134
- const getSessionTopicsData = async (client) => {
135
- const { storage } = (0, _dynamic_labs_sdk_client_core.getCore)(client);
136
- return await storage.getItem(sessionTopicsStorageKeySchema) ?? {};
137
- };
138
-
139
- //#endregion
140
- //#region src/signClient/getSignClient/createSignClient/createSignClient.ts
141
- const createSignClient = async (client) => {
142
- const core = (0, _dynamic_labs_sdk_client_core.getCore)(client);
143
- (0, _dynamic_labs_sdk_client_core.assertDefined)(client.projectSettings, "Project settings not available");
144
- const generalSettings = client.projectSettings.general;
145
- (0, _dynamic_labs_sdk_client_core.assertDefined)(generalSettings.displayName, "Please configure an app name in dashboard to use WalletConnect");
146
- const projectId = client.projectSettings.sdk.walletConnect?.projectId;
147
- (0, _dynamic_labs_sdk_client_core.assertDefined)(projectId, "Please configure a project ID for WalletConnect in dashboard");
148
- const customStoragePrefix = `dynamic_${core.environmentId}_walletconnect`;
149
- const displayName = core.metadata?.name ?? generalSettings.displayName;
150
- const appLogo = core.metadata?.iconUrl ?? generalSettings.appLogo;
151
- return _walletconnect_sign_client.default.init({
152
- customStoragePrefix,
153
- metadata: {
154
- description: "",
155
- icons: appLogo ? [appLogo] : [],
156
- name: displayName,
157
- url: ""
158
- },
159
- projectId
160
- });
161
- };
162
-
163
- //#endregion
164
- //#region src/signClient/getSignClient/getSignClient.ts
165
- const getSignClient = (0, _dynamic_labs_sdk_client_core.createRuntimeServiceAccessKey)("walletConnectSignClient", (client) => createSignClient(client));
166
-
167
- //#endregion
168
- //#region src/namespaceRegistry/getNamespaceRegistry/createNamespaceRegistry/createNamespaceRegistry.ts
169
- const createNamespaceRegistry = () => {
170
- const walletProviderBuilderMap = /* @__PURE__ */ new Map();
171
- return {
172
- getWalletProviderBuilder: (key) => walletProviderBuilderMap.get(key),
173
- registerNamespace: ({ createWalletProviderFromSession, namespaceKey }) => {
174
- walletProviderBuilderMap.set(namespaceKey, createWalletProviderFromSession);
175
- }
176
- };
177
- };
178
-
179
- //#endregion
180
- //#region src/namespaceRegistry/getNamespaceRegistry/getNamespaceRegistry.ts
181
- const getNamespaceRegistry = (0, _dynamic_labs_sdk_client_core.createRuntimeServiceAccessKey)("walletConnectNamespaceRegistry", createNamespaceRegistry);
182
-
183
- //#endregion
184
- //#region src/syncSessionsToWalletProviders/addMissingWalletProvidersForSessions/addMissingWalletProvidersForSessions.ts
185
- const addMissingWalletProvidersForSessions = async ({ sessions, allowOverridingWalletProviders }, client) => {
186
- const namespaceRegistry = getNamespaceRegistry(client);
187
- const walletProviderRegistry = (0, _dynamic_labs_sdk_client_core.getWalletProviderRegistry)(client);
188
- const sessionTopicsData = await getSessionTopicsData(client);
189
- const currentWalletAccounts = (0, _dynamic_labs_sdk_client.getWalletAccounts)(client);
190
- sessions.sort((sessionA, sessionB) => sessionA.expiry - sessionB.expiry);
191
- /**
192
- * All the WC wallet providers that are currently registered
193
- */
194
- const currentWalletProviders = walletProviderRegistry.listProviders().filter(require_isWalletConnectWalletProvider.isWalletConnectWalletProvider);
195
- /**
196
- * Keep record on which walletProviders are tied to active sessions
197
- */
198
- const walletProvidersWithActiveSession = [];
199
- /**
200
- * Maps the ID of the created wallet accounts to the wallet accounts themselves.
201
- */
202
- const createdWalletAccounts = {};
203
- /**
204
- * Keep record on which wallet account IDs were not in the current wallet accounts
205
- * and are therefore brand new.
206
- */
207
- const brandNewWalletAccounts = /* @__PURE__ */ new Set();
208
- /**
209
- * Keep record on which wallet account IDs already exist but will have
210
- * their wallet provider keys overridden to use the new WC wallet provider key.
211
- *
212
- * This will only ever be the case for unverified wallet accounts, however. That is
213
- * because verified wallets will already override the wallet provider key on the verification
214
- * step, which happens at the call to verifyWalletConnectAuths, which happens before this function.
215
- */
216
- const unverifiedWalletAccountsToOverride = /* @__PURE__ */ new Set();
217
- for (const session of sessions) {
218
- const namespaceKeys = Object.keys(session.namespaces);
219
- for (const namespaceKey of namespaceKeys) {
220
- const existingWalletProvider = currentWalletProviders.find((walletProvider$1) => walletProvider$1.namespaceKey === namespaceKey && walletProvider$1.topic === session.topic);
221
- if (existingWalletProvider) {
222
- walletProvidersWithActiveSession.push(existingWalletProvider);
223
- continue;
224
- }
225
- const walletProviderBuilder = namespaceRegistry.getWalletProviderBuilder(namespaceKey);
226
- /**
227
- * There being no wallet provider builder for this namespace simply means the customer
228
- * didn't add an extension for the corresponding chain.
229
- * For instance, if we get a namespace for Solana but only have an Evm extension,
230
- * the solana namespace would fall here. No need to throw.
231
- */
232
- if (!walletProviderBuilder) continue;
233
- const walletProvider = await walletProviderBuilder({ session }, client);
234
- walletProviderRegistry.register({
235
- priority: _dynamic_labs_sdk_client_core.WalletProviderPriority.WALLET_SDK,
236
- walletProvider
237
- });
238
- walletProvidersWithActiveSession.push(walletProvider);
239
- if (!sessionTopicsData[session.topic].shouldAddToDynamicWalletAccounts) continue;
240
- const namespaceWalletAccounts = createWalletAccountsForWalletConnectWalletProvider({ walletProvider });
241
- if (namespaceWalletAccounts.length === 0) continue;
242
- for (const createdWalletAccount of namespaceWalletAccounts) {
243
- if (!currentWalletAccounts.some((existingAccount$1) => existingAccount$1.id === createdWalletAccount.id)) {
244
- createdWalletAccounts[createdWalletAccount.id] = createdWalletAccount;
245
- brandNewWalletAccounts.add(createdWalletAccount.id);
246
- continue;
247
- }
248
- const existingAccount = currentWalletAccounts.find((existingAccount$1) => existingAccount$1.id === createdWalletAccount.id);
249
- if (!(existingAccount && existingAccount.walletProviderKey !== createdWalletAccount.walletProviderKey)) continue;
250
- const existingWalletIsWalletConnect = (0, _dynamic_labs_sdk_client_core.splitWalletProviderKey)(existingAccount.walletProviderKey).walletProviderType === _dynamic_labs_sdk_api_core.WalletProviderEnum.WalletConnect;
251
- if (allowOverridingWalletProviders || existingWalletIsWalletConnect) {
252
- createdWalletAccounts[createdWalletAccount.id] = createdWalletAccount;
253
- if (existingAccount.verifiedCredentialId)
254
- /**
255
- * A verified wallet account only arrives here when its wallet provider key is lacking the suffix,
256
- * which should be the sessionTopic. So we just need to add its entry to the verifiedCredentialId map.
257
- */
258
- (0, _dynamic_labs_sdk_client_core.updateWalletProviderKeysForVerifiedCredentials)({ keysToUpdate: { [existingAccount.verifiedCredentialId]: walletProvider.key } }, client);
259
- else unverifiedWalletAccountsToOverride.add(createdWalletAccount.id);
260
- }
261
- }
262
- }
263
- }
264
- if (unverifiedWalletAccountsToOverride.size === 0 && brandNewWalletAccounts.size === 0) return { walletProvidersWithActiveSession };
265
- (0, _dynamic_labs_sdk_client_core.setUnverifiedWalletAccounts)({ unverifiedWalletAccountsToUpdate: Object.values(createdWalletAccounts) }, client);
266
- if (brandNewWalletAccounts.size > 0) for (const walletAccountId of brandNewWalletAccounts) (0, _dynamic_labs_sdk_client_core.createVisit)({ walletAccount: createdWalletAccounts[walletAccountId] }, client);
267
- (0, _dynamic_labs_sdk_client_core.emitWalletAccountsChangedEvent)(client);
268
- return { walletProvidersWithActiveSession };
269
- };
270
-
271
- //#endregion
272
- //#region src/syncSessionsToWalletProviders/syncSessionsToWalletProviders.ts
273
- const doSyncSessionsToWalletProviders = async ({ allowOverridingWalletProviders }, client) => {
274
- await (0, _dynamic_labs_sdk_client_core.waitForProjectSettings)(client);
275
- const signClient = await getSignClient(client);
276
- const walletProviderRegistry = (0, _dynamic_labs_sdk_client_core.getWalletProviderRegistry)(client);
277
- const sessionTopicsData = await getSessionTopicsData(client);
278
- const sessions = signClient.session.getAll().filter((session) => {
279
- if (sessionTopicsData[session.topic]) return true;
280
- signClient.disconnect({
281
- reason: _walletconnect_utils.SDK_ERRORS.USER_DISCONNECTED,
282
- topic: session.topic
283
- });
284
- return false;
285
- });
286
- /**
287
- * All the WC wallet providers that are currently registered
288
- */
289
- const currentWalletProviders = walletProviderRegistry.listProviders().filter(require_isWalletConnectWalletProvider.isWalletConnectWalletProvider);
290
- for (const walletProvider of currentWalletProviders) if (!sessions.some((session) => session.topic === walletProvider.topic && walletProvider.namespaceKey in session.namespaces)) walletProviderRegistry.unregister(walletProvider.key);
291
- const { walletProvidersWithActiveSession: updatedWalletProviders } = await addMissingWalletProvidersForSessions({
292
- allowOverridingWalletProviders,
293
- sessions
294
- }, client);
295
- const currentWalletAccounts = (0, _dynamic_labs_sdk_client.getWalletAccounts)(client);
296
- /**
297
- * Maps which sessions have wallet accounts associated with them
298
- */
299
- const sessionTopicHasWalletAccountMap = /* @__PURE__ */ new Map();
300
- for (const walletProvider of updatedWalletProviders) {
301
- const sessionTopic = walletProvider.topic;
302
- if (!sessionTopicHasWalletAccountMap.has(sessionTopic)) sessionTopicHasWalletAccountMap.set(sessionTopic, false);
303
- if (sessionTopicHasWalletAccountMap.get(sessionTopic)) continue;
304
- if (currentWalletAccounts.filter((walletAccount) => walletAccount.walletProviderKey === walletProvider.key).length > 0) sessionTopicHasWalletAccountMap.set(sessionTopic, true);
305
- }
306
- for (const [sessionTopic, hasWalletAccount] of sessionTopicHasWalletAccountMap) {
307
- if (hasWalletAccount || !sessionTopicsData[sessionTopic].shouldAddToDynamicWalletAccounts) continue;
308
- const session = sessions.find((session$1) => session$1.topic === sessionTopic);
309
- if (session) signClient.disconnect({
310
- reason: _walletconnect_utils.SDK_ERRORS.USER_DISCONNECTED,
311
- topic: session.topic
312
- });
313
- for (const walletProvider of updatedWalletProviders) if (walletProvider.topic === sessionTopic) walletProviderRegistry.unregister(walletProvider.key);
314
- }
315
- const activeSessionTopics = new Set(signClient.session.getAll().map((session) => session.topic));
316
- await setSessionTopicsData({ update: (sessionTopicsData$1) => {
317
- return Object.fromEntries(Object.entries(sessionTopicsData$1).filter(([topic]) => activeSessionTopics.has(topic)));
318
- } }, client);
319
- };
320
- const syncSessionsToWalletProviders = async ({ debounceTime = 0, allowOverridingWalletProviders }, client) => {
321
- return (0, _dynamic_labs_sdk_client_core.getCore)(client).debouncedMutex({
322
- callback: async () => doSyncSessionsToWalletProviders({ allowOverridingWalletProviders }, client),
323
- debounceTime,
324
- lockKey: "wallet-connect-sync-sessions-to-wallet-providers"
325
- });
326
- };
327
-
328
139
  //#endregion
329
140
  //#region src/importWalletConnectSession/importWalletConnectSession.ts
330
141
  /**
331
142
  * This is a lower level function that is used to connect a WalletConnect session to the Dynamic SDK.
332
143
  * It will generate the wallet providers and wallet accounts for all accounts in the session.
333
144
  *
334
- * @param session - The session object created from the approval promise of the signClient.connect method.
145
+ * @param sessionTopic - The session topic of the WalletConnect session.
335
146
  * @param addToDynamicWalletAccounts - Whether to add the wallet accounts to the dynamic's wallet accounts.
336
147
  * @param client - The Dynamic client instance. Only required when using multiple Dynamic clients.
337
148
  * @returns The wallet accounts.
338
149
  */
339
- const importWalletConnectSession = async ({ addToDynamicWalletAccounts = true, session }, client) => {
150
+ const importWalletConnectSession = async ({ addToDynamicWalletAccounts = true, sessionTopic, walletProviderBuilder }, client) => {
340
151
  /**
341
152
  * You would normally get the session from the signClient.connect method. It takes the optionalNamespaces as an argument.
342
153
  *
@@ -369,30 +180,48 @@ const importWalletConnectSession = async ({ addToDynamicWalletAccounts = true, s
369
180
  * Where each chain is a CAIP-2 formatted string, for example: "eip155:1"
370
181
  * — see docs: https://chainagnostic.org/CAIPs/caip-2
371
182
  */
372
- const sessionData = { shouldAddToDynamicWalletAccounts: addToDynamicWalletAccounts };
373
- await setSessionTopicsData({ update: (sessionTopicsData) => ({
374
- ...sessionTopicsData,
375
- [session.topic]: sessionData
376
- }) }, client);
183
+ const walletProviderRegistry = (0, _dynamic_labs_sdk_client_core.getWalletProviderRegistry)(client);
184
+ const walletProvider = await walletProviderBuilder({ sessionTopic }, client);
185
+ walletProviderRegistry.register({
186
+ priority: _dynamic_labs_sdk_client_core.WalletProviderPriority.WALLET_SDK,
187
+ walletProvider
188
+ });
189
+ const walletAccounts = createWalletAccountsForWalletConnectWalletProvider({ walletProvider });
190
+ const existingWalletAccounts = (0, _dynamic_labs_sdk_client.getWalletAccounts)(client);
377
191
  /**
378
- * Once the connection is established, we ensure that all active connections (aka sessions)
379
- * are synced to our WalletConnect wallet providers.
380
- * Each session can be uniquely identified by its topic (a random string generated by WalletConnect).
381
- *
382
- * Each session may have multiple wallet providers, one for each namespace (aka our SDK chains)
192
+ * Keep track of which of the new wallet accounts are unverified to add later.
383
193
  */
384
- await syncSessionsToWalletProviders({ allowOverridingWalletProviders: true }, client);
385
- const sessionWalletProviders = (0, _dynamic_labs_sdk_client_core.getWalletProviders)(client).filter(require_isWalletConnectWalletProvider.isWalletConnectWalletProvider).filter((provider) => provider.topic === session.topic);
194
+ const unverifiedWalletAccounts = [];
195
+ let walletAccountsChanged = false;
386
196
  /**
387
- * Upon setting the session data to NOT externally owned, syncSessionsToWalletProviders will have
388
- * already created the wallet accounts for each of this session's wallet providers — so all we need
389
- * to do is retrieve them.
197
+ * For all verified wallet accounts that are tied to this session, we need to update
198
+ * the map of verified credential to wallet provider key
390
199
  */
391
- if (addToDynamicWalletAccounts) {
392
- const sessionWalletProviderKeys = sessionWalletProviders.map((provider) => provider.key);
393
- return { walletAccounts: (0, _dynamic_labs_sdk_client.getWalletAccounts)(client).filter((walletAccount) => sessionWalletProviderKeys.includes(walletAccount.walletProviderKey)) };
200
+ const verifiedCredentialIdToWalletProviderKeyMap = {};
201
+ walletAccounts.forEach((walletAccount) => {
202
+ const matchingWalletAccount = existingWalletAccounts.find((existingWalletAccount) => existingWalletAccount.id === walletAccount.id);
203
+ const matchingVerifiedCredentialId = matchingWalletAccount?.verifiedCredentialId;
204
+ if (!matchingVerifiedCredentialId) unverifiedWalletAccounts.push({
205
+ isNew: !matchingWalletAccount,
206
+ walletAccount
207
+ });
208
+ else {
209
+ verifiedCredentialIdToWalletProviderKeyMap[matchingVerifiedCredentialId] = walletProvider.key;
210
+ walletAccount.verifiedCredentialId = matchingVerifiedCredentialId;
211
+ }
212
+ });
213
+ if (Object.keys(verifiedCredentialIdToWalletProviderKeyMap).length > 0) {
214
+ (0, _dynamic_labs_sdk_client_core.updateWalletProviderKeysForVerifiedCredentials)({ keysToUpdate: verifiedCredentialIdToWalletProviderKeyMap }, client);
215
+ walletAccountsChanged = true;
216
+ }
217
+ if (addToDynamicWalletAccounts && unverifiedWalletAccounts.length > 0) {
218
+ walletAccountsChanged = true;
219
+ (0, _dynamic_labs_sdk_client_core.setUnverifiedWalletAccounts)({ unverifiedWalletAccountsToUpdate: unverifiedWalletAccounts.map(({ walletAccount }) => walletAccount) }, client);
220
+ const newWalletAccounts = unverifiedWalletAccounts.filter(({ isNew }) => isNew).map(({ walletAccount }) => walletAccount);
221
+ for (const walletAccount of newWalletAccounts) (0, _dynamic_labs_sdk_client_core.createVisit)({ walletAccount }, client);
394
222
  }
395
- return { walletAccounts: sessionWalletProviders.map((walletProvider) => createWalletAccountsForWalletConnectWalletProvider({ walletProvider })).flat() };
223
+ if (walletAccountsChanged) (0, _dynamic_labs_sdk_client_core.emitWalletAccountsChangedEvent)(client);
224
+ return { walletAccounts };
396
225
  };
397
226
 
398
227
  //#endregion
@@ -408,6 +237,8 @@ const verifyWalletConnectAuths = async ({ auths, chain, walletDisplayName }, cli
408
237
  *
409
238
  * Usually, there will be multiple auths if the wallet app bundles signatures for multiple
410
239
  * networks at once, so the only field that will change among auth iss will be the networkId under iss.
240
+ *
241
+ * Docs on Cacao: https://chainagnostic.org/CAIPs/caip-74
411
242
  */
412
243
  const visitedAddresses = /* @__PURE__ */ new Set();
413
244
  const authsFilteredByAddress = auths.filter((auth) => {
@@ -456,7 +287,7 @@ const verifyWalletConnectAuths = async ({ auths, chain, walletDisplayName }, cli
456
287
  * @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
457
288
  * @returns A promise that resolves to the connected and verified wallet account.
458
289
  */
459
- const importWalletConnectAuthResponse = async ({ authResponse, chain }, client) => {
290
+ const importWalletConnectAuthResponse = async ({ authResponse, chain, walletProviderBuilder }, client) => {
460
291
  const { session, auths } = authResponse;
461
292
  /**
462
293
  * Some wallets do not support WC's authenticate method (such as Rainbow).
@@ -464,20 +295,12 @@ const importWalletConnectAuthResponse = async ({ authResponse, chain }, client)
464
295
  * Therefore we must call verify individually.
465
296
  */
466
297
  if (!auths || auths.length === 0) {
467
- /**
468
- * Set addToDynamicWalletAccounts to false to avoid adding the wallet accounts to the dynamic's wallet accounts
469
- * while we are still verifying them.
470
- * After verifying, we set addToDynamicWalletAccounts back to true.
471
- */
472
298
  const { walletAccounts: walletAccounts$1 } = await importWalletConnectSession({
473
299
  addToDynamicWalletAccounts: false,
474
- session
300
+ sessionTopic: session.topic,
301
+ walletProviderBuilder
475
302
  }, client);
476
303
  for (const walletAccount of walletAccounts$1) await (0, _dynamic_labs_sdk_client.verifyWalletAccount)({ walletAccount }, client);
477
- await setSessionTopicsData({ update: (sessionTopicsData) => ({
478
- ...sessionTopicsData,
479
- [session.topic]: { shouldAddToDynamicWalletAccounts: true }
480
- }) }, client);
481
304
  return { walletAccounts: walletAccounts$1 };
482
305
  }
483
306
  /**
@@ -491,62 +314,54 @@ const importWalletConnectAuthResponse = async ({ authResponse, chain }, client)
491
314
  }, client);
492
315
  const { walletAccounts } = await importWalletConnectSession({
493
316
  addToDynamicWalletAccounts: true,
494
- session
317
+ sessionTopic: session.topic,
318
+ walletProviderBuilder
495
319
  }, client);
496
- (0, _dynamic_labs_sdk_client_core.updateWalletProviderKeysForVerifiedCredentials)({ keysToUpdate: Object.fromEntries(walletAccounts.map((walletAccount) => {
497
- const { verifiedCredentialId } = walletAccount;
498
- (0, _dynamic_labs_sdk_client_core.assertDefined)(verifiedCredentialId, `Wallet account of ID ${walletAccount.id} still has no verified credential after being verified`);
499
- return [verifiedCredentialId, walletAccount.walletProviderKey];
500
- })) }, client);
501
320
  return { walletAccounts };
502
321
  };
503
322
 
504
323
  //#endregion
505
- //#region src/sessionTopicsData/clearTemporarySessions/clearTemporarySessions.ts
506
- /**
507
- * Clears all temporary sessions and disconnects from them.
508
- *
509
- * External sessions are for WC sessions whose accounts are not supposed to be saved in
510
- * the current dynamic's wallet accounts.
511
- * Since we don't own the created wallet accounts, we must keep their corresponding WC sessions alive
512
- * until the page is refreshed or the customer removes the wallet account.
513
- *
514
- * @param client - The client instance.
515
- */
516
- const clearTemporarySessions = async (client) => {
517
- const signClient = await getSignClient(client);
518
- const sessionTopicsData = await getSessionTopicsData(client);
519
- const temporarySessionTopics = Object.keys(sessionTopicsData).filter((topic) => !sessionTopicsData[topic].shouldAddToDynamicWalletAccounts);
520
- if (temporarySessionTopics.length === 0) return;
521
- for (const topic of temporarySessionTopics) await signClient.disconnect({
522
- reason: _walletconnect_utils.SDK_ERRORS.USER_DISCONNECTED,
523
- topic
324
+ //#region src/signClient/getSignClient/createSignClient/createSignClient.ts
325
+ const createSignClient = async (client) => {
326
+ const core = (0, _dynamic_labs_sdk_client_core.getCore)(client);
327
+ (0, _dynamic_labs_sdk_client_core.assertDefined)(client.projectSettings, "Project settings not available");
328
+ const generalSettings = client.projectSettings.general;
329
+ (0, _dynamic_labs_sdk_client_core.assertDefined)(generalSettings.displayName, "Please configure an app name in dashboard to use WalletConnect");
330
+ const projectId = client.projectSettings.sdk.walletConnect?.projectId;
331
+ (0, _dynamic_labs_sdk_client_core.assertDefined)(projectId, "Please configure a project ID for WalletConnect in dashboard");
332
+ const customStoragePrefix = `dynamic_${core.environmentId}_walletconnect`;
333
+ const displayName = core.metadata?.name ?? generalSettings.displayName;
334
+ const appLogo = core.metadata?.iconUrl ?? generalSettings.appLogo;
335
+ return _walletconnect_sign_client.default.init({
336
+ customStoragePrefix,
337
+ metadata: {
338
+ description: "",
339
+ icons: appLogo ? [appLogo] : [],
340
+ name: displayName,
341
+ redirect: {
342
+ native: core.metadata?.nativeLink,
343
+ universal: core.metadata?.universalLink
344
+ },
345
+ url: core.metadata?.universalLink ?? ""
346
+ },
347
+ projectId
524
348
  });
525
- const newSessionTopicsData = Object.fromEntries(Object.entries(sessionTopicsData).filter(([, { shouldAddToDynamicWalletAccounts: isPermanentSession }]) => isPermanentSession));
526
- await setSessionTopicsData({ update: () => newSessionTopicsData }, client);
527
349
  };
528
350
 
529
351
  //#endregion
530
- //#region src/initializeWalletConnectExtension/initializeWalletConnectExtension.ts
531
- const initializeWalletConnectExtension = async (client) => {
532
- await clearTemporarySessions(client);
533
- await syncSessionsToWalletProviders({
534
- allowOverridingWalletProviders: false,
535
- debounceTime: 200
536
- }, client);
537
- };
352
+ //#region src/signClient/getSignClient/getSignClient.ts
353
+ const getSignClient = (0, _dynamic_labs_sdk_client_core.createRuntimeServiceAccessKey)("walletConnectSignClient", (client) => createSignClient(client));
538
354
 
539
355
  //#endregion
540
356
  //#region src/exports/core.ts
541
- (0, _dynamic_labs_sdk_assert_package_version.assertPackageVersion)(require_isWalletConnectWalletProvider.name, require_isWalletConnectWalletProvider.version);
357
+ (0, _dynamic_labs_sdk_assert_package_version.assertPackageVersion)(require_SessionClosedUnexpectedlyError.name, require_SessionClosedUnexpectedlyError.version);
542
358
 
543
359
  //#endregion
360
+ exports.SessionClosedUnexpectedlyError = require_SessionClosedUnexpectedlyError.SessionClosedUnexpectedlyError;
544
361
  exports.__toESM = __toESM;
545
- exports.getNamespaceRegistry = getNamespaceRegistry;
362
+ exports.createWalletConnectSessionEventEmitter = createWalletConnectSessionEventEmitter;
546
363
  exports.getSignClient = getSignClient;
547
364
  exports.importWalletConnectAuthResponse = importWalletConnectAuthResponse;
548
365
  exports.importWalletConnectSession = importWalletConnectSession;
549
- exports.initializeWalletConnectExtension = initializeWalletConnectExtension;
550
366
  exports.splitCaip10Token = splitCaip10Token;
551
- exports.syncSessionsToWalletProviders = syncSessionsToWalletProviders;
552
367
  //# sourceMappingURL=core.cjs.js.map