@reown/appkit 1.7.0-alpha.0 → 1.7.0-rc.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.
- package/dist/esm/exports/basic-react.js +39 -0
- package/dist/esm/exports/basic-react.js.map +1 -0
- package/dist/esm/exports/basic-vue.js +45 -0
- package/dist/esm/exports/basic-vue.js.map +1 -0
- package/dist/esm/exports/constants.js +1 -1
- package/dist/esm/exports/constants.js.map +1 -1
- package/dist/esm/exports/index.js +2 -2
- package/dist/esm/exports/index.js.map +1 -1
- package/dist/esm/exports/react-basic.js +39 -0
- package/dist/esm/exports/react-basic.js.map +1 -0
- package/dist/esm/exports/react.js +4 -4
- package/dist/esm/exports/react.js.map +1 -1
- package/dist/esm/exports/vue copy.js +44 -0
- package/dist/esm/exports/vue copy.js.map +1 -0
- package/dist/esm/exports/vue-basic.js +45 -0
- package/dist/esm/exports/vue-basic.js.map +1 -0
- package/dist/esm/exports/vue.js +2 -2
- package/dist/esm/exports/vue.js.map +1 -1
- package/dist/esm/package.json +18 -8
- package/dist/esm/src/adapters/ChainAdapterBlueprint.js +1 -1
- package/dist/esm/src/adapters/ChainAdapterBlueprint.js.map +1 -1
- package/dist/esm/src/client/appkit-basic.js +9 -5
- package/dist/esm/src/client/appkit-basic.js.map +1 -1
- package/dist/esm/src/client/appkit.js +95 -91
- package/dist/esm/src/client/appkit.js.map +1 -1
- package/dist/esm/src/client/core.js +149 -107
- package/dist/esm/src/client/core.js.map +1 -1
- package/dist/esm/src/client.js +34 -27
- package/dist/esm/src/client.js.map +1 -1
- package/dist/esm/src/connectors/WalletConnectConnector.js +1 -1
- package/dist/esm/src/connectors/WalletConnectConnector.js.map +1 -1
- package/dist/esm/src/library/react/index.js +1 -1
- package/dist/esm/src/library/react/index.js.map +1 -1
- package/dist/esm/src/library/vue/index.js +2 -2
- package/dist/esm/src/library/vue/index.js.map +1 -1
- package/dist/esm/src/store/ProviderUtil.js +3 -0
- package/dist/esm/src/store/ProviderUtil.js.map +1 -1
- package/dist/esm/src/universal-adapter/client.js +1 -1
- package/dist/esm/src/universal-adapter/client.js.map +1 -1
- package/dist/esm/src/utils/HelpersUtil.js +1 -1
- package/dist/esm/src/utils/HelpersUtil.js.map +1 -1
- package/dist/esm/tests/client/adapter-management.test.js +7 -5
- package/dist/esm/tests/client/adapter-management.test.js.map +1 -1
- package/dist/esm/tests/client/appkit-basic.test.js +7 -6
- package/dist/esm/tests/client/appkit-basic.test.js.map +1 -1
- package/dist/esm/tests/client/balance.test.js +7 -5
- package/dist/esm/tests/client/balance.test.js.map +1 -1
- package/dist/esm/tests/client/connection.test.js +24 -21
- package/dist/esm/tests/client/connection.test.js.map +1 -1
- package/dist/esm/tests/client/initialization.test.js +7 -8
- package/dist/esm/tests/client/initialization.test.js.map +1 -1
- package/dist/esm/tests/client/listeners.test.js +46 -12
- package/dist/esm/tests/client/listeners.test.js.map +1 -1
- package/dist/esm/tests/client/public-methods.test.js +95 -10
- package/dist/esm/tests/client/public-methods.test.js.map +1 -1
- package/dist/esm/tests/client/universal-adapter.test.js +7 -5
- package/dist/esm/tests/client/universal-adapter.test.js.map +1 -1
- package/dist/esm/tests/client/walletconnect-events.test.js +11 -9
- package/dist/esm/tests/client/walletconnect-events.test.js.map +1 -1
- package/dist/esm/tests/connectors/WalletConnectConnector.test.js +1 -1
- package/dist/esm/tests/connectors/WalletConnectConnector.test.js.map +1 -1
- package/dist/esm/tests/mocks/Adapter.js +9 -6
- package/dist/esm/tests/mocks/Adapter.js.map +1 -1
- package/dist/esm/tests/mocks/LocalStorage.js +37 -0
- package/dist/esm/tests/mocks/LocalStorage.js.map +1 -0
- package/dist/esm/tests/siwe.test.js +1 -1
- package/dist/esm/tests/siwe.test.js.map +1 -1
- package/dist/esm/tests/test-utils.js +19 -12
- package/dist/esm/tests/test-utils.js.map +1 -1
- package/dist/esm/tsconfig.build.tsbuildinfo +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/types/exports/basic-react.d.ts +16 -0
- package/dist/types/exports/basic-vue.d.ts +15 -0
- package/dist/types/exports/basic.d.ts +1 -1
- package/dist/types/exports/constants.d.ts +1 -1
- package/dist/types/exports/index.d.ts +2 -2
- package/dist/types/exports/react-basic.d.ts +16 -0
- package/dist/types/exports/react.d.ts +4 -4
- package/dist/types/exports/vue copy.d.ts +15 -0
- package/dist/types/exports/vue-basic.d.ts +15 -0
- package/dist/types/exports/vue.d.ts +3 -3
- package/dist/types/src/adapters/ChainAdapterBlueprint.d.ts +3 -3
- package/dist/types/src/adapters/ChainAdapterConnector.d.ts +1 -1
- package/dist/types/src/client/appkit-basic.d.ts +3 -3
- package/dist/types/src/client/appkit.d.ts +1 -2
- package/dist/types/src/client/core.d.ts +27 -11
- package/dist/types/src/client.d.ts +24 -28
- package/dist/types/src/library/react/index.d.ts +9 -9
- package/dist/types/src/library/vue/index.d.ts +8 -8
- package/dist/types/src/networks/bitcoin.d.ts +2 -2
- package/dist/types/src/networks/solana/solana.d.ts +1 -1
- package/dist/types/src/networks/solana/solanaDevnet.d.ts +1 -1
- package/dist/types/src/networks/solana/solanaTestnet.d.ts +1 -1
- package/dist/types/src/store/ProviderUtil.d.ts +2 -2
- package/dist/types/src/utils/TypesUtil.d.ts +1 -1
- package/dist/types/tests/mocks/Adapter.d.ts +3 -0
- package/dist/types/tests/mocks/LocalStorage.d.ts +12 -0
- package/dist/types/tests/mocks/Options.d.ts +1 -1
- package/dist/types/tests/test-utils.d.ts +0 -1
- package/package.json +25 -15
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import UniversalProvider from '@walletconnect/universal-provider';
|
|
2
2
|
import { ConstantsUtil, NetworkUtil, ParseUtil } from '@reown/appkit-common';
|
|
3
|
-
import { AccountController, AlertController, AssetUtil, BlockchainApiController, ChainController, ConnectionController, ConnectorController, CoreHelperUtil, EnsController, EventsController, ModalController, OptionsController, PublicStateController, RouterController, SnackController, StorageUtil, ThemeController } from '@reown/appkit-
|
|
3
|
+
import { AccountController, AlertController, AssetUtil, BlockchainApiController, ChainController, ConnectionController, ConnectorController, CoreHelperUtil, EnsController, EventsController, ModalController, OptionsController, PublicStateController, RouterController, SnackController, StorageUtil, ThemeController } from '@reown/appkit-controllers';
|
|
4
4
|
import { WalletUtil } from '@reown/appkit-scaffold-ui/utils';
|
|
5
5
|
import { setColorTheme, setThemeVariables } from '@reown/appkit-ui';
|
|
6
6
|
import { CaipNetworksUtil, ErrorUtil, HelpersUtil, LoggerUtil, ConstantsUtil as UtilConstantsUtil } from '@reown/appkit-utils';
|
|
@@ -14,6 +14,10 @@ export class AppKitCore {
|
|
|
14
14
|
// -- Public Internal ---------------------------------------------------
|
|
15
15
|
this.getCaipNetwork = (chainNamespace) => {
|
|
16
16
|
if (chainNamespace) {
|
|
17
|
+
const namespaceCaipNetwork = ChainController.getNetworkData(chainNamespace)?.caipNetwork;
|
|
18
|
+
if (namespaceCaipNetwork) {
|
|
19
|
+
return namespaceCaipNetwork;
|
|
20
|
+
}
|
|
17
21
|
return ChainController.getRequestedCaipNetworks(chainNamespace).filter(c => c.chainNamespace === chainNamespace)?.[0];
|
|
18
22
|
}
|
|
19
23
|
return ChainController.state.activeCaipNetwork || this.defaultCaipNetwork;
|
|
@@ -41,7 +45,7 @@ export class AppKitCore {
|
|
|
41
45
|
BlockchainApiController.setClientId(clientId);
|
|
42
46
|
};
|
|
43
47
|
this.getProvider = (namespace) => ProviderUtil.getProvider(namespace);
|
|
44
|
-
this.getProviderType = (namespace) => ProviderUtil.
|
|
48
|
+
this.getProviderType = (namespace) => ProviderUtil.getProviderId(namespace);
|
|
45
49
|
this.getPreferredAccountType = () => AccountController.state.preferredAccountType;
|
|
46
50
|
this.setCaipAddress = (caipAddress, chain) => {
|
|
47
51
|
AccountController.setCaipAddress(caipAddress, chain);
|
|
@@ -67,13 +71,22 @@ export class AppKitCore {
|
|
|
67
71
|
this.setCaipNetwork = caipNetwork => {
|
|
68
72
|
ChainController.setActiveCaipNetwork(caipNetwork);
|
|
69
73
|
};
|
|
74
|
+
this.setCaipNetworkOfNamespace = (caipNetwork, chainNamespace) => {
|
|
75
|
+
ChainController.setChainNetworkData(chainNamespace, { caipNetwork });
|
|
76
|
+
};
|
|
70
77
|
this.setAllAccounts = (addresses, chain) => {
|
|
71
78
|
AccountController.setAllAccounts(addresses, chain);
|
|
72
79
|
OptionsController.setHasMultipleAddresses(addresses?.length > 1);
|
|
73
80
|
};
|
|
74
81
|
this.setStatus = (status, chain) => {
|
|
75
|
-
StorageUtil.setConnectionStatus(status);
|
|
76
82
|
AccountController.setStatus(status, chain);
|
|
83
|
+
// If at least one namespace is connected, set the connection status
|
|
84
|
+
if (ConnectorController.isConnected()) {
|
|
85
|
+
StorageUtil.setConnectionStatus('connected');
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
StorageUtil.setConnectionStatus('disconnected');
|
|
89
|
+
}
|
|
77
90
|
};
|
|
78
91
|
this.getAddressByChainNamespace = (chainNamespace) => ChainController.getAccountProp('address', chainNamespace);
|
|
79
92
|
this.setConnectors = connectors => {
|
|
@@ -180,6 +193,7 @@ export class AppKitCore {
|
|
|
180
193
|
this.initializeChainController(options);
|
|
181
194
|
this.initializeThemeController(options);
|
|
182
195
|
this.initializeConnectionController(options);
|
|
196
|
+
this.initializeConnectorController();
|
|
183
197
|
}
|
|
184
198
|
initializeThemeController(options) {
|
|
185
199
|
if (options.themeMode) {
|
|
@@ -205,12 +219,11 @@ export class AppKitCore {
|
|
|
205
219
|
initializeConnectionController(options) {
|
|
206
220
|
ConnectionController.setWcBasic(options.basic ?? false);
|
|
207
221
|
}
|
|
222
|
+
initializeConnectorController() {
|
|
223
|
+
ConnectorController.initialize(this.chainNamespaces);
|
|
224
|
+
}
|
|
208
225
|
initializeOptionsController(options) {
|
|
209
226
|
OptionsController.setDebug(options.debug !== false);
|
|
210
|
-
if (!options.projectId) {
|
|
211
|
-
AlertController.open(ErrorUtil.ALERT_ERRORS.PROJECT_ID_NOT_CONFIGURED, 'error');
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
227
|
// On by default
|
|
215
228
|
OptionsController.setEnableWalletConnect(options.enableWalletConnect !== false);
|
|
216
229
|
OptionsController.setEnableWalletGuide(options.enableWalletGuide !== false);
|
|
@@ -239,6 +252,10 @@ export class AppKitCore {
|
|
|
239
252
|
OptionsController.setDisableAppend(options.disableAppend);
|
|
240
253
|
OptionsController.setEnableEmbedded(options.enableEmbedded);
|
|
241
254
|
OptionsController.setSIWX(options.siwx);
|
|
255
|
+
if (!options.projectId) {
|
|
256
|
+
AlertController.open(ErrorUtil.ALERT_ERRORS.PROJECT_ID_NOT_CONFIGURED, 'error');
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
242
259
|
const evmAdapter = options.adapters?.find(adapter => adapter.namespace === ConstantsUtil.CHAIN.EVM);
|
|
243
260
|
// Set the SIWE client for EVM chains
|
|
244
261
|
if (evmAdapter) {
|
|
@@ -333,14 +350,19 @@ export class AppKitCore {
|
|
|
333
350
|
createClients() {
|
|
334
351
|
this.connectionControllerClient = {
|
|
335
352
|
connectWalletConnect: async () => {
|
|
336
|
-
const
|
|
353
|
+
const activeChain = ChainController.state.activeChain;
|
|
354
|
+
const adapter = this.getAdapter(activeChain);
|
|
355
|
+
const chainId = this.getCaipNetwork(activeChain)?.id;
|
|
337
356
|
if (!adapter) {
|
|
338
357
|
throw new Error('Adapter not found');
|
|
339
358
|
}
|
|
340
|
-
const result = await adapter.connectWalletConnect(
|
|
359
|
+
const result = await adapter.connectWalletConnect(chainId);
|
|
341
360
|
this.close();
|
|
342
361
|
this.setClientId(result?.clientId || null);
|
|
343
362
|
StorageUtil.setConnectedNamespaces([...ChainController.state.chains.keys()]);
|
|
363
|
+
this.chainNamespaces.forEach(namespace => {
|
|
364
|
+
ConnectorController.setConnectorId(UtilConstantsUtil.CONNECTOR_TYPE_WALLET_CONNECT, namespace);
|
|
365
|
+
});
|
|
344
366
|
await this.syncWalletConnectAccount();
|
|
345
367
|
},
|
|
346
368
|
connectExternal: async ({ id, info, type, provider, chain, caipNetwork }) => {
|
|
@@ -356,14 +378,15 @@ export class AppKitCore {
|
|
|
356
378
|
if (!adapter) {
|
|
357
379
|
throw new Error('Adapter not found');
|
|
358
380
|
}
|
|
381
|
+
const fallbackCaipNetwork = this.getCaipNetwork(chainToUse);
|
|
359
382
|
const res = await adapter.connect({
|
|
360
383
|
id,
|
|
361
384
|
info,
|
|
362
385
|
type,
|
|
363
386
|
provider,
|
|
364
|
-
chainId: caipNetwork?.id ||
|
|
387
|
+
chainId: caipNetwork?.id || fallbackCaipNetwork?.id,
|
|
365
388
|
rpcUrl: caipNetwork?.rpcUrls?.default?.http?.[0] ||
|
|
366
|
-
|
|
389
|
+
fallbackCaipNetwork?.rpcUrls?.default?.http?.[0]
|
|
367
390
|
});
|
|
368
391
|
if (!res) {
|
|
369
392
|
return;
|
|
@@ -386,7 +409,7 @@ export class AppKitCore {
|
|
|
386
409
|
const namespace = ChainController.state.activeChain;
|
|
387
410
|
const adapter = this.getAdapter(namespace);
|
|
388
411
|
const provider = ProviderUtil.getProvider(namespace);
|
|
389
|
-
const providerType = ProviderUtil.
|
|
412
|
+
const providerType = ProviderUtil.getProviderId(namespace);
|
|
390
413
|
await adapter?.disconnect({ provider, providerType });
|
|
391
414
|
StorageUtil.removeConnectedNamespace(namespace);
|
|
392
415
|
ProviderUtil.resetChain(namespace);
|
|
@@ -501,7 +524,7 @@ export class AppKitCore {
|
|
|
501
524
|
ConnectionController.setClient(this.connectionControllerClient);
|
|
502
525
|
}
|
|
503
526
|
getApprovedCaipNetworksData() {
|
|
504
|
-
const providerType = ProviderUtil.
|
|
527
|
+
const providerType = ProviderUtil.getProviderId(ChainController.state.activeChain);
|
|
505
528
|
if (providerType === UtilConstantsUtil.CONNECTOR_TYPE_WALLET_CONNECT) {
|
|
506
529
|
const namespaces = this.universalProvider?.session?.namespaces;
|
|
507
530
|
return {
|
|
@@ -520,32 +543,27 @@ export class AppKitCore {
|
|
|
520
543
|
if (!caipNetwork) {
|
|
521
544
|
return;
|
|
522
545
|
}
|
|
523
|
-
|
|
546
|
+
const networkNamespace = caipNetwork.chainNamespace;
|
|
547
|
+
const namespaceAddress = this.getAddressByChainNamespace(caipNetwork.chainNamespace);
|
|
548
|
+
if (namespaceAddress) {
|
|
549
|
+
const provider = ProviderUtil.getProvider(networkNamespace);
|
|
550
|
+
const providerType = ProviderUtil.getProviderId(networkNamespace);
|
|
524
551
|
if (caipNetwork.chainNamespace === ChainController.state.activeChain) {
|
|
525
|
-
const adapter = this.getAdapter(
|
|
526
|
-
const provider = ProviderUtil.getProvider(ChainController.state.activeChain);
|
|
527
|
-
const providerType = ProviderUtil.state.providerIds[ChainController.state.activeChain];
|
|
552
|
+
const adapter = this.getAdapter(networkNamespace);
|
|
528
553
|
await adapter?.switchNetwork({ caipNetwork, provider, providerType });
|
|
529
|
-
this.setCaipNetwork(caipNetwork);
|
|
530
|
-
await this.syncAccount({
|
|
531
|
-
address: AccountController.state.address,
|
|
532
|
-
chainId: caipNetwork.id,
|
|
533
|
-
chainNamespace: caipNetwork.chainNamespace
|
|
534
|
-
});
|
|
535
554
|
}
|
|
536
555
|
else {
|
|
537
|
-
const providerType = ProviderUtil.state.providerIds[ChainController.state.activeChain];
|
|
538
556
|
this.setCaipNetwork(caipNetwork);
|
|
539
|
-
if (providerType ===
|
|
557
|
+
if (providerType === UtilConstantsUtil.CONNECTOR_TYPE_WALLET_CONNECT) {
|
|
540
558
|
this.syncWalletConnectAccount();
|
|
541
559
|
}
|
|
542
560
|
else {
|
|
543
|
-
const address = this.getAddressByChainNamespace(
|
|
561
|
+
const address = this.getAddressByChainNamespace(networkNamespace);
|
|
544
562
|
if (address) {
|
|
545
563
|
this.syncAccount({
|
|
546
564
|
address,
|
|
547
565
|
chainId: caipNetwork.id,
|
|
548
|
-
chainNamespace:
|
|
566
|
+
chainNamespace: networkNamespace
|
|
549
567
|
});
|
|
550
568
|
}
|
|
551
569
|
}
|
|
@@ -625,18 +643,13 @@ export class AppKitCore {
|
|
|
625
643
|
this.setStatus(connectionStatus, chainNamespace);
|
|
626
644
|
}
|
|
627
645
|
adapter.on('switchNetwork', ({ address, chainId }) => {
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
this.syncAccount({
|
|
636
|
-
address: AccountController.state.address,
|
|
637
|
-
chainId,
|
|
638
|
-
chainNamespace
|
|
639
|
-
});
|
|
646
|
+
const caipNetwork = this.caipNetworks?.find(n => n.id === chainId || n.caipNetworkId === chainId);
|
|
647
|
+
const isSameNamespace = ChainController.state.activeChain === chainNamespace;
|
|
648
|
+
const accountAddress = ChainController.getAccountProp('address', chainNamespace);
|
|
649
|
+
if (caipNetwork) {
|
|
650
|
+
const account = isSameNamespace && address ? address : accountAddress;
|
|
651
|
+
if (account) {
|
|
652
|
+
this.syncAccount({ address: account, chainId, chainNamespace });
|
|
640
653
|
}
|
|
641
654
|
}
|
|
642
655
|
else {
|
|
@@ -650,24 +663,27 @@ export class AppKitCore {
|
|
|
650
663
|
if (!address || !activeCaipNetwork?.id) {
|
|
651
664
|
return;
|
|
652
665
|
}
|
|
653
|
-
this.updateNativeBalance();
|
|
666
|
+
this.updateNativeBalance(address, activeCaipNetwork.id, activeCaipNetwork.chainNamespace);
|
|
654
667
|
});
|
|
655
668
|
adapter.on('accountChanged', ({ address, chainId }) => {
|
|
656
|
-
|
|
669
|
+
const isActiveChain = ChainController.state.activeChain === chainNamespace;
|
|
670
|
+
if (isActiveChain && chainId) {
|
|
657
671
|
this.syncAccount({
|
|
658
672
|
address,
|
|
659
673
|
chainId,
|
|
660
674
|
chainNamespace
|
|
661
675
|
});
|
|
662
676
|
}
|
|
663
|
-
else if (ChainController.state.
|
|
664
|
-
ChainController.state.activeCaipNetwork?.id) {
|
|
677
|
+
else if (isActiveChain && ChainController.state.activeCaipNetwork?.id) {
|
|
665
678
|
this.syncAccount({
|
|
666
679
|
address,
|
|
667
680
|
chainId: ChainController.state.activeCaipNetwork?.id,
|
|
668
681
|
chainNamespace
|
|
669
682
|
});
|
|
670
683
|
}
|
|
684
|
+
else {
|
|
685
|
+
this.syncAccountInfo(address, chainId, chainNamespace);
|
|
686
|
+
}
|
|
671
687
|
});
|
|
672
688
|
}
|
|
673
689
|
async createUniversalProviderForAdapter(chainNamespace) {
|
|
@@ -682,7 +698,7 @@ export class AppKitCore {
|
|
|
682
698
|
}
|
|
683
699
|
async syncNamespaceConnection(namespace) {
|
|
684
700
|
try {
|
|
685
|
-
const connectorId =
|
|
701
|
+
const connectorId = ConnectorController.getConnectorId(namespace);
|
|
686
702
|
this.setStatus('connecting', namespace);
|
|
687
703
|
switch (connectorId) {
|
|
688
704
|
case ConstantsUtil.CONNECTOR_ID.WALLET_CONNECT:
|
|
@@ -697,28 +713,31 @@ export class AppKitCore {
|
|
|
697
713
|
}
|
|
698
714
|
catch (err) {
|
|
699
715
|
console.warn("AppKit couldn't sync existing connection", err);
|
|
700
|
-
StorageUtil.deleteConnectedConnectorId(namespace);
|
|
701
716
|
this.setStatus('disconnected', namespace);
|
|
702
717
|
}
|
|
703
718
|
}
|
|
704
719
|
async syncAdapterConnection(namespace) {
|
|
705
720
|
const adapter = this.getAdapter(namespace);
|
|
706
|
-
const connectorId =
|
|
721
|
+
const connectorId = ConnectorController.getConnectorId(namespace);
|
|
707
722
|
const caipNetwork = this.getCaipNetwork();
|
|
723
|
+
const connector = ConnectorController.getConnectors(namespace).find(c => c.id === connectorId);
|
|
708
724
|
try {
|
|
709
|
-
if (!adapter || !
|
|
710
|
-
throw new Error(`Adapter or
|
|
725
|
+
if (!adapter || !connector) {
|
|
726
|
+
throw new Error(`Adapter or connector not found for namespace ${namespace}`);
|
|
727
|
+
}
|
|
728
|
+
if (!caipNetwork?.id) {
|
|
729
|
+
throw new Error('CaipNetwork not found');
|
|
711
730
|
}
|
|
712
731
|
const connection = await adapter?.syncConnection({
|
|
713
732
|
namespace,
|
|
714
|
-
id:
|
|
715
|
-
chainId: caipNetwork
|
|
733
|
+
id: connector.id,
|
|
734
|
+
chainId: caipNetwork.id,
|
|
716
735
|
rpcUrl: caipNetwork?.rpcUrls?.default?.http?.[0]
|
|
717
736
|
});
|
|
718
737
|
if (connection) {
|
|
719
738
|
const accounts = await adapter?.getAccounts({
|
|
720
739
|
namespace,
|
|
721
|
-
id:
|
|
740
|
+
id: connector.id
|
|
722
741
|
});
|
|
723
742
|
if (accounts && accounts.accounts.length > 0) {
|
|
724
743
|
this.setAllAccounts(accounts.accounts, namespace);
|
|
@@ -735,12 +754,11 @@ export class AppKitCore {
|
|
|
735
754
|
}
|
|
736
755
|
}
|
|
737
756
|
catch (e) {
|
|
738
|
-
StorageUtil.deleteConnectedConnectorId(namespace);
|
|
739
757
|
this.setStatus('disconnected', namespace);
|
|
740
758
|
}
|
|
741
759
|
}
|
|
742
760
|
async syncWalletConnectAccount() {
|
|
743
|
-
this.chainNamespaces.
|
|
761
|
+
const syncTasks = this.chainNamespaces.map(async (chainNamespace) => {
|
|
744
762
|
const adapter = this.getAdapter(chainNamespace);
|
|
745
763
|
const namespaceAccounts = this.universalProvider?.session?.namespaces?.[chainNamespace]?.accounts || [];
|
|
746
764
|
// We try and find the address for this network in the session object.
|
|
@@ -766,7 +784,7 @@ export class AppKitCore {
|
|
|
766
784
|
else {
|
|
767
785
|
ProviderUtil.setProvider(chainNamespace, this.universalProvider);
|
|
768
786
|
}
|
|
769
|
-
|
|
787
|
+
ConnectorController.setConnectorId(ConstantsUtil.CONNECTOR_ID.WALLET_CONNECT, chainNamespace);
|
|
770
788
|
StorageUtil.addConnectedNamespace(chainNamespace);
|
|
771
789
|
this.syncWalletConnectAccounts(chainNamespace);
|
|
772
790
|
await this.syncAccount({
|
|
@@ -775,8 +793,12 @@ export class AppKitCore {
|
|
|
775
793
|
chainNamespace
|
|
776
794
|
});
|
|
777
795
|
}
|
|
796
|
+
else {
|
|
797
|
+
this.setStatus('disconnected', chainNamespace);
|
|
798
|
+
}
|
|
799
|
+
await ChainController.setApprovedCaipNetworksData(chainNamespace);
|
|
778
800
|
});
|
|
779
|
-
await
|
|
801
|
+
await Promise.all(syncTasks);
|
|
780
802
|
}
|
|
781
803
|
syncWalletConnectAccounts(chainNamespace) {
|
|
782
804
|
const addresses = this.universalProvider?.session?.namespaces?.[chainNamespace]?.accounts
|
|
@@ -792,19 +814,16 @@ export class AppKitCore {
|
|
|
792
814
|
syncProvider({ type, provider, id, chainNamespace }) {
|
|
793
815
|
ProviderUtil.setProviderId(chainNamespace, type);
|
|
794
816
|
ProviderUtil.setProvider(chainNamespace, provider);
|
|
795
|
-
|
|
817
|
+
ConnectorController.setConnectorId(id, chainNamespace);
|
|
796
818
|
}
|
|
797
819
|
async syncAccount(params) {
|
|
820
|
+
const isActiveNamespace = params.chainNamespace === ChainController.state.activeChain;
|
|
821
|
+
const networkOfChain = ChainController.getCaipNetworkByNamespace(params.chainNamespace, params.chainId);
|
|
798
822
|
const { address, chainId, chainNamespace } = params;
|
|
799
823
|
const { chainId: activeChainId } = StorageUtil.getActiveNetworkProps();
|
|
800
824
|
const chainIdToUse = chainId || activeChainId;
|
|
801
825
|
const isUnsupportedNetwork = ChainController.state.activeCaipNetwork?.name === ConstantsUtil.UNSUPPORTED_NETWORK_NAME;
|
|
802
826
|
const shouldSupportAllNetworks = ChainController.getNetworkProp('supportsAllNetworks', chainNamespace);
|
|
803
|
-
// Only update state when needed
|
|
804
|
-
if (!HelpersUtil.isLowerCaseMatch(address, AccountController.state.address)) {
|
|
805
|
-
this.setCaipAddress(`${chainNamespace}:${chainId}:${address}`, chainNamespace);
|
|
806
|
-
await this.syncIdentity({ address, chainId, chainNamespace });
|
|
807
|
-
}
|
|
808
827
|
this.setStatus('connected', chainNamespace);
|
|
809
828
|
if (isUnsupportedNetwork && !shouldSupportAllNetworks) {
|
|
810
829
|
return;
|
|
@@ -834,23 +853,43 @@ export class AppKitCore {
|
|
|
834
853
|
this.setCaipNetwork(network);
|
|
835
854
|
}
|
|
836
855
|
}
|
|
856
|
+
else if (!isActiveNamespace) {
|
|
857
|
+
if (networkOfChain) {
|
|
858
|
+
this.setCaipNetworkOfNamespace(networkOfChain, chainNamespace);
|
|
859
|
+
}
|
|
860
|
+
}
|
|
837
861
|
this.syncConnectedWalletInfo(chainNamespace);
|
|
838
|
-
// Only update state when needed
|
|
839
862
|
if (!HelpersUtil.isLowerCaseMatch(address, AccountController.state.address)) {
|
|
840
|
-
this.
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
863
|
+
this.syncAccountInfo(address, network?.id, chainNamespace);
|
|
864
|
+
}
|
|
865
|
+
if (isActiveNamespace) {
|
|
866
|
+
await this.syncBalance({ address, chainId: network?.id, chainNamespace });
|
|
867
|
+
}
|
|
868
|
+
else {
|
|
869
|
+
await this.syncBalance({ address, chainId: networkOfChain?.id, chainNamespace });
|
|
846
870
|
}
|
|
847
|
-
await this.syncBalance({ address, chainId: network?.id, chainNamespace });
|
|
848
871
|
}
|
|
849
872
|
}
|
|
873
|
+
async syncAccountInfo(address, chainId, chainNamespace) {
|
|
874
|
+
const caipAddress = this.getCaipAddress(chainNamespace);
|
|
875
|
+
const newChainId = chainId || caipAddress?.split(':')[1];
|
|
876
|
+
if (!newChainId) {
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
const newCaipAddress = `${chainNamespace}:${newChainId}:${address}`;
|
|
880
|
+
this.setCaipAddress(newCaipAddress, chainNamespace);
|
|
881
|
+
await this.syncIdentity({
|
|
882
|
+
address,
|
|
883
|
+
chainId: newChainId,
|
|
884
|
+
chainNamespace
|
|
885
|
+
});
|
|
886
|
+
}
|
|
850
887
|
async syncIdentity({ address, chainId, chainNamespace }) {
|
|
851
888
|
const caipNetworkId = `${chainNamespace}:${chainId}`;
|
|
852
889
|
const activeCaipNetwork = this.caipNetworks?.find(n => n.caipNetworkId === caipNetworkId);
|
|
853
890
|
if (chainNamespace !== ConstantsUtil.CHAIN.EVM || activeCaipNetwork?.testnet) {
|
|
891
|
+
this.setProfileName(null, chainNamespace);
|
|
892
|
+
this.setProfileImage(null, chainNamespace);
|
|
854
893
|
return;
|
|
855
894
|
}
|
|
856
895
|
try {
|
|
@@ -905,7 +944,7 @@ export class AppKitCore {
|
|
|
905
944
|
}
|
|
906
945
|
}
|
|
907
946
|
syncConnectedWalletInfo(chainNamespace) {
|
|
908
|
-
const connectorId =
|
|
947
|
+
const connectorId = ConnectorController.getConnectorId(chainNamespace);
|
|
909
948
|
const providerType = ProviderUtil.getProviderId(chainNamespace);
|
|
910
949
|
if (providerType === UtilConstantsUtil.CONNECTOR_TYPE_ANNOUNCED ||
|
|
911
950
|
providerType === UtilConstantsUtil.CONNECTOR_TYPE_INJECTED) {
|
|
@@ -940,21 +979,21 @@ export class AppKitCore {
|
|
|
940
979
|
}
|
|
941
980
|
async syncBalance(params) {
|
|
942
981
|
const caipNetwork = NetworkUtil.getNetworksByNamespace(this.caipNetworks, params.chainNamespace).find(n => n.id.toString() === params.chainId?.toString());
|
|
943
|
-
if (!caipNetwork) {
|
|
982
|
+
if (!caipNetwork || !params.chainId) {
|
|
944
983
|
return;
|
|
945
984
|
}
|
|
946
|
-
await this.updateNativeBalance();
|
|
985
|
+
await this.updateNativeBalance(params.address, params.chainId, params.chainNamespace);
|
|
947
986
|
}
|
|
948
|
-
async updateNativeBalance() {
|
|
949
|
-
const adapter = this.getAdapter(
|
|
950
|
-
if (adapter
|
|
987
|
+
async updateNativeBalance(address, chainId, namespace) {
|
|
988
|
+
const adapter = this.getAdapter(namespace);
|
|
989
|
+
if (adapter) {
|
|
951
990
|
const balance = await adapter.getBalance({
|
|
952
|
-
address
|
|
953
|
-
chainId
|
|
954
|
-
caipNetwork: this.getCaipNetwork(),
|
|
991
|
+
address,
|
|
992
|
+
chainId,
|
|
993
|
+
caipNetwork: this.getCaipNetwork(namespace),
|
|
955
994
|
tokens: this.options.tokens
|
|
956
995
|
});
|
|
957
|
-
this.setBalance(balance.balance, balance.symbol,
|
|
996
|
+
this.setBalance(balance.balance, balance.symbol, namespace);
|
|
958
997
|
}
|
|
959
998
|
}
|
|
960
999
|
// -- Universal Provider ---------------------------------------------------
|
|
@@ -1084,17 +1123,14 @@ export class AppKitCore {
|
|
|
1084
1123
|
if (options?.uri) {
|
|
1085
1124
|
ConnectionController.setUri(options.uri);
|
|
1086
1125
|
}
|
|
1087
|
-
if (options?.namespace) {
|
|
1088
|
-
ConnectorController.setFilterByNamespace(options.namespace);
|
|
1089
|
-
}
|
|
1090
1126
|
await ModalController.open(options);
|
|
1091
1127
|
}
|
|
1092
1128
|
async close() {
|
|
1093
1129
|
await this.injectModalUi();
|
|
1094
1130
|
ModalController.close();
|
|
1095
1131
|
}
|
|
1096
|
-
setLoading(loading) {
|
|
1097
|
-
ModalController.setLoading(loading);
|
|
1132
|
+
setLoading(loading, namespace) {
|
|
1133
|
+
ModalController.setLoading(loading, namespace);
|
|
1098
1134
|
}
|
|
1099
1135
|
async disconnect() {
|
|
1100
1136
|
await ConnectionController.disconnect();
|
|
@@ -1120,9 +1156,7 @@ export class AppKitCore {
|
|
|
1120
1156
|
: null;
|
|
1121
1157
|
}
|
|
1122
1158
|
getWalletProviderType() {
|
|
1123
|
-
return ChainController.state.activeChain
|
|
1124
|
-
? ProviderUtil.state.providerIds[ChainController.state.activeChain]
|
|
1125
|
-
: null;
|
|
1159
|
+
return ProviderUtil.getProviderId(ChainController.state.activeChain);
|
|
1126
1160
|
}
|
|
1127
1161
|
subscribeProviders(callback) {
|
|
1128
1162
|
return ProviderUtil.subscribeProviders(callback);
|
|
@@ -1153,29 +1187,37 @@ export class AppKitCore {
|
|
|
1153
1187
|
getWalletInfo() {
|
|
1154
1188
|
return AccountController.state.connectedWalletInfo;
|
|
1155
1189
|
}
|
|
1190
|
+
getAccount(namespace) {
|
|
1191
|
+
const authConnector = ConnectorController.getAuthConnector(namespace);
|
|
1192
|
+
const accountState = ChainController.getAccountData(namespace);
|
|
1193
|
+
if (!accountState) {
|
|
1194
|
+
return undefined;
|
|
1195
|
+
}
|
|
1196
|
+
return {
|
|
1197
|
+
allAccounts: accountState.allAccounts,
|
|
1198
|
+
caipAddress: accountState.caipAddress,
|
|
1199
|
+
address: CoreHelperUtil.getPlainAddress(accountState.caipAddress),
|
|
1200
|
+
isConnected: Boolean(accountState.caipAddress),
|
|
1201
|
+
status: accountState.status,
|
|
1202
|
+
embeddedWalletInfo: authConnector
|
|
1203
|
+
? {
|
|
1204
|
+
user: accountState.user,
|
|
1205
|
+
authProvider: accountState.socialProvider ||
|
|
1206
|
+
'email',
|
|
1207
|
+
accountType: accountState.preferredAccountType,
|
|
1208
|
+
isSmartAccountDeployed: Boolean(accountState.smartAccountDeployed)
|
|
1209
|
+
}
|
|
1210
|
+
: undefined
|
|
1211
|
+
};
|
|
1212
|
+
}
|
|
1156
1213
|
subscribeAccount(callback, namespace) {
|
|
1157
|
-
|
|
1158
|
-
const
|
|
1159
|
-
|
|
1160
|
-
if (!accountState) {
|
|
1214
|
+
const updateVal = () => {
|
|
1215
|
+
const account = this.getAccount(namespace);
|
|
1216
|
+
if (!account) {
|
|
1161
1217
|
return;
|
|
1162
1218
|
}
|
|
1163
|
-
callback(
|
|
1164
|
-
|
|
1165
|
-
caipAddress: accountState.caipAddress,
|
|
1166
|
-
address: CoreHelperUtil.getPlainAddress(accountState.caipAddress),
|
|
1167
|
-
isConnected: Boolean(accountState.caipAddress),
|
|
1168
|
-
status: accountState.status,
|
|
1169
|
-
embeddedWalletInfo: authConnector
|
|
1170
|
-
? {
|
|
1171
|
-
user: accountState.user,
|
|
1172
|
-
authProvider: accountState.socialProvider || 'email',
|
|
1173
|
-
accountType: accountState.preferredAccountType,
|
|
1174
|
-
isSmartAccountDeployed: Boolean(accountState.smartAccountDeployed)
|
|
1175
|
-
}
|
|
1176
|
-
: undefined
|
|
1177
|
-
});
|
|
1178
|
-
}
|
|
1219
|
+
callback(account);
|
|
1220
|
+
};
|
|
1179
1221
|
if (namespace) {
|
|
1180
1222
|
ChainController.subscribeChainProp('accountState', updateVal, namespace);
|
|
1181
1223
|
}
|