From 0c101a58107e87c3d9e7f0adf4d1e5a81397e695 Mon Sep 17 00:00:00 2001 From: aelf-lxy Date: Wed, 7 Aug 2024 13:47:52 +0800 Subject: [PATCH] feat: enhanceLocalStorage --- packages/login/package.json | 2 +- packages/login/src/config.ts | 1 + packages/login/src/context.tsx | 1 + packages/login/src/hooks/useCallContract.ts | 1 + packages/login/src/hooks/useGetAccount.ts | 1 + .../login/src/utils/enhancedLocalStorage.ts | 44 +++++++++++++++++++ packages/login/src/utils/getUrl.ts | 1 + .../src/wallets/discover/detectProvider.ts | 1 + .../login/src/wallets/discover/useDiscover.ts | 1 + packages/login/src/wallets/elf/useElf.ts | 1 + .../src/wallets/portkey/Portkey/index.tsx | 1 + .../src/wallets/portkey/Portkey/indexV1.tsx | 1 + .../src/wallets/portkey/usePortkey/index.ts | 1 + .../src/wallets/portkey/usePortkey/indexV1.ts | 1 + 14 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 packages/login/src/utils/enhancedLocalStorage.ts diff --git a/packages/login/package.json b/packages/login/package.json index 9cd409b3..3607d407 100644 --- a/packages/login/package.json +++ b/packages/login/package.json @@ -1,6 +1,6 @@ { "name": "aelf-web-login", - "version": "2.1.3", + "version": "2.1.4", "main": "dist/esm/index.js", "types": "dist/types/index.d.ts", "exports": { diff --git a/packages/login/src/config.ts b/packages/login/src/config.ts index 773ab1af..6c919335 100644 --- a/packages/login/src/config.ts +++ b/packages/login/src/config.ts @@ -6,6 +6,7 @@ import { GlobalConfigProps as GlobalConfigPropsV1 } from '@portkey-v1/did-ui-rea import { GlobalConfigProps } from '@portkey/did-ui-react/dist/_types/src/components/config-provider/types'; import { EventEmitter } from 'ahooks/lib/useEventEmitter'; import { PortkeyDidV1, PortkeyDid } from './index'; +import { enhancedLocalStorage as localStorage } from './utils/enhancedLocalStorage'; // copy from @aelf-react/core, cause it's not exported export type AelfNode = { diff --git a/packages/login/src/context.tsx b/packages/login/src/context.tsx index ed32f52c..aa36258c 100644 --- a/packages/login/src/context.tsx +++ b/packages/login/src/context.tsx @@ -26,6 +26,7 @@ import clsx from 'clsx'; import { PortkeyDid, PortkeyDidV1 } from './index'; import { getStorageVersion } from './utils/getUrl'; import CommonErrorBoundary from './components/ErrorBoundary'; +import { enhancedLocalStorage as localStorage } from './utils/enhancedLocalStorage'; const INITIAL_STATE = { loginState: WebLoginState.initial, diff --git a/packages/login/src/hooks/useCallContract.ts b/packages/login/src/hooks/useCallContract.ts index f80ae2d6..d834f448 100644 --- a/packages/login/src/hooks/useCallContract.ts +++ b/packages/login/src/hooks/useCallContract.ts @@ -18,6 +18,7 @@ import { SendOptions } from '@portkey/types'; import { SendOptions as SendOptionsV1 } from '@portkey-v1/types'; import useWebLoginEvent from './useWebLoginEvent'; import { getFaviconUrl, getStorageVersion, getUrl } from '../utils/getUrl'; +import { enhancedLocalStorage as localStorage } from '../utils/enhancedLocalStorage'; const getAElfInstance = (() => { const instances = new Map(); diff --git a/packages/login/src/hooks/useGetAccount.ts b/packages/login/src/hooks/useGetAccount.ts index 120f9262..e9cc7aa8 100644 --- a/packages/login/src/hooks/useGetAccount.ts +++ b/packages/login/src/hooks/useGetAccount.ts @@ -5,6 +5,7 @@ import { ChainId } from '@portkey/provider-types'; import { did } from '@portkey/did-ui-react'; import { did as didV1 } from '@portkey-v1/did-ui-react'; import { getStorageVersion } from '../utils/getUrl'; +import { enhancedLocalStorage as localStorage } from '../utils/enhancedLocalStorage'; export default function useGetAccount(chainId: string) { const WEB_LOGIN_VERSION = getStorageVersion(); diff --git a/packages/login/src/utils/enhancedLocalStorage.ts b/packages/login/src/utils/enhancedLocalStorage.ts new file mode 100644 index 00000000..df76107d --- /dev/null +++ b/packages/login/src/utils/enhancedLocalStorage.ts @@ -0,0 +1,44 @@ +export interface Storage { + getItem(key: string, ...args: Array): any; + setItem(key: string, value: any, ...args: Array): any; + removeItem(key: string, ...args: Array): any; + clear(): void; +} + +const localStorageMock = (function () { + let store: { [key: string]: string } = {}; + + return { + getItem: function (key: string): string | null { + return store[key] || null; + }, + setItem: function (key: string, value: string) { + store[key] = value.toString(); + }, + removeItem: function (key: string) { + delete store[key]; + }, + clear: function () { + store = {}; + }, + }; +})(); + +// let enhancedLocalStorage: Storage = {} as Storage; +// if (typeof window !== 'undefined') { +// enhancedLocalStorage = window.localStorage; +// } else { +// import('node-localstorage').then(({ LocalStorage }) => { +// enhancedLocalStorage = new LocalStorage('./scratch'); +// }); +// } +// export { enhancedLocalStorage }; + +let enhancedLocalStorage: Storage; +if (typeof window !== 'undefined') { + console.log('enhancedLocalStorage in window'); + enhancedLocalStorage = window.localStorage; +} else { + enhancedLocalStorage = localStorageMock; +} +export { enhancedLocalStorage, localStorageMock }; diff --git a/packages/login/src/utils/getUrl.ts b/packages/login/src/utils/getUrl.ts index 74d8fbdd..7acba4cc 100644 --- a/packages/login/src/utils/getUrl.ts +++ b/packages/login/src/utils/getUrl.ts @@ -1,5 +1,6 @@ import { LOGIN_VERSION } from '../constants'; import { getConfig } from '../config'; +import { enhancedLocalStorage as localStorage } from './enhancedLocalStorage'; export function getUrl() { return new URL(location.href); diff --git a/packages/login/src/wallets/discover/detectProvider.ts b/packages/login/src/wallets/discover/detectProvider.ts index 63cfaaf5..59ca597d 100644 --- a/packages/login/src/wallets/discover/detectProvider.ts +++ b/packages/login/src/wallets/discover/detectProvider.ts @@ -1,6 +1,7 @@ import detectProvider from '@portkey/detect-provider'; import { IPortkeyProvider } from '@portkey/provider-types'; import { getStorageVersion } from '../../utils/getUrl'; +import { enhancedLocalStorage as localStorage } from '../../utils/enhancedLocalStorage'; export default async function detectDiscoverProvider(version?: string | null): Promise { const WEB_LOGIN_VERSION = getStorageVersion(); diff --git a/packages/login/src/wallets/discover/useDiscover.ts b/packages/login/src/wallets/discover/useDiscover.ts index e34df260..8e0e2270 100644 --- a/packages/login/src/wallets/discover/useDiscover.ts +++ b/packages/login/src/wallets/discover/useDiscover.ts @@ -23,6 +23,7 @@ import useWebLoginEvent from '../../hooks/useWebLoginEvent'; import { removeOtherKey, useWebLogin } from '../../context'; import isPortkeyApp, { isPortkeyV2 } from '../../utils/isPortkeyApp'; import { getStorageVersion } from '../../utils/getUrl'; +import { enhancedLocalStorage as localStorage } from '../../utils/enhancedLocalStorage'; export type DiscoverDetectState = 'unknown' | 'detected' | 'not-detected'; export type DiscoverInterface = WalletHookInterface & { diff --git a/packages/login/src/wallets/elf/useElf.ts b/packages/login/src/wallets/elf/useElf.ts index 2d878278..134ed305 100644 --- a/packages/login/src/wallets/elf/useElf.ts +++ b/packages/login/src/wallets/elf/useElf.ts @@ -9,6 +9,7 @@ import checkSignatureParams from '../../utils/signatureParams'; import detectNightElf from './detectNightElf'; import { zeroFill } from '../../utils/zeroFill'; import { removeOtherKey } from '../../context'; +import { enhancedLocalStorage as localStorage } from '../../utils/enhancedLocalStorage'; export function useElf({ options, diff --git a/packages/login/src/wallets/portkey/Portkey/index.tsx b/packages/login/src/wallets/portkey/Portkey/index.tsx index 104679ad..05f1269c 100644 --- a/packages/login/src/wallets/portkey/Portkey/index.tsx +++ b/packages/login/src/wallets/portkey/Portkey/index.tsx @@ -24,6 +24,7 @@ import { changePortkeyVersion } from '../../../utils/isPortkeyApp'; import isMobile from '../../../utils/isMobile'; import { useWebLogin } from '../../../context'; import { getStorageVersion } from '../../../utils/getUrl'; +import { enhancedLocalStorage as localStorage } from '../../../utils/enhancedLocalStorage'; interface IPortkeyPanelProps { open: boolean; diff --git a/packages/login/src/wallets/portkey/Portkey/indexV1.tsx b/packages/login/src/wallets/portkey/Portkey/indexV1.tsx index f5c98471..b7c5ba3f 100644 --- a/packages/login/src/wallets/portkey/Portkey/indexV1.tsx +++ b/packages/login/src/wallets/portkey/Portkey/indexV1.tsx @@ -22,6 +22,7 @@ import { FetchRequest } from '@portkey-v1/request'; import { changePortkeyVersion } from '../../../utils/isPortkeyApp'; import isMobile from '../../../utils/isMobile'; import { getStorageVersion } from '../../../utils/getUrl'; +import { enhancedLocalStorage as localStorage } from '../../../utils/enhancedLocalStorage'; export default function Portkey({ open, diff --git a/packages/login/src/wallets/portkey/usePortkey/index.ts b/packages/login/src/wallets/portkey/usePortkey/index.ts index 549e3e12..1ebbfc87 100644 --- a/packages/login/src/wallets/portkey/usePortkey/index.ts +++ b/packages/login/src/wallets/portkey/usePortkey/index.ts @@ -38,6 +38,7 @@ import { addPrefix } from '../../../utils/getDidAndVersion'; import { aes } from '@portkey/utils'; import useTelegram from './useTelegram'; import { NetworkType } from '@portkey/provider-types'; +import { enhancedLocalStorage as localStorage } from '../../../utils/enhancedLocalStorage'; export type PortkeyInterface = WalletHookInterface & { isManagerExists: boolean; diff --git a/packages/login/src/wallets/portkey/usePortkey/indexV1.ts b/packages/login/src/wallets/portkey/usePortkey/indexV1.ts index 150df525..afc7b9d3 100644 --- a/packages/login/src/wallets/portkey/usePortkey/indexV1.ts +++ b/packages/login/src/wallets/portkey/usePortkey/indexV1.ts @@ -18,6 +18,7 @@ import checkSignatureParams from '../../../utils/signatureParams'; import { PortkeyOptions } from 'src/types'; import { sendAdapter } from '../../../hooks/useCallContract'; import { message } from 'antd'; +import { enhancedLocalStorage as localStorage } from '../../../utils/enhancedLocalStorage'; export type PortkeyInterface = WalletHookInterface & { isManagerExists: boolean;