diff --git a/src/screens/walletConnect/walletConnect2.utils.ts b/src/screens/walletConnect/walletConnect2.utils.ts index f3b65e714..c518b1ef4 100644 --- a/src/screens/walletConnect/walletConnect2.utils.ts +++ b/src/screens/walletConnect/walletConnect2.utils.ts @@ -8,6 +8,8 @@ import { getEnvSetting } from 'core/config' import { SETTINGS } from 'core/types' import { AcceptedValue, MMKVStorage } from 'storage/MMKVStorage' +const WC2 = 'WC2' + export type WalletConnect2SdkErrorString = Parameters[0] const WalletConnect2SdkErrorEnum: { [P in WalletConnect2SdkErrorString]: P } = { @@ -36,11 +38,13 @@ const WalletConnect2SdkErrorEnum: { [P in WalletConnect2SdkErrorString]: P } = { type StorageTypeFromCore = InstanceType['storage'] -const MMKVWC = new MMKVStorage('WC2') -export const deleteWCSessions = MMKVWC.deleteAll +export const deleteWCSessions = () => { + const storage = new MMKVStorage('WC2') + storage.deleteAll() +} class MMKVCoreStorage implements StorageTypeFromCore { - storage = MMKVWC + storage = new MMKVStorage(WC2) getEntries(): Promise<[string, T][]> { const keys = this.storage.getAllKeys() diff --git a/src/storage/ChainStorage.ts b/src/storage/ChainStorage.ts index c36204f05..44cb3b9c1 100644 --- a/src/storage/ChainStorage.ts +++ b/src/storage/ChainStorage.ts @@ -1,11 +1,9 @@ import { ChainID } from 'lib/eoaWallet' -import { MMKVStorage } from 'storage/MMKVStorage' - -const ChainStorage = new MMKVStorage('chainStorage') +import { MainStorage } from './MainStorage' export const getCurrentChainId: () => ChainID = () => - ChainStorage.get('chainId') || 31 + MainStorage.get('chainId') || 31 export const setCurrentChainId = (chainId: ChainID) => - ChainStorage.set('chainId', chainId) + MainStorage.set('chainId', chainId) diff --git a/src/storage/MMKVStorage.ts b/src/storage/MMKVStorage.ts index 3abc004a2..42ede60fb 100644 --- a/src/storage/MMKVStorage.ts +++ b/src/storage/MMKVStorage.ts @@ -4,10 +4,10 @@ import { initializeMMKVFlipper } from 'react-native-mmkv-flipper-plugin' export type AcceptedValue = boolean | string | number | object export class MMKVStorage { - private id = 'mmkv.default' - private storage: MMKV | null = null + private id: string + private storage: MMKV - constructor(id = 'mmkv.default', encryptionKey?: string) { + constructor(id = 'main_storage', encryptionKey?: string) { this.id = id this.storage = new MMKV({ id, @@ -20,42 +20,29 @@ export class MMKVStorage { } public set(key: string, value: AcceptedValue) { - if (this.storage && typeof value !== 'undefined') { + if (typeof value !== 'undefined') { this.storage.set(key, JSON.stringify(value)) } } public get(key = 'default') { - if (this.storage) { - const value = this.storage.getString(key) - return value && JSON.parse(value) - } + const value = this.storage.getString(key) + return value && JSON.parse(value) } public has(key = 'default') { - if (this.storage) { - return this.storage.contains(key) - } else { - return false - } + return this.storage.contains(key) } public delete(key: string) { - if (this.storage) { - this.storage.delete(key) - } + this.storage.delete(key) } public deleteAll() { - if (this.storage) { - this.storage.clearAll() - } + this.storage.clearAll() } public getAllKeys() { - if (this.storage) { - return this.storage.getAllKeys() - } - return [] + return this.storage.getAllKeys() || [] } }