@@ -32,7 +32,12 @@ type Properties = {
3232 chainId : number ;
3333 } > ;
3434} ;
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" ;
3641
3742/**
3843 * Connect to an in-app wallet using the auth strategy of your choice.
@@ -92,7 +97,11 @@ export function inAppWalletConnector(
9297 name : "In-App wallet" ,
9398 type : "in-app" ,
9499 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" ) ;
96105 if ( params ?. isReconnecting ) {
97106 const { autoConnect } = await import ( "thirdweb/wallets" ) ;
98107 const chainId = lastChainId || args . smartAccount ?. chain ?. id || 1 ;
@@ -130,7 +139,10 @@ export function inAppWalletConnector(
130139 chain,
131140 } as InAppWalletConnectionOptions ;
132141 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 ) ;
134146 return { accounts : [ getAddress ( account . address ) ] , chainId : chain . id } ;
135147 } ,
136148 disconnect : async ( ) => {
@@ -147,7 +159,7 @@ export function inAppWalletConnector(
147159 return wallet . getChain ( ) ?. id || 1 ;
148160 } ,
149161 getProvider : async ( params ) => {
150- const lastChainId = await config . storage ?. getItem ( "tw. lastChainId" ) ;
162+ const lastChainId = await config . storage ?. getItem ( "thirdweb: lastChainId" ) ;
151163 const chain = defineChain (
152164 params ?. chainId || args . smartAccount ?. chain ?. id || lastChainId || 1 ,
153165 ) ;
@@ -169,9 +181,13 @@ export function inAppWalletConnector(
169181 switchChain : async ( params ) => {
170182 const chain = config . chains . find ( ( x ) => x . id === params . chainId ) ;
171183 if ( ! chain ) {
172- throw new Error ( `Chain ${ params . chainId } not supported ` ) ;
184+ throw new Error ( `Chain ${ params . chainId } not configured ` ) ;
173185 }
174186 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 ) ;
175191 return chain ;
176192 } ,
177193 onAccountsChanged : ( ) => {
0 commit comments