@@ -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,26 @@ 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 < { remainingTime : number , cachedPassword ?: string } > {
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
+ const expiresAt = savedPasswords [ address ] ?. expiresAt ?? 0 ;
141
+ const remainingTime = expiresAt - Date . now ( ) ;
142
+ const cachedPassword = savedPasswords [ address ] ?. password ;
145
143
146
- if ( remainingTime < 0 ) {
147
- this . #cachedUnlocks [ address ] = 0 ;
148
- pair . lock ( ) ;
144
+ if ( remainingTime > 0 ) {
145
+ return { remainingTime , cachedPassword } ;
146
+ }
149
147
150
- return 0 ;
148
+ if ( address in savedPasswords ) {
149
+ delete savedPasswords [ address ] ;
150
+ await chromeStorage . setItem ( 'password-cache' , ( ) => ( savedPasswords ) ) ;
151
151
}
152
152
153
- return remainingTime ;
153
+ pair . lock ( ) ;
154
+
155
+ return { remainingTime : 0 } ;
154
156
}
155
157
156
158
private accountsShow ( { address, isShowing } : RequestAccountShow ) : boolean {
@@ -334,7 +336,7 @@ export default class Extension {
334
336
} ;
335
337
}
336
338
337
- private async signingApprovePassword ( { id, password, savePass } : RequestSigningApprovePassword , getContentPort : GetContentPort ) : Promise < void > {
339
+ private async signingApprovePassword ( { id, password : inputPassword , savePass } : RequestSigningApprovePassword , getContentPort : GetContentPort ) : Promise < void > {
338
340
const queued = await this . #state. getSignRequest ( id ) ;
339
341
340
342
assert ( queued , 'Unable to find request' ) ;
@@ -351,10 +353,12 @@ export default class Extension {
351
353
throw error ;
352
354
}
353
355
354
- this . refreshAccountPasswordCache ( pair ) ;
356
+ const { cachedPassword, remainingTime } = await this . refreshAccountPasswordCache ( pair ) ;
357
+
358
+ const password = cachedPassword ?? inputPassword ;
355
359
356
360
// if the keyring pair is locked, the password is needed
357
- if ( pair . isLocked && ! password ) {
361
+ if ( ! password ) {
358
362
const error = new Error ( 'Password needed to unlock the account' ) ;
359
363
360
364
await this . #state. removeSignRequest ( id ) ;
@@ -408,13 +412,14 @@ export default class Extension {
408
412
. createType ( 'ExtrinsicPayload' , payload , { version : payload . version } )
409
413
. sign ( pair ) ;
410
414
411
- if ( savePass ) {
415
+ if ( savePass && remainingTime <= 0 ) {
412
416
// unlike queued.account.address the following
413
417
// address is encoded with the default prefix
414
418
// which what is used for password caching mapping
415
- this . #cachedUnlocks[ pair . address ] = Date . now ( ) + PASSWORD_EXPIRY_MS ;
416
- } else {
417
- pair . lock ( ) ;
419
+
420
+ await chromeStorage . setItem ( 'password-cache' , ( savedPasswords ) => (
421
+ { ...savedPasswords , [ pair . address ] : { password, expiresAt : Date . now ( ) + PASSWORD_EXPIRY_MS } }
422
+ ) ) ;
418
423
}
419
424
420
425
await this . #state. removeSignRequest ( id ) ;
@@ -449,11 +454,11 @@ export default class Extension {
449
454
450
455
assert ( pair , 'Unable to find pair' ) ;
451
456
452
- const remainingTime = this . refreshAccountPasswordCache ( pair ) ;
457
+ const { remainingTime } = await this . refreshAccountPasswordCache ( pair ) ;
453
458
454
459
return {
455
- isLocked : pair . isLocked ,
456
- remainingTime
460
+ remainingTime ,
461
+ isLocked : remainingTime <= 0
457
462
} ;
458
463
}
459
464
0 commit comments