-
Notifications
You must be signed in to change notification settings - Fork 664
Expand file tree
/
Copy pathautoConnectCore.ts
More file actions
391 lines (358 loc) · 11.5 KB
/
autoConnectCore.ts
File metadata and controls
391 lines (358 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import type { Chain } from "../../chains/types.js";
import type { ThirdwebClient } from "../../client/client.js";
import type { AsyncStorage } from "../../utils/storage/AsyncStorage.js";
import { timeoutPromise } from "../../utils/timeoutPromise.js";
import { isEcosystemWallet } from "../ecosystem/is-ecosystem-wallet.js";
import { ClientScopedStorage } from "../in-app/core/authentication/client-scoped-storage.js";
import { linkAccount } from "../in-app/core/authentication/linkAccount.js";
import type {
AuthArgsType,
AuthStoredTokenWithCookieReturnType,
} from "../in-app/core/authentication/types.js";
import { isInAppSigner } from "../in-app/core/wallet/is-in-app-signer.js";
import { getUrlToken } from "../in-app/web/lib/get-url-token.js";
import type { Wallet } from "../interfaces/wallet.js";
import {
type ConnectionManager,
getLastConnectedChain,
getStoredActiveWalletId,
getStoredConnectedWalletIds,
} from "../manager/index.js";
import type { WalletId } from "../wallet-types.js";
import type { AutoConnectProps } from "./types.js";
type AutoConnectCoreProps = {
storage: AsyncStorage;
props: AutoConnectProps & { wallets: Wallet[] };
createWalletFn: (id: WalletId) => Wallet;
manager: ConnectionManager;
connectOverride?: (
walletOrFn: Wallet | (() => Promise<Wallet>),
) => Promise<Wallet | null>;
setLastAuthProvider?: (
authProvider: AuthArgsType["strategy"],
storage: AsyncStorage,
) => Promise<void>;
/**
* If true, the auto connect will be forced even if autoConnect has already been attempted successfully earlier.
*
* @default `false`
*/
force?: boolean;
};
let lastAutoConnectionResultPromise: Promise<boolean> | undefined;
/**
* @internal
*/
export const autoConnectCore = async (props: AutoConnectCoreProps) => {
// if an auto connect was attempted already
if (lastAutoConnectionResultPromise && !props.force) {
// wait for its resolution
const lastResult = await lastAutoConnectionResultPromise;
// if it was successful, return true
// if not continue with the new auto connect
if (lastResult) {
return true;
}
}
const resultPromise = _autoConnectCore(props);
lastAutoConnectionResultPromise = resultPromise;
return resultPromise;
};
const _autoConnectCore = async ({
storage,
props,
createWalletFn,
manager,
connectOverride,
setLastAuthProvider,
}: AutoConnectCoreProps): Promise<boolean> => {
const { wallets, onConnect } = props;
const timeout = props.timeout ?? 15000;
let autoConnected = false;
manager.isAutoConnecting.setValue(true);
let [lastConnectedWalletIds, lastActiveWalletId] = await Promise.all([
getStoredConnectedWalletIds(storage),
getStoredActiveWalletId(storage),
]);
const urlToken = getUrlToken();
// Handle linking flow: autoconnect with stored credentials, then link the new profile
if (urlToken?.authFlow === "link" && urlToken.authResult) {
const linkingResult = await handleLinkingFlow({
client: props.client,
connectOverride,
createWalletFn,
manager,
onConnect,
props,
setLastAuthProvider,
storage,
timeout,
urlToken,
wallets,
});
return linkingResult;
}
// If an auth cookie is found and this site supports the wallet, we'll set the auth cookie in the client storage
const wallet = wallets.find((w) => w.id === urlToken?.walletId);
if (urlToken?.authCookie && wallet) {
const clientStorage = new ClientScopedStorage({
clientId: props.client.clientId,
ecosystem: isEcosystemWallet(wallet)
? {
id: wallet.id,
partnerId: wallet.getConfig()?.partnerId,
}
: undefined,
storage,
});
await clientStorage.saveAuthCookie(urlToken.authCookie);
}
if (urlToken?.walletId) {
lastActiveWalletId = urlToken.walletId;
lastConnectedWalletIds = lastConnectedWalletIds?.includes(urlToken.walletId)
? lastConnectedWalletIds
: [urlToken.walletId, ...(lastConnectedWalletIds || [])];
}
if (urlToken?.authProvider) {
await setLastAuthProvider?.(urlToken.authProvider, storage);
}
// if no wallets were last connected or we didn't receive an auth token
if (!lastConnectedWalletIds) {
return autoConnected;
}
// this flow can actually be used for a first connection in the case of a redirect
// in that case, we default to the passed chain to connect to
const lastConnectedChain =
(await getLastConnectedChain(storage)) || props.chain;
const availableWallets = lastConnectedWalletIds.map((id) => {
const specifiedWallet = wallets.find((w) => w.id === id);
if (specifiedWallet) {
return specifiedWallet;
}
return createWalletFn(id as WalletId);
});
const activeWallet =
lastActiveWalletId &&
(availableWallets.find((w) => w.id === lastActiveWalletId) ||
createWalletFn(lastActiveWalletId));
if (activeWallet) {
manager.activeWalletConnectionStatusStore.setValue("connecting"); // only set connecting status if we are connecting the last active EOA
await timeoutPromise(
handleWalletConnection({
authResult: urlToken?.authResult,
client: props.client,
lastConnectedChain,
wallet: activeWallet,
}),
{
message: `AutoConnect timeout: ${timeout}ms limit exceeded.`,
ms: timeout,
},
).catch((err) => {
console.warn(err.message);
if (props.onTimeout) {
props.onTimeout();
}
});
try {
// connected wallet could be activeWallet or smart wallet
await (connectOverride
? connectOverride(activeWallet)
: manager.connect(activeWallet, {
accountAbstraction: props.accountAbstraction,
client: props.client,
}));
} catch (e) {
if (e instanceof Error) {
console.warn("Error auto connecting wallet:", e.message);
}
manager.activeWalletConnectionStatusStore.setValue("disconnected");
}
} else {
manager.activeWalletConnectionStatusStore.setValue("disconnected");
}
// then connect wallets that were last connected but were not set as active
const otherWallets = availableWallets.filter(
(w) => w.id !== lastActiveWalletId && lastConnectedWalletIds.includes(w.id),
);
for (const wallet of otherWallets) {
try {
await handleWalletConnection({
authResult: urlToken?.authResult,
client: props.client,
lastConnectedChain,
wallet,
});
manager.addConnectedWallet(wallet);
} catch {
// no-op
}
}
// Auto-login with SIWE
const isIAW =
activeWallet &&
isInAppSigner({
connectedWallets: activeWallet
? [activeWallet, ...otherWallets]
: otherWallets,
wallet: activeWallet,
});
if (
isIAW &&
props.siweAuth?.requiresAuth &&
!props.siweAuth?.isLoggedIn &&
!props.siweAuth?.isLoggingIn
) {
await props.siweAuth?.doLogin().catch((err) => {
console.warn("Error signing in with SIWE:", err.message);
});
}
manager.isAutoConnecting.setValue(false);
const connectedActiveWallet = manager.activeWalletStore.getValue();
const allConnectedWallets = manager.connectedWallets.getValue();
if (connectedActiveWallet) {
autoConnected = true;
try {
onConnect?.(connectedActiveWallet, allConnectedWallets);
} catch (e) {
console.error("Error calling onConnect callback:", e);
}
} else {
manager.activeWalletConnectionStatusStore.setValue("disconnected");
}
return autoConnected; // useQuery needs a return value
};
/**
* Handles the linking flow when returning from an OAuth redirect with authFlow=link.
* This autoconnects using stored credentials, then links the new profile from the URL token.
* @internal
*/
async function handleLinkingFlow(params: {
client: ThirdwebClient;
urlToken: NonNullable<ReturnType<typeof getUrlToken>>;
wallets: Wallet[];
storage: AsyncStorage;
manager: ConnectionManager;
onConnect?: (wallet: Wallet, connectedWallets: Wallet[]) => void;
timeout: number;
connectOverride?: (
walletOrFn: Wallet | (() => Promise<Wallet>),
) => Promise<Wallet | null>;
createWalletFn: (id: WalletId) => Wallet;
setLastAuthProvider?: (
authProvider: AuthArgsType["strategy"],
storage: AsyncStorage,
) => Promise<void>;
props: AutoConnectProps & { wallets: Wallet[] };
}): Promise<boolean> {
const {
client,
connectOverride,
createWalletFn,
manager,
onConnect,
props,
setLastAuthProvider,
storage,
timeout,
urlToken,
wallets,
} = params;
// Get stored wallet credentials (not from URL)
const [storedConnectedWalletIds, storedActiveWalletId] = await Promise.all([
getStoredConnectedWalletIds(storage),
getStoredActiveWalletId(storage),
]);
const lastConnectedChain =
(await getLastConnectedChain(storage)) || props.chain;
if (!storedActiveWalletId || !storedConnectedWalletIds) {
console.warn("No stored wallet found for linking flow");
manager.isAutoConnecting.setValue(false);
return false;
}
// Update auth provider if provided
if (urlToken.authProvider) {
await setLastAuthProvider?.(urlToken.authProvider, storage);
}
// Find or create the active wallet from stored credentials
const activeWallet =
wallets.find((w) => w.id === storedActiveWalletId) ||
createWalletFn(storedActiveWalletId);
// Autoconnect WITHOUT the URL token (use stored credentials)
manager.activeWalletConnectionStatusStore.setValue("connecting");
try {
await timeoutPromise(
handleWalletConnection({
authResult: undefined, // Don't use URL token for connection
client,
lastConnectedChain,
wallet: activeWallet,
}),
{
message: `AutoConnect timeout: ${timeout}ms limit exceeded.`,
ms: timeout,
},
);
await (connectOverride
? connectOverride(activeWallet)
: manager.connect(activeWallet, {
accountAbstraction: props.accountAbstraction,
client,
}));
} catch (e) {
console.warn("Failed to auto-connect for linking:", e);
manager.activeWalletConnectionStatusStore.setValue("disconnected");
manager.isAutoConnecting.setValue(false);
return false;
}
// Now link the new profile using URL auth token
const ecosystem = isEcosystemWallet(activeWallet)
? {
id: activeWallet.id,
partnerId: activeWallet.getConfig()?.partnerId,
}
: undefined;
const clientStorage = new ClientScopedStorage({
clientId: client.clientId,
ecosystem,
storage,
});
try {
await linkAccount({
client,
ecosystem,
storage: clientStorage,
tokenToLink: urlToken.authResult!.storedToken.cookieString,
});
} catch (e) {
console.error("Failed to link profile after redirect:", e);
// Continue - user is still connected, just linking failed
}
manager.isAutoConnecting.setValue(false);
const connectedWallet = manager.activeWalletStore.getValue();
const allConnectedWallets = manager.connectedWallets.getValue();
if (connectedWallet) {
try {
onConnect?.(connectedWallet, allConnectedWallets);
} catch (e) {
console.error("Error calling onConnect callback:", e);
}
return true;
}
return false;
}
/**
* @internal
*/
export async function handleWalletConnection(props: {
wallet: Wallet;
client: ThirdwebClient;
authResult: AuthStoredTokenWithCookieReturnType | undefined;
lastConnectedChain: Chain | undefined;
}) {
return props.wallet.autoConnect({
authResult: props.authResult,
chain: props.lastConnectedChain,
client: props.client,
});
}