Skip to content

Commit

Permalink
chore: add safety for localstorage (#2770)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoruka authored Aug 30, 2024
1 parent 25c6f28 commit fe31c83
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 166 deletions.
50 changes: 24 additions & 26 deletions packages/base/adapters/evm/ethers/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import type { W3mFrameTypes } from '@web3modal/wallet'
import type { AppKit } from '../../../src/client.js'
import type { AppKitOptions } from '../../../utils/TypesUtil.js'
import type { OptionsControllerState } from '@web3modal/core'
import { SafeLocalStorage } from '../../../utils/SafeLocalStorage.js'

// -- Types ---------------------------------------------------------------------
export interface AdapterOptions extends Pick<AppKitOptions, 'siweConfig'> {
Expand Down Expand Up @@ -168,7 +169,7 @@ export class EVMEthersClient {

getApprovedCaipNetworksData: async () =>
new Promise(async resolve => {
const walletChoice = localStorage.getItem(EthersConstantsUtil.WALLET_ID)
const walletChoice = SafeLocalStorage.getItem(EthersConstantsUtil.WALLET_ID)
if (walletChoice?.includes(ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID)) {
const provider = await this.getWalletConnectProvider()
if (!provider) {
Expand Down Expand Up @@ -343,7 +344,7 @@ export class EVMEthersClient {
disconnect: async () => {
const provider = EthersStoreUtil.state.provider
const providerType = EthersStoreUtil.state.providerType
localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(EthersConstantsUtil.WALLET_ID)
EthersStoreUtil.reset()
this.appKit?.setClientId(null)
if (siweConfig?.options?.signOutOnDisconnect) {
Expand All @@ -368,7 +369,7 @@ export class EVMEthersClient {
}

provider?.emit?.('disconnect')
localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(EthersConstantsUtil.WALLET_ID)
EthersStoreUtil.reset()
},

Expand Down Expand Up @@ -641,7 +642,7 @@ export class EVMEthersClient {
public async disconnect() {
const { provider, providerType } = EthersStoreUtil.state

localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(EthersConstantsUtil.WALLET_ID)
EthersStoreUtil.reset()
this.appKit?.setClientId(null)

Expand Down Expand Up @@ -752,7 +753,7 @@ export class EVMEthersClient {

private async checkActiveWalletConnectProvider() {
const WalletConnectProvider = await this.getWalletConnectProvider()
const walletId = localStorage.getItem(EthersConstantsUtil.WALLET_ID)
const walletId = SafeLocalStorage.getItem(EthersConstantsUtil.WALLET_ID)

if (WalletConnectProvider) {
if (walletId === ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID) {
Expand All @@ -766,7 +767,7 @@ export class EVMEthersClient {

private checkActiveInjectedProvider(config: ProviderType) {
const InjectedProvider = config.injected
const walletId = localStorage.getItem(EthersConstantsUtil.WALLET_ID)
const walletId = SafeLocalStorage.getItem(EthersConstantsUtil.WALLET_ID)

if (InjectedProvider) {
if (walletId === ConstantsUtil.INJECTED_CONNECTOR_ID) {
Expand All @@ -778,23 +779,23 @@ export class EVMEthersClient {

private checkActiveCoinbaseProvider(config: ProviderType) {
const CoinbaseProvider = config.coinbase as unknown as ExternalProvider
const walletId = localStorage.getItem(EthersConstantsUtil.WALLET_ID)
const walletId = SafeLocalStorage.getItem(EthersConstantsUtil.WALLET_ID)

if (CoinbaseProvider) {
if (walletId === ConstantsUtil.COINBASE_SDK_CONNECTOR_ID) {
if (CoinbaseProvider.accounts && CoinbaseProvider.accounts?.length > 0) {
this.setCoinbaseProvider(config)
this.watchCoinbase(config)
} else {
localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(EthersConstantsUtil.WALLET_ID)
EthersStoreUtil.reset()
}
}
}
}

private checkActive6963Provider() {
const currentActiveWallet = window?.localStorage.getItem(EthersConstantsUtil.WALLET_ID)
const currentActiveWallet = SafeLocalStorage.getItem(EthersConstantsUtil.WALLET_ID)
if (currentActiveWallet) {
const currentProvider = this.EIP6963Providers.find(
provider => provider.info.name === currentActiveWallet
Expand All @@ -806,7 +807,7 @@ export class EVMEthersClient {
}

private async setWalletConnectProvider() {
window?.localStorage.setItem(
SafeLocalStorage.setItem(
EthersConstantsUtil.WALLET_ID,
ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID
)
Expand Down Expand Up @@ -835,7 +836,7 @@ export class EVMEthersClient {
}

private async setInjectedProvider(config: ProviderType) {
window?.localStorage.setItem(EthersConstantsUtil.WALLET_ID, ConstantsUtil.INJECTED_CONNECTOR_ID)
SafeLocalStorage.setItem(EthersConstantsUtil.WALLET_ID, ConstantsUtil.INJECTED_CONNECTOR_ID)
const InjectedProvider = config.injected

if (InjectedProvider) {
Expand All @@ -857,7 +858,7 @@ export class EVMEthersClient {
}

private async setEIP6963Provider(provider: Provider, name: string) {
window?.localStorage.setItem(EthersConstantsUtil.WALLET_ID, name)
SafeLocalStorage.setItem(EthersConstantsUtil.WALLET_ID, name)

if (provider) {
const { addresses, chainId } = await EthersHelpersUtil.getUserInfo(provider)
Expand All @@ -878,10 +879,7 @@ export class EVMEthersClient {
}

private async setCoinbaseProvider(config: ProviderType) {
window?.localStorage.setItem(
EthersConstantsUtil.WALLET_ID,
ConstantsUtil.COINBASE_SDK_CONNECTOR_ID
)
SafeLocalStorage.setItem(EthersConstantsUtil.WALLET_ID, ConstantsUtil.COINBASE_SDK_CONNECTOR_ID)
const CoinbaseProvider = config.coinbase
if (CoinbaseProvider) {
const { addresses, chainId } = await EthersHelpersUtil.getUserInfo(CoinbaseProvider)
Expand All @@ -902,7 +900,7 @@ export class EVMEthersClient {
}

private async setAuthProvider() {
window?.localStorage.setItem(EthersConstantsUtil.WALLET_ID, ConstantsUtil.AUTH_CONNECTOR_ID)
SafeLocalStorage.setItem(EthersConstantsUtil.WALLET_ID, ConstantsUtil.AUTH_CONNECTOR_ID)

if (this.authProvider) {
this.appKit?.setLoading(true)
Expand Down Expand Up @@ -945,7 +943,7 @@ export class EVMEthersClient {
const provider = await this.getWalletConnectProvider()

function disconnectHandler() {
localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(EthersConstantsUtil.WALLET_ID)
EthersStoreUtil.reset()
provider?.removeListener('disconnect', disconnectHandler)
provider?.removeListener('accountsChanged', accountsChangedHandler)
Expand Down Expand Up @@ -976,7 +974,7 @@ export class EVMEthersClient {
const provider = config.injected

function disconnectHandler() {
localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(EthersConstantsUtil.WALLET_ID)
EthersStoreUtil.reset()

provider?.removeListener('disconnect', disconnectHandler)
Expand All @@ -989,7 +987,7 @@ export class EVMEthersClient {
if (currentAccount) {
EthersStoreUtil.setAddress(getOriginalAddress(currentAccount) as Address)
} else {
localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(EthersConstantsUtil.WALLET_ID)
EthersStoreUtil.reset()
}
}
Expand All @@ -1013,7 +1011,7 @@ export class EVMEthersClient {

private watchEIP6963(provider: Provider) {
function disconnectHandler() {
localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(EthersConstantsUtil.WALLET_ID)
EthersStoreUtil.reset()
provider.removeListener('disconnect', disconnectHandler)
provider.removeListener('accountsChanged', accountsChangedHandler)
Expand All @@ -1030,7 +1028,7 @@ export class EVMEthersClient {
)
} else {
this.appKit?.setAllAccounts([], this.chain)
localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(EthersConstantsUtil.WALLET_ID)
EthersStoreUtil.reset()
}
}
Expand All @@ -1054,9 +1052,9 @@ export class EVMEthersClient {

private watchCoinbase(config: ProviderType) {
const provider = config.coinbase
const walletId = localStorage.getItem(EthersConstantsUtil.WALLET_ID)
const walletId = SafeLocalStorage.getItem(EthersConstantsUtil.WALLET_ID)
function disconnectHandler() {
localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(EthersConstantsUtil.WALLET_ID)
EthersStoreUtil.reset()

provider?.removeListener('disconnect', disconnectHandler)
Expand All @@ -1069,7 +1067,7 @@ export class EVMEthersClient {
if (currentAccount) {
EthersStoreUtil.setAddress(getOriginalAddress(currentAccount) as Address)
} else {
localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(EthersConstantsUtil.WALLET_ID)
EthersStoreUtil.reset()
}
}
Expand Down Expand Up @@ -1322,7 +1320,7 @@ export class EVMEthersClient {
}

private syncConnectedWalletInfo() {
const currentActiveWallet = window?.localStorage.getItem(EthersConstantsUtil.WALLET_ID)
const currentActiveWallet = SafeLocalStorage.getItem(EthersConstantsUtil.WALLET_ID)
const providerType = EthersStoreUtil.state.providerType

if (providerType === ConstantsUtil.EIP6963_CONNECTOR_ID) {
Expand Down
Loading

0 comments on commit fe31c83

Please sign in to comment.