diff options
| author | Adam Soltys <[email protected]> | 2026-02-16 13:17:43 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-02-16 13:17:43 -0800 |
| commit | ac06683100e089ad589661b14ebaf5dc9582a256 (patch) | |
| tree | f561f90aa09f3a6a3747cd703984043f028f9067 /src/lib/passwordCache.ts | |
| parent | e39bf4f977bba2952a7de4a307b14c5ec862a3a7 (diff) | |
| download | coinos-ui-ac06683100e089ad589661b14ebaf5dc9582a256.tar.gz coinos-ui-ac06683100e089ad589661b14ebaf5dc9582a256.zip | |
passkey tweaks
Diffstat (limited to 'src/lib/passwordCache.ts')
| -rw-r--r-- | src/lib/passwordCache.ts | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/lib/passwordCache.ts b/src/lib/passwordCache.ts index 5a2a4796..98272e0f 100644 --- a/src/lib/passwordCache.ts +++ b/src/lib/passwordCache.ts @@ -59,3 +59,45 @@ export const getRememberedWalletPassword = () => { walletPassword.set(password); return password; }; + +// PRF key cache +const PRF_KEY = "prfKey"; +const PRF_EXPIRES_KEY = "prfKeyExpiresAt"; + +function hexFromBuffer(buf: ArrayBuffer): string { + return Array.from(new Uint8Array(buf)) + .map((b) => b.toString(16).padStart(2, "0")) + .join(""); +} + +function bufferFromHex(hex: string): ArrayBuffer { + const bytes = new Uint8Array(hex.length / 2); + for (let i = 0; i < hex.length; i += 2) bytes[i / 2] = parseInt(hex.slice(i, i + 2), 16); + return bytes.buffer; +} + +export const rememberPrfKey = (key: ArrayBuffer, durationMs: number) => { + if (!browser) return; + const expiresAt = Date.now() + durationMs; + localStorage.setItem(PRF_KEY, hexFromBuffer(key)); + localStorage.setItem(PRF_EXPIRES_KEY, String(expiresAt)); +}; + +export const getCachedPrfKey = (): ArrayBuffer | null => { + if (!browser) return null; + const hex = localStorage.getItem(PRF_KEY); + if (!hex) return null; + const expiresAtRaw = localStorage.getItem(PRF_EXPIRES_KEY); + const expiresAt = expiresAtRaw ? Number(expiresAtRaw) : 0; + if (expiresAt && Date.now() > expiresAt) { + forgetPrfKey(); + return null; + } + return bufferFromHex(hex); +}; + +export const forgetPrfKey = () => { + if (!browser) return; + localStorage.removeItem(PRF_KEY); + localStorage.removeItem(PRF_EXPIRES_KEY); +}; |
