Skip to content

Commit

Permalink
Merge pull request #30 from ensdomains/fix/localstorage
Browse files Browse the repository at this point in the history
Removed direct references to localstorage
  • Loading branch information
TateB authored Aug 4, 2022
2 parents dc67ccb + 50b3600 commit 1de1f41
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/ensjs/src/utils/labels.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { solidityKeccak256 } from 'ethers/lib/utils'
import { truncateFormat } from './format'

const hasLocalStorage = typeof localStorage !== 'undefined'

export const labelhash = (input: string) =>
solidityKeccak256(['string'], [input])

Expand Down Expand Up @@ -37,13 +39,13 @@ export function isEncodedLabelhash(hash: string) {
}

function getLabels() {
return localStorage
return hasLocalStorage
? JSON.parse(localStorage.getItem('ensjs:labels') as string) || {}
: {}
}

function _saveLabel(hash: string, label: any) {
if (!localStorage) return hash
if (!hasLocalStorage) return hash
const labels = getLabels()
localStorage.setItem(
'ensjs:labels',
Expand Down Expand Up @@ -106,7 +108,7 @@ export function decryptName(name: string) {
export const truncateUndecryptedName = (name: string) => truncateFormat(name)

export function checkLocalStorageSize() {
if (!localStorage) return 'Empty (0 KB)'
if (!hasLocalStorage) return 'Empty (0 KB)'
let allStrings = ''
for (const key in window.localStorage) {
if (Object.prototype.hasOwnProperty.call(window.localStorage, key)) {
Expand Down

0 comments on commit 1de1f41

Please sign in to comment.