story-research-zapwall/lib/keyManagementStorage.ts

63 lines
1.9 KiB
TypeScript

import type { EncryptedPayload } from './keyManagementEncryption'
import { storageService } from './storage/indexedDB'
export const KEY_STORAGE_KEY = 'nostr_encrypted_key'
/**
* Store account identifier in browser storage
* This is just a flag to indicate that an account exists
*/
export function storeAccountFlag(): void {
if (typeof window === 'undefined' || !window.localStorage) {
throw new Error('localStorage not available')
}
localStorage.setItem('nostr_account_exists', 'true')
}
/**
* Check if account flag exists
*/
export function hasAccountFlag(): boolean {
if (typeof window === 'undefined' || !window.localStorage) {
return false
}
return localStorage.getItem('nostr_account_exists') === 'true'
}
/**
* Remove account flag from browser storage
*/
export function removeAccountFlag(): void {
if (typeof window !== 'undefined' && window.localStorage) {
localStorage.removeItem('nostr_account_exists')
}
}
export async function getEncryptedKey(): Promise<EncryptedPayload | null> {
return await storageService.get<EncryptedPayload>(KEY_STORAGE_KEY, 'nostr_key_storage')
}
export async function setEncryptedKey(encryptedNsec: EncryptedPayload): Promise<void> {
await storageService.set(KEY_STORAGE_KEY, encryptedNsec, 'nostr_key_storage')
}
export async function getPublicKeys(): Promise<{ publicKey: string; npub: string } | null> {
try {
const stored = await storageService.get<{ publicKey: string; npub: string }>('nostr_public_key', 'nostr_key_storage')
return stored
} catch {
return null
}
}
export async function setPublicKeys(publicKey: string, npub: string): Promise<void> {
await storageService.set('nostr_public_key', { publicKey, npub }, 'nostr_key_storage')
}
export async function deleteStoredKeys(): Promise<void> {
await storageService.delete(KEY_STORAGE_KEY)
await storageService.delete('nostr_public_key')
}