diff --git a/packages/appkit/src/client.ts b/packages/appkit/src/client.ts index d75d35ed4e..78e2edd9ee 100644 --- a/packages/appkit/src/client.ts +++ b/packages/appkit/src/client.ts @@ -196,13 +196,12 @@ export class AppKit { } ) { this.caipNetworks = this.extendCaipNetworks(options) - + this.defaultCaipNetwork = this.extendDefaultCaipNetwork(options) + await this.initControllers(options) this.createAuthProvider() await this.createUniversalProvider() this.createClients() ChainController.initialize(options.adapters ?? []) - this.defaultCaipNetwork = this.extendDefaultCaipNetwork(options) - this.initControllers(options) this.chainAdapters = await this.createAdapters( options.adapters as unknown as AdapterBlueprint[] ) @@ -805,11 +804,11 @@ export class AppKit { }) if (res) { - await this.syncAccount({ + this.syncProvider({ ...res, chainNamespace: chain || (ChainController.state.activeChain as ChainNamespace) }) - this.syncProvider({ + await this.syncAccount({ ...res, chainNamespace: chain || (ChainController.state.activeChain as ChainNamespace) }) @@ -1388,6 +1387,7 @@ export class AppKit { this.setCaipNetwork(this.caipNetworks?.find(n => n.chainNamespace === chainNamespace)) } + this.syncConnectedWalletInfo(chainNamespace) const adapter = this.getAdapter(chainNamespace) const balance = await adapter?.getBalance({ @@ -1403,6 +1403,48 @@ export class AppKit { } } + private syncConnectedWalletInfo(chainNamespace: ChainNamespace) { + const currentActiveWallet = SafeLocalStorage.getItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR) + const providerType = ProviderUtil.state.providerIds[chainNamespace] + + if ( + providerType === UtilConstantsUtil.CONNECTOR_TYPE_ANNOUNCED || + providerType === UtilConstantsUtil.CONNECTOR_TYPE_INJECTED + ) { + if (currentActiveWallet) { + const connector = this.getConnectors().find(c => c.id === currentActiveWallet) + + if (connector?.info) { + this.setConnectedWalletInfo({ ...connector.info }, chainNamespace) + } + } + } else if (providerType === UtilConstantsUtil.CONNECTOR_TYPE_WALLET_CONNECT) { + const provider = ProviderUtil.getProvider(chainNamespace) + + if (provider?.session) { + this.setConnectedWalletInfo( + { + ...provider.session.peer.metadata, + name: provider.session.peer.metadata.name, + icon: provider.session.peer.metadata.icons?.[0] + }, + chainNamespace + ) + } + } else if (providerType === UtilConstantsUtil.COINBASE_CONNECTOR_ID) { + const connector = this.getConnectors().find( + c => c.id === UtilConstantsUtil.COINBASE_CONNECTOR_ID + ) + + this.setConnectedWalletInfo( + { name: 'Coinbase Wallet', icon: this.getConnectorImage(connector) }, + chainNamespace + ) + } else if (currentActiveWallet) { + this.setConnectedWalletInfo({ name: currentActiveWallet }, chainNamespace) + } + } + private async syncIdentity({ address, chainId, diff --git a/packages/appkit/src/tests/appkit.test.ts b/packages/appkit/src/tests/appkit.test.ts index 70b64d79bb..690374edd4 100644 --- a/packages/appkit/src/tests/appkit.test.ts +++ b/packages/appkit/src/tests/appkit.test.ts @@ -20,7 +20,12 @@ import { ChainController, type Connector } from '@reown/appkit-core' -import type { CaipNetwork } from '@reown/appkit-common' +import { + SafeLocalStorage, + SafeLocalStorageKeys, + type CaipNetwork, + type SafeLocalStorageItems +} from '@reown/appkit-common' import { mockOptions } from './mocks/Options' // Mock all controllers and UniversalAdapterClient @@ -465,5 +470,38 @@ describe('Base', () => { expect(ChainController.switchActiveNetwork).toHaveBeenCalledTimes(1) }) + + it('should set connected wallet info when syncing account', async () => { + // Mock the connector data + const mockConnector = { + id: 'test-wallet' + } as Connector + + vi.mocked(ConnectorController.getConnectors).mockReturnValue([mockConnector]) + + const mockAccountData = { + address: '0x123', + chainId: '1', + chainNamespace: 'eip155' as const + } + + vi.spyOn(SafeLocalStorage, 'getItem').mockImplementation( + (key: keyof SafeLocalStorageItems) => { + if (key === SafeLocalStorageKeys.CONNECTED_CONNECTOR) { + return mockConnector.id + } + return undefined + } + ) + + await appKit['syncAccount'](mockAccountData) + + expect(AccountController.setConnectedWalletInfo).toHaveBeenCalledWith( + expect.objectContaining({ + name: mockConnector.id + }), + 'eip155' + ) + }) }) }) diff --git a/packages/common/src/utils/SafeLocalStorage.ts b/packages/common/src/utils/SafeLocalStorage.ts index 040a0e1e1a..730efd1994 100644 --- a/packages/common/src/utils/SafeLocalStorage.ts +++ b/packages/common/src/utils/SafeLocalStorage.ts @@ -8,7 +8,6 @@ export type SafeLocalStorageItems = { '@appkit/connected_social': string '@appkit/connected_social_username': string '@appkit/recent_wallets': string - '@appkit/deeplink_choice': string '@appkit/connected_namespace': string /* * DO NOT CHANGE: @walletconnect/universal-provider requires us to set this specific key @@ -27,9 +26,8 @@ export const SafeLocalStorageKeys = { CONNECTED_SOCIAL: '@appkit/connected_social', CONNECTED_SOCIAL_USERNAME: '@appkit/connected_social_username', RECENT_WALLETS: '@appkit/recent_wallets', - DEEPLINK_CHOICE: '@appkit/deeplink_choice', - CONNECTED_NAMESPACE: '@appkit/connected_namespace', - WALLETCONNECT_DEEPLINK_CHOICE: 'WALLETCONNECT_DEEPLINK_CHOICE' + DEEPLINK_CHOICE: 'WALLETCONNECT_DEEPLINK_CHOICE', + CONNECTED_NAMESPACE: '@appkit/connected_namespace' } as const export const SafeLocalStorage = { diff --git a/packages/core/tests/utils/StorageUtil.test.ts b/packages/core/tests/utils/StorageUtil.test.ts index c9bedc0322..62afc5905e 100644 --- a/packages/core/tests/utils/StorageUtil.test.ts +++ b/packages/core/tests/utils/StorageUtil.test.ts @@ -45,6 +45,7 @@ describe('StorageUtil', () => { it('should set WalletConnect deep link in localStorage', () => { const deepLink = { href: 'https://example.com', name: 'Example Wallet' } StorageUtil.setWalletConnectDeepLink(deepLink) + expect(SafeLocalStorageKeys.DEEPLINK_CHOICE).toBe('WALLETCONNECT_DEEPLINK_CHOICE') const savedDL = SafeLocalStorage.getItem(SafeLocalStorageKeys.DEEPLINK_CHOICE) expect(savedDL).toBe(JSON.stringify({ href: deepLink.href, name: deepLink.name })) })