@@ -18,13 +18,12 @@ import { accounts as accountsObservable } from '@polkadot/ui-keyring/observable/
18
18
import { assert , isHex , u8aToHex } from '@polkadot/util' ;
19
19
import { keyExtractSuri , mnemonicGenerate , mnemonicValidate } from '@polkadot/util-crypto' ;
20
20
21
+ import chromeStorage from './chromeStorage' ;
21
22
import { POPUP_CREATE_WINDOW_DATA } from './consts' ;
22
23
import { openCenteredWindow } from './helpers' ;
23
24
import State from './State' ;
24
25
import { createSubscription , unsubscribe } from './subscriptions' ;
25
26
26
- type CachedUnlocks = Record < string , number > ;
27
-
28
27
type GetContentPort = ( tabId : number ) => chrome . runtime . Port
29
28
30
29
const SEED_DEFAULT_LENGTH = 12 ;
@@ -40,12 +39,9 @@ function isJsonPayload (value: SignerPayloadJSON | SignerPayloadRaw): value is S
40
39
}
41
40
42
41
export default class Extension {
43
- readonly #cachedUnlocks: CachedUnlocks ;
44
-
45
42
readonly #state: State ;
46
43
47
44
constructor ( state : State ) {
48
- this . #cachedUnlocks = { } ;
49
45
this . #state = state ;
50
46
}
51
47
@@ -137,20 +133,27 @@ export default class Extension {
137
133
return true ;
138
134
}
139
135
140
- private refreshAccountPasswordCache ( pair : KeyringPair ) : number {
136
+ private async refreshAccountPasswordCache ( pair : KeyringPair ) : Promise < number > {
141
137
const { address } = pair ;
142
138
143
- const savedExpiry = this . #cachedUnlocks[ address ] || 0 ;
144
- const remainingTime = savedExpiry - Date . now ( ) ;
139
+ const savedPasswords = await chromeStorage . getItem ( 'password-cache' ) ;
140
+
141
+ if ( savedPasswords ) {
142
+ const remainingTime = savedPasswords [ address ] ?. expiresAt - Date . now ( ) ;
143
+
144
+ if ( remainingTime < 0 ) {
145
+ delete savedPasswords [ address ] ;
145
146
146
- if ( remainingTime < 0 ) {
147
- this . #cachedUnlocks[ address ] = 0 ;
148
- pair . lock ( ) ;
147
+ await chromeStorage . setItem ( 'password-cache' , ( ) => ( savedPasswords ) ) ;
148
+ pair . lock ( ) ;
149
+
150
+ return 0 ;
151
+ }
149
152
150
- return 0 ;
153
+ return remainingTime ;
151
154
}
152
155
153
- return remainingTime ;
156
+ return 0 ;
154
157
}
155
158
156
159
private accountsShow ( { address, isShowing } : RequestAccountShow ) : boolean {
@@ -334,14 +337,18 @@ export default class Extension {
334
337
} ;
335
338
}
336
339
337
- private async signingApprovePassword ( { id, password, savePass } : RequestSigningApprovePassword , getContentPort : GetContentPort ) : Promise < void > {
340
+ private async signingApprovePassword ( { id, password : inputPassword , savePass } : RequestSigningApprovePassword , getContentPort : GetContentPort ) : Promise < void > {
338
341
const queued = await this . #state. getSignRequest ( id ) ;
339
342
340
343
assert ( queued , 'Unable to find request' ) ;
341
344
342
345
const { payload } = queued ;
343
346
const pair = keyring . getPair ( queued . account . address ) ;
344
347
348
+ const savedPasswords = await chromeStorage . getItem ( 'password-cache' ) ;
349
+
350
+ const password = savedPasswords && savedPasswords [ pair . address ] ?. expiresAt - Date . now ( ) > 0 ? savedPasswords [ pair . address ] . password : inputPassword ;
351
+
345
352
if ( ! pair ) {
346
353
const error = new Error ( 'Unable to find pair' ) ;
347
354
@@ -351,7 +358,7 @@ export default class Extension {
351
358
throw error ;
352
359
}
353
360
354
- this . refreshAccountPasswordCache ( pair ) ;
361
+ await this . refreshAccountPasswordCache ( pair ) ;
355
362
356
363
// if the keyring pair is locked, the password is needed
357
364
if ( pair . isLocked && ! password ) {
@@ -408,13 +415,20 @@ export default class Extension {
408
415
. createType ( 'ExtrinsicPayload' , payload , { version : payload . version } )
409
416
. sign ( pair ) ;
410
417
411
- if ( savePass ) {
418
+ if ( savePass && password ) {
412
419
// unlike queued.account.address the following
413
420
// address is encoded with the default prefix
414
421
// which what is used for password caching mapping
415
- this . #cachedUnlocks[ pair . address ] = Date . now ( ) + PASSWORD_EXPIRY_MS ;
422
+
423
+ await chromeStorage . setItem ( 'password-cache' , ( savedPasswords ) => ( { ...savedPasswords , [ pair . address ] : { password, expiresAt : Date . now ( ) + PASSWORD_EXPIRY_MS } } ) ) ;
416
424
} else {
417
- pair . lock ( ) ;
425
+ const savedPasswords = await chromeStorage . getItem ( 'password-cache' ) ;
426
+
427
+ const remainingTime = ( savedPasswords && savedPasswords [ pair . address ] ?. expiresAt - Date . now ( ) ) || 0 ;
428
+
429
+ if ( remainingTime <= 0 ) {
430
+ pair . lock ( ) ;
431
+ }
418
432
}
419
433
420
434
await this . #state. removeSignRequest ( id ) ;
@@ -449,7 +463,7 @@ export default class Extension {
449
463
450
464
assert ( pair , 'Unable to find pair' ) ;
451
465
452
- const remainingTime = this . refreshAccountPasswordCache ( pair ) ;
466
+ const remainingTime = await this . refreshAccountPasswordCache ( pair ) ;
453
467
454
468
return {
455
469
isLocked : pair . isLocked ,
0 commit comments