@@ -7,7 +7,7 @@ import type {
7
7
AccountType ,
8
8
IAccount ,
9
9
IAccountRaw ,
10
- IKey ,
10
+ IEncryptionData ,
11
11
IFormSelectOption ,
12
12
Protocol ,
13
13
ProtocolRecord ,
@@ -31,8 +31,9 @@ import {
31
31
encrypt ,
32
32
endSession ,
33
33
excludeFalsy ,
34
- generateKey ,
35
- getSessionKey ,
34
+ restoreEncryptionData ,
35
+ initializeEncryptionData ,
36
+ getSessionEncryptionData ,
36
37
prepareAccountSelectOptions ,
37
38
startSession ,
38
39
watchUntilTruthy ,
@@ -60,7 +61,7 @@ const {
60
61
61
62
const areAccountsRestored = ref ( false ) ;
62
63
const isMnemonicRestored = ref ( false ) ;
63
- const passwordKey = ref < IKey | null > ( null ) ;
64
+ const encryptionData = ref < IEncryptionData | null > ( ) ;
64
65
65
66
const mnemonic = useSecureStorageRef < string > (
66
67
'' ,
@@ -73,7 +74,7 @@ const mnemonic = useSecureStorageRef<string>(
73
74
] ,
74
75
onRestored : async ( val ) => {
75
76
const hasStoredMnemonic = (
76
- await WalletStorage . get ( STORAGE_KEYS . mnemonic )
77
+ WalletStorage . get ( STORAGE_KEYS . mnemonic )
77
78
|| await SecureMobileStorage . get ( STORAGE_KEYS . mnemonic )
78
79
) ;
79
80
isMnemonicRestored . value = ! ! val || ! hasStoredMnemonic ;
@@ -252,22 +253,27 @@ export function useAccounts() {
252
253
/**
253
254
* Setting/Resetting the password key logs the user in/out.
254
255
*/
255
- function setPasswordKey ( key : IKey | null ) {
256
- passwordKey . value = key ;
256
+ function setEncryptionData ( newEncryptionData : IEncryptionData | null ) {
257
+ encryptionData . value = newEncryptionData ;
257
258
if ( IS_EXTENSION ) {
258
- if ( key ) {
259
- startSession ( key , secureLoginTimeout . value ) ;
259
+ if ( newEncryptionData ) {
260
+ startSession ( newEncryptionData , secureLoginTimeout . value ) ;
260
261
} else {
261
262
endSession ( ) ;
262
263
}
263
264
}
264
265
}
265
266
267
+ async function getEncryptionData ( ) {
268
+ await watchUntilTruthy ( encryptionData ) ;
269
+ return encryptionData . value ;
270
+ }
271
+
266
272
async function openLoginModal ( ) {
267
273
setLoaderVisible ( true ) ;
268
- const sessionKey = await getSessionKey ( ) ;
269
- if ( sessionKey ) {
270
- setPasswordKey ( sessionKey ) ;
274
+ const sessionEncryptionData = await getSessionEncryptionData ( ) ;
275
+ if ( sessionEncryptionData ) {
276
+ setEncryptionData ( sessionEncryptionData ) ;
271
277
const { getAeSdk } = useAeSdk ( ) ;
272
278
await getAeSdk ( ) ;
273
279
setLoaderVisible ( false ) ;
@@ -278,26 +284,27 @@ export function useAccounts() {
278
284
const { openModal } = useModals ( ) ;
279
285
280
286
await openModal ( MODAL_PASSWORD_LOGIN ) ;
281
- if ( ! passwordKey . value ) {
282
- throw new Error ( 'passwordKey was not set after login.' ) ;
287
+ if ( ! encryptionData . value ) {
288
+ throw new Error ( 'encryptionData was not set after login.' ) ;
283
289
}
284
290
}
285
291
286
- async function setPassword ( newMnemonic : string , password : string ) {
287
- const key = await generateKey ( password ) ;
288
- const encryptedMnemonic = await encrypt ( key , newMnemonic ) ;
292
+ async function setPasswordAndEncryptMnemonic ( newMnemonic : string , password : string ) {
293
+ const newEncryptionData = await initializeEncryptionData ( password ) ;
294
+ const encryptedMnemonic = await encrypt ( newEncryptionData , newMnemonic ) ;
289
295
WalletStorage . set ( STORAGE_KEYS . mnemonic , encryptedMnemonic ) ;
290
-
291
- // Saved key needs to be generated from the password and the encrypted mnemonic
292
- setPasswordKey ( await generateKey ( password , encryptedMnemonic ) ) ;
296
+ setEncryptionData ( newEncryptionData ) ;
293
297
}
294
298
295
299
async function updatePassword ( currentPassword : string , newPassword : string ) {
296
- const encryptedMnemonic = await WalletStorage . get < string > ( STORAGE_KEYS . mnemonic ) || undefined ;
297
- const key = await generateKey ( currentPassword , encryptedMnemonic ) ;
298
- const decryptedMnemonic = await decrypt ( key , encryptedMnemonic ! ) ;
300
+ const encryptedMnemonic = WalletStorage . get < string > ( STORAGE_KEYS . mnemonic ) ;
301
+ if ( ! encryptedMnemonic ) {
302
+ throw new Error ( 'Mnemonic not found.' ) ;
303
+ }
304
+ const restoredEncryptionData = await restoreEncryptionData ( currentPassword , encryptedMnemonic ) ;
305
+ const decryptedMnemonic = await decrypt ( restoredEncryptionData , encryptedMnemonic ) ;
299
306
if ( decryptedMnemonic ) {
300
- await setPassword ( decryptedMnemonic , newPassword ) ;
307
+ await setPasswordAndEncryptMnemonic ( decryptedMnemonic , newPassword ) ;
301
308
} else {
302
309
throw new Error ( 'Incorrect password.' ) ;
303
310
}
@@ -310,10 +317,10 @@ export function useAccounts() {
310
317
isRestoredWallet : isRestored ,
311
318
} ) ;
312
319
313
- await setPassword ( newMnemonic , password ) ;
320
+ await setPasswordAndEncryptMnemonic ( newMnemonic , password ) ;
314
321
}
315
322
316
- async function setMnemonic ( newMnemonic : string , isRestored = false ) {
323
+ async function setMnemonicAndInitializePassword ( newMnemonic : string , isRestored = false ) {
317
324
if ( ! IS_MOBILE_APP ) {
318
325
await openSetPasswordModal ( newMnemonic , isRestored ) . catch ( ( ) => {
319
326
throw new Error ( 'Password was not set.' ) ;
@@ -323,7 +330,7 @@ export function useAccounts() {
323
330
}
324
331
325
332
async function setGeneratedMnemonic ( ) {
326
- await setMnemonic ( generateMnemonic ( ) ) . catch ( ( ) => {
333
+ await setMnemonicAndInitializePassword ( generateMnemonic ( ) ) . catch ( ( ) => {
327
334
throw new Error ( 'Mnemonic was not set.' ) ;
328
335
} ) ;
329
336
}
@@ -366,12 +373,12 @@ export function useAccounts() {
366
373
activeAccountGlobalIdx . value = 0 ;
367
374
}
368
375
369
- async function waitForSessionKey ( ) {
376
+ async function waitForSessionEncryptionData ( ) {
370
377
await new Promise < void > ( ( resolve ) => {
371
378
const interval = setInterval ( async ( ) => {
372
- const key = await getSessionKey ( ) ;
373
- if ( key ) {
374
- setPasswordKey ( key ) ;
379
+ const sessionEncryptionData = await getSessionEncryptionData ( ) ;
380
+ if ( sessionEncryptionData ) {
381
+ setEncryptionData ( sessionEncryptionData ) ;
375
382
clearInterval ( interval ) ;
376
383
resolve ( ) ;
377
384
}
@@ -382,9 +389,9 @@ export function useAccounts() {
382
389
( async ( ) => {
383
390
if ( ! composableInitialized ) {
384
391
composableInitialized = true ;
385
- const storedMnemonic = await WalletStorage . get ( STORAGE_KEYS . mnemonic ) ;
392
+ const storedMnemonic = WalletStorage . get ( STORAGE_KEYS . mnemonic ) ;
386
393
if (
387
- ! passwordKey . value
394
+ ! encryptionData . value
388
395
&& ! IS_MOBILE_APP
389
396
// If the mnemonic is stored but is not valid as plaintext
390
397
// it means that user is trying to access an existing & encrypted wallet
@@ -393,8 +400,8 @@ export function useAccounts() {
393
400
await openLoginModal ( ) ;
394
401
}
395
402
396
- if ( IS_OFFSCREEN_TAB && ! passwordKey . value ) {
397
- await waitForSessionKey ( ) ;
403
+ if ( IS_OFFSCREEN_TAB && ! encryptionData . value ) {
404
+ await waitForSessionEncryptionData ( ) ;
398
405
return ;
399
406
}
400
407
@@ -420,13 +427,14 @@ export function useAccounts() {
420
427
isLoggedIn,
421
428
isActiveAccountAirGap,
422
429
mnemonic,
423
- passwordKey ,
430
+ encryptionData ,
424
431
mnemonicSeed,
425
432
protocolsInUse,
426
433
openLoginModal,
427
434
discoverAccounts,
428
435
isLocalAccountAddress,
429
436
addRawAccount,
437
+ getEncryptionData,
430
438
getAccountByAddress,
431
439
getAccountByGlobalIdx,
432
440
getLastActiveProtocolAccount,
@@ -435,8 +443,8 @@ export function useAccounts() {
435
443
setActiveAccountByGlobalIdx,
436
444
setActiveAccountByProtocolAndIdx,
437
445
setActiveAccountByProtocol,
438
- setMnemonic ,
439
- setPasswordKey ,
446
+ setMnemonicAndInitializePassword ,
447
+ setEncryptionData ,
440
448
updatePassword,
441
449
setGeneratedMnemonic,
442
450
resetAccounts,
0 commit comments