Skip to content

Commit 7b47f37

Browse files
committed
fix(mobile): resolve TypeScript errors and remove unused code
1 parent a5224d9 commit 7b47f37

File tree

5 files changed

+95
-121
lines changed

5 files changed

+95
-121
lines changed

apps/mobile/v1/package-lock.json

Lines changed: 13 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/mobile/v1/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"react-native-worklets": "0.5.1"
5353
},
5454
"devDependencies": {
55+
"@types/node-forge": "^1.3.14",
5556
"@types/react": "~19.1.0",
5657
"eslint": "^9.25.0",
5758
"eslint-config-expo": "~10.0.0",

apps/mobile/v1/src/hooks/useMasterPassword.ts

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export function useMasterPassword() {
2323
const [needsUnlock, setNeedsUnlock] = useState(false);
2424
const [isNewSetup, setIsNewSetup] = useState(false);
2525
const [isChecking, setIsChecking] = useState(true);
26+
const [, setLastCheckTime] = useState<number>(0);
2627

2728
const userId = user?.id;
2829

@@ -117,42 +118,35 @@ export function useMasterPassword() {
117118
throw new Error('Password is required');
118119
}
119120

120-
try {
121-
if (isNewSetup) {
122-
// Setting up new master password
123-
if (__DEV__) {
124-
console.log('🔑 Setting up new master password...');
125-
}
126-
await setupMasterPassword(password, userId);
127-
if (__DEV__) {
128-
console.log('🔑 Master password setup completed');
129-
}
130-
} else {
131-
// Unlocking with existing password
132-
if (__DEV__) {
133-
console.log('🔑 Unlocking with existing password...');
134-
}
135-
const success = await unlockWithMasterPassword(password, userId);
136-
if (!success) {
137-
throw new Error('Invalid password');
138-
}
139-
if (__DEV__) {
140-
console.log('🔑 Master password unlock completed');
141-
}
121+
if (isNewSetup) {
122+
// Setting up new master password
123+
if (__DEV__) {
124+
console.log('🔑 Setting up new master password...');
142125
}
143-
144-
// Successfully authenticated
126+
await setupMasterPassword(password, userId);
145127
if (__DEV__) {
146-
console.log('🔑 Setting needsUnlock=false, isNewSetup=false');
128+
console.log('🔑 Master password setup completed');
129+
}
130+
} else {
131+
// Unlocking with existing password
132+
if (__DEV__) {
133+
console.log('🔑 Unlocking with existing password...');
134+
}
135+
const success = await unlockWithMasterPassword(password, userId);
136+
if (!success) {
137+
throw new Error('Invalid password');
147138
}
148-
setNeedsUnlock(false);
149-
setIsNewSetup(false);
150-
} catch (error) {
151139
if (__DEV__) {
152-
console.log('🔑 onPasswordSuccess error:', error);
140+
console.log('🔑 Master password unlock completed');
153141
}
154-
throw error; // Re-throw to let the UI handle the error
155142
}
143+
144+
// Successfully authenticated
145+
if (__DEV__) {
146+
console.log('🔑 Setting needsUnlock=false, isNewSetup=false');
147+
}
148+
setNeedsUnlock(false);
149+
setIsNewSetup(false);
156150
};
157151

158152
const signOut = async () => {
@@ -174,4 +168,4 @@ export function useMasterPassword() {
174168
signOut,
175169
refreshStatus: checkMasterPasswordStatus,
176170
};
177-
}
171+
}

apps/mobile/v1/src/lib/encryption/core/aes.ts

Lines changed: 56 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,31 @@ export async function encryptWithAESGCM(
1414
keyBase64: string,
1515
ivBase64: string
1616
): Promise<string> {
17-
try {
18-
// Convert base64 to forge-compatible format
19-
const key = forge.util.decode64(keyBase64);
20-
const iv = forge.util.decode64(ivBase64);
17+
// Convert base64 to forge-compatible format
18+
const key = forge.util.decode64(keyBase64);
19+
const iv = forge.util.decode64(ivBase64);
2120

22-
// Create AES-GCM cipher
23-
const cipher = forge.cipher.createCipher('AES-GCM', key);
21+
// Create AES-GCM cipher
22+
const cipher = forge.cipher.createCipher('AES-GCM', key);
2423

25-
// Start encryption with IV
26-
cipher.start({ iv });
24+
// Start encryption with IV
25+
cipher.start({ iv: forge.util.createBuffer(iv) });
2726

28-
// Update with plaintext
29-
cipher.update(forge.util.createBuffer(plaintext, 'utf8'));
27+
// Update with plaintext
28+
cipher.update(forge.util.createBuffer(plaintext, 'utf8'));
3029

31-
// Finish encryption
32-
cipher.finish();
30+
// Finish encryption
31+
cipher.finish();
3332

34-
// Get ciphertext and auth tag
35-
const ciphertext = cipher.output.getBytes();
36-
const authTag = cipher.mode.tag.getBytes();
33+
// Get ciphertext and auth tag
34+
const ciphertext = cipher.output.getBytes();
35+
const authTag = cipher.mode.tag.getBytes();
3736

38-
// Combine ciphertext + auth tag (Web Crypto API format)
39-
const encryptedWithTag = ciphertext + authTag;
37+
// Combine ciphertext + auth tag (Web Crypto API format)
38+
const encryptedWithTag = ciphertext + authTag;
4039

41-
// Convert to base64
42-
return forge.util.encode64(encryptedWithTag);
43-
} catch (error) {
44-
throw new Error(`AES-GCM encryption failed: ${error}`);
45-
}
40+
// Convert to base64
41+
return forge.util.encode64(encryptedWithTag);
4642
}
4743

4844
/**
@@ -53,48 +49,43 @@ export async function decryptWithAESGCM(
5349
keyBase64: string,
5450
ivBase64: string
5551
): Promise<string> {
56-
try {
57-
// Convert base64 to forge-compatible format
58-
const key = forge.util.decode64(keyBase64);
59-
const iv = forge.util.decode64(ivBase64);
60-
const encryptedDataWithTag = forge.util.decode64(encryptedBase64);
61-
62-
// For node-forge GCM, we need to manually handle the auth tag
63-
// Web Crypto API embeds the auth tag at the end of the encrypted data
64-
const tagLength = ENCRYPTION_CONFIG.GCM_TAG_LENGTH;
65-
66-
if (encryptedDataWithTag.length < tagLength) {
67-
throw new Error(
68-
`Encrypted data too short for GCM (${encryptedDataWithTag.length} bytes, need at least ${tagLength})`
69-
);
70-
}
71-
72-
// Split the data: ciphertext + auth tag (last 16 bytes)
73-
const ciphertext = encryptedDataWithTag.slice(0, -tagLength);
74-
const authTag = encryptedDataWithTag.slice(-tagLength);
75-
76-
// Create AES-GCM decipher
77-
const decipher = forge.cipher.createDecipher('AES-GCM', key);
78-
79-
// Start decryption with IV and auth tag
80-
decipher.start({
81-
iv,
82-
tag: authTag,
83-
});
84-
85-
// Update with ciphertext
86-
decipher.update(forge.util.createBuffer(ciphertext));
87-
88-
// Finish and verify auth tag
89-
const success = decipher.finish();
90-
91-
if (!success) {
92-
throw new Error('GCM authentication failed - auth tag verification failed');
93-
}
94-
95-
const decryptedText = decipher.output.toString('utf8');
96-
return decryptedText;
97-
} catch (error) {
98-
throw new Error(`AES-GCM decryption failed: ${error}`);
52+
// Convert base64 to forge-compatible format
53+
const key = forge.util.decode64(keyBase64);
54+
const iv = forge.util.decode64(ivBase64);
55+
const encryptedDataWithTag = forge.util.decode64(encryptedBase64);
56+
57+
// For node-forge GCM, we need to manually handle the auth tag
58+
// Web Crypto API embeds the auth tag at the end of the encrypted data
59+
const tagLength = ENCRYPTION_CONFIG.GCM_TAG_LENGTH;
60+
61+
if (encryptedDataWithTag.length < tagLength) {
62+
throw new Error(
63+
`Encrypted data too short for GCM (${encryptedDataWithTag.length} bytes, need at least ${tagLength})`
64+
);
9965
}
66+
67+
// Split the data: ciphertext + auth tag (last 16 bytes)
68+
const ciphertext = encryptedDataWithTag.slice(0, -tagLength);
69+
const authTag = encryptedDataWithTag.slice(-tagLength);
70+
71+
// Create AES-GCM decipher
72+
const decipher = forge.cipher.createDecipher('AES-GCM', key);
73+
74+
// Start decryption with IV and auth tag
75+
decipher.start({
76+
iv: forge.util.createBuffer(iv),
77+
tag: forge.util.createBuffer(authTag),
78+
});
79+
80+
// Update with ciphertext
81+
decipher.update(forge.util.createBuffer(ciphertext));
82+
83+
// Finish and verify auth tag
84+
const success = decipher.finish();
85+
86+
if (!success) {
87+
throw new Error('GCM authentication failed - auth tag verification failed');
88+
}
89+
90+
return decipher.output.toString();
10091
}

apps/mobile/v1/src/lib/encryption/core/crypto.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,6 @@
33
* Conversion and encoding helpers
44
*/
55

6-
export function arrayBufferToBase64(buffer: ArrayBuffer): string {
7-
const bytes = new Uint8Array(buffer);
8-
let binary = '';
9-
for (let i = 0; i < bytes.byteLength; i++) {
10-
binary += String.fromCharCode(bytes[i]);
11-
}
12-
return btoa(binary);
13-
}
14-
15-
export function base64ToUint8Array(base64: string): Uint8Array {
16-
const binary = atob(base64);
17-
const bytes = new Uint8Array(binary.length);
18-
for (let i = 0; i < binary.length; i++) {
19-
bytes[i] = binary.charCodeAt(i);
20-
}
21-
return bytes;
22-
}
23-
24-
export function uint8ArrayToString(bytes: Uint8Array): string {
25-
const decoder = new TextDecoder('utf-8');
26-
return decoder.decode(bytes);
27-
}
28-
296
export function stringToUint8Array(str: string): Uint8Array {
307
const encoder = new TextEncoder();
318
return encoder.encode(str);

0 commit comments

Comments
 (0)