58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import type { EncryptedPayload } from './keyManagementEncryption'
|
|
import { storageService } from './storage/indexedDB'
|
|
|
|
export const KEY_STORAGE_KEY = 'nostr_encrypted_key'
|
|
|
|
/**
|
|
* Store account identifier in IndexedDB
|
|
* This is just a flag to indicate that an account exists
|
|
*/
|
|
export async function storeAccountFlag(): Promise<void> {
|
|
await storageService.set('nostr_account_exists', true, 'nostr_key_storage')
|
|
}
|
|
|
|
/**
|
|
* Check if account flag exists
|
|
*/
|
|
export async function hasAccountFlag(): Promise<boolean> {
|
|
try {
|
|
const exists = await storageService.get<boolean>('nostr_account_exists', 'nostr_key_storage')
|
|
return exists === true
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove account flag from IndexedDB
|
|
*/
|
|
export async function removeAccountFlag(): Promise<void> {
|
|
await storageService.delete('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')
|
|
}
|