Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update local storage keys #2859

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .changeset/wise-bobcats-hang.md
Original file line number Diff line number Diff line change
@@ -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
26 changes: 16 additions & 10 deletions packages/adapters/ethers/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -557,7 +563,7 @@ describe('EthersAdapter', () => {
)[1]
await disconnectHandler()

expect(SafeLocalStorage.removeItem).toHaveBeenCalledWith(WcConstantsUtil.WALLET_ID)
expect(SafeLocalStorage.removeItem).toHaveBeenCalledWith(SafeLocalStorageKeys.WALLET_ID)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to keep all local storage keys to single place for maintainability

expect(mockProvider.removeListener).toHaveBeenCalledTimes(3)
})

Expand Down Expand Up @@ -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
})

Expand All @@ -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
Expand All @@ -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()
})
Expand All @@ -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()
})
Expand Down
26 changes: 16 additions & 10 deletions packages/adapters/ethers5/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
})

Expand Down Expand Up @@ -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
})

Expand All @@ -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
Expand All @@ -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()
})
Expand All @@ -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()
})
Expand Down
1 change: 0 additions & 1 deletion packages/appkit-utils/src/ethers/EthersConstantsUtil.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export const EthersConstantsUtil = {
WALLET_ID: '@w3m/wallet_id',
ERROR_CODE_UNRECOGNIZED_CHAIN_ID: 4902,
ERROR_CODE_DEFAULT: 5000
} as const
2 changes: 0 additions & 2 deletions packages/appkit-utils/src/solana/SolanaConstantsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
3 changes: 0 additions & 3 deletions packages/appkit/src/utils/ConstantsUtil.ts
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 23 additions & 15 deletions packages/common/src/utils/SafeLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -24,7 +32,7 @@ export const SafeLocalStorage = {
value: SafeLocalStorageItems[Key]
): void {
if (isSafe()) {
localStorage.setItem(key, JSON.stringify(value))
localStorage.setItem(key, value)
}
},
getItem<Key extends keyof SafeLocalStorageItems>(key: Key): SafeLocalStorageItems[Key] | null {
Expand Down
14 changes: 7 additions & 7 deletions packages/common/tests/SafeLocalStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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')
})
})
28 changes: 11 additions & 17 deletions packages/core/src/utils/StorageUtil.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
/* eslint-disable no-console */
import { 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 }))
localStorage.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)
const deepLink = localStorage.getItem(SafeLocalStorageKeys.DEEPLINK_CHOICE)
if (deepLink) {
return JSON.parse(deepLink)
}
Expand All @@ -33,7 +27,7 @@ export const StorageUtil = {

deleteWalletConnectDeepLink() {
try {
localStorage.removeItem(WC_DEEPLINK)
localStorage.removeItem(SafeLocalStorageKeys.DEEPLINK_CHOICE)
} catch {
console.info('Unable to delete WalletConnect deep link')
}
Expand All @@ -48,7 +42,7 @@ export const StorageUtil = {
if (recentWallets.length > 2) {
recentWallets.pop()
}
localStorage.setItem(W3M_RECENT, JSON.stringify(recentWallets))
localStorage.setItem(SafeLocalStorageKeys.RECENT_WALLETS, JSON.stringify(recentWallets))
}
} catch {
console.info('Unable to set AppKit recent')
Expand All @@ -57,7 +51,7 @@ export const StorageUtil = {

getRecentWallets(): WcWallet[] {
try {
const recent = localStorage.getItem(W3M_RECENT)
const recent = localStorage.getItem(SafeLocalStorageKeys.RECENT_WALLETS)

return recent ? JSON.parse(recent) : []
} catch {
Expand All @@ -69,15 +63,15 @@ export const StorageUtil = {

setConnectedConnector(connectorType: ConnectorType) {
try {
localStorage.setItem(W3M_CONNECTED_CONNECTOR, connectorType)
localStorage.setItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR, connectorType)
} catch {
console.info('Unable to set Connected Connector')
}
},

getConnectedConnector() {
try {
return localStorage.getItem(W3M_CONNECTED_CONNECTOR) as ConnectorType
return localStorage.getItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR) as ConnectorType
} catch {
console.info('Unable to get Connected Connector')
}
Expand All @@ -87,15 +81,15 @@ export const StorageUtil = {

setConnectedSocialProvider(socialProvider: SocialProvider) {
try {
localStorage.setItem(W3M_CONNECTED_SOCIAL, socialProvider)
localStorage.setItem(SafeLocalStorageKeys.CONNECTED_SOCIAL, socialProvider)
} catch {
console.info('Unable to set Connected Social Provider')
}
},

getConnectedSocialProvider() {
try {
return localStorage.getItem(W3M_CONNECTED_SOCIAL)
return localStorage.getItem(SafeLocalStorageKeys.CONNECTED_SOCIAL)
} catch {
console.info('Unable to get Connected Social Provider')
}
Expand All @@ -105,7 +99,7 @@ export const StorageUtil = {

getConnectedSocialUsername() {
try {
return localStorage.getItem(W3M_CONNECTED_SOCIAL_USERNAME)
return localStorage.getItem(SafeLocalStorageKeys.CONNECTED_SOCIAL_USERNAME)
} catch {
console.info('Unable to get Connected Social Username')
}
Expand Down
Loading
Loading