1- import { EXPLORER_URL } from '../../config' ;
2- import { FROM_BACK_TO_RECORD , TX_SUCCESS } from '../constant/types' ;
1+ import { EXPLORER_URL , LOCK_TIME } from '../../config' ;
2+ import { FROM_BACK_TO_RECORD , SET_LOCK , TX_SUCCESS } from '../constant/types' ;
33import { getLanguage } from '../i18n' ;
44import { getTxStatus , sendStakeTx , sendTx } from './api' ;
55import { signPayment , stakePayment } from './lib' ;
66import { get , removeValue , save } from './storageService' ;
7- import { ACCOUNT_TYPE } from "../constant/walletType"
7+ import { ACCOUNT_TYPE } from "../constant/walletType"
88import extension from 'extensionizer'
99
1010const ObservableStore = require ( 'obs-store' )
@@ -26,20 +26,26 @@ class APIService {
2626 data : '' ,
2727 password : '' ,
2828 currentAccount : { } ,
29- mne :""
29+ mne : ""
3030 } )
3131 }
3232 getStore = ( ) => {
3333 return this . memStore . getState ( )
3434 } ;
35- getCreateMnemonic = ( ) => {
36- let mne = this . getStore ( ) . mne
37- if ( mne ) {
38- return mne
39- } else {
35+ getCreateMnemonic = ( isNewMne ) => {
36+ if ( isNewMne ) {
4037 let mnemonic = generateMne ( )
4138 this . memStore . updateState ( { mne : mnemonic } )
4239 return mnemonic
40+ } else {
41+ let mne = this . getStore ( ) . mne
42+ if ( mne ) {
43+ return mne
44+ } else {
45+ let mnemonic = generateMne ( )
46+ this . memStore . updateState ( { mne : mnemonic } )
47+ return mnemonic
48+ }
4349 }
4450 }
4551 filterCurrentAccount = ( accountList , currentAddress ) => {
@@ -62,16 +68,41 @@ class APIService {
6268 } )
6369 return this . getAccountWithoutPrivate ( currentAccount )
6470 } catch ( error ) {
65- return { error : 'passwordError' , type :"local" }
71+ return { error : 'passwordError' , type : "local" }
6672 }
6773 } ;
6874 checkPassword ( password ) {
6975 return this . getStore ( ) . password === password
7076 }
77+ setLastActiveTime ( ) {
78+ const timeoutMinutes = LOCK_TIME
79+ let localData = this . getStore ( ) . data
80+ let isUnlocked = this . getStore ( ) . isUnlocked
81+ if ( localData && isUnlocked ) {
82+ if ( this . timer ) {
83+ clearTimeout ( this . timer )
84+ }
85+ if ( ! timeoutMinutes ) {
86+ return
87+ }
88+
89+ this . timer = setTimeout ( ( ) => {
90+ this . setUnlockedStatus ( false )
91+ } , timeoutMinutes * 60 * 1000 )
92+ }
93+
94+ }
7195 setUnlockedStatus ( status ) {
7296 let account = this . memStore . getState ( ) . currentAccount
7397 if ( ! status ) {
74- this . memStore . updateState ( { currentAccount : { ...account } } )
98+ this . memStore . updateState ( {
99+ currentAccount : { ...account } ,
100+ password : ""
101+ } )
102+ extension . runtime . sendMessage ( {
103+ type : FROM_BACK_TO_RECORD ,
104+ action : SET_LOCK ,
105+ } ) ;
75106 }
76107 this . memStore . updateState ( { isUnlocked : status } )
77108 } ;
@@ -177,7 +208,7 @@ class APIService {
177208 for ( let index = 0 ; index < accounts . length ; index ++ ) {
178209 const account = accounts [ index ] ;
179210 if ( account . address === address ) {
180- error = { "error" : 'improtRepeat' , type :"local" }
211+ error = { "error" : 'improtRepeat' , type : "local" }
181212 break
182213 }
183214 }
@@ -227,7 +258,7 @@ class APIService {
227258 this . memStore . updateState ( { currentAccount : account } )
228259 return this . getAccountWithoutPrivate ( account )
229260 } catch ( error ) {
230- return { "error" : "privateError" , type :"local" }
261+ return { "error" : "privateError" , type : "local" }
231262 }
232263
233264 }
@@ -238,45 +269,45 @@ class APIService {
238269 * @param {* } accountName
239270 * @returns
240271 */
241- addAccountByKeyStore = async ( keystore , password , accountName ) => {
242- let wallet = await importWalletByKeystore ( keystore , password )
243- if ( wallet . error ) {
272+ addAccountByKeyStore = async ( keystore , password , accountName ) => {
273+ let wallet = await importWalletByKeystore ( keystore , password )
274+ if ( wallet . error ) {
244275 return wallet
245276 }
246- let currentAccount = await this . addImportAccount ( wallet . priKey , accountName )
277+ let currentAccount = await this . addImportAccount ( wallet . priKey , accountName )
247278 return currentAccount
248279 }
249280 addWatchModeAccount = async ( address , accountName ) => {
250- try {
251- let data = this . getStore ( ) . data
252- let accounts = data [ 0 ] . accounts
253- let error = this . _checkWalletRepeat ( accounts , address ) ;
254- if ( error . error ) {
255- return error
256- }
257- let typeIndex = this . _findWalletIndex ( accounts , ACCOUNT_TYPE . WALLET_WATCH ) ;
258- const account = {
259- address : address ,
260- type : ACCOUNT_TYPE . WALLET_WATCH ,
261- accountName,
262- typeIndex
263- }
264- data [ 0 ] . currentAddress = account . address
265- data [ 0 ] . accounts . push ( account )
266- let encryptData = await encryptUtils . encrypt ( this . getStore ( ) . password , data )
281+ try {
282+ let data = this . getStore ( ) . data
283+ let accounts = data [ 0 ] . accounts
284+ let error = this . _checkWalletRepeat ( accounts , address ) ;
285+ if ( error . error ) {
286+ return error
287+ }
288+ let typeIndex = this . _findWalletIndex ( accounts , ACCOUNT_TYPE . WALLET_WATCH ) ;
289+ const account = {
290+ address : address ,
291+ type : ACCOUNT_TYPE . WALLET_WATCH ,
292+ accountName,
293+ typeIndex
294+ }
295+ data [ 0 ] . currentAddress = account . address
296+ data [ 0 ] . accounts . push ( account )
297+ let encryptData = await encryptUtils . encrypt ( this . getStore ( ) . password , data )
267298
268- this . memStore . updateState ( { data : data } )
269- save ( { keyringData : encryptData } )
270- this . memStore . updateState ( { currentAccount : account } )
271- return this . getAccountWithoutPrivate ( account )
272- } catch ( error ) {
273- return { "error" : JSON . stringify ( error ) }
274- }
299+ this . memStore . updateState ( { data : data } )
300+ save ( { keyringData : encryptData } )
301+ this . memStore . updateState ( { currentAccount : account } )
302+ return this . getAccountWithoutPrivate ( account )
303+ } catch ( error ) {
304+ return { "error" : JSON . stringify ( error ) }
305+ }
275306 }
276307 /**
277308 * 导入ledger钱包
278309 */
279- addLedgerAccount = async ( address , accountName , ledgerPathAccountIndex ) => {
310+ addLedgerAccount = async ( address , accountName , ledgerPathAccountIndex ) => {
280311 try {
281312 let data = this . getStore ( ) . data
282313 let accounts = data [ 0 ] . accounts
@@ -302,7 +333,7 @@ class APIService {
302333 this . memStore . updateState ( { currentAccount : account } )
303334 return this . getAccountWithoutPrivate ( account )
304335 } catch ( error ) {
305- return { "error" : JSON . stringify ( error ) }
336+ return { "error" : JSON . stringify ( error ) }
306337 }
307338 }
308339 /**
@@ -361,7 +392,7 @@ class APIService {
361392 return item . address !== address
362393 } )
363394 let currentAccount = this . getStore ( ) . currentAccount
364- if ( address === currentAccount . address ) {
395+ if ( address === currentAccount . address ) {
365396 currentAccount = accounts [ 0 ]
366397 data [ 0 ] . currentAddress = currentAccount . address
367398 }
@@ -371,7 +402,7 @@ class APIService {
371402 save ( { keyringData : encryptData } )
372403 return this . getAccountWithoutPrivate ( currentAccount )
373404 } else {
374- return { error : 'passwordError' , type :"local" }
405+ return { error : 'passwordError' , type : "local" }
375406 }
376407 }
377408 getMnemonic = async ( pwd ) => {
@@ -382,7 +413,7 @@ class APIService {
382413 let mnemonic = await encryptUtils . decrypt ( this . getStore ( ) . password , mnemonicEn )
383414 return mnemonic
384415 } else {
385- return { error : 'passwordError' , type :"local" }
416+ return { error : 'passwordError' , type : "local" }
386417 }
387418 }
388419 updateSecPassword = async ( oldPwd , pwd ) => {
@@ -417,10 +448,10 @@ class APIService {
417448 await save ( { keyringData : encryptData } )
418449 return { code : 0 }
419450 } else {
420- return { error : 'passwordError' , type :"local" }
451+ return { error : 'passwordError' , type : "local" }
421452 }
422453 } catch ( error ) {
423- return { error : 'passwordError' , type :"local" }
454+ return { error : 'passwordError' , type : "local" }
424455 }
425456
426457 }
@@ -436,7 +467,7 @@ class APIService {
436467 const privateKey = await encryptUtils . decrypt ( pwd , nowAccount . privateKey )
437468 return privateKey
438469 } else {
439- return { error : 'passwordError' , type :"local" }
470+ return { error : 'passwordError' , type : "local" }
440471 }
441472 }
442473 getCurrentPrivateKey = async ( ) => {
@@ -487,7 +518,7 @@ class APIService {
487518 notification = ( hash ) => {
488519 let id = hash
489520 extension . notifications &&
490- extension . notifications . onClicked . addListener ( function ( id ) {
521+ extension . notifications . onClicked . addListener ( function ( id ) {
491522 let url = EXPLORER_URL + id
492523 extension . tabs . create ( { url : url } ) ;
493524 } ) ;
@@ -518,7 +549,7 @@ class APIService {
518549 if ( this . timer ) {
519550 clearTimeout ( this . timer ) ;
520551 this . timer = null ;
521- }
552+ }
522553 } else {
523554 this . timer = setTimeout ( ( ) => {
524555 this . fetchTransactionStatus ( paymentId , hash ) ;
0 commit comments