@@ -6,26 +6,25 @@ import { SERVICE_TYPES, APPLICATION_STATE, SERVER_URL } from "statics";
66import { Response as Request } from "api/types" ;
77import { removeQueryParam } from "helpers" ;
88import { Sender , SendResponseInterface } from "../types" ;
9+ import {
10+ hasPrivateKeySelector ,
11+ privateKeySelector ,
12+ store ,
13+ logIn ,
14+ logOut ,
15+ mnemonicPhraseSelector ,
16+ publicKeySelector ,
17+ } from "../ducks/session" ;
18+ import { SessionTimer } from "../helpers/session" ;
919
1020const server = new StellarSdk . Server ( SERVER_URL ) ;
1121
12- let KEY_STORE : { privateKey : string } | null = null ;
13-
14- interface UiData {
15- publicKey : string ;
16- mnemonicPhrase : string ;
17- [ key : string ] : string ;
18- }
19-
20- export const uiData : UiData = {
21- publicKey : "" ,
22- mnemonicPhrase : "" ,
23- } ;
24-
2522const KEY_ID = "keyId" ;
2623const WHITELIST_ID = "whitelist" ;
2724const APPLICATION_ID = "applicationState" ;
2825
26+ const sessionTimer = new SessionTimer ( ) ;
27+
2928export const responseQueue : Array < ( message ?: any ) => void > = [ ] ;
3029export const transactionQueue : Array < { sign : ( sourceKeys : { } ) => void } > = [ ] ;
3130
@@ -55,14 +54,17 @@ const internalMessageListener = (
5554 password : string ;
5655 wallet : StellarHdWallet ;
5756 } ) => {
58- uiData . publicKey = wallet . getPublicKey ( 0 ) ;
57+ const publicKey = wallet . getPublicKey ( 0 ) ;
58+ const privateKey = wallet . getSecret ( 0 ) ;
59+
60+ store . dispatch ( logIn ( { publicKey, mnemonicPhrase } ) ) ;
5961
6062 const keyMetadata = {
6163 key : {
6264 extra : { mnemonicPhrase } ,
6365 type : KeyType . plaintextKey ,
64- publicKey : uiData . publicKey ,
65- privateKey : wallet . getSecret ( 0 ) ,
66+ publicKey,
67+ privateKey,
6668 } ,
6769
6870 password,
@@ -83,8 +85,8 @@ const internalMessageListener = (
8385 const createAccount = async ( ) => {
8486 const { password } = request ;
8587
86- uiData . mnemonicPhrase = generateMnemonic ( { entropyBits : 128 } ) ;
87- const wallet = fromMnemonic ( uiData . mnemonicPhrase ) ;
88+ const mnemonicPhrase = generateMnemonic ( { entropyBits : 128 } ) ;
89+ const wallet = fromMnemonic ( mnemonicPhrase ) ;
8890
8991 try {
9092 const response = await fetch (
@@ -99,30 +101,31 @@ const internalMessageListener = (
99101 throw new Error ( "Error creating account" ) ;
100102 }
101103
102- _storeAccount ( {
104+ await _storeAccount ( {
103105 password,
104106 wallet,
105- mnemonicPhrase : uiData . mnemonicPhrase ,
107+ mnemonicPhrase,
106108 } ) ;
107109 localStorage . setItem ( APPLICATION_ID , APPLICATION_STATE . PASSWORD_CREATED ) ;
108110
109- sendResponse ( { publicKey : uiData . publicKey } ) ;
111+ sendResponse ( { publicKey : publicKeySelector ( store . getState ( ) ) } ) ;
110112 } ;
111113
112114 const loadAccount = ( ) => {
113115 sendResponse ( {
114- publicKey : uiData . publicKey ,
116+ publicKey : publicKeySelector ( store . getState ( ) ) ,
115117 applicationState : localStorage . getItem ( APPLICATION_ID ) || "" ,
116118 } ) ;
117119 } ;
118120
119121 const getMnemonicPhrase = ( ) => {
120- sendResponse ( { mnemonicPhrase : uiData . mnemonicPhrase } ) ;
122+ sendResponse ( { mnemonicPhrase : mnemonicPhraseSelector ( store . getState ( ) ) } ) ;
121123 } ;
122124
123125 const confirmMnemonicPhrase = ( ) => {
124126 const isCorrectPhrase =
125- uiData . mnemonicPhrase === request . mnemonicPhraseToConfirm ;
127+ mnemonicPhraseSelector ( store . getState ( ) ) ===
128+ request . mnemonicPhraseToConfirm ;
126129
127130 const applicationState = isCorrectPhrase
128131 ? APPLICATION_STATE . MNEMONIC_PHRASE_CONFIRMED
@@ -158,13 +161,14 @@ const internalMessageListener = (
158161 }
159162
160163 sendResponse ( {
161- publicKey : uiData . publicKey ,
164+ publicKey : publicKeySelector ( store . getState ( ) ) ,
162165 applicationState : localStorage . getItem ( APPLICATION_ID ) || "" ,
163166 } ) ;
164167 } ;
165168
166169 const confirmPassword = async ( ) => {
167170 const { password } = request ;
171+ const state = store . getState ( ) ;
168172 let keyStore ;
169173 try {
170174 keyStore = await keyManager . loadKey (
@@ -175,14 +179,16 @@ const internalMessageListener = (
175179 console . error ( e ) ;
176180 }
177181 let publicKey = "" ;
182+ let privateKey = "" ;
178183 if ( keyStore ) {
179- ( { publicKey } = keyStore ) ;
180- uiData . publicKey = publicKey ;
181- KEY_STORE = keyStore ;
184+ ( { privateKey , publicKey } = keyStore ) ;
185+ store . dispatch ( logIn ( { publicKey } ) ) ;
186+ sessionTimer . startSession ( { privateKey } ) ;
182187 }
183188
184189 sendResponse ( {
185- publicKey : uiData . publicKey ,
190+ publicKey : publicKeySelector ( state ) ,
191+ hasPrivateKey : hasPrivateKeySelector ( state ) ,
186192 applicationState : localStorage . getItem ( APPLICATION_ID ) || "" ,
187193 } ) ;
188194 } ;
@@ -216,8 +222,8 @@ const internalMessageListener = (
216222 } ;
217223
218224 const signTransaction = async ( ) => {
219- if ( KEY_STORE ) {
220- const { privateKey } = KEY_STORE ;
225+ const privateKey = privateKeySelector ( store . getState ( ) ) ;
226+ if ( privateKey . length ) {
221227 const sourceKeys = StellarSdk . Keypair . fromSecret ( privateKey ) ;
222228
223229 let response ;
@@ -239,6 +245,8 @@ const internalMessageListener = (
239245 transactionResponse ( response ) ;
240246 sendResponse ( { } ) ;
241247 }
248+ } else {
249+ sendResponse ( { error : "Session timed out" } ) ;
242250 }
243251 } ;
244252
@@ -251,12 +259,10 @@ const internalMessageListener = (
251259 } ;
252260
253261 const signOut = ( ) => {
254- Object . keys ( uiData ) . forEach ( ( key ) => {
255- uiData [ key ] = "" ;
256- } ) ;
262+ store . dispatch ( logOut ( ) ) ;
257263
258264 sendResponse ( {
259- publicKey : uiData . publicKey ,
265+ publicKey : publicKeySelector ( store . getState ( ) ) ,
260266 applicationState : localStorage . getItem ( APPLICATION_ID ) || "" ,
261267 } ) ;
262268 } ;
0 commit comments