@@ -32,7 +32,12 @@ type Properties = {
32
32
chainId : number ;
33
33
} > ;
34
34
} ;
35
- type StorageItem = { "tw.lastChainId" : number } ;
35
+ type StorageItem = {
36
+ "thirdweb:lastChainId" : number ;
37
+ } ;
38
+
39
+ const activeWalletIdKey = "thirdweb:active-wallet-id" ;
40
+ const connectedWalletIdsKey = "thirdweb:connected-wallet-ids" ;
36
41
37
42
/**
38
43
* Connect to an in-app wallet using the auth strategy of your choice.
@@ -92,7 +97,11 @@ export function inAppWalletConnector(
92
97
name : "In-App wallet" ,
93
98
type : "in-app" ,
94
99
connect : async ( params ) => {
95
- const lastChainId = await config . storage ?. getItem ( "tw.lastChainId" ) ;
100
+ const rawStorage =
101
+ typeof window !== "undefined" && window . localStorage
102
+ ? window . localStorage
103
+ : undefined ;
104
+ const lastChainId = await config . storage ?. getItem ( "thirdweb:lastChainId" ) ;
96
105
if ( params ?. isReconnecting ) {
97
106
const { autoConnect } = await import ( "thirdweb/wallets" ) ;
98
107
const chainId = lastChainId || args . smartAccount ?. chain ?. id || 1 ;
@@ -130,7 +139,10 @@ export function inAppWalletConnector(
130
139
chain,
131
140
} as InAppWalletConnectionOptions ;
132
141
const account = await wallet . connect ( decoratedOptions ) ;
133
- await config . storage ?. setItem ( "tw.lastChainId" , chain . id ) ;
142
+ // setting up raw local storage value for autoConnect
143
+ rawStorage ?. setItem ( connectedWalletIdsKey , JSON . stringify ( [ wallet . id ] ) ) ;
144
+ rawStorage ?. setItem ( activeWalletIdKey , wallet . id ) ;
145
+ await config . storage ?. setItem ( "thirdweb:lastChainId" , chain . id ) ;
134
146
return { accounts : [ getAddress ( account . address ) ] , chainId : chain . id } ;
135
147
} ,
136
148
disconnect : async ( ) => {
@@ -147,7 +159,7 @@ export function inAppWalletConnector(
147
159
return wallet . getChain ( ) ?. id || 1 ;
148
160
} ,
149
161
getProvider : async ( params ) => {
150
- const lastChainId = await config . storage ?. getItem ( "tw. lastChainId" ) ;
162
+ const lastChainId = await config . storage ?. getItem ( "thirdweb: lastChainId" ) ;
151
163
const chain = defineChain (
152
164
params ?. chainId || args . smartAccount ?. chain ?. id || lastChainId || 1 ,
153
165
) ;
@@ -169,9 +181,13 @@ export function inAppWalletConnector(
169
181
switchChain : async ( params ) => {
170
182
const chain = config . chains . find ( ( x ) => x . id === params . chainId ) ;
171
183
if ( ! chain ) {
172
- throw new Error ( `Chain ${ params . chainId } not supported ` ) ;
184
+ throw new Error ( `Chain ${ params . chainId } not configured ` ) ;
173
185
}
174
186
await wallet . switchChain ( defineChain ( chain . id ) ) ;
187
+ config . emitter . emit ( "change" , {
188
+ chainId : chain . id ,
189
+ } ) ;
190
+ await config . storage ?. setItem ( "thirdweb:lastChainId" , chain . id ) ;
175
191
return chain ;
176
192
} ,
177
193
onAccountsChanged : ( ) => {
0 commit comments