@dynamic-labs-sdk/wallet-connect 0.1.2 → 0.2.3

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 (148) hide show
  1. package/dist/core.cjs.js +552 -0
  2. package/dist/core.cjs.js.map +1 -0
  3. package/dist/core.esm.js +516 -0
  4. package/dist/core.esm.js.map +1 -0
  5. package/dist/createWalletAccountsForWalletConnectWalletProvider/createWalletAccountsForWalletConnectWalletProvider.d.ts.map +1 -0
  6. package/dist/createWalletAccountsForWalletConnectWalletProvider/index.d.ts.map +1 -0
  7. package/{src → dist}/events.d.ts +2 -0
  8. package/dist/events.d.ts.map +1 -0
  9. package/dist/exports/core.d.ts.map +1 -0
  10. package/dist/exports/index.d.ts.map +1 -0
  11. package/dist/importWalletConnectAuthResponse/importWalletConnectAuthResponse.d.ts.map +1 -0
  12. package/dist/importWalletConnectAuthResponse/index.d.ts.map +1 -0
  13. package/dist/importWalletConnectAuthResponse/verifyWalletConnectAuths/index.d.ts.map +1 -0
  14. package/dist/importWalletConnectAuthResponse/verifyWalletConnectAuths/verifyWalletConnectAuths.d.ts.map +1 -0
  15. package/dist/importWalletConnectSession/importWalletConnectSession.d.ts.map +1 -0
  16. package/dist/importWalletConnectSession/index.d.ts.map +1 -0
  17. package/dist/index.cjs.js +17 -0
  18. package/dist/index.cjs.js.map +1 -0
  19. package/dist/index.esm.js +10 -0
  20. package/dist/index.esm.js.map +1 -0
  21. package/dist/initializeWalletConnectExtension/index.d.ts.map +1 -0
  22. package/dist/initializeWalletConnectExtension/initializeWalletConnectExtension.d.ts.map +1 -0
  23. package/dist/isWalletConnectWalletProvider/index.d.ts.map +1 -0
  24. package/dist/isWalletConnectWalletProvider/isWalletConnectWalletProvider.d.ts.map +1 -0
  25. package/dist/isWalletConnectWalletProvider-BNtoAWXb.esm.js +18 -0
  26. package/dist/isWalletConnectWalletProvider-BNtoAWXb.esm.js.map +1 -0
  27. package/dist/isWalletConnectWalletProvider-Caep_Q46.cjs.js +36 -0
  28. package/dist/isWalletConnectWalletProvider-Caep_Q46.cjs.js.map +1 -0
  29. package/dist/namespaceRegistry/getNamespaceRegistry/createNamespaceRegistry/createNamespaceRegistry.d.ts.map +1 -0
  30. package/dist/namespaceRegistry/getNamespaceRegistry/createNamespaceRegistry/index.d.ts.map +1 -0
  31. package/dist/namespaceRegistry/getNamespaceRegistry/getNamespaceRegistry.d.ts +2 -0
  32. package/dist/namespaceRegistry/getNamespaceRegistry/getNamespaceRegistry.d.ts.map +1 -0
  33. package/dist/namespaceRegistry/getNamespaceRegistry/index.d.ts.map +1 -0
  34. package/dist/namespaceRegistry/index.d.ts.map +1 -0
  35. package/dist/namespaceRegistry/namespaceRegistry.types.d.ts.map +1 -0
  36. package/dist/sessionTopicsData/clearTemporarySessions/clearTemporarySessions.d.ts.map +1 -0
  37. package/dist/sessionTopicsData/clearTemporarySessions/index.d.ts.map +1 -0
  38. package/{src → dist}/sessionTopicsData/getSessionTopicsData/getSessionTopicsData.d.ts +3 -1
  39. package/dist/sessionTopicsData/getSessionTopicsData/getSessionTopicsData.d.ts.map +1 -0
  40. package/dist/sessionTopicsData/getSessionTopicsData/index.d.ts.map +1 -0
  41. package/dist/sessionTopicsData/schema.d.ts +4 -0
  42. package/dist/sessionTopicsData/schema.d.ts.map +1 -0
  43. package/dist/sessionTopicsData/sessionTopicsData.types.d.ts.map +1 -0
  44. package/dist/sessionTopicsData/setSessionTopicsData/index.d.ts.map +1 -0
  45. package/{src → dist}/sessionTopicsData/setSessionTopicsData/setSessionTopicsData.d.ts +1 -1
  46. package/dist/sessionTopicsData/setSessionTopicsData/setSessionTopicsData.d.ts.map +1 -0
  47. package/dist/signClient/getSignClient/createSignClient/createSignClient.d.ts.map +1 -0
  48. package/dist/signClient/getSignClient/createSignClient/index.d.ts.map +1 -0
  49. package/dist/signClient/getSignClient/getSignClient.d.ts +2 -0
  50. package/dist/signClient/getSignClient/getSignClient.d.ts.map +1 -0
  51. package/dist/signClient/getSignClient/index.d.ts.map +1 -0
  52. package/dist/splitCaip10Token/index.d.ts.map +1 -0
  53. package/dist/splitCaip10Token/splitCaip10Token.d.ts.map +1 -0
  54. package/dist/syncSessionsToWalletProviders/addMissingWalletProvidersForSessions/addMissingWalletProvidersForSessions.d.ts.map +1 -0
  55. package/dist/syncSessionsToWalletProviders/addMissingWalletProvidersForSessions/index.d.ts.map +1 -0
  56. package/dist/syncSessionsToWalletProviders/index.d.ts.map +1 -0
  57. package/{src → dist}/syncSessionsToWalletProviders/syncSessionsToWalletProviders.d.ts +1 -1
  58. package/dist/syncSessionsToWalletProviders/syncSessionsToWalletProviders.d.ts.map +1 -0
  59. package/dist/tsconfig.lib.tsbuildinfo +1 -0
  60. package/dist/walletConnectWalletProvider.types.d.ts.map +1 -0
  61. package/package.json +27 -15
  62. package/CHANGELOG.md +0 -606
  63. package/core.cjs.d.ts +0 -1
  64. package/core.cjs.js +0 -636
  65. package/core.esm.d.ts +0 -1
  66. package/core.esm.js +0 -609
  67. package/index.cjs.d.ts +0 -1
  68. package/index.cjs.js +0 -13
  69. package/index.esm.d.ts +0 -1
  70. package/index.esm.js +0 -6
  71. package/isWalletConnectWalletProvider.cjs.js +0 -23
  72. package/isWalletConnectWalletProvider.esm.js +0 -19
  73. package/src/createWalletAccountsForWalletConnectWalletProvider/createWalletAccountsForWalletConnectWalletProvider.d.ts.map +0 -1
  74. package/src/createWalletAccountsForWalletConnectWalletProvider/index.d.ts.map +0 -1
  75. package/src/events.d.ts.map +0 -1
  76. package/src/exports/core.d.ts.map +0 -1
  77. package/src/exports/index.d.ts.map +0 -1
  78. package/src/importWalletConnectAuthResponse/importWalletConnectAuthResponse.d.ts.map +0 -1
  79. package/src/importWalletConnectAuthResponse/index.d.ts.map +0 -1
  80. package/src/importWalletConnectAuthResponse/verifyWalletConnectAuths/index.d.ts.map +0 -1
  81. package/src/importWalletConnectAuthResponse/verifyWalletConnectAuths/verifyWalletConnectAuths.d.ts.map +0 -1
  82. package/src/importWalletConnectSession/importWalletConnectSession.d.ts.map +0 -1
  83. package/src/importWalletConnectSession/index.d.ts.map +0 -1
  84. package/src/initializeWalletConnectExtension/index.d.ts.map +0 -1
  85. package/src/initializeWalletConnectExtension/initializeWalletConnectExtension.d.ts.map +0 -1
  86. package/src/isWalletConnectWalletProvider/index.d.ts.map +0 -1
  87. package/src/isWalletConnectWalletProvider/isWalletConnectWalletProvider.d.ts.map +0 -1
  88. package/src/namespaceRegistry/getNamespaceRegistry/createNamespaceRegistry/createNamespaceRegistry.d.ts.map +0 -1
  89. package/src/namespaceRegistry/getNamespaceRegistry/createNamespaceRegistry/index.d.ts.map +0 -1
  90. package/src/namespaceRegistry/getNamespaceRegistry/getNamespaceRegistry.d.ts +0 -2
  91. package/src/namespaceRegistry/getNamespaceRegistry/getNamespaceRegistry.d.ts.map +0 -1
  92. package/src/namespaceRegistry/getNamespaceRegistry/index.d.ts.map +0 -1
  93. package/src/namespaceRegistry/index.d.ts.map +0 -1
  94. package/src/namespaceRegistry/namespaceRegistry.types.d.ts.map +0 -1
  95. package/src/sessionTopicsData/clearTemporarySessions/clearTemporarySessions.d.ts.map +0 -1
  96. package/src/sessionTopicsData/clearTemporarySessions/index.d.ts.map +0 -1
  97. package/src/sessionTopicsData/getSessionTopicsData/getSessionTopicsData.d.ts.map +0 -1
  98. package/src/sessionTopicsData/getSessionTopicsData/index.d.ts.map +0 -1
  99. package/src/sessionTopicsData/schema.d.ts +0 -2
  100. package/src/sessionTopicsData/schema.d.ts.map +0 -1
  101. package/src/sessionTopicsData/sessionTopicsData.types.d.ts.map +0 -1
  102. package/src/sessionTopicsData/setSessionTopicsData/index.d.ts.map +0 -1
  103. package/src/sessionTopicsData/setSessionTopicsData/setSessionTopicsData.d.ts.map +0 -1
  104. package/src/signClient/getSignClient/createSignClient/createSignClient.d.ts.map +0 -1
  105. package/src/signClient/getSignClient/createSignClient/index.d.ts.map +0 -1
  106. package/src/signClient/getSignClient/getSignClient.d.ts +0 -2
  107. package/src/signClient/getSignClient/getSignClient.d.ts.map +0 -1
  108. package/src/signClient/getSignClient/index.d.ts.map +0 -1
  109. package/src/splitCaip10Token/index.d.ts.map +0 -1
  110. package/src/splitCaip10Token/splitCaip10Token.d.ts.map +0 -1
  111. package/src/syncSessionsToWalletProviders/addMissingWalletProvidersForSessions/addMissingWalletProvidersForSessions.d.ts.map +0 -1
  112. package/src/syncSessionsToWalletProviders/addMissingWalletProvidersForSessions/index.d.ts.map +0 -1
  113. package/src/syncSessionsToWalletProviders/index.d.ts.map +0 -1
  114. package/src/syncSessionsToWalletProviders/syncSessionsToWalletProviders.d.ts.map +0 -1
  115. package/src/walletConnectWalletProvider.types.d.ts.map +0 -1
  116. /package/{src → dist}/createWalletAccountsForWalletConnectWalletProvider/createWalletAccountsForWalletConnectWalletProvider.d.ts +0 -0
  117. /package/{src → dist}/createWalletAccountsForWalletConnectWalletProvider/index.d.ts +0 -0
  118. /package/{src → dist}/exports/core.d.ts +0 -0
  119. /package/{src → dist}/exports/index.d.ts +0 -0
  120. /package/{src → dist}/importWalletConnectAuthResponse/importWalletConnectAuthResponse.d.ts +0 -0
  121. /package/{src → dist}/importWalletConnectAuthResponse/index.d.ts +0 -0
  122. /package/{src → dist}/importWalletConnectAuthResponse/verifyWalletConnectAuths/index.d.ts +0 -0
  123. /package/{src → dist}/importWalletConnectAuthResponse/verifyWalletConnectAuths/verifyWalletConnectAuths.d.ts +0 -0
  124. /package/{src → dist}/importWalletConnectSession/importWalletConnectSession.d.ts +0 -0
  125. /package/{src → dist}/importWalletConnectSession/index.d.ts +0 -0
  126. /package/{src → dist}/initializeWalletConnectExtension/index.d.ts +0 -0
  127. /package/{src → dist}/initializeWalletConnectExtension/initializeWalletConnectExtension.d.ts +0 -0
  128. /package/{src → dist}/isWalletConnectWalletProvider/index.d.ts +0 -0
  129. /package/{src → dist}/isWalletConnectWalletProvider/isWalletConnectWalletProvider.d.ts +0 -0
  130. /package/{src → dist}/namespaceRegistry/getNamespaceRegistry/createNamespaceRegistry/createNamespaceRegistry.d.ts +0 -0
  131. /package/{src → dist}/namespaceRegistry/getNamespaceRegistry/createNamespaceRegistry/index.d.ts +0 -0
  132. /package/{src → dist}/namespaceRegistry/getNamespaceRegistry/index.d.ts +0 -0
  133. /package/{src → dist}/namespaceRegistry/index.d.ts +0 -0
  134. /package/{src → dist}/namespaceRegistry/namespaceRegistry.types.d.ts +0 -0
  135. /package/{src → dist}/sessionTopicsData/clearTemporarySessions/clearTemporarySessions.d.ts +0 -0
  136. /package/{src → dist}/sessionTopicsData/clearTemporarySessions/index.d.ts +0 -0
  137. /package/{src → dist}/sessionTopicsData/getSessionTopicsData/index.d.ts +0 -0
  138. /package/{src → dist}/sessionTopicsData/sessionTopicsData.types.d.ts +0 -0
  139. /package/{src → dist}/sessionTopicsData/setSessionTopicsData/index.d.ts +0 -0
  140. /package/{src → dist}/signClient/getSignClient/createSignClient/createSignClient.d.ts +0 -0
  141. /package/{src → dist}/signClient/getSignClient/createSignClient/index.d.ts +0 -0
  142. /package/{src → dist}/signClient/getSignClient/index.d.ts +0 -0
  143. /package/{src → dist}/splitCaip10Token/index.d.ts +0 -0
  144. /package/{src → dist}/splitCaip10Token/splitCaip10Token.d.ts +0 -0
  145. /package/{src → dist}/syncSessionsToWalletProviders/addMissingWalletProvidersForSessions/addMissingWalletProvidersForSessions.d.ts +0 -0
  146. /package/{src → dist}/syncSessionsToWalletProviders/addMissingWalletProvidersForSessions/index.d.ts +0 -0
  147. /package/{src → dist}/syncSessionsToWalletProviders/index.d.ts +0 -0
  148. /package/{src → dist}/walletConnectWalletProvider.types.d.ts +0 -0
