Skip to content

Commit faa3a1b

Browse files
author
Jarvis
committed
Add comprehensive debug logging for group storage bug
Debug logs added to trace the full save/load flow: - πŸ”΅ saveVault: tracks encryption and storage writes - πŸ”“ unlockVault: tracks storage reads and decryption - πŸ—„οΈ cacheVault: tracks background cache updates - πŸ“¦ handleGetVaultData: tracks cache retrieval - πŸ“ handleVaultUpdate: tracks React state updates - πŸš€ initialize: tracks popup startup - βž• addGroupToVault: tracks group addition - πŸ†• handleCreateGroup: tracks group creation flow - πŸ”‘ OnboardingScreen: tracks identity generation Each log includes a unique ID to correlate related operations.
1 parent d451836 commit faa3a1b

File tree

5 files changed

+186
-21
lines changed

5 files changed

+186
-21
lines changed

β€Žsrc/background/service-worker.tsβ€Ž

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,40 +530,64 @@ async function handleImportKey(payload: ImportKeyPayload) {
530530
* Return cached vault data for popup without re-login
531531
*/
532532
function handleGetVaultData() {
533+
const getId = Math.random().toString(36).substring(7);
534+
console.log(`πŸ“¦ [handleGetVaultData:${getId}] START`);
535+
533536
if (!cachedVaultData) {
537+
console.log(`πŸ“¦ [handleGetVaultData:${getId}] No cached data, returning null`);
534538
return { vault: null };
535539
}
540+
541+
console.log(`πŸ“¦ [handleGetVaultData:${getId}] Returning cached vault - keys: ${cachedVaultData.keys.length}, groups: ${cachedVaultData.groups.length}`);
542+
console.log(`πŸ“¦ [handleGetVaultData:${getId}] Cached group IDs:`, cachedVaultData.groups.map(g => ({ id: g.id, name: g.name })));
543+
536544
return { vault: cachedVaultData };
537545
}
538546

539547
/**
540548
* Cache vault data in memory (from popup)
541549
*/
542550
async function handleCacheVault(payload: { masterPassword: string }) {
551+
const cacheId = Math.random().toString(36).substring(7);
552+
console.log(`πŸ—„οΈ [handleCacheVault:${cacheId}] START - received CACHE_VAULT message`);
553+
543554
const { masterPassword } = payload;
544555
if (!masterPassword) {
556+
console.log(`πŸ—„οΈ [handleCacheVault:${cacheId}] ERROR: Missing master password`);
545557
return { cached: false, error: 'Missing master password' };
546558
}
547559

548560
const ok = await cacheVault(masterPassword);
561+
console.log(`πŸ—„οΈ [handleCacheVault:${cacheId}] cacheVault returned: ${ok}`);
562+
console.log(`πŸ—„οΈ [handleCacheVault:${cacheId}] cachedVaultData now has - keys: ${cachedVaultData?.keys?.length}, groups: ${cachedVaultData?.groups?.length}`);
549563
return { cached: ok };
550564
}
551565

552566
/**
553567
* Export functions for popup to cache vault data
554568
*/
555569
export async function cacheVault(masterPassword: string): Promise<boolean> {
570+
const cacheId = Math.random().toString(36).substring(7);
571+
console.log(`πŸ—„οΈ [cacheVault:${cacheId}] START - about to call unlockVault to re-read from storage...`);
572+
console.log(`πŸ—„οΈ [cacheVault:${cacheId}] BEFORE: cachedVaultData has - keys: ${cachedVaultData?.keys?.length}, groups: ${cachedVaultData?.groups?.length}`);
573+
556574
try {
557575
const vaultData = await unlockVault(masterPassword);
558576
if (!vaultData) {
577+
console.log(`πŸ—„οΈ [cacheVault:${cacheId}] unlockVault returned null`);
559578
return false;
560579
}
561580

581+
console.log(`πŸ—„οΈ [cacheVault:${cacheId}] unlockVault returned - keys: ${vaultData.keys.length}, groups: ${vaultData.groups.length}`);
582+
console.log(`πŸ—„οΈ [cacheVault:${cacheId}] Group IDs from storage:`, vaultData.groups.map(g => ({ id: g.id, name: g.name })));
583+
562584
cachedVaultData = vaultData;
563585
cachedMasterPassword = masterPassword;
586+
587+
console.log(`πŸ—„οΈ [cacheVault:${cacheId}] AFTER: cachedVaultData now has - keys: ${cachedVaultData.keys.length}, groups: ${cachedVaultData.groups.length}`);
564588
return true;
565589
} catch (error) {
566-
console.error('Failed to cache vault:', error);
590+
console.error(`πŸ—„οΈ [cacheVault:${cacheId}] FAILED:`, error);
567591
return false;
568592
}
569593
}

β€Žsrc/popup/App.tsxβ€Ž

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,86 +24,135 @@ function App() {
2424
}, []);
2525

