From eea9b15768f9bb3737302b80b3cfadd5bbd49b16 Mon Sep 17 00:00:00 2001 From: enesozturk Date: Tue, 17 Sep 2024 14:01:09 +0300 Subject: [PATCH 1/6] refactor: update local storage keys --- .../adapters/ethers/src/tests/client.test.ts | 26 ++++--- .../adapters/ethers5/src/tests/client.test.ts | 26 ++++--- .../src/ethers/EthersConstantsUtil.ts | 1 - .../src/solana/SolanaConstantsUtil.ts | 2 - packages/appkit/src/utils/ConstantsUtil.ts | 3 - packages/common/src/utils/SafeLocalStorage.ts | 36 +++++---- .../common/tests/SafeLocalStorage.test.ts | 14 ++-- packages/core/src/utils/StorageUtil.ts | 77 ++++--------------- packages/core/tests/utils/StorageUtil.test.ts | 20 ++--- packages/wallet/src/W3mFrameConstants.ts | 3 +- packages/wallet/src/W3mFrameProvider.ts | 1 - 11 files changed, 89 insertions(+), 120 deletions(-) diff --git a/packages/adapters/ethers/src/tests/client.test.ts b/packages/adapters/ethers/src/tests/client.test.ts index 385c33d41e..c278bef6e3 100644 --- a/packages/adapters/ethers/src/tests/client.test.ts +++ b/packages/adapters/ethers/src/tests/client.test.ts @@ -9,8 +9,8 @@ import { EthersHelpersUtil, type ProviderId, type ProviderType } from '@reown/ap import { ConstantsUtil } from '@reown/appkit-utils' import { arbitrum, mainnet, polygon } from '@reown/appkit/networks' import { ProviderUtil } from '@reown/appkit/store' -import { SafeLocalStorage } from '@reown/appkit-common' -import { WcConstantsUtil, type BlockchainApiLookupEnsName } from '@reown/appkit' +import { SafeLocalStorage, SafeLocalStorageKeys } from '@reown/appkit-common' +import { type BlockchainApiLookupEnsName } from '@reown/appkit' import { InfuraProvider, JsonRpcProvider } from 'ethers' import type { CaipNetwork, ChainNamespace } from '@reown/appkit-common' @@ -519,8 +519,14 @@ describe('EthersAdapter', () => { const mockProvider = { request: vi.fn() } await client['setProvider'](mockProvider as any, 'injected', 'MetaMask') - expect(SafeLocalStorage.setItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID, 'injected') - expect(SafeLocalStorage.setItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_NAME, 'MetaMask') + expect(SafeLocalStorage.setItem).toHaveBeenCalledWith( + SafeLocalStorageKeys.WALLET_ID, + 'injected' + ) + expect(SafeLocalStorage.setItem).toHaveBeenCalledWith( + SafeLocalStorageKeys.WALLET_NAME, + 'MetaMask' + ) expect(mockAppKit.setCaipNetwork).toHaveBeenCalled() expect(mockAppKit.setCaipAddress).toHaveBeenCalled() expect(ProviderUtil.setProviderId).toHaveBeenCalledWith('eip155', 'injected') @@ -557,7 +563,7 @@ describe('EthersAdapter', () => { )[1] await disconnectHandler() - expect(SafeLocalStorage.removeItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID) + expect(SafeLocalStorage.removeItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID) expect(mockProvider.removeListener).toHaveBeenCalledTimes(3) }) @@ -595,8 +601,8 @@ describe('EthersAdapter', () => { } vi.spyOn(SafeLocalStorage, 'getItem').mockImplementation(key => { - if (key === WcConstantsUtil.WALLET_ID) return ConstantsUtil.INJECTED_CONNECTOR_ID - if (key === WcConstantsUtil.WALLET_NAME) return 'MetaMask' + if (key === SafeLocalStorageKeys.WALLET_ID) return ConstantsUtil.INJECTED_CONNECTOR_ID + if (key === SafeLocalStorageKeys.WALLET_NAME) return 'MetaMask' return null }) @@ -613,7 +619,7 @@ describe('EthersAdapter', () => { client['checkActiveProviders'](mockConfig as ProviderType) - expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID) + expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID) expect(client['setProvider']).toHaveBeenCalledWith( mockInjectedProvider, ConstantsUtil.INJECTED_CONNECTOR_ID @@ -635,7 +641,7 @@ describe('EthersAdapter', () => { client['checkActiveProviders'](mockConfig as ProviderType) - expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID) + expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID) expect(client['setProvider']).not.toHaveBeenCalled() expect(client['setupProviderListeners']).not.toHaveBeenCalled() }) @@ -649,7 +655,7 @@ describe('EthersAdapter', () => { client['checkActiveProviders'](mockConfig as ProviderType) - expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID) + expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID) expect(client['setProvider']).not.toHaveBeenCalled() expect(client['setupProviderListeners']).not.toHaveBeenCalled() }) diff --git a/packages/adapters/ethers5/src/tests/client.test.ts b/packages/adapters/ethers5/src/tests/client.test.ts index 37dd009a4e..6966e3db5b 100644 --- a/packages/adapters/ethers5/src/tests/client.test.ts +++ b/packages/adapters/ethers5/src/tests/client.test.ts @@ -9,8 +9,8 @@ import { EthersHelpersUtil, type ProviderId, type ProviderType } from '@reown/ap import { ConstantsUtil } from '@reown/appkit-utils' import { arbitrum, mainnet, polygon } from '@reown/appkit/networks' import { ProviderUtil } from '@reown/appkit/store' -import { SafeLocalStorage } from '@reown/appkit-common' -import { WcConstantsUtil, type BlockchainApiLookupEnsName } from '@reown/appkit' +import { SafeLocalStorage, SafeLocalStorageKeys } from '@reown/appkit-common' +import { type BlockchainApiLookupEnsName } from '@reown/appkit' import { ethers } from 'ethers5' import type { CaipNetwork, ChainNamespace } from '@reown/appkit-common' @@ -524,8 +524,14 @@ describe('EthersAdapter', () => { const mockProvider = { request: vi.fn() } await client['setProvider'](mockProvider as any, 'injected', 'MetaMask') - expect(SafeLocalStorage.setItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID, 'injected') - expect(SafeLocalStorage.setItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_NAME, 'MetaMask') + expect(SafeLocalStorage.setItem).toHaveBeenCalledWith( + SafeLocalStorageKeys.WALLET_ID, + 'injected' + ) + expect(SafeLocalStorage.setItem).toHaveBeenCalledWith( + SafeLocalStorageKeys.WALLET_NAME, + 'MetaMask' + ) expect(mockAppKit.setCaipNetwork).toHaveBeenCalled() expect(ProviderUtil.setProviderId).toHaveBeenCalledWith('eip155', 'injected') expect(ProviderUtil.setProvider).toHaveBeenCalledWith('eip155', mockProvider) @@ -561,7 +567,7 @@ describe('EthersAdapter', () => { )[1] await disconnectHandler() - expect(SafeLocalStorage.removeItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID) + expect(SafeLocalStorage.removeItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID) expect(mockProvider.removeListener).toHaveBeenCalledTimes(3) }) @@ -599,8 +605,8 @@ describe('EthersAdapter', () => { } vi.spyOn(SafeLocalStorage, 'getItem').mockImplementation(key => { - if (key === WcConstantsUtil.WALLET_ID) return ConstantsUtil.INJECTED_CONNECTOR_ID - if (key === WcConstantsUtil.WALLET_NAME) return 'MetaMask' + if (key === SafeLocalStorageKeys.WALLET_ID) return ConstantsUtil.INJECTED_CONNECTOR_ID + if (key === SafeLocalStorageKeys.WALLET_NAME) return 'MetaMask' return null }) @@ -617,7 +623,7 @@ describe('EthersAdapter', () => { client['checkActiveProviders'](mockConfig as ProviderType) - expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID) + expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID) expect(client['setProvider']).toHaveBeenCalledWith( mockInjectedProvider, ConstantsUtil.INJECTED_CONNECTOR_ID @@ -639,7 +645,7 @@ describe('EthersAdapter', () => { client['checkActiveProviders'](mockConfig as ProviderType) - expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID) + expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID) expect(client['setProvider']).not.toHaveBeenCalled() expect(client['setupProviderListeners']).not.toHaveBeenCalled() }) @@ -653,7 +659,7 @@ describe('EthersAdapter', () => { client['checkActiveProviders'](mockConfig as ProviderType) - expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID) + expect(SafeLocalStorage.getItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID) expect(client['setProvider']).not.toHaveBeenCalled() expect(client['setupProviderListeners']).not.toHaveBeenCalled() }) diff --git a/packages/appkit-utils/src/ethers/EthersConstantsUtil.ts b/packages/appkit-utils/src/ethers/EthersConstantsUtil.ts index f123e4a819..70696b74c3 100644 --- a/packages/appkit-utils/src/ethers/EthersConstantsUtil.ts +++ b/packages/appkit-utils/src/ethers/EthersConstantsUtil.ts @@ -1,5 +1,4 @@ export const EthersConstantsUtil = { - WALLET_ID: '@w3m/wallet_id', ERROR_CODE_UNRECOGNIZED_CHAIN_ID: 4902, ERROR_CODE_DEFAULT: 5000 } as const diff --git a/packages/appkit-utils/src/solana/SolanaConstantsUtil.ts b/packages/appkit-utils/src/solana/SolanaConstantsUtil.ts index a1c2de0d50..3c03554ad3 100644 --- a/packages/appkit-utils/src/solana/SolanaConstantsUtil.ts +++ b/packages/appkit-utils/src/solana/SolanaConstantsUtil.ts @@ -19,8 +19,6 @@ export const SolConstantsUtil = { /** * Mainnet program ID */ - WALLET_ID: '@w3m/solana_wallet', - CAIP_CHAIN_ID: '@w3m/solana_caip_chain', ERROR_CODE_UNRECOGNIZED_CHAIN_ID: 4902, ERROR_CODE_DEFAULT: 5000, DEFAULT_CHAIN: { diff --git a/packages/appkit/src/utils/ConstantsUtil.ts b/packages/appkit/src/utils/ConstantsUtil.ts index 39773b1535..3ff57eb05b 100644 --- a/packages/appkit/src/utils/ConstantsUtil.ts +++ b/packages/appkit/src/utils/ConstantsUtil.ts @@ -1,7 +1,4 @@ export const WcConstantsUtil = { - WALLET_ID: '@w3m/wallet_id' as const, - WALLET_NAME: '@w3m/wallet_name' as const, - ACTIVE_CAIPNETWORK: '@w3m/active_caipnetwork' as const, ERROR_CODE_UNRECOGNIZED_CHAIN_ID: 4902, ERROR_CODE_DEFAULT: 5000 } diff --git a/packages/common/src/utils/SafeLocalStorage.ts b/packages/common/src/utils/SafeLocalStorage.ts index 107003d96b..f1fc7007df 100644 --- a/packages/common/src/utils/SafeLocalStorage.ts +++ b/packages/common/src/utils/SafeLocalStorage.ts @@ -1,21 +1,29 @@ export type SafeLocalStorageItems = { - '@w3m/wallet_id': string - '@w3m/wallet_name': string - '@w3m/solana_wallet': string - '@w3m/solana_caip_chain': string - '@w3m/active_caip_network': string - '@w3m/active_caip_network_id': string - '@w3m/connected_connector': string + '@appkit/wallet_id': string + '@appkit/wallet_name': string + '@appkit/solana_wallet': string + '@appkit/solana_caip_chain': string + '@appkit/active_caip_network': string + '@appkit/active_caip_network_id': string + '@appkit/connected_connector': string + '@appkit/connected_social': string + '@appkit/connected_social_username': string + '@appkit/recent_wallets': string + '@appkit/deeplink_choice': string } export const SafeLocalStorageKeys = { - WALLET_ID: '@w3m/wallet_id', - WALLET_NAME: '@w3m/wallet_name', - SOLANA_WALLET: '@w3m/solana_wallet', - SOLANA_CAIP_CHAIN: '@w3m/solana_caip_chain', - ACTIVE_CAIP_NETWORK: '@w3m/active_caip_network', - ACTIVE_CAIP_NETWORK_ID: '@w3m/active_caip_network_id', - CONNECTED_CONNECTOR: '@w3m/connected_connector' + WALLET_ID: '@appkit/wallet_id', + WALLET_NAME: '@appkit/wallet_name', + SOLANA_WALLET: '@appkit/solana_wallet', + SOLANA_CAIP_CHAIN: '@appkit/solana_caip_chain', + ACTIVE_CAIP_NETWORK: '@appkit/active_caip_network', + ACTIVE_CAIP_NETWORK_ID: '@appkit/active_caip_network_id', + CONNECTED_CONNECTOR: '@appkit/connected_connector', + CONNECTED_SOCIAL: '@appkit/connected_social', + CONNECTED_SOCIAL_USERNAME: '@appkit/connected_social_username', + RECENT_WALLETS: '@appkit/recent_wallets', + DEEPLINK_CHOICE: '@appkit/deeplink_choice' } as const export const SafeLocalStorage = { diff --git a/packages/common/tests/SafeLocalStorage.test.ts b/packages/common/tests/SafeLocalStorage.test.ts index dfabb7e131..96c174d653 100644 --- a/packages/common/tests/SafeLocalStorage.test.ts +++ b/packages/common/tests/SafeLocalStorage.test.ts @@ -15,7 +15,7 @@ describe('SafeLocalStorage unsafe', () => { }) it('should not setItem', () => { - const key = '@w3m/wallet_id' + const key = '@appkit/wallet_id' expect(SafeLocalStorage.setItem(key, '1')).toBe(undefined) expect(SafeLocalStorage.getItem(key)).toBe(null) @@ -33,17 +33,17 @@ describe('SafeLocalStorage safe', () => { }) it('should setItem', () => { - expect(SafeLocalStorage.setItem('@w3m/wallet_id', 'test')).toBe(undefined) - expect(setItem).toHaveBeenCalledWith('@w3m/wallet_id', '"test"') + expect(SafeLocalStorage.setItem('@appkit/wallet_id', 'test')).toBe(undefined) + expect(setItem).toHaveBeenCalledWith('@appkit/wallet_id', '"test"') }) it('should getItem', () => { - expect(SafeLocalStorage.getItem('@w3m/wallet_id')).toEqual({ test: 'test' }) - expect(getItem).toHaveBeenCalledWith('@w3m/wallet_id') + expect(SafeLocalStorage.getItem('@appkit/wallet_id')).toEqual({ test: 'test' }) + expect(getItem).toHaveBeenCalledWith('@appkit/wallet_id') }) it('should removeItem', () => { - expect(SafeLocalStorage.removeItem('@w3m/wallet_id')).toBe(undefined) - expect(removeItem).toHaveBeenCalledWith('@w3m/wallet_id') + expect(SafeLocalStorage.removeItem('@appkit/wallet_id')).toBe(undefined) + expect(removeItem).toHaveBeenCalledWith('@appkit/wallet_id') }) }) diff --git a/packages/core/src/utils/StorageUtil.ts b/packages/core/src/utils/StorageUtil.ts index 964c299c56..0895874686 100644 --- a/packages/core/src/utils/StorageUtil.ts +++ b/packages/core/src/utils/StorageUtil.ts @@ -1,42 +1,28 @@ /* eslint-disable no-console */ +import { SafeLocalStorage, SafeLocalStorageKeys } from '@reown/appkit-common' import type { WcWallet, ConnectorType, SocialProvider } from './TypeUtil.js' -// -- Helpers ----------------------------------------------------------------- -const WC_DEEPLINK = 'WALLETCONNECT_DEEPLINK_CHOICE' -const W3M_RECENT = '@w3m/recent' -const W3M_CONNECTED_CONNECTOR = '@w3m/connected_connector' -const W3M_CONNECTED_SOCIAL = '@w3m/connected_social' -const W3M_CONNECTED_SOCIAL_USERNAME = '@w3m-storage/SOCIAL_USERNAME' - // -- Utility ----------------------------------------------------------------- export const StorageUtil = { setWalletConnectDeepLink({ href, name }: { href: string; name: string }) { try { - localStorage.setItem(WC_DEEPLINK, JSON.stringify({ href, name })) + SafeLocalStorage.setItem(SafeLocalStorageKeys.DEEPLINK_CHOICE, JSON.stringify({ href, name })) } catch { console.info('Unable to set WalletConnect deep link') } }, getWalletConnectDeepLink() { - try { - const deepLink = localStorage.getItem(WC_DEEPLINK) - if (deepLink) { - return JSON.parse(deepLink) - } - } catch { - console.info('Unable to get WalletConnect deep link') + const deepLink = SafeLocalStorage.getItem(SafeLocalStorageKeys.DEEPLINK_CHOICE) + if (deepLink) { + return JSON.parse(deepLink) } return undefined }, deleteWalletConnectDeepLink() { - try { - localStorage.removeItem(WC_DEEPLINK) - } catch { - console.info('Unable to delete WalletConnect deep link') - } + SafeLocalStorage.removeItem(SafeLocalStorageKeys.DEEPLINK_CHOICE) }, setAppKitRecent(wallet: WcWallet) { @@ -48,7 +34,7 @@ export const StorageUtil = { if (recentWallets.length > 2) { recentWallets.pop() } - localStorage.setItem(W3M_RECENT, JSON.stringify(recentWallets)) + SafeLocalStorage.setItem(SafeLocalStorageKeys.RECENT_WALLETS, JSON.stringify(recentWallets)) } } catch { console.info('Unable to set AppKit recent') @@ -56,60 +42,31 @@ export const StorageUtil = { }, getRecentWallets(): WcWallet[] { - try { - const recent = localStorage.getItem(W3M_RECENT) - - return recent ? JSON.parse(recent) : [] - } catch { - console.info('Unable to get AppKit recent') - } - - return [] + const recentWallets = SafeLocalStorage.getItem(SafeLocalStorageKeys.RECENT_WALLETS) + return (recentWallets as unknown as WcWallet[]) || [] }, setConnectedConnector(connectorType: ConnectorType) { - try { - localStorage.setItem(W3M_CONNECTED_CONNECTOR, connectorType) - } catch { - console.info('Unable to set Connected Connector') - } + SafeLocalStorage.setItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR, connectorType) }, getConnectedConnector() { - try { - return localStorage.getItem(W3M_CONNECTED_CONNECTOR) as ConnectorType - } catch { - console.info('Unable to get Connected Connector') - } - - return undefined + return SafeLocalStorage.getItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR) as ConnectorType }, setConnectedSocialProvider(socialProvider: SocialProvider) { - try { - localStorage.setItem(W3M_CONNECTED_SOCIAL, socialProvider) - } catch { - console.info('Unable to set Connected Social Provider') - } + SafeLocalStorage.setItem(SafeLocalStorageKeys.CONNECTED_SOCIAL, socialProvider) }, getConnectedSocialProvider() { - try { - return localStorage.getItem(W3M_CONNECTED_SOCIAL) - } catch { - console.info('Unable to get Connected Social Provider') - } + return SafeLocalStorage.getItem(SafeLocalStorageKeys.CONNECTED_SOCIAL) as SocialProvider + }, - return undefined + setConnectedSocialUsername(username: string) { + SafeLocalStorage.setItem(SafeLocalStorageKeys.CONNECTED_SOCIAL_USERNAME, username) }, getConnectedSocialUsername() { - try { - return localStorage.getItem(W3M_CONNECTED_SOCIAL_USERNAME) - } catch { - console.info('Unable to get Connected Social Username') - } - - return undefined + return SafeLocalStorage.getItem(SafeLocalStorageKeys.CONNECTED_SOCIAL_USERNAME) as string } } diff --git a/packages/core/tests/utils/StorageUtil.test.ts b/packages/core/tests/utils/StorageUtil.test.ts index 8f2b810d45..227a228934 100644 --- a/packages/core/tests/utils/StorageUtil.test.ts +++ b/packages/core/tests/utils/StorageUtil.test.ts @@ -36,7 +36,7 @@ describe('StorageUtil', () => { it('should set WalletConnect deep link in localStorage', () => { const deepLink = { href: 'https://example.com', name: 'Example Wallet' } StorageUtil.setWalletConnectDeepLink(deepLink) - expect(localStorage.getItem('WALLETCONNECT_DEEPLINK_CHOICE')).toBe(JSON.stringify(deepLink)) + expect(localStorage.getItem('@appkit/deeplink_choice')).toBe(JSON.stringify(deepLink)) }) it('should handle errors when setting deep link', () => { @@ -53,7 +53,7 @@ describe('StorageUtil', () => { describe('getWalletConnectDeepLink', () => { it('should get WalletConnect deep link from localStorage', () => { const deepLink = { href: 'https://example.com', name: 'Example Wallet' } - localStorage.setItem('WALLETCONNECT_DEEPLINK_CHOICE', JSON.stringify(deepLink)) + localStorage.setItem('@appkit/deeplink_choice', JSON.stringify(deepLink)) expect(StorageUtil.getWalletConnectDeepLink()).toEqual(deepLink) }) @@ -75,11 +75,11 @@ describe('StorageUtil', () => { describe('deleteWalletConnectDeepLink', () => { it('should delete WalletConnect deep link from localStorage', () => { localStorage.setItem( - 'WALLETCONNECT_DEEPLINK_CHOICE', + '@appkit/deeplink_choice', JSON.stringify({ href: 'https://example.com', name: 'Example Wallet' }) ) StorageUtil.deleteWalletConnectDeepLink() - expect(localStorage.getItem('WALLETCONNECT_DEEPLINK_CHOICE')).toBeNull() + expect(localStorage.getItem('@appkit/deeplink_choice')).toBeNull() }) it('should handle errors when deleting deep link', () => { @@ -125,7 +125,7 @@ describe('StorageUtil', () => { it('should return recent wallets', () => { const wallet: WcWallet = { id: 'wallet1', name: 'Wallet 1' } - localStorage.setItem('@w3m/recent', JSON.stringify([wallet])) + localStorage.setItem('@appkit/recent', JSON.stringify([wallet])) expect(StorageUtil.getRecentWallets()).toEqual([wallet]) }) }) @@ -134,14 +134,14 @@ describe('StorageUtil', () => { it('should set connected connector', () => { const connector: ConnectorType = 'INJECTED' StorageUtil.setConnectedConnector(connector) - expect(localStorage.getItem('@w3m/connected_connector')).toBe(connector) + expect(localStorage.getItem('@appkit/connected_connector')).toBe(connector) }) }) describe('getConnectedConnector', () => { it('should get connected connector', () => { const connector: ConnectorType = 'INJECTED' - localStorage.setItem('@w3m/connected_connector', connector) + localStorage.setItem('@appkit/connected_connector', connector) expect(StorageUtil.getConnectedConnector()).toBe(connector) }) }) @@ -150,14 +150,14 @@ describe('StorageUtil', () => { it('should set connected social provider', () => { const provider: SocialProvider = 'google' StorageUtil.setConnectedSocialProvider(provider) - expect(localStorage.getItem('@w3m/connected_social')).toBe(provider) + expect(localStorage.getItem('@appkit/connected_social')).toBe(provider) }) }) describe('getConnectedSocialProvider', () => { it('should get connected social provider', () => { const provider: SocialProvider = 'google' - localStorage.setItem('@w3m/connected_social', provider) + localStorage.setItem('@appkit/connected_social', provider) expect(StorageUtil.getConnectedSocialProvider()).toBe(provider) }) }) @@ -165,7 +165,7 @@ describe('StorageUtil', () => { describe('getConnectedSocialUsername', () => { it('should get connected social username', () => { const username = 'testuser' - localStorage.setItem('@w3m-storage/SOCIAL_USERNAME', username) + localStorage.setItem('@appkit-wallet/SOCIAL_USERNAME', username) expect(StorageUtil.getConnectedSocialUsername()).toBe(username) }) }) diff --git a/packages/wallet/src/W3mFrameConstants.ts b/packages/wallet/src/W3mFrameConstants.ts index 6d83e2d4a1..02761eb121 100644 --- a/packages/wallet/src/W3mFrameConstants.ts +++ b/packages/wallet/src/W3mFrameConstants.ts @@ -7,7 +7,7 @@ export const W3mFrameConstants = { APP_EVENT_KEY: '@w3m-app/', FRAME_EVENT_KEY: '@w3m-frame/', RPC_METHOD_KEY: 'RPC_', - STORAGE_KEY: '@w3m-storage/', + STORAGE_KEY: '@appkit-wallet/', SESSION_TOKEN_KEY: 'SESSION_TOKEN_KEY', EMAIL_LOGIN_USED_KEY: 'EMAIL_LOGIN_USED_KEY', @@ -18,7 +18,6 @@ export const W3mFrameConstants = { SMART_ACCOUNT_ENABLED: 'SMART_ACCOUNT_ENABLED', SMART_ACCOUNT_ENABLED_NETWORKS: 'SMART_ACCOUNT_ENABLED_NETWORKS', SOCIAL_USERNAME: 'SOCIAL_USERNAME', - SOCIAL: '@w3m/connected_social', APP_SWITCH_NETWORK: '@w3m-app/SWITCH_NETWORK', APP_CONNECT_EMAIL: '@w3m-app/CONNECT_EMAIL', diff --git a/packages/wallet/src/W3mFrameProvider.ts b/packages/wallet/src/W3mFrameProvider.ts index 678d5c64f7..925bee1a68 100644 --- a/packages/wallet/src/W3mFrameProvider.ts +++ b/packages/wallet/src/W3mFrameProvider.ts @@ -502,7 +502,6 @@ export class W3mFrameProvider { W3mFrameStorage.delete(W3mFrameConstants.EMAIL) W3mFrameStorage.delete(W3mFrameConstants.LAST_USED_CHAIN_KEY) W3mFrameStorage.delete(W3mFrameConstants.SOCIAL_USERNAME) - W3mFrameStorage.delete(W3mFrameConstants.SOCIAL, true) } private setLastUsedChainId(chainId: string | number) { From 920ba1659f7724de2a558eae67fd4fa5a317502d Mon Sep 17 00:00:00 2001 From: enesozturk Date: Tue, 17 Sep 2024 14:05:43 +0300 Subject: [PATCH 2/6] chore: format files --- packages/adapters/ethers5/src/tests/client.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/adapters/ethers5/src/tests/client.test.ts b/packages/adapters/ethers5/src/tests/client.test.ts index 6966e3db5b..7d7ee470eb 100644 --- a/packages/adapters/ethers5/src/tests/client.test.ts +++ b/packages/adapters/ethers5/src/tests/client.test.ts @@ -10,7 +10,7 @@ import { ConstantsUtil } from '@reown/appkit-utils' import { arbitrum, mainnet, polygon } from '@reown/appkit/networks' import { ProviderUtil } from '@reown/appkit/store' import { SafeLocalStorage, SafeLocalStorageKeys } from '@reown/appkit-common' -import { type BlockchainApiLookupEnsName } from '@reown/appkit' +import { type BlockchainApiLookupEnsName } from '@reown/appkit' import { ethers } from 'ethers5' import type { CaipNetwork, ChainNamespace } from '@reown/appkit-common' From 66a7bc6833bde573a773f0faffb7b026738ab36f Mon Sep 17 00:00:00 2001 From: enesozturk Date: Tue, 17 Sep 2024 14:37:23 +0300 Subject: [PATCH 3/6] chore: revert storage util --- packages/common/src/utils/SafeLocalStorage.ts | 2 +- packages/core/src/utils/StorageUtil.ts | 71 ++++++++++++++----- packages/core/tests/utils/StorageUtil.test.ts | 19 ++--- 3 files changed, 66 insertions(+), 26 deletions(-) diff --git a/packages/common/src/utils/SafeLocalStorage.ts b/packages/common/src/utils/SafeLocalStorage.ts index f1fc7007df..deef953139 100644 --- a/packages/common/src/utils/SafeLocalStorage.ts +++ b/packages/common/src/utils/SafeLocalStorage.ts @@ -32,7 +32,7 @@ export const SafeLocalStorage = { value: SafeLocalStorageItems[Key] ): void { if (isSafe()) { - localStorage.setItem(key, JSON.stringify(value)) + localStorage.setItem(key, value) } }, getItem(key: Key): SafeLocalStorageItems[Key] | null { diff --git a/packages/core/src/utils/StorageUtil.ts b/packages/core/src/utils/StorageUtil.ts index 0895874686..bc240673c6 100644 --- a/packages/core/src/utils/StorageUtil.ts +++ b/packages/core/src/utils/StorageUtil.ts @@ -1,28 +1,36 @@ /* eslint-disable no-console */ -import { SafeLocalStorage, SafeLocalStorageKeys } from '@reown/appkit-common' +import { SafeLocalStorageKeys } from '@reown/appkit-common' import type { WcWallet, ConnectorType, SocialProvider } from './TypeUtil.js' // -- Utility ----------------------------------------------------------------- export const StorageUtil = { setWalletConnectDeepLink({ href, name }: { href: string; name: string }) { try { - SafeLocalStorage.setItem(SafeLocalStorageKeys.DEEPLINK_CHOICE, JSON.stringify({ href, name })) + localStorage.setItem(SafeLocalStorageKeys.DEEPLINK_CHOICE, JSON.stringify({ href, name })) } catch { console.info('Unable to set WalletConnect deep link') } }, getWalletConnectDeepLink() { - const deepLink = SafeLocalStorage.getItem(SafeLocalStorageKeys.DEEPLINK_CHOICE) - if (deepLink) { - return JSON.parse(deepLink) + try { + const deepLink = localStorage.getItem(SafeLocalStorageKeys.DEEPLINK_CHOICE) + if (deepLink) { + return JSON.parse(deepLink) + } + } catch { + console.info('Unable to get WalletConnect deep link') } return undefined }, deleteWalletConnectDeepLink() { - SafeLocalStorage.removeItem(SafeLocalStorageKeys.DEEPLINK_CHOICE) + try { + localStorage.removeItem(SafeLocalStorageKeys.DEEPLINK_CHOICE) + } catch { + console.info('Unable to delete WalletConnect deep link') + } }, setAppKitRecent(wallet: WcWallet) { @@ -34,7 +42,7 @@ export const StorageUtil = { if (recentWallets.length > 2) { recentWallets.pop() } - SafeLocalStorage.setItem(SafeLocalStorageKeys.RECENT_WALLETS, JSON.stringify(recentWallets)) + localStorage.setItem(SafeLocalStorageKeys.RECENT_WALLETS, JSON.stringify(recentWallets)) } } catch { console.info('Unable to set AppKit recent') @@ -42,31 +50,60 @@ export const StorageUtil = { }, getRecentWallets(): WcWallet[] { - const recentWallets = SafeLocalStorage.getItem(SafeLocalStorageKeys.RECENT_WALLETS) - return (recentWallets as unknown as WcWallet[]) || [] + try { + const recent = localStorage.getItem(SafeLocalStorageKeys.RECENT_WALLETS) + + return recent ? JSON.parse(recent) : [] + } catch { + console.info('Unable to get AppKit recent') + } + + return [] }, setConnectedConnector(connectorType: ConnectorType) { - SafeLocalStorage.setItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR, connectorType) + try { + localStorage.setItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR, connectorType) + } catch { + console.info('Unable to set Connected Connector') + } }, getConnectedConnector() { - return SafeLocalStorage.getItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR) as ConnectorType + try { + return localStorage.getItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR) as ConnectorType + } catch { + console.info('Unable to get Connected Connector') + } + + return undefined }, setConnectedSocialProvider(socialProvider: SocialProvider) { - SafeLocalStorage.setItem(SafeLocalStorageKeys.CONNECTED_SOCIAL, socialProvider) + try { + localStorage.setItem(SafeLocalStorageKeys.CONNECTED_SOCIAL, socialProvider) + } catch { + console.info('Unable to set Connected Social Provider') + } }, getConnectedSocialProvider() { - return SafeLocalStorage.getItem(SafeLocalStorageKeys.CONNECTED_SOCIAL) as SocialProvider - }, + try { + return localStorage.getItem(SafeLocalStorageKeys.CONNECTED_SOCIAL) + } catch { + console.info('Unable to get Connected Social Provider') + } - setConnectedSocialUsername(username: string) { - SafeLocalStorage.setItem(SafeLocalStorageKeys.CONNECTED_SOCIAL_USERNAME, username) + return undefined }, getConnectedSocialUsername() { - return SafeLocalStorage.getItem(SafeLocalStorageKeys.CONNECTED_SOCIAL_USERNAME) as string + try { + return localStorage.getItem(SafeLocalStorageKeys.CONNECTED_SOCIAL_USERNAME) + } catch { + console.info('Unable to get Connected Social Username') + } + + return undefined } } diff --git a/packages/core/tests/utils/StorageUtil.test.ts b/packages/core/tests/utils/StorageUtil.test.ts index 227a228934..9a2d9352c3 100644 --- a/packages/core/tests/utils/StorageUtil.test.ts +++ b/packages/core/tests/utils/StorageUtil.test.ts @@ -1,6 +1,7 @@ -import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' +import { describe, it, expect, vi, afterEach, beforeAll, beforeEach } from 'vitest' import { StorageUtil } from '../../src/utils/StorageUtil' import type { WcWallet, ConnectorType, SocialProvider } from '../../src/utils/TypeUtil' +import { SafeLocalStorageKeys } from '@reown/appkit-common' // Mock localStorage const localStorageMock = (() => { @@ -36,7 +37,9 @@ describe('StorageUtil', () => { it('should set WalletConnect deep link in localStorage', () => { const deepLink = { href: 'https://example.com', name: 'Example Wallet' } StorageUtil.setWalletConnectDeepLink(deepLink) - expect(localStorage.getItem('@appkit/deeplink_choice')).toBe(JSON.stringify(deepLink)) + expect(localStorageMock.getItem(SafeLocalStorageKeys.DEEPLINK_CHOICE)).toBe( + JSON.stringify(deepLink) + ) }) it('should handle errors when setting deep link', () => { @@ -125,7 +128,7 @@ describe('StorageUtil', () => { it('should return recent wallets', () => { const wallet: WcWallet = { id: 'wallet1', name: 'Wallet 1' } - localStorage.setItem('@appkit/recent', JSON.stringify([wallet])) + localStorage.setItem(SafeLocalStorageKeys.RECENT_WALLETS, JSON.stringify([wallet])) expect(StorageUtil.getRecentWallets()).toEqual([wallet]) }) }) @@ -134,14 +137,14 @@ describe('StorageUtil', () => { it('should set connected connector', () => { const connector: ConnectorType = 'INJECTED' StorageUtil.setConnectedConnector(connector) - expect(localStorage.getItem('@appkit/connected_connector')).toBe(connector) + expect(localStorage.getItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR)).toBe(connector) }) }) describe('getConnectedConnector', () => { it('should get connected connector', () => { const connector: ConnectorType = 'INJECTED' - localStorage.setItem('@appkit/connected_connector', connector) + localStorage.setItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR, connector) expect(StorageUtil.getConnectedConnector()).toBe(connector) }) }) @@ -150,14 +153,14 @@ describe('StorageUtil', () => { it('should set connected social provider', () => { const provider: SocialProvider = 'google' StorageUtil.setConnectedSocialProvider(provider) - expect(localStorage.getItem('@appkit/connected_social')).toBe(provider) + expect(localStorage.getItem(SafeLocalStorageKeys.CONNECTED_SOCIAL)).toBe(provider) }) }) describe('getConnectedSocialProvider', () => { it('should get connected social provider', () => { const provider: SocialProvider = 'google' - localStorage.setItem('@appkit/connected_social', provider) + localStorage.setItem(SafeLocalStorageKeys.CONNECTED_SOCIAL, provider) expect(StorageUtil.getConnectedSocialProvider()).toBe(provider) }) }) @@ -165,7 +168,7 @@ describe('StorageUtil', () => { describe('getConnectedSocialUsername', () => { it('should get connected social username', () => { const username = 'testuser' - localStorage.setItem('@appkit-wallet/SOCIAL_USERNAME', username) + localStorage.setItem(SafeLocalStorageKeys.CONNECTED_SOCIAL_USERNAME, username) expect(StorageUtil.getConnectedSocialUsername()).toBe(username) }) }) From 3730a42ace26d3d669691aaccd6d44e27e78f7ae Mon Sep 17 00:00:00 2001 From: enesozturk Date: Tue, 17 Sep 2024 14:43:12 +0300 Subject: [PATCH 4/6] chore: linter and test issues --- packages/common/tests/SafeLocalStorage.test.ts | 2 +- packages/core/tests/utils/StorageUtil.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/common/tests/SafeLocalStorage.test.ts b/packages/common/tests/SafeLocalStorage.test.ts index 96c174d653..9e528ea3f3 100644 --- a/packages/common/tests/SafeLocalStorage.test.ts +++ b/packages/common/tests/SafeLocalStorage.test.ts @@ -34,7 +34,7 @@ describe('SafeLocalStorage safe', () => { it('should setItem', () => { expect(SafeLocalStorage.setItem('@appkit/wallet_id', 'test')).toBe(undefined) - expect(setItem).toHaveBeenCalledWith('@appkit/wallet_id', '"test"') + expect(setItem).toHaveBeenCalledWith('@appkit/wallet_id', 'test') }) it('should getItem', () => { diff --git a/packages/core/tests/utils/StorageUtil.test.ts b/packages/core/tests/utils/StorageUtil.test.ts index 9a2d9352c3..2a59a5da51 100644 --- a/packages/core/tests/utils/StorageUtil.test.ts +++ b/packages/core/tests/utils/StorageUtil.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, vi, afterEach, beforeAll, beforeEach } from 'vitest' +import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest' import { StorageUtil } from '../../src/utils/StorageUtil' import type { WcWallet, ConnectorType, SocialProvider } from '../../src/utils/TypeUtil' import { SafeLocalStorageKeys } from '@reown/appkit-common' From 67065e8b9429c6bf6472847010a731e4266890a6 Mon Sep 17 00:00:00 2001 From: enesozturk Date: Tue, 17 Sep 2024 16:14:01 +0300 Subject: [PATCH 5/6] chore: add changeset file --- .changeset/wise-bobcats-hang.md | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 .changeset/wise-bobcats-hang.md diff --git a/.changeset/wise-bobcats-hang.md b/.changeset/wise-bobcats-hang.md new file mode 100644 index 0000000000..d2b1000398 --- /dev/null +++ b/.changeset/wise-bobcats-hang.md @@ -0,0 +1,38 @@ +--- +'@reown/appkit-adapter-ethers5': patch +'@reown/appkit-adapter-ethers': patch +'@reown/appkit-utils': patch +'@reown/appkit': patch +'@reown/appkit-common': patch +'@reown/appkit-wallet': patch +'@reown/appkit-core': patch +'@apps/demo': patch +'@apps/gallery': patch +'@apps/laboratory': patch +'@examples/html-ethers': patch +'@examples/html-ethers5': patch +'@examples/html-wagmi': patch +'@examples/next-ethers': patch +'@examples/next-wagmi': patch +'@examples/react-ethers': patch +'@examples/react-ethers5': patch +'@examples/react-solana': patch +'@examples/react-wagmi': patch +'@examples/vue-ethers5': patch +'@examples/vue-solana': patch +'@examples/vue-wagmi': patch +'@reown/appkit-adapter-polkadot': patch +'@reown/appkit-adapter-solana': patch +'@reown/appkit-adapter-wagmi': patch +'@reown/appkit-cdn': patch +'@reown/appkit-ethers': patch +'@reown/appkit-ethers5': patch +'@reown/appkit-polyfills': patch +'@reown/appkit-scaffold-ui': patch +'@reown/appkit-siwe': patch +'@reown/appkit-solana': patch +'@reown/appkit-ui': patch +'@reown/appkit-wagmi': patch +--- + +Updates the localstorage keys From a288e874120da9d5dbf4317b121d84666abb4f75 Mon Sep 17 00:00:00 2001 From: enesozturk Date: Tue, 17 Sep 2024 17:31:39 +0300 Subject: [PATCH 6/6] Trigger workflow