Skip to content

Commit

Permalink
fix: localStorage check
Browse files Browse the repository at this point in the history
  • Loading branch information
isordo committed Oct 24, 2023
1 parent 4017f26 commit f26d7ae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
3 changes: 2 additions & 1 deletion packages/beacon-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { getLogger, Logger, setLogger } from './utils/Logger'
import { windowRef } from './MockWindow'
import { CommunicationClient } from './transports/clients/CommunicationClient'
import { ClientEvents } from './transports/clients/ClientEvents'
import { isLocalStorageAvailable } from './utils/storage-utils'
// import { EncryptionType } from './types/EncryptionType'
// import { EncryptionOperation } from './types/EncryptionOperation'

Expand Down Expand Up @@ -67,7 +68,7 @@ export {
export { Transport, MessageBasedClient, CommunicationClient }

// Storage
export { ChromeStorage, LocalStorage, getStorage }
export { ChromeStorage, LocalStorage, getStorage, isLocalStorageAvailable }

// Managers
export { PeerManager, AccountManager, AppMetadataManager, PermissionManager }
Expand Down
10 changes: 10 additions & 0 deletions packages/beacon-core/src/utils/storage-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const isLocalStorageAvailable = () => {
var test = 'test'
try {
localStorage.setItem(test, test)
localStorage.removeItem(test)
return true
} catch (e) {
return false
}
}
36 changes: 20 additions & 16 deletions packages/beacon-dapp/src/dapp-client/DAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import {
AppMetadataManager,
Serializer,
LocalStorage,
isLocalStorageAvailable,
getAccountIdentifier,
getSenderId,
Logger,
Expand Down Expand Up @@ -435,9 +436,8 @@ export class DAppClient extends Client {
}

private async resetWCSnapshot() {
if (!localStorage) {
if (!isLocalStorageAvailable()) {
return

}
const storage = new LocalStorage()

Expand Down Expand Up @@ -1644,13 +1644,15 @@ export class DAppClient extends Client {
logger.log('makeRequest', 'after init')

const transport = await this.transport
const regexp = new RegExp(/^(\[\])$/)
const pairingMissing = regexp.test(
(await this.storage.get(StorageKey.WC_2_CORE_PAIRING)) ?? '[]'
)
const sessionMissing = regexp.test(
(await this.storage.get(StorageKey.WC_2_CLIENT_SESSION)) ?? '[]'
)

let pairingMissing,
sessionMissing = false

if (isLocalStorageAvailable()) {
const storage = new LocalStorage()
pairingMissing = ((await storage.get(StorageKey.WC_2_CORE_PAIRING)) ?? '[]') === '[]'
sessionMissing = ((await storage.get(StorageKey.WC_2_CLIENT_SESSION)) ?? '[]') === '[]'
}

if (
requestInput.type === BeaconMessageType.PermissionRequest &&
Expand Down Expand Up @@ -1780,13 +1782,15 @@ export class DAppClient extends Client {
}

const transport = await this.transport
const regexp = new RegExp(/^(\[\])$/)
const pairingMissing = regexp.test(
(await this.storage.get(StorageKey.WC_2_CORE_PAIRING)) ?? '[]'
)
const sessionMissing = regexp.test(
(await this.storage.get(StorageKey.WC_2_CLIENT_SESSION)) ?? '[]'
)

let pairingMissing,
sessionMissing = false

if (isLocalStorageAvailable()) {
const storage = new LocalStorage()
pairingMissing = ((await storage.get(StorageKey.WC_2_CORE_PAIRING)) ?? '[]') === '[]'
sessionMissing = ((await storage.get(StorageKey.WC_2_CLIENT_SESSION)) ?? '[]') === '[]'
}

if (
requestInput.type === BeaconMessageType.PermissionRequest &&
Expand Down

0 comments on commit f26d7ae

Please sign in to comment.