@reown/appkit 1.3.2 → 1.4.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 (34) hide show
  1. package/dist/esm/exports/adapters.js +2 -0
  2. package/dist/esm/exports/adapters.js.map +1 -0
  3. package/dist/esm/exports/constants.js +1 -1
  4. package/dist/esm/package.json +9 -1
  5. package/dist/esm/src/adapters/ChainAdapterBlueprint.js +138 -0
  6. package/dist/esm/src/adapters/ChainAdapterBlueprint.js.map +1 -0
  7. package/dist/esm/src/adapters/ChainAdapterConnector.js +2 -0
  8. package/dist/esm/src/adapters/ChainAdapterConnector.js.map +1 -0
  9. package/dist/esm/src/adapters/index.js +2 -0
  10. package/dist/esm/src/adapters/index.js.map +1 -0
  11. package/dist/esm/src/client.js +888 -40
  12. package/dist/esm/src/client.js.map +1 -1
  13. package/dist/esm/src/store/ProviderUtil.js.map +1 -1
  14. package/dist/esm/src/universal-adapter/client.js +98 -532
  15. package/dist/esm/src/universal-adapter/client.js.map +1 -1
  16. package/dist/esm/src/universal-adapter/index.js +1 -1
  17. package/dist/esm/src/universal-adapter/index.js.map +1 -1
  18. package/dist/esm/src/utils/HelpersUtil.js +6 -0
  19. package/dist/esm/src/utils/HelpersUtil.js.map +1 -1
  20. package/dist/esm/tsconfig.build.tsbuildinfo +1 -1
  21. package/dist/types/exports/adapters.d.ts +1 -0
  22. package/dist/types/exports/constants.d.ts +1 -1
  23. package/dist/types/src/adapters/ChainAdapterBlueprint.d.ts +324 -0
  24. package/dist/types/src/adapters/ChainAdapterConnector.d.ts +5 -0
  25. package/dist/types/src/adapters/index.d.ts +1 -0
  26. package/dist/types/src/client.d.ts +49 -10
  27. package/dist/types/src/library/react/index.d.ts +1 -1
  28. package/dist/types/src/library/vue/index.d.ts +1 -1
  29. package/dist/types/src/store/ProviderUtil.d.ts +5 -4
  30. package/dist/types/src/store/index.d.ts +1 -1
  31. package/dist/types/src/universal-adapter/client.d.ts +32 -44
  32. package/dist/types/src/universal-adapter/index.d.ts +1 -1
  33. package/dist/types/src/utils/HelpersUtil.d.ts +1 -0
  34. package/package.json +17 -9