2626
async function initialize() {
27+
const initId = Math.random().toString(36).substring(7);
28+
console.log(`πŸš€ [initialize:${initId}] START`);
29+
2730
// Check if vault exists
2831
const exists = await vaultExists();
32+
console.log(`πŸš€ [initialize:${initId}] Vault exists: ${exists}`);
2933

3034
if (!exists) {
35+
console.log(`πŸš€ [initialize:${initId}] No vault, going to setup`);
3136
setScreen('setup');
3237
return;
3338
}
3439

3540
// Check if already unlocked
3641
const session = await getSession();
42+
console.log(`πŸš€ [initialize:${initId}] Session unlocked: ${session.unlocked}`);
43+
3744
if (session.unlocked) {
3845
// Try to pull cached vault data from background (no password prompt)
3946
try {
47+
console.log(`πŸš€ [initialize:${initId}] Requesting cached vault from background...`);
4048
const resp = await chrome.runtime.sendMessage({ type: 'GET_VAULT_DATA' });
49+
console.log(`πŸš€ [initialize:${initId}] Background response:`, resp ? 'got response' : 'null');
50+
4151
if (resp?.vault) {
42-
setVaultData(resp.vault as VaultData);
52+
const vault = resp.vault as VaultData;
53+
console.log(`πŸš€ [initialize:${initId}] Cached vault - keys: ${vault.keys?.length}, groups: ${vault.groups?.length}`);
54+
console.log(`πŸš€ [initialize:${initId}] Cached group IDs:`, vault.groups?.map(g => ({ id: g.id, name: g.name })));
55+
setVaultData(vault);
4356
setScreen('dashboard');
4457
return;
58+
} else {
59+
console.log(`πŸš€ [initialize:${initId}] No vault in response, fallback to login`);
4560
}
4661
} catch (e) {
47-
console.warn('Could not retrieve cached vault, fallback to login', e);
62+
console.warn(`πŸš€ [initialize:${initId}] Could not retrieve cached vault, fallback to login`, e);
4863
}
4964
}
65+
console.log(`πŸš€ [initialize:${initId}] Going to login screen`);
5066
setScreen('login');
5167
}
5268

5369
async function handleSetup(password: string) {
70+
const setupId = Math.random().toString(36).substring(7);
71+
console.log(`🎬 [handleSetup:${setupId}] START`);
72+
5473
try {
74+
console.log(`🎬 [handleSetup:${setupId}] Creating vault...`);
5575
await createVault(password);
76+
77+
console.log(`🎬 [handleSetup:${setupId}] Setting master password in state`);
5678
setMasterPassword(password);
79+
5780
const emptyVault: VaultData = { keys: [], groups: [] };
81+
console.log(`🎬 [handleSetup:${setupId}] Setting empty vault in state`);
5882
setVaultData(emptyVault);
83+
84+
console.log(`🎬 [handleSetup:${setupId}] Marking vault unlocked`);
5985
await markVaultUnlocked();
86+
87+
console.log(`🎬 [handleSetup:${setupId}] Caching in background...`);
6088
await cacheVaultInBackground(emptyVault, password);
89+
90+
console.log(`🎬 [handleSetup:${setupId}] SUCCESS - going to onboarding`);
6191
// New vault = needs onboarding
6292
setScreen('onboarding');
6393
} catch (error) {
64-
console.error('Setup failed:', error);
94+
console.error(`🎬 [handleSetup:${setupId}] FAILED:`, error);
6595
alert('Failed to create vault. Please try again.');
6696
}
6797
}
6898

6999
async function handleLogin(password: string) {
100+
const loginId = Math.random().toString(36).substring(7);
101+
console.log(`πŸ” [handleLogin:${loginId}] START`);
102+
70103
try {
104+
console.log(`πŸ” [handleLogin:${loginId}] Calling unlockVault...`);
71105
const data = await unlockVault(password);
72106
if (!data) {
107+
console.log(`πŸ” [handleLogin:${loginId}] unlockVault returned null (wrong password)`);
73108
alert('Incorrect password');
74109
return;
75110
}
76111

112+
console.log(`πŸ” [handleLogin:${loginId}] unlockVault returned - keys: ${data.keys.length}, groups: ${data.groups.length}`);
113+
console.log(`πŸ” [handleLogin:${loginId}] Group IDs from storage:`, data.groups.map(g => ({ id: g.id, name: g.name })));
114+
77115
setMasterPassword(password);
78116
setVaultData(data);
79117
await markVaultUnlocked();
80118

81119
// Cache vault in background script
120+
console.log(`πŸ” [handleLogin:${loginId}] Caching in background...`);
82121
await cacheVaultInBackground(data, password);
83122

123+
console.log(`πŸ” [handleLogin:${loginId}] SUCCESS - going to dashboard`);
84124
setScreen('dashboard');
85125
} catch (error) {
86-
console.error('Login failed:', error);
126+
console.error(`πŸ” [handleLogin:${loginId}] FAILED:`, error);
87127
alert('Failed to unlock vault. Please try again.');
88128
}
89129
}
90130

