@@ -21,6 +21,9 @@ import { asyncLocalStorage } from "../../utils/asyncLocalStorage.js";
21
21
import type { ThirdwebClient } from "../../../../client/client.js" ;
22
22
import type { AppMetadata } from "../../../../wallets/types.js" ;
23
23
import { timeoutPromise } from "../../utils/timeoutPromise.js" ;
24
+ import type { SmartWalletOptions } from "../../../../wallets/smart/types.js" ;
25
+ import { createWallet } from "../../../../wallets/create-wallet.js" ;
26
+ import { getSavedConnectParamsFromStorage } from "../../../../wallets/storage/walletStorage.js" ;
24
27
25
28
let autoConnectAttempted = false ;
26
29
@@ -94,6 +97,22 @@ export type AutoConnectProps = {
94
97
* ```
95
98
*/
96
99
timeout ?: number ;
100
+
101
+ /**
102
+ * Enable Account abstraction for all wallets. This will connect to the users's smart account based on the connected personal wallet and the given options.
103
+ *
104
+ * If you are connecting to smart wallet using personal wallet - setting this configuration will autoConnect the personal wallet and then connect to the smart wallet.
105
+ *
106
+ * ```tsx
107
+ * <AutoConnect
108
+ * accountAbstraction={{
109
+ * factoryAddress: "0x123...",
110
+ * chain: sepolia,
111
+ * gasless: true;
112
+ * }}
113
+ * />
114
+ */
115
+ accountAbstraction ?: SmartWalletOptions ;
97
116
} ;
98
117
99
118
/**
@@ -128,7 +147,7 @@ export function AutoConnect(props: AutoConnectProps) {
128
147
const setConnectionStatus = useSetActiveWalletConnectionStatus ( ) ;
129
148
const { connect } = useConnect ( ) ;
130
149
const { isAutoConnecting } = connectionManager ;
131
- const { wallets } = props ;
150
+ const { wallets, accountAbstraction } = props ;
132
151
const timeout = props . timeout ?? 15000 ;
133
152
// get the supported wallets from thirdweb provider
134
153
// check the storage for last connected wallets and connect them all
@@ -153,92 +172,74 @@ export function AutoConnect(props: AutoConnectProps) {
153
172
154
173
async function handleWalletConnection ( wallet : Wallet ) {
155
174
setConnectionStatus ( "connecting" ) ;
175
+ return wallet . autoConnect ( {
176
+ client : props . client ,
177
+ } ) ;
178
+ }
156
179
157
- // if this wallet requires a personal wallet to be connected
158
- // if (walletConfig.personalWalletConfigs) {
159
- // // get saved connection params for this wallet
160
- // const savedParams = await getSavedConnectParamsFromStorage(
161
- // asyncLocalStorage,
162
- // walletConfig.metadata.id,
163
- // );
164
-
165
- // // if must be an object with `personalWalletId` property
166
- // if (!isValidWithPersonalWalletConnectionOptions(savedParams)) {
167
- // throw new Error("Invalid connection params");
168
- // }
169
-
170
- // // find the personal wallet config
171
- // const personalWalletConfig = walletConfig.personalWalletConfigs.find(
172
- // (w) => w.metadata.id === savedParams.personalWalletId,
173
- // );
174
-
175
- // if (!personalWalletConfig) {
176
- // throw new Error("Personal wallet not found");
177
- // }
178
-
179
- // // create and auto connect the personal wallet to get personal account
180
- // const personalWallet = personalWalletConfig.create({
181
- // client,
182
- // appMetadata,
183
- // });
180
+ if ( lastActiveWalletId === "smart" ) {
181
+ if ( ! accountAbstraction ) {
182
+ return ;
183
+ }
184
184
185
- // const account = await personalWallet.autoConnect();
185
+ const savedParams = await getSavedConnectParamsFromStorage (
186
+ asyncLocalStorage ,
187
+ "accountAbstraction" ,
188
+ ) ;
186
189
187
- // // create wallet
188
- // const wallet = walletConfig.create({
189
- // client,
190
- // appMetadata,
191
- // }) as WalletWithPersonalAccount;
190
+ const personalWalletId =
191
+ savedParams && "personalWalletId" in savedParams
192
+ ? savedParams . personalWalletId
193
+ : null ;
192
194
193
- // // auto connect the wallet using the personal account
194
- // await wallet.autoConnect({
195
- // personalAccount: account,
196
- // });
195
+ if ( personalWalletId ) {
196
+ const personalWallet = wallets . find ( ( w ) => w . id === personalWalletId ) ;
197
197
198
- // return wallet;
199
- // }
198
+ if ( personalWallet ) {
199
+ try {
200
+ const account = await timeoutPromise (
201
+ handleWalletConnection ( personalWallet ) ,
202
+ {
203
+ ms : timeout ,
204
+ message :
205
+ "AutoConnect timeout : " + timeout + "ms limit exceeded." ,
206
+ } ,
207
+ ) ;
200
208
201
- if ( false ) {
202
- // do nothing
203
- }
209
+ const smartWallet = createWallet ( "smart" , accountAbstraction ) ;
210
+ await smartWallet . connect ( {
211
+ personalAccount : account ,
212
+ client : props . client ,
213
+ } ) ;
204
214
205
- // if this wallet does not require a personal wallet to be connected
206
- else {
207
- await wallet . autoConnect ( {
208
- client : props . client ,
209
- } ) ;
210
- return wallet ;
215
+ connect ( smartWallet ) ;
216
+ } catch ( e ) {
217
+ console . error ( "Failed to auto connect personal wallet" ) ;
218
+ console . error ( e ) ;
219
+ setConnectionStatus ( "disconnected" ) ;
220
+ }
221
+ }
211
222
}
212
- }
213
-
214
- // connect the last active wallet and set it as active
215
- const activeWalletConfig = wallets . find (
216
- ( w ) => w . id === lastActiveWalletId ,
217
- ) ;
223
+ } else {
224
+ const activeWallet = wallets . find ( ( w ) => w . id === lastActiveWalletId ) ;
218
225
219
- if ( activeWalletConfig ) {
220
- try {
221
- const wallet = await timeoutPromise (
222
- handleWalletConnection ( activeWalletConfig ) ,
223
- {
226
+ if ( activeWallet ) {
227
+ try {
228
+ await timeoutPromise ( handleWalletConnection ( activeWallet ) , {
224
229
ms : timeout ,
225
230
message :
226
231
"AutoConnect timeout : " + timeout + "ms limit exceeded." ,
227
- } ,
228
- ) ;
232
+ } ) ;
229
233
230
- if ( wallet ) {
231
- connect ( wallet ) ;
232
- } else {
234
+ connect ( activeWallet ) ;
235
+ } catch ( e ) {
236
+ console . error ( "Failed to auto connect last active wallet" ) ;
237
+ console . error ( e ) ;
233
238
setConnectionStatus ( "disconnected" ) ;
234
239
}
235
- } catch ( e ) {
236
- console . error ( "Failed to auto connect last active wallet" ) ;
237
- console . error ( e ) ;
240
+ } else {
238
241
setConnectionStatus ( "disconnected" ) ;
239
242
}
240
- } else {
241
- setConnectionStatus ( "disconnected" ) ;
242
243
}
243
244
244
245
// then connect wallets that were last connected but were not set as active
@@ -247,10 +248,14 @@ export function AutoConnect(props: AutoConnectProps) {
247
248
w . id !== lastActiveWalletId && lastConnectedWalletIds . includes ( w . id ) ,
248
249
) ;
249
250
250
- otherWallets . forEach ( async ( config ) => {
251
- const account = await handleWalletConnection ( config ) ;
252
- if ( account ) {
253
- connectionManager . addConnectedWallet ( account ) ;
251
+ otherWallets . forEach ( async ( wallet ) => {
252
+ try {
253
+ await handleWalletConnection ( wallet ) ;
254
+ connectionManager . addConnectedWallet ( wallet ) ;
255
+ } catch ( e ) {
256
+ console . error ( "Failed to auto connect a non-active connected wallet" ) ;
257
+ console . error ( e ) ;
258
+ setConnectionStatus ( "disconnected" ) ;
254
259
}
255
260
} ) ;
256
261
} ;
0 commit comments