📄 src/web3/wallet/credentials.ts
D-OPEN SOVEREIGN

Documentation & Insights

Stores the private key in localStorage in plain text.
@param chainId The chain/network ID
@param privateKey The raw private key (with or without 0x)
Retrieves the private key from localStorage.
@param chainId The chain/network ID
@returns The private key or null if not found
Checks if a private key exists in localStorage for a given chainId.
Checks if any private key exists in localStorage.
1const PK_STORAGE_KEY_PREFIX = 'wallet_pk_';
2const STORAGE_KEY_PREFIX = '@the_library/web3-session:';
3
4let cachedEmail: string | null = null;
5
6export function getCachedEmail(): string | null {
7  return cachedEmail;
8}
9
10export function setCachedEmail(email: string | null) {
11  cachedEmail = email;
12}
13
14function isLocalStorageAvailable(): boolean {
15  if (typeof window === 'undefined' || typeof localStorage === 'undefined') {
16    return false;
17  }
18  try {
19    const testKey = '__storage_test__';
20    localStorage.setItem(testKey, 'test');
21    localStorage.removeItem(testKey);
22    return true;
23  } catch {
24    return false;
25  }
26}
27
28function getItem<T = any>(key: string): T | null {
29  if (!isLocalStorageAvailable()) {
30    return null;
31  }
32  try {
33    const item = localStorage.getItem(STORAGE_KEY_PREFIX + key);
34    return item ? JSON.parse(item) : null;
35  } catch {
36    return null;
37  }
38}
39
40function setItem<T = any>(key: string, value: T): void {
41  if (!isLocalStorageAvailable()) {
42    return;
43  }
44  try {
45    localStorage.setItem(STORAGE_KEY_PREFIX + key, JSON.stringify(value));
46  } catch (error) {
47    console.warn('Failed to save to localStorage:', error);
48  }
49}
50
51/**
52 * Stores the private key in localStorage in plain text.
53 * @param chainId The chain/network ID
54 * @param privateKey The raw private key (with or without 0x)
55 */
56export function storePrivateKey(chainId: string | number, privateKey: string): void {
57  if (!privateKey) {
58    throw new Error('Private key is required');
59  }
60  const key = `${PK_STORAGE_KEY_PREFIX}${chainId}`;
61  setItem(key, privateKey);
62}
63
64/**
65 * Retrieves the private key from localStorage.
66 * @param chainId The chain/network ID
67 * @returns The private key or null if not found
68 */
69export function getPrivateKey(chainId: string | number): string | null {
70  const key = `${PK_STORAGE_KEY_PREFIX}${chainId}`;
71  return getItem<string>(key);
72}
73
74/**
75 * Checks if a private key exists in localStorage for a given chainId.
76 */
77export function hasPrivateKey(chainId: string | number): boolean {
78  const key = `${PK_STORAGE_KEY_PREFIX}${chainId}`;
79  return !!getItem<string>(key);
80}
81
82/**
83 * Checks if any private key exists in localStorage.
84 */
85export function hasAnyPrivateKey(): boolean {
86  if (!isLocalStorageAvailable()) return false;
87  const prefix = `${STORAGE_KEY_PREFIX}${PK_STORAGE_KEY_PREFIX}`;
88  for (let i = 0; i < localStorage.length; i++) {
89    if (localStorage.key(i)?.startsWith(prefix)) return true;
90  }
91  return false;
92}
93
94// Aliases for compatibility
95export const storeEncryptedPrivateKey = (chainId: string | number, privateKey: string, _email?: string) =>
96  storePrivateKey(chainId, privateKey);
97export const getDecryptedPrivateKey = (chainId: string | number, _email?: string) => getPrivateKey(chainId);
98export const hasEncryptedPrivateKey = (chainId: string | number) => hasPrivateKey(chainId);
99