ndwallet-core
Version:
Core cryptographic library for NDWallet browser environments
53 lines (52 loc) • 1.84 kB
JavaScript
// Common key identifiers
export const KEY_SHARE = 'share';
export const KEY_AUTH_TOKEN = 'auth_token';
export const KEY_CREDENTIAL = 'credential';
export const KEY_DEVICE_SECRET = 'device_secret';
/**
* Normalize a string by converting it to lowercase and trimming whitespace
* @param value - The string to normalize
* @returns The normalized string
*/
function _normalizeString(value) {
return value.toLowerCase().trim();
}
/**
* Save local data for a specific account
* @param email - The email address associated with the account
* @param key - The key identifier for the data (e.g., 'share', 'auth_token')
* @param data - The data to save
*/
export function saveLocalData(email, key, data) {
email = _normalizeString(email);
if (typeof window === 'undefined') {
throw new Error('This function can only be called in a browser environment');
}
try {
// Store the data with the email and key as part of the storage key
localStorage.setItem(`${key}-${email}`, JSON.stringify(data));
}
catch (error) {
console.error(`Error saving ${key} to localStorage:`, error);
if (error instanceof Error) {
throw new Error(`Failed to save ${key}: ${error.message}`);
}
else {
throw new Error(`Failed to save ${key}: ${String(error)}`);
}
}
}
/**
* Get local data for a specific account
* @param email - The email address associated with the account
* @param key - The key identifier for the data (e.g., 'share', 'auth_token')
* @returns The data or null if not found
*/
export function getLocalData(email, key) {
email = _normalizeString(email);
if (typeof window === 'undefined') {
return null;
}
const data = localStorage.getItem(`${key}-${email}`);
return data ? JSON.parse(data) : null;
}