@@ -1,557 +1,123 @@
1
- /* eslint-disable max-depth */
2
- import { AccountController, ChainController, ConnectionController, CoreHelperUtil, StorageUtil, AlertController, BlockchainApiController } from '@reown/appkit-core';
3
- import { ConstantsUtil, ErrorUtil, LoggerUtil, PresetsUtil } from '@reown/appkit-utils';
4
- import UniversalProvider from '@walletconnect/universal-provider';
5
- import { WcHelpersUtil } from '../utils/HelpersUtil.js';
6
- import { SafeLocalStorage, SafeLocalStorageKeys, ConstantsUtil as CommonConstantsUtil } from '@reown/appkit-common';
7
- import { ProviderUtil } from '../store/index.js';
8
- import bs58 from 'bs58';
9
- const OPTIONAL_METHODS = [
10
- 'eth_accounts',
11
- 'eth_requestAccounts',
12
- 'eth_sendRawTransaction',
13
- 'eth_sign',
14
- 'eth_signTransaction',
15
- 'eth_signTypedData',
16
- 'eth_signTypedData_v3',
17
- 'eth_signTypedData_v4',
18
- 'eth_sendTransaction',
19
- 'personal_sign',
20
- 'wallet_switchEthereumChain',
21
- 'wallet_addEthereumChain',
22
- 'wallet_getPermissions',
23
- 'wallet_requestPermissions',
24
- 'wallet_registerOnboarding',
25
- 'wallet_watchAsset',
26
- 'wallet_scanQRCode',
27
- // EIP-5792
28
- 'wallet_getCallsStatus',
29
- 'wallet_sendCalls',
30
- 'wallet_getCapabilities',
31
- // EIP-7715
32
- 'wallet_grantPermissions',
33
- 'wallet_revokePermissions'
34
- ];
35
- // -- Client --------------------------------------------------------------------
36
- export class UniversalAdapterClient {
37
- constructor(options) {
38
- this.appKit = undefined;
39
- this.isUniversalAdapterClient = true;
40
- this.options = undefined;
41
- this.adapterType = 'universal';
42
- this.reportedAlertErrors = {};
43
- const { siweConfig, metadata } = options;
44
- this.caipNetworks = options.networks;
45
- this.chainNamespace = CommonConstantsUtil.CHAIN.EVM;
46
- this.metadata = metadata;
47
- this.networkControllerClient = {
48
- // @ts-expect-error switchCaipNetwork is async for some adapter but not for this adapter
49
- switchCaipNetwork: caipNetwork => {
50
- if (caipNetwork) {
51
- this.switchNetwork(caipNetwork);
52
- }
53
- },
54
- getApprovedCaipNetworksData: async () => {
55
- const provider = await this.getWalletConnectProvider();
56
- if (!provider) {
57
- return Promise.resolve({
58
- supportsAllNetworks: false,
59
- approvedCaipNetworkIds: []
60
- });
61
- }
62
- const approvedCaipNetworkIds = WcHelpersUtil.getChainsFromNamespaces(provider.session?.namespaces);
63
- return Promise.resolve({
64
- supportsAllNetworks: false,
65
- approvedCaipNetworkIds
66
- });
67
- }
68
- };
69
- this.connectionControllerClient = {
70
- connectWalletConnect: async (onUri) => {
71
- const WalletConnectProvider = await this.getWalletConnectProvider();
72
- if (!WalletConnectProvider) {
73
- throw new Error('connectionControllerClient:getWalletConnectUri - provider is undefined');
74
- }
75
- WalletConnectProvider.on('display_uri', (uri) => {
76
- onUri(uri);
77
- });
78
- if (ChainController.state.activeChain &&
79
- ChainController.state?.chains?.get(ChainController.state.activeChain)?.adapterType ===
80
- 'wagmi') {
81
- const adapter = ChainController.state.chains.get(ChainController.state.activeChain);
82
- await adapter?.connectionControllerClient?.connectWalletConnect?.(onUri);
83
- this.setWalletConnectProvider();
84
- }
85
- else {
86
- const siweParams = await siweConfig?.getMessageParams?.();
87
- const isSiweEnabled = siweConfig?.options?.enabled;
88
- const isProviderSupported = typeof WalletConnectProvider?.authenticate === 'function';
89
- const isSiweParamsValid = siweParams && Object.keys(siweParams || {}).length > 0;
90
- const clientId = await WalletConnectProvider?.client?.core?.crypto?.getClientId();
91
- if (clientId) {
92
- this.appKit?.setClientId(clientId);
93
- }
94
- if (siweConfig &&
95
- isSiweEnabled &&
96
- siweParams &&
97
- isProviderSupported &&
98
- isSiweParamsValid &&
99
- ChainController.state.activeChain === CommonConstantsUtil.CHAIN.EVM) {
100
- const { SIWEController, getDidChainId, getDidAddress } = await import('@reown/appkit-siwe');
101
- const chains = this.caipNetworks
102
- ?.filter(network => network.chainNamespace === CommonConstantsUtil.CHAIN.EVM)
103
- .map(chain => chain.caipNetworkId);
104
- siweParams.chains = this.caipNetworks
105
- ?.filter(network => network.chainNamespace === CommonConstantsUtil.CHAIN.EVM)
106
- .map(chain => chain.id);
107
- const result = await WalletConnectProvider.authenticate({
108
- nonce: await siweConfig?.getNonce?.(),
109
- methods: [...OPTIONAL_METHODS],
110
- ...siweParams,
111
- chains
112
- });
113
- // Auths is an array of signed CACAO objects https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-74.md
114
- const signedCacao = result?.auths?.[0];
115
- if (signedCacao) {
116
- const { p, s } = signedCacao;
117
- const cacaoChainId = getDidChainId(p.iss);
118
- const address = getDidAddress(p.iss);
119
- if (address && cacaoChainId) {
120
- SIWEController.setSession({
121
- address,
122
- chainId: parseInt(cacaoChainId, 10)
123
- });
124
- }
125
- try {
126
- // Kicks off verifyMessage and populates external states
127
- const message = WalletConnectProvider.client.formatAuthMessage({
128
- request: p,
129
- iss: p.iss
130
- });
131
- await SIWEController.verifyMessage({
132
- message,
133
- signature: s.s,
134
- cacao: signedCacao
135
- });
136
- }
137
- catch (error) {
138
- // eslint-disable-next-line no-console
139
- console.error('Error verifying message', error);
140
- // eslint-disable-next-line no-console
141
- await WalletConnectProvider.disconnect().catch(console.error);
142
- // eslint-disable-next-line no-console
143
- await SIWEController.signOut().catch(console.error);
144
- throw error;
145
- }
146
- }
147
- }
148
- else {
149
- const optionalNamespaces = WcHelpersUtil.createNamespaces(this.caipNetworks);
150
- await WalletConnectProvider.connect({ optionalNamespaces });
151
- }
152
- this.setWalletConnectProvider();
153
- }
154
- },
155
- disconnect: async () => {
156
- SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID);
157
- if (siweConfig?.options?.signOutOnDisconnect) {
158
- const { SIWEController } = await import('@reown/appkit-siwe');
159
- await SIWEController.signOut();
160
- }
161
- await this.walletConnectProvider?.disconnect();
162
- this.appKit?.resetAccount(CommonConstantsUtil.CHAIN.EVM);
163
- this.appKit?.resetAccount(CommonConstantsUtil.CHAIN.SOLANA);
164
- },
165
- signMessage: async (message) => {
166
- const provider = await this.getWalletConnectProvider();
167
- const caipAddress = ChainController.state.activeCaipAddress;
168
- const address = CoreHelperUtil.getPlainAddress(caipAddress);
169
- if (!provider) {
170
- throw new Error('connectionControllerClient:signMessage - provider is undefined');
171
- }
172
- let signature = '';
173
- if (ChainController.state.activeCaipNetwork?.chainNamespace ===
174
- CommonConstantsUtil.CHAIN.SOLANA) {
175
- const response = await provider.request({
176
- method: 'solana_signMessage',
177
- params: {
178
- message: bs58.encode(new TextEncoder().encode(message)),
179
- pubkey: address
180
- }
181
- }, ChainController.state.activeCaipNetwork?.caipNetworkId);
182
- signature = response.signature;
183
- }
184
- else {
185
- signature = await provider.request({
186
- method: 'personal_sign',
187
- params: [message, address]
188
- }, ChainController.state.activeCaipNetwork?.caipNetworkId);
189
- }
190
- return signature;
191
- },
192
- estimateGas: async () => await Promise.resolve(BigInt(0)),
193
- // -- Transaction methods ---------------------------------------------------
194
- /**
195
- *
196
- * These methods are supported only on `wagmi` and `ethers` since the Solana SDK does not support them in the same way.
197
- * These function definition is to have a type parity between the clients. Currently not in use.
198
- */
199
- getEnsAvatar: async (value) => await Promise.resolve(value),
200
- getEnsAddress: async (value) => await Promise.resolve(value),
201
- writeContract: async () => await Promise.resolve('0x'),
202
- getCapabilities: async (params) => {
203
- const provider = await this.getWalletConnectProvider();
204
- if (!provider) {
205
- throw new Error('connectionControllerClient:getCapabilities - provider is undefined');
206
- }
207
- const walletCapabilitiesString = provider.session?.sessionProperties?.['capabilities'];
208
- if (walletCapabilitiesString) {
209
- const walletCapabilities = this.parseWalletCapabilities(walletCapabilitiesString);
210
- const accountCapabilities = walletCapabilities[params];
211
- if (accountCapabilities) {
212
- return accountCapabilities;
213
- }
214
- }
215
- return await provider.request({ method: 'wallet_getCapabilities', params: [params] });
216
- },
217
- grantPermissions: async (params) => {
218
- const provider = await this.getWalletConnectProvider();
219
- if (!provider) {
220
- throw new Error('connectionControllerClient:grantPermissions - provider is undefined');
221
- }
222
- return provider.request({ method: 'wallet_grantPermissions', params });
223
- },
224
- revokePermissions: async (session) => {
225
- const provider = await this.getWalletConnectProvider();
226
- if (!provider) {
227
- throw new Error('connectionControllerClient:grantPermissions - provider is undefined');
228
- }
229
- return provider.request({ method: 'wallet_revokePermissions', params: [session] });
230
- },
231
- sendTransaction: async () => await Promise.resolve('0x'),
232
- parseUnits: () => BigInt(0),
233
- formatUnits: () => ''
234
- };
235
- ChainController.subscribeKey('activeCaipNetwork', val => {
236
- const caipAddress = this.appKit?.getCaipAddress(this.chainNamespace);
237
- if (val && caipAddress) {
238
- this.syncBalance(CoreHelperUtil.getPlainAddress(caipAddress), val);
239
- this.syncAccount();
240
- }
241
- });
242
- ChainController.subscribeKey('activeCaipAddress', val => {
243
- const caipNetwork = ChainController.state.activeCaipNetwork;
244
- if (val && caipNetwork) {
245
- this.syncBalance(CoreHelperUtil.getPlainAddress(val), caipNetwork);
246
- this.syncAccount();
247
- }
1
+ import { AdapterBlueprint } from '../adapters/ChainAdapterBlueprint.js';
2
+ import { WcHelpersUtil } from '../utils/index.js';
3
+ export class UniversalAdapter extends AdapterBlueprint {
4
+ async connectWalletConnect(onUri) {
5
+ const connector = this.connectors.find(c => c.type === 'WALLET_CONNECT');
6
+ const provider = connector?.provider;
7
+ if (!this.caipNetworks || !provider) {
8
+ throw new Error('UniversalAdapter:connectWalletConnect - caipNetworks or provider is undefined');
9
+ }
10
+ provider.on('display_uri', (uri) => {
11
+ onUri(uri);
248
12
  });
13
+ const namespaces = WcHelpersUtil.createNamespaces(this.caipNetworks);
14
+ await provider.connect({ optionalNamespaces: namespaces });
249
15
  }
250
- // -- Public ------------------------------------------------------------------
251
- construct(appkit, options) {
252
- this.appKit = appkit;
253
- this.options = options;
254
- this.createProvider();
255
- this.syncRequestedNetworks(this.caipNetworks);
256
- this.syncConnectors();
257
- }
258
- switchNetwork(caipNetwork) {
259
- if (caipNetwork) {
260
- if (this.walletConnectProvider) {
261
- this.walletConnectProvider.setDefaultChain(caipNetwork.caipNetworkId);
262
- }
263
- }
16
+ async connect(params) {
17
+ return Promise.resolve({
18
+ id: 'WALLET_CONNECT',
19
+ type: 'WALLET_CONNECT',
20
+ chainId: Number(params.chainId),
21
+ provider: this.provider,
22
+ address: ''
23
+ });
264
24
  }
265
25
  async disconnect() {
266
- if (this.walletConnectProvider) {
267
- await this.walletConnectProvider.disconnect();
268
- this.appKit?.resetAccount(CommonConstantsUtil.CHAIN.EVM);
269
- this.appKit?.resetAccount(CommonConstantsUtil.CHAIN.SOLANA);
270
- }
271
- }
272
- async getWalletConnectProvider() {
273
- if (!this.walletConnectProvider) {
274
- try {
275
- await this.createProvider();
276
- }
277
- catch (error) {
278
- throw new Error('EthereumAdapter:getWalletConnectProvider - Cannot create provider');
279
- }
280
- }
281
- return this.walletConnectProvider;
282
- }
283
- // -- Private -----------------------------------------------------------------
284
- async syncBalance(address, caipNetwork) {
285
- const isExistingNetwork = this.appKit
286
- ?.getCaipNetworks(caipNetwork.chainNamespace)
287
- .find(network => network.id === caipNetwork.id);
288
- // How to fetch balance on non-evm networks?
289
- if (caipNetwork && isExistingNetwork) {
290
- try {
291
- const { balances } = await BlockchainApiController.getBalance(address, String(caipNetwork.id));
292
- const balance = balances.find(b => b.symbol === caipNetwork.nativeCurrency.symbol);
293
- this.appKit?.setBalance(balance?.quantity.numeric || '0', caipNetwork.nativeCurrency.symbol, this.chainNamespace);
294
- }
295
- catch (error) {
296
- // eslint-disable-next-line no-console
297
- console.error('Error fetching balance', error);
298
- }
299
- }
300
- }
301
- createProvider() {
302
- if (!this.walletConnectProviderInitPromise &&
303
- typeof window !== 'undefined' &&
304
- this.options?.projectId) {
305
- this.walletConnectProviderInitPromise = this.initWalletConnectProvider(this.options?.projectId);
306
- }
307
- return this.walletConnectProviderInitPromise;
26
+ const connector = this.connectors.find(c => c.id === 'WALLET_CONNECT');
27
+ const provider = connector?.provider;
28
+ await provider?.disconnect();
29
+ }
30
+ async syncConnectors() {
31
+ return Promise.resolve();
32
+ }
33
+ async getBalance() {
34
+ return Promise.resolve({
35
+ balance: '0',
36
+ decimals: 0,
37
+ symbol: ''
38
+ });
308
39
  }
309
- handleAlertError(error) {
310
- const matchedUniversalProviderError = Object.entries(ErrorUtil.UniversalProviderErrors).find(([, { message }]) => error.message.includes(message));
311
- const [errorKey, errorValue] = matchedUniversalProviderError ?? [];
312
- const { message, alertErrorKey } = errorValue ?? {};
313
- if (errorKey && message && !this.reportedAlertErrors[errorKey]) {
314
- const alertError = ErrorUtil.ALERT_ERRORS[alertErrorKey];
315
- if (alertError) {
316
- AlertController.open(alertError, 'error');
317
- this.reportedAlertErrors[errorKey] = true;
318
- }
40
+ async signMessage(params) {
41
+ const { provider, message, address } = params;
42
+ if (!provider) {
43
+ throw new Error('UniversalAdapter:signMessage - provider is undefined');
319
44
  }
320
- }
321
- async initWalletConnectProvider(projectId) {
322
- const logger = LoggerUtil.createLogger((error, ...args) => {
323
- if (error) {
324
- this.handleAlertError(error);
325
- }
326
- // eslint-disable-next-line no-console
327
- console.error(...args);
45
+ const signature = await provider.request({
46
+ method: 'personal_sign',
47
+ params: [message, address]
48
+ });
49
+ return { signature: signature };
50
+ }
51
+ // -- Transaction methods ---------------------------------------------------
52
+ /**
53
+ *
54
+ * These methods are supported only on `wagmi` and `ethers` since the Solana SDK does not support them in the same way.
55
+ * These function definition is to have a type parity between the clients. Currently not in use.
56
+ */
57
+ async estimateGas() {
58
+ return Promise.resolve({
59
+ gas: BigInt(0)
328
60
  });
329
- const walletConnectProviderOptions = {
330
- projectId,
331
- metadata: {
332
- name: this.metadata ? this.metadata.name : '',
333
- description: this.metadata ? this.metadata.description : '',
334
- url: this.metadata ? this.metadata.url : '',
335
- icons: this.metadata ? this.metadata.icons : ['']
336
- },
337
- logger
338
- };
339
- this.walletConnectProvider = await UniversalProvider.init(walletConnectProviderOptions);
340
- await this.checkActiveWalletConnectProvider();
341
61
  }
342
- syncRequestedNetworks(caipNetworks) {
343
- const uniqueChainNamespaces = [
344
- ...new Set(caipNetworks.map(caipNetwork => caipNetwork.chainNamespace))
345
- ];
346
- uniqueChainNamespaces
347
- .filter(c => Boolean(c))
348
- .forEach(chainNamespace => {
349
- this.appKit?.setRequestedCaipNetworks(caipNetworks.filter(caipNetwork => caipNetwork.chainNamespace === chainNamespace), chainNamespace);
62
+ async getProfile() {
63
+ return Promise.resolve({
64
+ profileImage: '',
65
+ profileName: ''
350
66
  });
351
67
  }
352
- async checkActiveWalletConnectProvider() {
353
- const WalletConnectProvider = await this.getWalletConnectProvider();
354
- const walletId = SafeLocalStorage.getItem(SafeLocalStorageKeys.WALLET_ID);
355
- if (WalletConnectProvider) {
356
- if (walletId === ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID) {
357
- this.setWalletConnectProvider();
358
- }
359
- }
68
+ async sendTransaction() {
69
+ return Promise.resolve({
70
+ hash: ''
71
+ });
360
72
  }
361
- setWalletConnectProvider() {
362
- SafeLocalStorage.setItem(SafeLocalStorageKeys.WALLET_ID, ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID);
363
- const nameSpaces = this.walletConnectProvider?.session?.namespaces;
364
- if (nameSpaces) {
365
- const reversedChainNamespaces = Object.keys(nameSpaces).reverse();
366
- reversedChainNamespaces.forEach(chainNamespace => {
367
- const caipAddress = nameSpaces?.[chainNamespace]?.accounts[0];
368
- ProviderUtil.setProvider(chainNamespace, this.walletConnectProvider);
369
- ProviderUtil.setProviderId(chainNamespace, 'walletConnect');
370
- this.appKit?.setApprovedCaipNetworksData(chainNamespace);
371
- if (caipAddress) {
372
- this.appKit?.setCaipAddress(caipAddress, chainNamespace);
373
- }
374
- });
375
- const storedCaipNetwork = StorageUtil.getStoredActiveCaipNetwork();
376
- const activeCaipNetwork = ChainController.state.activeCaipNetwork;
377
- try {
378
- if (storedCaipNetwork) {
379
- ChainController.setActiveCaipNetwork(storedCaipNetwork);
380
- }
381
- else if (!activeCaipNetwork ||
382
- !ChainController.getAllApprovedCaipNetworkIds().includes(activeCaipNetwork.caipNetworkId)) {
383
- this.setDefaultNetwork(nameSpaces);
384
- }
385
- }
386
- catch (error) {
387
- console.warn('>>> Error setting active caip network', error);
388
- }
389
- }
390
- this.syncAccount();
391
- this.watchWalletConnect();
73
+ async writeContract() {
74
+ return Promise.resolve({
75
+ hash: ''
76
+ });
392
77
  }
393
- setDefaultNetwork(nameSpaces) {
394
- const chainNamespace = this.caipNetworks[0]?.chainNamespace;
395
- if (chainNamespace) {
396
- const namespace = nameSpaces?.[chainNamespace];
397
- if (namespace?.chains) {
398
- const chainId = namespace.chains[0];
399
- if (chainId) {
400
- const requestedCaipNetworks = ChainController.getRequestedCaipNetworks(chainNamespace);
401
- if (requestedCaipNetworks) {
402
- const network = requestedCaipNetworks.find(c => c.caipNetworkId === chainId);
403
- if (network) {
404
- ChainController.setActiveCaipNetwork(network);
405
- }
406
- }
407
- }
408
- }
409
- }
78
+ async getEnsAddress() {
79
+ return Promise.resolve({
80
+ address: false
81
+ });
410
82
  }
411
- async watchWalletConnect() {
412
- const provider = await this.getWalletConnectProvider();
413
- const namespaces = provider?.session?.namespaces || {};
414
- function disconnectHandler() {
415
- Object.keys(namespaces).forEach(key => {
416
- AccountController.resetAccount(key);
417
- });
418
- ConnectionController.resetWcConnection();
419
- SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID);
420
- provider?.removeListener('disconnect', disconnectHandler);
421
- provider?.removeListener('accountsChanged', accountsChangedHandler);
422
- }
423
- const accountsChangedHandler = (accounts) => {
424
- if (accounts.length > 0) {
425
- this.syncAccount();
426
- }
427
- };
428
- const chainChanged = (chainId) => {
429
- // eslint-disable-next-line eqeqeq
430
- const caipNetwork = this.caipNetworks.find(c => c.id == chainId);
431
- const currentCaipNetwork = this.appKit?.getCaipNetwork();
432
- if (!caipNetwork) {
433
- const namespace = this.appKit?.getActiveChainNamespace() || CommonConstantsUtil.CHAIN.EVM;
434
- ChainController.setActiveCaipNetwork({
435
- id: chainId,
436
- caipNetworkId: `${namespace}:${chainId}`,
437
- name: 'Unknown Network',
438
- chainNamespace: namespace,
439
- nativeCurrency: {
440
- name: '',
441
- decimals: 0,
442
- symbol: ''
443
- },
444
- rpcUrls: {
445
- default: {
446
- http: []
447
- }
448
- }
449
- });
450
- return;
451
- }
452
- if (!currentCaipNetwork || currentCaipNetwork?.id !== caipNetwork?.id) {
453
- this.appKit?.setCaipNetwork(caipNetwork);
454
- }
455
- };
456
- if (provider) {
457
- provider.on('disconnect', disconnectHandler);
458
- provider.on('accountsChanged', accountsChangedHandler);
459
- provider.on('chainChanged', chainChanged);
460
- provider.on('connect', this.syncAccount.bind(this));
461
- }
83
+ parseUnits() {
84
+ return 0n;
462
85
  }
463
- getProviderData() {
464
- const namespaces = this.walletConnectProvider?.session?.namespaces || {};
465
- const isConnected = this.appKit?.getIsConnectedState() || false;
466
- const preferredAccountType = this.appKit?.getPreferredAccountType() || '';
467
- return {
468
- provider: this.walletConnectProvider,
469
- namespaces,
470
- namespaceKeys: namespaces ? Object.keys(namespaces) : [],
471
- isConnected,
472
- preferredAccountType
473
- };
86
+ formatUnits() {
87
+ return '0';
474
88
  }
475
- syncAccount() {
476
- const { namespaceKeys, namespaces } = this.getProviderData();
477
- const preferredAccountType = this.appKit?.getPreferredAccountType();
478
- const isConnected = this.appKit?.getIsConnectedState() || false;
479
- if (isConnected) {
480
- namespaceKeys.forEach(async (key) => {
481
- const chainNamespace = key;
482
- const address = namespaces?.[key]?.accounts[0];
483
- const isNamespaceConnected = this.appKit?.getCaipAddress(chainNamespace);
484
- if (!isNamespaceConnected) {
485
- this.appKit?.setPreferredAccountType(preferredAccountType, chainNamespace);
486
- this.appKit?.setCaipAddress(address, chainNamespace);
487
- this.syncConnectedWalletInfo();
488
- await Promise.all([this.appKit?.setApprovedCaipNetworksData(chainNamespace)]);
489
- }
490
- this.syncAccounts();
491
- });
492
- }
493
- else {
494
- this.appKit?.resetWcConnection();
495
- this.appKit?.resetNetwork(this.chainNamespace);
496
- this.syncAccounts(true);
497
- }
89
+ async getCapabilities() {
90
+ return Promise.resolve({});
498
91
  }
499
- syncAccounts(reset = false) {
500
- const { namespaces } = this.getProviderData();
501
- const chainNamespaces = Object.keys(namespaces);
502
- chainNamespaces.forEach(chainNamespace => {
503
- const addresses = namespaces?.[chainNamespace]?.accounts
504
- ?.map(account => {
505
- const [, , address] = account.split(':');
506
- return address;
507
- })
508
- .filter((address, index, self) => self.indexOf(address) === index);
509
- if (reset) {
510
- this.appKit?.setAllAccounts([], chainNamespace);
511
- }
512
- if (addresses) {
513
- this.appKit?.setAllAccounts(addresses.map(address => ({ address, type: 'eoa' })), chainNamespace);
514
- }
515
- });
92
+ async grantPermissions() {
93
+ return Promise.resolve({});
516
94
  }
517
- syncConnectedWalletInfo() {
518
- const currentActiveWallet = SafeLocalStorage.getItem(SafeLocalStorageKeys.WALLET_ID);
519
- const namespaces = this.walletConnectProvider?.session?.namespaces || {};
520
- const chainNamespaces = Object.keys(namespaces);
521
- chainNamespaces.forEach(chainNamespace => {
522
- if (this.walletConnectProvider?.session) {
523
- this.appKit?.setConnectedWalletInfo({
524
- ...this.walletConnectProvider.session.peer.metadata,
525
- name: this.walletConnectProvider.session.peer.metadata.name,
526
- icon: this.walletConnectProvider.session.peer.metadata.icons?.[0]
527
- }, chainNamespace);
528
- }
529
- else if (currentActiveWallet) {
530
- this.appKit?.setConnectedWalletInfo({ name: currentActiveWallet }, CommonConstantsUtil.CHAIN.EVM);
531
- this.appKit?.setConnectedWalletInfo({ name: currentActiveWallet }, CommonConstantsUtil.CHAIN.SOLANA);
532
- }
533
- });
95
+ async revokePermissions() {
96
+ return Promise.resolve('0x');
534
97
  }
535
- syncConnectors() {
536
- const w3mConnectors = [];
537
- w3mConnectors.push({
538
- id: ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID,
539
- explorerId: PresetsUtil.ConnectorExplorerIds[ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID],
540
- imageId: PresetsUtil.ConnectorImageIds[ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID],
541
- name: PresetsUtil.ConnectorNamesMap[ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID],
98
+ async syncConnection() {
99
+ return Promise.resolve({
100
+ id: 'WALLET_CONNECT',
542
101
  type: 'WALLET_CONNECT',
543
- chain: this.chainNamespace
102
+ chainId: 1,
103
+ provider: this.provider,
104
+ address: ''
544
105
  });
545
- this.appKit?.setConnectors(w3mConnectors);
546
106
  }
547
- parseWalletCapabilities(walletCapabilitiesString) {
548
- try {
549
- const walletCapabilities = JSON.parse(walletCapabilitiesString);
550
- return walletCapabilities;
551
- }
552
- catch (error) {
553
- throw new Error('Error parsing wallet capabilities');
107
+ // eslint-disable-next-line @typescript-eslint/require-await
108
+ async switchNetwork(params) {
109
+ const { caipNetwork } = params;
110
+ const connector = this.connectors.find(c => c.type === 'WALLET_CONNECT');
111
+ const provider = connector?.provider;
112
+ if (!provider) {
113
+ throw new Error('UniversalAdapter:switchNetwork - provider is undefined');
554
114
  }
115
+ provider.setDefaultChain(`${caipNetwork.chainNamespace}:${String(caipNetwork.id)}`);
116
+ }
117
+ getWalletConnectProvider() {
118
+ const connector = this.connectors.find(c => c.type === 'WALLET_CONNECT');
119
+ const provider = connector?.provider;
120
+ return provider;
555
121
  }
556
122
  }
557
123
  //# sourceMappingURL=client.js.map