91131
async function cacheVaultInBackground(_data: VaultData, _password: string) {
132+
const cacheId = Math.random().toString(36).substring(7);
133+
console.log(`πŸ“€ [cacheVaultInBackground:${cacheId}] START - Sending CACHE_VAULT to background`);
134+
console.log(`πŸ“€ [cacheVaultInBackground:${cacheId}] Current local state - keys: ${_data.keys.length}, groups: ${_data.groups.length}`);
135+
console.log(`πŸ“€ [cacheVaultInBackground:${cacheId}] Group IDs in local state:`, _data.groups.map(g => ({ id: g.id, name: g.name })));
136+
92137
try {
93-
await chrome.runtime.sendMessage({
138+
const response = await chrome.runtime.sendMessage({
94139
type: 'CACHE_VAULT',
95140
payload: { masterPassword: _password },
96141
});
142+
console.log(`πŸ“€ [cacheVaultInBackground:${cacheId}] Response from background:`, response);
97143
} catch (err) {
98-
console.error('Failed to cache vault in background', err);
144+
console.error(`πŸ“€ [cacheVaultInBackground:${cacheId}] FAILED:`, err);
99145
}
100146
}
101147

102148
async function handleLock() {
149+
console.log(`πŸ”’ [handleLock] Locking vault...`);
150+
console.log(`πŸ”’ [handleLock] Current state before lock - keys: ${vaultData?.keys?.length}, groups: ${vaultData?.groups?.length}`);
103151
await markVaultLocked();
104152
setVaultData(null);
105153
setMasterPassword('');
106154
setScreen('login');
155+
console.log(`πŸ”’ [handleLock] Vault locked, going to login`);
107156
}
108157

109158
function handleCompose() {
@@ -158,18 +207,30 @@ function App() {
158207
}
159208

160209
async function handleVaultUpdate(updatedVault: VaultData) {
210+
const updateId = Math.random().toString(36).substring(7);
211+
console.log(`πŸ“ [handleVaultUpdate:${updateId}] START`);
212+
console.log(`πŸ“ [handleVaultUpdate:${updateId}] Incoming vault - keys: ${updatedVault.keys.length}, groups: ${updatedVault.groups.length}`);
213+
console.log(`πŸ“ [handleVaultUpdate:${updateId}] Group IDs:`, updatedVault.groups.map(g => ({ id: g.id, name: g.name })));
214+
console.log(`πŸ“ [handleVaultUpdate:${updateId}] Current state - keys: ${vaultData?.keys?.length}, groups: ${vaultData?.groups?.length}`);
215+
161216
try {
162217
// Save to storage FIRST before updating UI state
218+
console.log(`πŸ“ [handleVaultUpdate:${updateId}] Step 1: Calling saveVault...`);
163219
const { saveVault } = await import('@/storage/vault');
164220
await saveVault(updatedVault, masterPassword);
221+
console.log(`πŸ“ [handleVaultUpdate:${updateId}] saveVault completed successfully`);
165222

166223
// Only update state after successful save
224+
console.log(`πŸ“ [handleVaultUpdate:${updateId}] Step 2: Updating React state...`);
167225
setVaultData(updatedVault);
168226

169227
// Update background cache
228+
console.log(`πŸ“ [handleVaultUpdate:${updateId}] Step 3: Updating background cache...`);
170229
await cacheVaultInBackground(updatedVault, masterPassword);
230+
231+
console.log(`πŸ“ [handleVaultUpdate:${updateId}] SUCCESS - all steps completed`);
171232
} catch (error) {
172-
console.error('Failed to save vault update:', error);
233+
console.error(`πŸ“ [handleVaultUpdate:${updateId}] FAILED:`, error);
173234
alert('⚠️ Failed to save changes. Please try again. If this persists, export your vault backup from Settings.');
174235
// Don't update UI state if save failed - keep showing old data
175236
}

β€Žsrc/popup/screens/DashboardScreen.tsxβ€Ž

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ function DashboardScreen({ vaultData, onVaultUpdate, onLock, onCompose, onDecryp
143143
// ============================================================================
144144

145145
async function handleCreateGroup() {
146+
const createId = Math.random().toString(36).substring(7);
147+
console.log(`πŸ†• [handleCreateGroup:${createId}] START - name: "${newGroupName}"`);
148+
146149
if (!newGroupName.trim()) {
147150
alert('Please enter a group name');
148151
return;
@@ -151,20 +154,33 @@ function DashboardScreen({ vaultData, onVaultUpdate, onLock, onCompose, onDecryp
151154
setIsGenerating(true);
152155

153156
try {
157+
console.log(`πŸ†• [handleCreateGroup:${createId}] Current vaultData - keys: ${vaultData.keys.length}, groups: ${vaultData.groups.length}`);
158+
console.log(`πŸ†• [handleCreateGroup:${createId}] Existing group IDs:`, vaultData.groups.map(g => ({ id: g.id, name: g.name })));
159+
154160
const primaryKey = getPrimaryPersonalKey(vaultData);
161+
console.log(`πŸ†• [handleCreateGroup:${createId}] Creating group with createGroup()...`);
155162
const group = await createGroup(
156163
newGroupName,
157164
newGroupEmoji,
158165
undefined,
159166
primaryKey?.fingerprint
160167
);
168+
console.log(`πŸ†• [handleCreateGroup:${createId}] Created group - id: ${group.id}, name: ${group.name}`);
169+
170+
console.log(`πŸ†• [handleCreateGroup:${createId}] Adding to vault with addGroupToVault()...`);
161171
const updatedVault = await addGroupToVault(group, vaultData);
172+
console.log(`πŸ†• [handleCreateGroup:${createId}] Updated vault - keys: ${updatedVault.keys.length}, groups: ${updatedVault.groups.length}`);
173+
console.log(`πŸ†• [handleCreateGroup:${createId}] Updated group IDs:`, updatedVault.groups.map(g => ({ id: g.id, name: g.name })));
174+
175+
console.log(`πŸ†• [handleCreateGroup:${createId}] Calling onVaultUpdate()...`);
162176
onVaultUpdate(updatedVault);
177+
163178
setNewGroupName('');
164179
setNewGroupEmoji('πŸ¦†');
165180
setModal(null);
181+
console.log(`πŸ†• [handleCreateGroup:${createId}] SUCCESS`);
166182
} catch (error: any) {
167-
console.error('Failed to create group:', error);
183+
console.error(`πŸ†• [handleCreateGroup:${createId}] FAILED:`, error);
168184
alert(error.message || 'Failed to create group. Please try again.');
169185
} finally {
170186
setIsGenerating(false);

β€Žsrc/popup/screens/OnboardingScreen.tsxβ€Ž

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ function OnboardingScreen({ vaultData, onVaultUpdate, onComplete, onImport }: On
1919
const [copySuccess, setCopySuccess] = useState(false);
2020

2121
async function handleGenerateIdentity() {
22+
const genId = Math.random().toString(36).substring(7);
23+
console.log(`πŸ”‘ [OnboardingScreen.handleGenerateIdentity:${genId}] START`);
24+
2225
if (!identityName.trim()) {
2326
alert('Please enter a name for your identity');
2427
return;
@@ -27,17 +30,27 @@ function OnboardingScreen({ vaultData, onVaultUpdate, onComplete, onImport }: On
2730
setIsGenerating(true);
2831

2932
try {
33+
console.log(`πŸ”‘ [OnboardingScreen.handleGenerateIdentity:${genId}] Current vaultData - keys: ${vaultData.keys.length}, groups: ${vaultData.groups.length}`);
34+
35+
console.log(`πŸ”‘ [OnboardingScreen.handleGenerateIdentity:${genId}] Generating key...`);
3036
const key = await generatePersonalKey(identityName);
37+
console.log(`πŸ”‘ [OnboardingScreen.handleGenerateIdentity:${genId}] Key generated - id: ${key.id}`);
38+
39+
console.log(`πŸ”‘ [OnboardingScreen.handleGenerateIdentity:${genId}] Adding key to vault...`);
3140
const updatedVault = await addKeyToVault(key, vaultData);
41+
console.log(`πŸ”‘ [OnboardingScreen.handleGenerateIdentity:${genId}] Updated vault - keys: ${updatedVault.keys.length}, groups: ${updatedVault.groups.length}`);
42+
43+
console.log(`πŸ”‘ [OnboardingScreen.handleGenerateIdentity:${genId}] Calling onVaultUpdate...`);
3244
onVaultUpdate(updatedVault);
3345

3446
// Get the public key string for sharing
3547
const keyString = exportPublicKey(key);
3648
setGeneratedKeyString(keyString);
3749

50+
console.log(`πŸ”‘ [OnboardingScreen.handleGenerateIdentity:${genId}] SUCCESS - going to share-key step`);
3851
setStep('share-key');
3952
} catch (error) {
40-
console.error('Failed to generate identity:', error);
53+
console.error(`πŸ”‘ [OnboardingScreen.handleGenerateIdentity:${genId}] FAILED:`, error);
4154
alert('Failed to generate identity. Please try again.');
4255
} finally {
4356
setIsGenerating(false);

0 commit comments

Comments
Β (0)