@@ -11,15 +11,13 @@ import type {
11
11
} from "../../wallet" ;
12
12
import type { StorageService } from "../storage/storage.service.types" ;
13
13
import type { Options } from "../../options.types" ;
14
- import type { ContractState , ModuleState , Store } from "../../store.types" ;
14
+ import type { ModuleState , Store } from "../../store.types" ;
15
15
import { EventEmitter } from "../event-emitter/event-emitter.service" ;
16
16
import type { WalletSelectorEvents } from "../../wallet-selector.types" ;
17
17
import { Logger , logger } from "../logger/logger.service" ;
18
18
import {
19
19
RECENTLY_SIGNED_IN_WALLETS ,
20
20
PACKAGE_NAME ,
21
- PENDING_CONTRACT ,
22
- PENDING_SELECTED_WALLET_ID ,
23
21
REMEMBER_RECENT_WALLETS ,
24
22
REMEMBER_RECENT_WALLETS_STATE ,
25
23
} from "../../constants" ;
@@ -77,49 +75,10 @@ export class WalletModules {
77
75
78
76
private async resolveStorageState ( ) {
79
77
const jsonStorage = new JsonStorage ( this . storage , PACKAGE_NAME ) ;
80
- const pendingSelectedWalletId = await jsonStorage . getItem < string > (
81
- PENDING_SELECTED_WALLET_ID
82
- ) ;
83
- const pendingContract = await jsonStorage . getItem < ContractState > (
84
- PENDING_CONTRACT
85
- ) ;
86
78
const rememberRecentWallets = await jsonStorage . getItem < string > (
87
79
REMEMBER_RECENT_WALLETS
88
80
) ;
89
81
90
- if ( pendingSelectedWalletId && pendingContract ) {
91
- const accounts = await this . validateWallet ( pendingSelectedWalletId ) ;
92
-
93
- await jsonStorage . removeItem ( PENDING_SELECTED_WALLET_ID ) ;
94
- await jsonStorage . removeItem ( PENDING_CONTRACT ) ;
95
-
96
- if ( accounts . length ) {
97
- const { selectedWalletId } = this . store . getState ( ) ;
98
- const selectedWallet = await this . getWallet ( selectedWalletId ) ;
99
-
100
- if ( selectedWallet && pendingSelectedWalletId !== selectedWalletId ) {
101
- await selectedWallet . signOut ( ) . catch ( ( err ) => {
102
- logger . log ( "Failed to sign out existing wallet" ) ;
103
- logger . error ( err ) ;
104
- } ) ;
105
- }
106
-
107
- let recentlySignedInWalletsFromPending : Array < string > = [ ] ;
108
- if ( rememberRecentWallets === REMEMBER_RECENT_WALLETS_STATE . ENABLED ) {
109
- recentlySignedInWalletsFromPending =
110
- await this . setWalletAsRecentlySignedIn ( pendingSelectedWalletId ) ;
111
- }
112
- return {
113
- accounts,
114
- contract : pendingContract ,
115
- selectedWalletId : pendingSelectedWalletId ,
116
- recentlySignedInWallets : recentlySignedInWalletsFromPending ,
117
- rememberRecentWallets :
118
- rememberRecentWallets || REMEMBER_RECENT_WALLETS_STATE . ENABLED ,
119
- } ;
120
- }
121
- }
122
-
123
82
const { contract, selectedWalletId } = this . store . getState ( ) ;
124
83
const accounts = await this . validateWallet ( selectedWalletId ) ;
125
84
@@ -188,21 +147,8 @@ export class WalletModules {
188
147
{ accounts, contractId, methodNames } : WalletEvents [ "signedIn" ]
189
148
) {
190
149
const { selectedWalletId, rememberRecentWallets } = this . store . getState ( ) ;
191
- const jsonStorage = new JsonStorage ( this . storage , PACKAGE_NAME ) ;
192
150
const contract = { contractId, methodNames } ;
193
151
194
- if ( ! accounts . length ) {
195
- const module = this . getModule ( walletId ) ! ;
196
- // We can't guarantee the user will actually sign in with browser wallets.
197
- // Best we can do is set in storage and validate on init.
198
- if ( module . type === "browser" ) {
199
- await jsonStorage . setItem ( PENDING_SELECTED_WALLET_ID , walletId ) ;
200
- await jsonStorage . setItem < ContractState > ( PENDING_CONTRACT , contract ) ;
201
- }
202
-
203
- return ;
204
- }
205
-
206
152
if ( selectedWalletId && selectedWalletId !== walletId ) {
207
153
await this . signOutWallet ( selectedWalletId ) ;
208
154
}
@@ -389,49 +335,66 @@ export class WalletModules {
389
335
}
390
336
391
337
async setup ( ) {
392
- const modules : Array < ModuleState > = [ ] ;
338
+ for ( let i = 0 ; i < this . factories . length ; i ++ ) {
339
+ const factory = this . factories [ i ] ;
340
+ factory ( { options : this . options } )
341
+ . then ( async ( module ) => {
342
+ // Filter out wallets that aren't available.
343
+ if ( ! module ) {
344
+ return ;
345
+ }
393
346
394
- for ( let i = 0 ; i < this . factories . length ; i += 1 ) {
395
- const module = await this . factories [ i ] ( { options : this . options } ) . catch (
396
- ( err ) => {
397
- logger . log ( "Failed to setup module" ) ;
398
- logger . error ( err ) ;
347
+ const moduleState = {
348
+ id : module . id ,
349
+ type : module . type ,
350
+ metadata : module . metadata ,
351
+ listIndex : i ,
352
+ wallet : async ( ) => {
353
+ let instance = this . instances [ module . id ] ;
399
354
400
- return null ;
401
- }
402
- ) ;
355
+ if ( instance ) {
356
+ return instance ;
357
+ }
403
358
404
- // Filter out wallets that aren't available.
405
- if ( ! module ) {
406
- continue ;
407
- }
359
+ instance = await this . setupInstance ( module ) ;
408
360
409
- // Skip duplicated module.
410
- if ( modules . some ( ( x ) => x . id === module . id ) ) {
411
- continue ;
412
- }
361
+ this . instances [ module . id ] = instance ;
413
362
414
- modules . push ( {
415
- id : module . id ,
416
- type : module . type ,
417
- metadata : module . metadata ,
418
- wallet : async ( ) => {
419
- let instance = this . instances [ module . id ] ;
363
+ return instance ;
364
+ } ,
365
+ } ;
420
366
421
- if ( instance ) {
422
- return instance ;
423
- }
367
+ this . modules . push ( moduleState ) ;
424
368
425
- instance = await this . setupInstance ( module ) ;
369
+ this . store . dispatch ( {
370
+ type : "ADD_WALLET_MODULE" ,
371
+ payload : {
372
+ module : moduleState ,
373
+ } ,
374
+ } ) ;
426
375
427
- this . instances [ module . id ] = instance ;
376
+ if ( moduleState . type !== "instant-link" ) {
377
+ return ;
378
+ }
428
379
429
- return instance ;
430
- } ,
431
- } ) ;
432
- }
380
+ const wallet = ( await moduleState . wallet ( ) ) as InstantLinkWallet ;
381
+ if ( ! wallet . metadata . runOnStartup ) {
382
+ return ;
383
+ }
433
384
434
- this . modules = modules ;
385
+ try {
386
+ await wallet . signIn ( { contractId : wallet . getContractId ( ) } ) ;
387
+ } catch ( err ) {
388
+ logger . error (
389
+ "Failed to sign in to wallet. " + wallet . metadata . name + err
390
+ ) ;
391
+ }
392
+ } )
393
+ . catch ( ( err ) => {
394
+ logger . log ( "Failed to setup module" ) ;
395
+ logger . error ( err ) ;
396
+ } ) ;
397
+ }
435
398
436
399
const {
437
400
accounts,
@@ -442,32 +405,14 @@ export class WalletModules {
442
405
} = await this . resolveStorageState ( ) ;
443
406
444
407
this . store . dispatch ( {
445
- type : "SETUP_WALLET_MODULES " ,
408
+ type : "SETUP " ,
446
409
payload : {
447
- modules,
448
410
accounts,
449
411
contract,
450
412
selectedWalletId,
451
413
recentlySignedInWallets,
452
414
rememberRecentWallets,
453
415
} ,
454
416
} ) ;
455
-
456
- for ( let i = 0 ; i < this . modules . length ; i ++ ) {
457
- if ( this . modules [ i ] . type !== "instant-link" ) {
458
- continue ;
459
- }
460
-
461
- const wallet = ( await this . modules [ i ] . wallet ( ) ) as InstantLinkWallet ;
462
- if ( ! wallet . metadata . runOnStartup ) {
463
- continue ;
464
- }
465
-
466
- try {
467
- await wallet . signIn ( { contractId : wallet . getContractId ( ) } ) ;
468
- } catch ( err ) {
469
- logger . error ( "Failed to sign in to wallet. " + err ) ;
470
- }
471
- }
472
417
}
473
418
}
0 commit comments