Skip to content

Commit

Permalink
fix walletconnect storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jfschwarz committed Dec 14, 2023
1 parent f1738f7 commit 78deb72
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 122 deletions.
102 changes: 50 additions & 52 deletions extension/.pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
11 changes: 5 additions & 6 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@
"@types/react-modal": "^3.16.3",
"@typescript-eslint/eslint-plugin": "^5.48.1",
"@typescript-eslint/parser": "^5.48.1",
"@walletconnect/core": "^2.10.1",
"@walletconnect/ethereum-provider": "^2.10.1",
"@walletconnect/keyvaluestorage": "^1.0.2",
"@walletconnect/core": "^2.10.6",
"@walletconnect/ethereum-provider": "^2.10.6",
"@walletconnect/keyvaluestorage": "^1.1.1",
"@walletconnect/modal": "^2.6.2",
"@walletconnect/safe-json": "^1.0.2",
"@walletconnect/sign-client": "^2.10.1",
"@walletconnect/universal-provider": "^2.10.1",
"@walletconnect/sign-client": "^2.10.6",
"@walletconnect/universal-provider": "^2.10.6",
"classnames": "^2.3.1",
"copy-to-clipboard": "^3.3.1",
"cspell": "^5.21.2",
Expand Down
27 changes: 7 additions & 20 deletions extension/src/providers/useWalletConnect.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { safeJsonParse, safeJsonStringify } from '@walletconnect/safe-json'
import { Core } from '@walletconnect/core'
import WalletConnectEthereumProvider from '@walletconnect/ethereum-provider'
import { WalletConnectModal } from '@walletconnect/modal'
import { useCallback, useEffect, useMemo, useState } from 'react'
import { RequestArguments } from '@walletconnect/ethereum-provider/dist/types/types'
import { UniversalProvider } from '@walletconnect/universal-provider'
import { SignClient } from '@walletconnect/sign-client'
import { IKeyValueStorage, parseEntry } from '@walletconnect/keyvaluestorage'
import { KeyValueStorage } from '@walletconnect/keyvaluestorage'
import { EthereumProviderOptions } from '@walletconnect/ethereum-provider/dist/types/EthereumProvider'

import { RPC } from '../networks'
Expand Down Expand Up @@ -100,7 +99,7 @@ class WalletConnectEthereumMultiProvider extends WalletConnectEthereumProvider {
name: this.connectionId,
core: new Core({
...opts,
storage: new PrefixedLocalStorage(`${this.connectionId}:`),
storage: new PrefixedKeyValueStorage(`${this.connectionId}:`),
}),
}),
})
Expand Down Expand Up @@ -247,36 +246,24 @@ const useWalletConnect = (
export default useWalletConnect

/** Adjusted from https://github.com/WalletConnect/walletconnect-utils/blob/master/misc/keyvaluestorage/src/browser/index.ts */
class PrefixedLocalStorage implements IKeyValueStorage {
private readonly localStorage: Storage = localStorage
class PrefixedKeyValueStorage extends KeyValueStorage {
private readonly prefix: string

constructor(prefix: string) {
super()
this.prefix = prefix
}

public async getKeys(): Promise<string[]> {
return Object.keys(this.localStorage)
}

public async getEntries<T = any>(): Promise<[string, T][]> {
return Object.entries(this.localStorage).map(parseEntry)
}

public async getItem<T = any>(key: string): Promise<T | undefined> {
const item = this.localStorage.getItem(this.prefix + key)
if (item === null) {
return undefined
}
return safeJsonParse(item) as T
return super.getItem(this.prefix + key)
}

public async setItem<T = any>(key: string, value: T): Promise<void> {
this.localStorage.setItem(this.prefix + key, safeJsonStringify(value))
super.setItem(this.prefix + key, value)
}

public async removeItem(key: string): Promise<void> {
this.localStorage.removeItem(this.prefix + key)
super.removeItem(this.prefix + key)
}
}

Expand Down
Loading

0 comments on commit 78deb72

Please sign in to comment.