@@ -11,13 +11,15 @@ 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 { ModuleState , Store } from "../../store.types" ;
14
+ import type { ContractState , 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 ,
21
23
REMEMBER_RECENT_WALLETS ,
22
24
REMEMBER_RECENT_WALLETS_STATE ,
23
25
} from "../../constants" ;
@@ -75,10 +77,49 @@ export class WalletModules {
75
77
76
78
private async resolveStorageState ( ) {
77
79
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
+ ) ;
78
86
const rememberRecentWallets = await jsonStorage . getItem < string > (
79
87
REMEMBER_RECENT_WALLETS
80
88
) ;
81
89
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
+
82
123
const { contract, selectedWalletId } = this . store . getState ( ) ;
83
124
const accounts = await this . validateWallet ( selectedWalletId ) ;
84
125
@@ -147,8 +188,21 @@ export class WalletModules {
147
188
{ accounts, contractId, methodNames } : WalletEvents [ "signedIn" ]
148
189
) {
149
190
const { selectedWalletId, rememberRecentWallets } = this . store . getState ( ) ;
191
+ const jsonStorage = new JsonStorage ( this . storage , PACKAGE_NAME ) ;
150
192
const contract = { contractId, methodNames } ;
151
193
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
+
152
206
if ( selectedWalletId && selectedWalletId !== walletId ) {
153
207
await this . signOutWallet ( selectedWalletId ) ;
154
208
}
0 commit comments