forked from polkadot-js/extension
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update .nvmrc file since it uses wrong version
- Loading branch information
Showing
5 changed files
with
339 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
18 | ||
18.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 19 additions & 48 deletions
67
packages/extension-base/src/background/handlers/chromeStorage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,32 @@ | ||
import { z } from 'zod'; | ||
|
||
import { PASSWORD_EXPIRY_MS } from '../../defaults'; | ||
// use namespaces to add typization for your storage data | ||
const namespaces = { 'password-cache': z.record(z.string(), z.object({ password: z.string(), expiresAt: z.number() })) } as const satisfies Record<string, z.ZodSchema>; | ||
|
||
const addressSchema = z.string(); | ||
const passwordSchema = z.record(z.string(), z.object({ expiry: z.number(), password: z.string() })); | ||
export const setItem = async <N extends keyof typeof namespaces>(namespace: N, updater: (currData?: z.TypeOf<typeof namespaces[N]>) => z.TypeOf<typeof namespaces[N]>) => { | ||
const currentData = await getItem(namespace); | ||
|
||
type Password = { expiry: number, password?: string } | ||
|
||
async function getPassword (address: string): Promise<Password> { | ||
if (addressSchema.safeParse(address).success) { | ||
const { savePass } = await chrome.storage.session.get('savePass'); | ||
|
||
const pass = passwordSchema.safeParse(savePass); | ||
|
||
if (pass.success) { | ||
return pass.data[address]; | ||
} else { | ||
return { expiry: 0 }; | ||
} | ||
} else { | ||
return { expiry: 0 }; | ||
} | ||
} | ||
|
||
async function setPassword (address: string, password: string): Promise<void> { | ||
if (addressSchema.safeParse(address).success) { | ||
const { savePass } = await chrome.storage.session.get('savePass'); | ||
await chrome.storage.session.set({ [namespace]: updater(currentData) }); | ||
}; | ||
|
||
const savedPasswords = passwordSchema.safeParse(savePass); | ||
export const getItem = async <N extends keyof typeof namespaces>(namespace: N): Promise<z.infer<typeof namespaces[N]> | undefined> => { | ||
const { [namespace]: cachedData } = await chrome.storage.session.get(namespace); | ||
|
||
if (savedPasswords.success) { | ||
await chrome.storage.session.set({ savePass: { ...savedPasswords.data, [address]: { password, expiry: Date.now() + PASSWORD_EXPIRY_MS } } }); | ||
} else { | ||
await chrome.storage.session.set({ savePass: { [address]: { expiry: Date.now() + PASSWORD_EXPIRY_MS, password } } }); | ||
} | ||
} else { | ||
console.error('Provided address did not pass validation'); | ||
try { | ||
return namespaces[namespace].parse(cachedData); | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
|
||
async function removePassword (address: string): Promise<void> { | ||
if (addressSchema.safeParse(address).success) { | ||
const { savePass } = await chrome.storage.session.get('savePass'); | ||
|
||
const pass = passwordSchema.safeParse(savePass); | ||
}; | ||
|
||
if (pass.success) { | ||
delete pass.data[address]; | ||
await chrome.storage.session.set({ savePass: pass.data }); | ||
} | ||
} | ||
} | ||
export const removeItem = async <N extends keyof typeof namespaces>(namespace: N) => { | ||
await chrome.storage.session.remove(namespace); | ||
}; | ||
|
||
const chromeStorage = { | ||
setPassword, | ||
getPassword, | ||
removePassword | ||
getItem, | ||
setItem, | ||
removeItem | ||
}; | ||
|
||
export default chromeStorage; |
Oops, something went wrong.