@nuwa-ai/identity-kit-web
Version:
Web extensions for Nuwa Identity Kit
64 lines (63 loc) • 1.69 kB
JavaScript
/**
* Browser LocalStorage implementation of KeyStore
*/
export class LocalStorageKeyStore {
constructor(options = {}) {
this.prefix = options.prefix || 'nuwa_keystore_';
}
/**
* List all key IDs stored in this KeyStore
*/
async listKeyIds() {
const keys = [];
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key && key.startsWith(this.prefix)) {
keys.push(key.substring(this.prefix.length));
}
}
return keys;
}
/**
* Load a key by ID, or all keys if no ID is provided
*/
async load(keyId) {
if (!keyId) {
const keys = await this.listKeyIds();
if (keys.length === 0) {
return null;
}
return this.load(keys[0]);
}
const stored = localStorage.getItem(this.prefix + keyId);
if (!stored) {
return null;
}
try {
return JSON.parse(stored);
}
catch (e) {
console.error('Failed to parse stored key:', e);
return null;
}
}
/**
* Save a key to storage
*/
async save(key) {
localStorage.setItem(this.prefix + key.keyId, JSON.stringify(key));
}
/**
* Clear a key from storage, or all keys if no ID is provided
*/
async clear(keyId) {
if (!keyId) {
const keys = await this.listKeyIds();
for (const key of keys) {
await this.clear(key);
}
return;
}
localStorage.removeItem(this.prefix + keyId);
}
}