package/core.esm.js DELETED
@@ -1,609 +0,0 @@
1
- import { assertPackageVersion } from '@dynamic-labs-sdk/assert-package-version';
2
- import { i as isWalletConnectWalletProvider, n as name, v as version } from './isWalletConnectWalletProvider.esm.js';
3
- import { getWalletAccounts, verifyWalletAccount } from '@dynamic-labs-sdk/client';
4
- import { assertDefined, filterDuplicates, formatWalletAccountId, createStorageKeySchema, getCore, createRuntimeServiceAccessKey, getWalletProviderRegistry, WalletProviderPriority, splitWalletProviderKey, updateWalletProviderKeysForVerifiedCredentials, setUnverifiedWalletAccounts, createVisit, emitWalletAccountsChangedEvent, waitForProjectSettings, getWalletProviders, verifySignInMessage, updateAuthFromVerifyResponse } from '@dynamic-labs-sdk/client/core';
5
- import * as z from 'zod/mini';
6
- import { SDK_ERRORS, formatMessage } from '@walletconnect/utils';
7
- import SignClient from '@walletconnect/sign-client';
8
- import { WalletProviderEnum } from '@dynamic-labs/sdk-api-core';
9
-
10
- function _extends() {
11
- _extends = Object.assign || function assign(target) {
12
- for(var i = 1; i < arguments.length; i++){
13
- var source = arguments[i];
14
- for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
15
- }
16
- return target;
17
- };
18
- return _extends.apply(this, arguments);
19
- }
20
-
21
- /**
22
- * An account in WalletConnect follows the CAIP-10 standard
23
- * and is formatted as follows:
24
- *
25
- * `<namespace>:<chain>:<address>`
26
- *
27
- * Where:
28
- * - `<namespace>` is the namespace key (e.g. "eip155")
29
- * - `<chain>` is what we refer to as a network ID (e.g. "1")
30
- * - `<address>` is the address of the account (e.g. "0x1234567890")
31
- *
32
- * See docs: https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-10.md
33
- *
34
- * @returns An object with the address, namespace key, and network id
35
- */ const splitCaip10Token = (caip10Token)=>{
36
- const [namespaceKey, networkId, address] = caip10Token.split(':');
37
- return {
38
- address,
39
- namespaceKey,
40
- networkId
41
- };
42
- };
43
-
44
- const createWalletAccountsForWalletConnectWalletProvider = ({ walletProvider })=>{
45
- /**
46
- * Since each session can have multiple namespaces, we need to find the namespace
47
- * that corresponds to this specific wallet provider.
48
- *
49
- * A WalletConnect namespace is the equivalent of what we refer to as a chain in the SDK,
50
- * and they have attributed to them the methods, events and networks that are expected
51
- * to be supported by the wallet application, as well as which accounts are connected.
52
- */ const namespace = walletProvider.session.namespaces[walletProvider.namespaceKey];
53
- assertDefined(namespace, `Namespace key ${walletProvider.namespaceKey} of wallet provider ${walletProvider.key} not found in its session ${walletProvider.topic}`);
54
- const accountAddresses = filterDuplicates(namespace.accounts.map((account)=>splitCaip10Token(account).address));
55
- var _walletProvider_formatWalletAddress;
56
- const formatAddress = (_walletProvider_formatWalletAddress = walletProvider.formatWalletAddress) != null ? _walletProvider_formatWalletAddress : ({ address })=>address;
57
- return accountAddresses.map((address)=>({
58
- address: formatAddress({
59
- address
60
- }),
61
- chain: walletProvider.chain,
62
- id: formatWalletAccountId({
63
- address,
64
- chain: walletProvider.chain,
65
- walletProviderKey: walletProvider.key
66
- }),
67
- lastSelectedAt: null,
68
- verifiedCredentialId: null,
69
- walletProviderKey: walletProvider.key
70
- }));
71
- };
72
-
73
- const sessionTopicsStorageKeySchema = createStorageKeySchema({
74
- key: 'sessionTopics',
75
- schema: z.record(/**
76
- * The session topic that identifies this session.
77
- */ z.string(), /**
78
- * The session data.
79
- */ z.object({
80
- /**
81
- * Whether this session's wallet accounts should be added to the dynamic's wallet accounts.
82
- * When a customer connects a session with addToDynamicWalletAccounts set to false, the resulting session
83
- * should not produce wallet accounts to be added to the dynamic's wallet accounts.
84
- * When that happens, we cannot clear them until the page is refreshed or the wallet account is removed
85
- * by the external owner by calling removeWalletAccount.
86
- */ shouldAddToDynamicWalletAccounts: z.boolean()
87
- }))
88
- });
89
-
90
- /**
91
- * Sets the session topics data.
92
- *
93
- * Read more about it in the sessionTopicsData.types.ts file.
94
- *
95
- * @param update - The function to update the session topics data.
96
- * @param client - The client instance.
97
- */ const setSessionTopicsData = async ({ update }, client)=>{
98
- const { storage } = getCore(client);
99
- const sessionTopicsData = await storage.getItem(sessionTopicsStorageKeySchema);
100
- const newSessionTopicsData = update(sessionTopicsData != null ? sessionTopicsData : {});
101
- if (Object.keys(newSessionTopicsData).length === 0) {
102
- return storage.removeItem(sessionTopicsStorageKeySchema);
103
- }
104
- return storage.setItem(sessionTopicsStorageKeySchema, newSessionTopicsData);
105
- };
106
-
107
- /**
108
- * Gets the session topics data.
109
- *
110
- * Read more about it in the sessionTopicsData.types.ts file.
111
- * @param client - The client instance.
112
- */ const getSessionTopicsData = async (client)=>{
113
- const { storage } = getCore(client);
114
- const sessionTopicsData = await storage.getItem(sessionTopicsStorageKeySchema);
115
- return sessionTopicsData != null ? sessionTopicsData : {};
116
- };
117
-
118
- const createSignClient = async (client)=>{
119
- var _client_projectSettings_sdk_walletConnect, _core_metadata, _core_metadata1;
120
- const core = getCore(client);
121
- assertDefined(client.projectSettings, 'Project settings not available');
122
- const generalSettings = client.projectSettings.general;
123
- assertDefined(generalSettings.displayName, 'Please configure an app name in dashboard to use WalletConnect');
124
- const projectId = (_client_projectSettings_sdk_walletConnect = client.projectSettings.sdk.walletConnect) == null ? void 0 : _client_projectSettings_sdk_walletConnect.projectId;
125
- assertDefined(projectId, 'Please configure a project ID for WalletConnect in dashboard');
126
- const customStoragePrefix = `dynamic_${core.environmentId}_walletconnect`;
127
- var _core_metadata_name;
128
- const displayName = (_core_metadata_name = (_core_metadata = core.metadata) == null ? void 0 : _core_metadata.name) != null ? _core_metadata_name : generalSettings.displayName;
129
- var _core_metadata_iconUrl;
130
- const appLogo = (_core_metadata_iconUrl = (_core_metadata1 = core.metadata) == null ? void 0 : _core_metadata1.iconUrl) != null ? _core_metadata_iconUrl : generalSettings.appLogo;
131
- return SignClient.init({
132
- customStoragePrefix,
133
- metadata: {
134
- description: '',
135
- icons: appLogo ? [
136
- appLogo
137
- ] : [],
138
- name: displayName,
139
- url: ''
140
- },
141
- projectId
142
- });
143
- };
144
-
145
- const getSignClient = createRuntimeServiceAccessKey('walletConnectSignClient', (client)=>createSignClient(client));
146
-
147
- const createNamespaceRegistry = ()=>{
148
- const walletProviderBuilderMap = new Map();
149
- return {
150
- getWalletProviderBuilder: (key)=>walletProviderBuilderMap.get(key),
151
- registerNamespace: ({ createWalletProviderFromSession, namespaceKey })=>{
152
- walletProviderBuilderMap.set(namespaceKey, createWalletProviderFromSession);
153
- }
154
- };
155
- };
156
-
157
- const getNamespaceRegistry = createRuntimeServiceAccessKey('walletConnectNamespaceRegistry', createNamespaceRegistry);
158
-
159
- const addMissingWalletProvidersForSessions = async ({ sessions, allowOverridingWalletProviders }, client)=>{
160
- const namespaceRegistry = getNamespaceRegistry(client);
161
- const walletProviderRegistry = getWalletProviderRegistry(client);
162
- const sessionTopicsData = await getSessionTopicsData(client);
163
- const currentWalletAccounts = getWalletAccounts(client);
164
- // Sort so the oldest sessions are first and the newer sessions will
165
- // override the older ones' wallet accounts (if there is any overlap).
166
- sessions.sort((sessionA, sessionB)=>sessionA.expiry - sessionB.expiry);
167
- /**
168
- * All the WC wallet providers that are currently registered
169
- */ const currentWalletProviders = walletProviderRegistry.listProviders().filter(isWalletConnectWalletProvider);
170
- /**
171
- * Keep record on which walletProviders are tied to active sessions
172
- */ const walletProvidersWithActiveSession = [];
173
- /**
174
- * Maps the ID of the created wallet accounts to the wallet accounts themselves.
175
- */ const createdWalletAccounts = {};
176
- /**
177
- * Keep record on which wallet account IDs were not in the current wallet accounts
178
- * and are therefore brand new.
179
- */ const brandNewWalletAccounts = new Set();
180
- /**
181
- * Keep record on which wallet account IDs already exist but will have
182
- * their wallet provider keys overridden to use the new WC wallet provider key.
183
- *
184
- * This will only ever be the case for unverified wallet accounts, however. That is
185
- * because verified wallets will already override the wallet provider key on the verification
186
- * step, which happens at the call to verifyWalletConnectAuths, which happens before this function.
187
- */ const unverifiedWalletAccountsToOverride = new Set();
188
- for (const session of sessions){
189
- const namespaceKeys = Object.keys(session.namespaces);
190
- for (const namespaceKey of namespaceKeys){
191
- const existingWalletProvider = currentWalletProviders.find((walletProvider)=>walletProvider.namespaceKey === namespaceKey && walletProvider.topic === session.topic);
192
- if (existingWalletProvider) {
193
- walletProvidersWithActiveSession.push(existingWalletProvider);
194
- continue;
195
- }
196
- const walletProviderBuilder = namespaceRegistry.getWalletProviderBuilder(namespaceKey);
197
- /**
198
- * There being no wallet provider builder for this namespace simply means the customer
199
- * didn't add an extension for the corresponding chain.
200
- * For instance, if we get a namespace for Solana but only have an Evm extension,
201
- * the solana namespace would fall here. No need to throw.
202
- */ if (!walletProviderBuilder) {
203
- continue;
204
- }
205
- const walletProvider = await walletProviderBuilder({
206
- session
207
- }, client);
208
- walletProviderRegistry.register({
209
- priority: WalletProviderPriority.WALLET_SDK,
210
- walletProvider
211
- });
212
- walletProvidersWithActiveSession.push(walletProvider);
213
- const sessionData = sessionTopicsData[session.topic];
214
- if (!sessionData.shouldAddToDynamicWalletAccounts) {
215
- continue;
216
- }
217
- const namespaceWalletAccounts = createWalletAccountsForWalletConnectWalletProvider({
218
- walletProvider
219
- });
220
- if (namespaceWalletAccounts.length === 0) {
221
- continue;
222
- }
223
- for (const createdWalletAccount of namespaceWalletAccounts){
224
- const isBrandNew = !currentWalletAccounts.some((existingAccount)=>existingAccount.id === createdWalletAccount.id);
225
- if (isBrandNew) {
226
- createdWalletAccounts[createdWalletAccount.id] = createdWalletAccount;
227
- brandNewWalletAccounts.add(createdWalletAccount.id);
228
- continue;
229
- }
230
- const existingAccount = currentWalletAccounts.find((existingAccount)=>existingAccount.id === createdWalletAccount.id);
231
- const isOverrideCandidate = existingAccount && existingAccount.walletProviderKey !== createdWalletAccount.walletProviderKey;
232
- if (!isOverrideCandidate) {
233
- continue;
234
- }
235
- // If overriding wallet providers is not allowed, we can still override
236
- // as long as the wallet account being overridden is also a wallet connect wallet account.
237
- const existingWalletIsWalletConnect = splitWalletProviderKey(existingAccount.walletProviderKey).walletProviderType === WalletProviderEnum.WalletConnect;
238
- if (allowOverridingWalletProviders || existingWalletIsWalletConnect) {
239
- createdWalletAccounts[createdWalletAccount.id] = createdWalletAccount;
240
- if (existingAccount.verifiedCredentialId) {
241
- /**
242
- * A verified wallet account only arrives here when its wallet provider key is lacking the suffix,
243
- * which should be the sessionTopic. So we just need to add its entry to the verifiedCredentialId map.
244
- */ updateWalletProviderKeysForVerifiedCredentials({
245
- keysToUpdate: {
246
- [existingAccount.verifiedCredentialId]: walletProvider.key
247
- }
248
- }, client);
249
- } else {
250
- unverifiedWalletAccountsToOverride.add(createdWalletAccount.id);
251
- }
252
- }
253
- }
254
- }
255
- }
256
- if (unverifiedWalletAccountsToOverride.size === 0 && brandNewWalletAccounts.size === 0) {
257
- return {
258
- walletProvidersWithActiveSession
259
- };
260
- }
261
- setUnverifiedWalletAccounts({
262
- unverifiedWalletAccountsToUpdate: Object.values(createdWalletAccounts)
263
- }, client);
264
- if (brandNewWalletAccounts.size > 0) {
265
- for (const walletAccountId of brandNewWalletAccounts){
266
- void createVisit({
267
- walletAccount: createdWalletAccounts[walletAccountId]
268
- }, client);
269
- }
270
- }
271
- emitWalletAccountsChangedEvent(client);
272
- return {
273
- walletProvidersWithActiveSession
274
- };
275
- };
276
-
277
- const doSyncSessionsToWalletProviders = async ({ allowOverridingWalletProviders }, client)=>{
278
- await waitForProjectSettings(client);
279
- const signClient = await getSignClient(client);
280
- const walletProviderRegistry = getWalletProviderRegistry(client);
281
- const sessionTopicsData = await getSessionTopicsData(client);
282
- const sessions = signClient.session.getAll().filter((session)=>{
283
- if (sessionTopicsData[session.topic]) {
284
- return true;
285
- }
286
- // If we don't have data for this session, it is corrupted and we need to disconnect
287
- void signClient.disconnect({
288
- reason: SDK_ERRORS.USER_DISCONNECTED,
289
- topic: session.topic
290
- });
291
- return false;
292
- });
293
- /**
294
- * All the WC wallet providers that are currently registered
295
- */ const currentWalletProviders = walletProviderRegistry.listProviders().filter(isWalletConnectWalletProvider);
296
- // Step 1: remove wallet providers whose sessions have been cleared
297
- for (const walletProvider of currentWalletProviders){
298
- const hasSession = sessions.some((session)=>session.topic === walletProvider.topic && walletProvider.namespaceKey in session.namespaces);
299
- if (!hasSession) {
300
- walletProviderRegistry.unregister(walletProvider.key);
301
- }
302
- }
303
- // Step 2: add wallet providers that are missing for current sessions
304
- const { walletProvidersWithActiveSession: updatedWalletProviders } = await addMissingWalletProvidersForSessions({
305
- allowOverridingWalletProviders,
306
- sessions
307
- }, client);
308
- // Step 3: identify which sessions have no more associated wallet accounts for clean up
309
- const currentWalletAccounts = getWalletAccounts(client);
310
- /**
311
- * Maps which sessions have wallet accounts associated with them
312
- */ const sessionTopicHasWalletAccountMap = new Map();
313
- for (const walletProvider of updatedWalletProviders){
314
- const sessionTopic = walletProvider.topic;
315
- if (!sessionTopicHasWalletAccountMap.has(sessionTopic)) {
316
- sessionTopicHasWalletAccountMap.set(sessionTopic, false);
317
- }
318
- // Early return if we already know this session has wallet accounts associated with it
319
- if (sessionTopicHasWalletAccountMap.get(sessionTopic)) {
320
- continue;
321
- }
322
- const walletProviderWalletAccounts = currentWalletAccounts.filter((walletAccount)=>walletAccount.walletProviderKey === walletProvider.key);
323
- if (walletProviderWalletAccounts.length > 0) {
324
- sessionTopicHasWalletAccountMap.set(sessionTopic, true);
325
- }
326
- }
327
- // Step 4: kill sessions and their wallet providers if they have no more wallet accounts
328
- // Ignore sessions that are NOT marked as shouldAddToDynamicWalletAccounts
329
- for (const [sessionTopic, hasWalletAccount] of sessionTopicHasWalletAccountMap){
330
- if (hasWalletAccount || !sessionTopicsData[sessionTopic].shouldAddToDynamicWalletAccounts) {
331
- continue;
332
- }
333
- const session = sessions.find((session)=>session.topic === sessionTopic);
334
- if (session) {
335
- void signClient.disconnect({
336
- reason: SDK_ERRORS.USER_DISCONNECTED,
337
- topic: session.topic
338
- });
339
- }
340
- for (const walletProvider of updatedWalletProviders){
341
- if (walletProvider.topic === sessionTopic) {
342
- walletProviderRegistry.unregister(walletProvider.key);
343
- }
344
- }
345
- }
346
- // Step 5: compile which session topics are active to clean up stale session data
347
- // We do session.getAll() again here to catch any sessions that might have been disconnected from other steps or even externally.
348
- const activeSessionTopics = new Set(signClient.session.getAll().map((session)=>session.topic));
349
- await setSessionTopicsData({
350
- update: (sessionTopicsData)=>{
351
- return Object.fromEntries(Object.entries(sessionTopicsData).filter(([topic])=>activeSessionTopics.has(topic)));
352
- }
353
- }, client);
354
- };
355
- const syncSessionsToWalletProviders = async ({ debounceTime = 0, allowOverridingWalletProviders }, client)=>{
356
- const core = getCore(client);
357
- // This is called on adding WC extensions, which could be many in quick succession, so let's debounce
358
- // the syncSessionsToWalletProviders call to ensure it only runs once and avoid race conditions.
359
- return core.debouncedMutex({
360
- callback: async ()=>doSyncSessionsToWalletProviders({
361
- allowOverridingWalletProviders
362
- }, client),
363
- debounceTime,
364
- lockKey: 'wallet-connect-sync-sessions-to-wallet-providers'
365
- });
366
- };
367
-
368
- /**
369
- * This is a lower level function that is used to connect a WalletConnect session to the Dynamic SDK.
370
- * It will generate the wallet providers and wallet accounts for all accounts in the session.
371
- *
372
- * @param session - The session object created from the approval promise of the signClient.connect method.
373
- * @param addToDynamicWalletAccounts - Whether to add the wallet accounts to the dynamic's wallet accounts.
374
- * @param client - The Dynamic client instance. Only required when using multiple Dynamic clients.
375
- * @returns The wallet accounts.
376
- */ const importWalletConnectSession = async ({ addToDynamicWalletAccounts = true, session }, client)=>{
377
- /**
378
- * You would normally get the session from the signClient.connect method. It takes the optionalNamespaces as an argument.
379
- *
380
- * Each chain should build its own "connectWithWalletConnect<chain-name>" function.
381
- * Understand how to build the optionalNamespaces object:
382
- *
383
- * The namespace configurations are used at the time a wallet application receives the connection URI.
384
- * It will use the configurations we supply to understand which methods, events and networks it should provide.
385
- *
386
- * The wallet application will then accept the connection request and, in the session`object, provide its own namespace object
387
- * in return, which will contain the methods, events and networks it does in fact support, as well as
388
- * which accounts are available.
389
- *
390
- * WalletConnect expects namespaces defined in the following format:
391
- * ```typescript
392
- * {
393
- * <namespaceKey>: {
394
- * "accounts": ["<account1>", "<account2>", "<account3>", ...],
395
- * "events": ["accountsChanged", "chainChanged", "disconnected"],
396
- * "methods": ["personal_sign", "eth_sendTransaction", "eth_accounts", ...],
397
- * "chains": ["<chain1>", "<chain2>", "<chain3>", ...]
398
- * },
399
- * ...
400
- * }
401
- * ```
402
- *
403
- * Where each account is a CAIP-10 formatted string, for example: "eip155:1:0x1234567890"
404
- * — see docs: https://chainagnostic.org/CAIPs/caip-10
405
- *
406
- * Where each chain is a CAIP-2 formatted string, for example: "eip155:1"
407
- * — see docs: https://chainagnostic.org/CAIPs/caip-2
408
- */ const sessionData = {
409
- shouldAddToDynamicWalletAccounts: addToDynamicWalletAccounts
410
- };
411
- await setSessionTopicsData({
412
- update: (sessionTopicsData)=>_extends({}, sessionTopicsData, {
413
- [session.topic]: sessionData
414
- })
415
- }, client);
416
- /**
417
- * Once the connection is established, we ensure that all active connections (aka sessions)
418
- * are synced to our WalletConnect wallet providers.
419
- * Each session can be uniquely identified by its topic (a random string generated by WalletConnect).
420
- *
421
- * Each session may have multiple wallet providers, one for each namespace (aka our SDK chains)
422
- */ await syncSessionsToWalletProviders({
423
- allowOverridingWalletProviders: true
424
- }, client);
425
- const sessionWalletProviders = getWalletProviders(client).filter(isWalletConnectWalletProvider).filter((provider)=>provider.topic === session.topic);
426
- /**
427
- * Upon setting the session data to NOT externally owned, syncSessionsToWalletProviders will have
428
- * already created the wallet accounts for each of this session's wallet providers — so all we need
429
- * to do is retrieve them.
430
- */ if (addToDynamicWalletAccounts) {
431
- const sessionWalletProviderKeys = sessionWalletProviders.map((provider)=>provider.key);
432
- const walletAccounts = getWalletAccounts(client).filter((walletAccount)=>sessionWalletProviderKeys.includes(walletAccount.walletProviderKey));
433
- return {
434
- walletAccounts
435
- };
436
- }
437
- /**
438
- * If we didn't add the wallet accounts to the dynamic's wallet accounts,
439
- * we never created them at all, so we need to create them now and provide
440
- * them to the caller, which will become their [external] owner.
441
- */ const walletAccounts = sessionWalletProviders.map((walletProvider)=>createWalletAccountsForWalletConnectWalletProvider({
442
- walletProvider
443
- })).flat();
444
- return {
445
- walletAccounts
446
- };
447
- };
448
-
449
- const getAddressFromIss = (iss)=>iss.split(':')[4];
450
- const verifyWalletConnectAuths = async ({ auths, chain, walletDisplayName }, client)=>{
451
- /**
452
- * The only differences between each auth object will be the signature (auth.s.s)
453
- * and the iss (auth.p.iss).
454
- *
455
- * The iss is comprised of 2 WC fields that are irrelevant to us and then the CAIP-10 token
456
- * of the address, where the first 2 fields are also split by the ":" character.
457
- *
458
- * Usually, there will be multiple auths if the wallet app bundles signatures for multiple
459
- * networks at once, so the only field that will change among auth iss will be the networkId under iss.
460
- */ const visitedAddresses = new Set();
461
- const authsFilteredByAddress = auths.filter((auth)=>{
462
- const address = getAddressFromIss(auth.p.iss);
463
- if (!address || visitedAddresses.has(address)) {
464
- return false;
465
- }
466
- visitedAddresses.add(address);
467
- return true;
468
- });
469
- let lastResponse = undefined;
470
- /**
471
- * WalletConnect's auth object is what they call a "Cacao" object, with data on the SIWE message
472
- * and the signature itself.
473
- * By consulting how they implemented their "validateSignedCacao" function,
474
- * we are able to reconstruct the original message which can be used to verify the signature
475
- * in our backend.
476
- */ for (const auth of authsFilteredByAddress){
477
- const publicWalletAddress = getAddressFromIss(auth.p.iss);
478
- assertDefined(publicWalletAddress, `Unable to get public wallet address from WalletConnect Cacao object iss: ${auth.p.iss}`);
479
- const messageToSign = formatMessage(auth.p, auth.p.iss);
480
- const signature = auth.s.s;
481
- lastResponse = await verifySignInMessage({
482
- chain,
483
- messageToSign,
484
- signature,
485
- walletAddress: publicWalletAddress,
486
- walletDisplayName,
487
- walletProviderType: WalletProviderEnum.WalletConnect
488
- }, client);
489
- }
490
- if (!lastResponse) {
491
- return;
492
- }
493
- /**
494
- * Adds the user with the new wallet verified credentials
495
- * This will add the new verified wallet accounts to the client`s state
496
- */ updateAuthFromVerifyResponse({
497
- response: lastResponse
498
- }, client);
499
- };
500
-
501
- /**
502
- * A low level function that is used to connect a WalletConnect authenticate response to the Dynamic SDK.
503
- * It will generate the wallet providers and wallet accounts for all accounts in the session.
504
- *
505
- * @see WalletConnect's docs on [session authentication](https://docs.reown.com/advanced/api/sign/dapp-usage#session-authenticate-with-recaps)
506
- * @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
507
- * @returns A promise that resolves to the connected and verified wallet account.
508
- */ const importWalletConnectAuthResponse = async ({ authResponse, chain }, client)=>{
509
- const { session, auths } = authResponse;
510
- /**
511
- * Some wallets do not support WC's authenticate method (such as Rainbow).
512
- * For these wallets, the authenticate method will act as a regular connect method.
513
- * Therefore we must call verify individually.
514
- */ if (!auths || auths.length === 0) {
515
- /**
516
- * Set addToDynamicWalletAccounts to false to avoid adding the wallet accounts to the dynamic's wallet accounts
517
- * while we are still verifying them.
518
- * After verifying, we set addToDynamicWalletAccounts back to true.
519
- */ const { walletAccounts } = await importWalletConnectSession({
520
- addToDynamicWalletAccounts: false,
521
- session
522
- }, client);
523
- // This should not be concurrent because the first will be a verify and the rest will be a link
524
- for (const walletAccount of walletAccounts){
525
- await verifyWalletAccount({
526
- walletAccount
527
- }, client);
528
- }
529
- await setSessionTopicsData({
530
- update: (sessionTopicsData)=>_extends({}, sessionTopicsData, {
531
- [session.topic]: {
532
- shouldAddToDynamicWalletAccounts: true
533
- }
534
- })
535
- }, client);
536
- return {
537
- walletAccounts
538
- };
539
- }
540
- /**
541
- * This reconstructs the message that was signed by the wallet and verifies the signature
542
- * in the backend. It will update the user with the new verified credentials.
543
- */ await verifyWalletConnectAuths({
544
- auths,
545
- chain,
546
- walletDisplayName: session.peer.metadata.name
547
- }, client);
548
- const { walletAccounts } = await importWalletConnectSession({
549
- addToDynamicWalletAccounts: true,
550
- session
551
- }, client);
552
- const verifiedCredentialWalletProviderKeyMap = Object.fromEntries(walletAccounts.map((walletAccount)=>{
553
- const { verifiedCredentialId } = walletAccount;
554
- assertDefined(verifiedCredentialId, `Wallet account of ID ${walletAccount.id} still has no verified credential after being verified`);
555
- return [
556
- verifiedCredentialId,
557
- walletAccount.walletProviderKey
558
- ];
559
- }));
560
- updateWalletProviderKeysForVerifiedCredentials({
561
- keysToUpdate: verifiedCredentialWalletProviderKeyMap
562
- }, client);
563
- return {
564
- walletAccounts
565
- };
566
- };
567
-
568
- /**
569
- * Clears all temporary sessions and disconnects from them.
570
- *
571
- * External sessions are for WC sessions whose accounts are not supposed to be saved in
572
- * the current dynamic's wallet accounts.
573
- * Since we don't own the created wallet accounts, we must keep their corresponding WC sessions alive
574
- * until the page is refreshed or the customer removes the wallet account.
575
- *
576
- * @param client - The client instance.
577
- */ const clearTemporarySessions = async (client)=>{
578
- const signClient = await getSignClient(client);
579
- const sessionTopicsData = await getSessionTopicsData(client);
580
- const temporarySessionTopics = Object.keys(sessionTopicsData).filter((topic)=>!sessionTopicsData[topic].shouldAddToDynamicWalletAccounts);
581
- if (temporarySessionTopics.length === 0) {
582
- return;
583
- }
584
- for (const topic of temporarySessionTopics){
585
- await signClient.disconnect({
586
- reason: SDK_ERRORS.USER_DISCONNECTED,
587
- topic
588
- });
589
- }
590
- const newSessionTopicsData = Object.fromEntries(Object.entries(sessionTopicsData).filter(([, { shouldAddToDynamicWalletAccounts: isPermanentSession }])=>isPermanentSession));
591
- await setSessionTopicsData({
592
- update: ()=>newSessionTopicsData
593
- }, client);
594
- };
595
-
596
- const initializeWalletConnectExtension = async (client)=>{
597
- // Clear external session topics that were created in previous browser sessions
598
- // Read more in the clearTemporarySessions function.
599
- await clearTemporarySessions(client);
600
- // Add debounce to account for multiple WC extension additions in quick succession
601
- await syncSessionsToWalletProviders({
602
- allowOverridingWalletProviders: false,
603
- debounceTime: 200
604
- }, client);
605
- };
606
-
607
- assertPackageVersion(name, version);
608
-
609
- export { getNamespaceRegistry, getSignClient, importWalletConnectAuthResponse, importWalletConnectSession, initializeWalletConnectExtension, splitCaip10Token, syncSessionsToWalletProviders };
package/index.cjs.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./src/exports/index";
package/index.cjs.js DELETED
@@ -1,13 +0,0 @@
1
- 'use strict';
2
-
3
- var assertPackageVersion = require('@dynamic-labs-sdk/assert-package-version');
4
- var isWalletConnectWalletProvider = require('./isWalletConnectWalletProvider.cjs.js');
5
- var utils = require('@walletconnect/utils');
6
-
7
- assertPackageVersion.assertPackageVersion(isWalletConnectWalletProvider.name, isWalletConnectWalletProvider.version);
8
-
9
- exports.isWalletConnectWalletProvider = isWalletConnectWalletProvider.isWalletConnectWalletProvider;
10
- Object.defineProperty(exports, "WC_ERRORS", {
11
- enumerable: true,
12
- get: function () { return utils.SDK_ERRORS; }
13
- });
package/index.esm.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./src/exports/index";
package/index.esm.js DELETED
@@ -1,6 +0,0 @@
1
- import { assertPackageVersion } from '@dynamic-labs-sdk/assert-package-version';
2
- import { n as name, v as version } from './isWalletConnectWalletProvider.esm.js';
3
- export { i as isWalletConnectWalletProvider } from './isWalletConnectWalletProvider.esm.js';
4
- export { SDK_ERRORS as WC_ERRORS } from '@walletconnect/utils';
5
-
6
- assertPackageVersion(name, version);
@@ -1,23 +0,0 @@
1
- 'use strict';
2
-
3
- var name = "@dynamic-labs-sdk/wallet-connect";
4
- var version = "0.1.2";
5
-
6
- /**
7
- * This is a way to ensure Typescript will guard that all keys defined in
8
- * WalletConnectWalletProvider are accounted for in this function.
9
- *
10
- * It will complain if any key is missing or if there are any extra keys.
11
- */ const wcExclusiveKeysRecord = {
12
- namespaceKey: null,
13
- session: null,
14
- topic: null
15
- };
16
- const wcExclusiveKeys = Object.keys(wcExclusiveKeysRecord);
17
- const isWalletConnectWalletProvider = (walletProvider)=>{
18
- return wcExclusiveKeys.every((key)=>key in walletProvider);
19
- };
20
-
21
- exports.isWalletConnectWalletProvider = isWalletConnectWalletProvider;
22
- exports.name = name;
23
- exports.version = version;
@@ -1,19 +0,0 @@
1
- var name = "@dynamic-labs-sdk/wallet-connect";
2
- var version = "0.1.2";
3
-
4
- /**
5
- * This is a way to ensure Typescript will guard that all keys defined in
6
- * WalletConnectWalletProvider are accounted for in this function.
7
- *
8
- * It will complain if any key is missing or if there are any extra keys.
9
- */ const wcExclusiveKeysRecord = {
10
- namespaceKey: null,
11
- session: null,
12
- topic: null
13
- };
14
- const wcExclusiveKeys = Object.keys(wcExclusiveKeysRecord);
15
- const isWalletConnectWalletProvider = (walletProvider)=>{
16
- return wcExclusiveKeys.every((key)=>key in walletProvider);
17
- };
18
-
19
- export { isWalletConnectWalletProvider as i, name as n, version as v };