Skip to content

Commit b633293

Browse files
feat: auto-migrate in-app wallets to enclave system (#5217)
1 parent d65387b commit b633293

File tree

7 files changed

+50
-44
lines changed

7 files changed

+50
-44
lines changed

.changeset/empty-needles-reply.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Automatically migrate in-app wallets to the new enclave system

packages/thirdweb/src/react/web/wallets/shared/PassKeyLogin.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ function LoginScreen(props: {
160160
await setLastAuthProvider("passkey", webLocalStorage);
161161
}
162162
done();
163-
} catch {
163+
} catch (e) {
164+
console.error("Failed to login with passkey", e);
164165
setStatus("error");
165166
}
166167
}

packages/thirdweb/src/wallets/in-app/core/actions/generate-wallet.enclave.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import type { Ecosystem } from "../wallet/types.js";
99
* @internal
1010
*/
1111
export async function generateWallet({
12-
authToken,
1312
client,
1413
ecosystem,
14+
authToken,
1515
}: {
1616
client: ThirdwebClient;
17-
ecosystem: Ecosystem;
1817
authToken: string;
18+
ecosystem?: Ecosystem;
1919
}) {
2020
const clientFetch = getClientFetch(client, ecosystem);
2121
const response = await clientFetch(

packages/thirdweb/src/wallets/in-app/native/native-connector.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,26 @@ export class InAppNativeConnector implements InAppConnector {
8080
}
8181
let wallet = user.wallets[0];
8282

83-
// TODO (enclaves): Migration to enclave wallet for in-app wallets as well
84-
if (
85-
authResult &&
86-
this.storage.ecosystem &&
87-
wallet &&
88-
wallet.type === "sharded"
89-
) {
90-
const { migrateToEnclaveWallet } = await import(
91-
"./helpers/wallet/migration.js"
92-
);
93-
wallet = await migrateToEnclaveWallet({
94-
client: this.client,
95-
storage: this.storage,
96-
storedToken: authResult.storedToken,
97-
encryptionKey,
98-
});
83+
if (authResult && wallet && wallet.type === "sharded") {
84+
try {
85+
const { migrateToEnclaveWallet } = await import(
86+
"./helpers/wallet/migration.js"
87+
);
88+
wallet = await migrateToEnclaveWallet({
89+
client: this.client,
90+
storage: this.storage,
91+
storedToken: authResult.storedToken,
92+
encryptionKey,
93+
});
94+
} catch {
95+
console.warn(
96+
"Failed to migrate from sharded to enclave wallet, continuing with sharded wallet",
97+
);
98+
}
9999
}
100100

101-
if (authResult && this.ecosystem && !wallet) {
102-
// new ecosystem user, generate enclave wallet
103-
// TODO (enclaves): same flow for in-app wallets
101+
if (authResult && !wallet) {
102+
// new user, generate enclave wallet
104103
const { generateWallet } = await import(
105104
"../core/actions/generate-wallet.enclave.js"
106105
);

packages/thirdweb/src/wallets/in-app/web/lib/auth/iframe-auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export class Auth {
128128
});
129129
}
130130

131-
if (user.wallets.length === 0 && this.ecosystem) {
131+
if (user.wallets.length === 0) {
132132
// If this is a new ecosystem wallet without an enclave yet, we'll generate an enclave
133133
const result = await generateWallet({
134134
authToken: authToken.storedToken.cookieString,

packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ export class InAppWebConnector implements InAppConnector {
101101
onAuthSuccess: async (authResult) => {
102102
onAuthSuccess?.(authResult);
103103

104-
if (
105-
this.ecosystem &&
106-
authResult.storedToken.authDetails.walletType === "sharded"
107-
) {
104+
if (authResult.storedToken.authDetails.walletType === "sharded") {
108105
// If this is an existing sharded ecosystem wallet, we'll need to migrate
109106
const result = await this.querier.call<boolean>({
110107
procedureName: "migrateFromShardToEnclave",
@@ -113,11 +110,15 @@ export class InAppWebConnector implements InAppConnector {
113110
},
114111
});
115112
if (!result) {
116-
throw new Error("Failed to migrate from sharded to enclave wallet");
113+
console.warn(
114+
"Failed to migrate from sharded to enclave wallet, continuing with sharded wallet",
115+
);
117116
}
118117
}
119118

120-
await this.initializeWallet(authResult.storedToken.cookieString);
119+
this.wallet = await this.initializeWallet(
120+
authResult.storedToken.cookieString,
121+
);
121122

122123
if (!this.wallet) {
123124
throw new Error("Failed to initialize wallet");
@@ -133,7 +134,7 @@ export class InAppWebConnector implements InAppConnector {
133134
deviceShareStored,
134135
});
135136

136-
if (authResult.storedToken.authDetails.walletType !== "enclave") {
137+
if (this.wallet instanceof IFrameWallet) {
137138
await this.querier.call({
138139
procedureName: "initIframe",
139140
params: {
@@ -163,7 +164,7 @@ export class InAppWebConnector implements InAppConnector {
163164
});
164165
}
165166

166-
async initializeWallet(authToken?: string) {
167+
async initializeWallet(authToken?: string): Promise<IWebWallet> {
167168
const storedAuthToken = await this.storage.getAuthCookie();
168169
if (!authToken && storedAuthToken === null) {
169170
throw new Error(
@@ -176,6 +177,7 @@ export class InAppWebConnector implements InAppConnector {
176177
client: this.client,
177178
ecosystem: this.ecosystem,
178179
});
180+
179181
if (!user) {
180182
throw new Error("Cannot initialize wallet, no user logged in");
181183
}
@@ -186,16 +188,15 @@ export class InAppWebConnector implements InAppConnector {
186188
}
187189

188190
if (user.wallets[0]?.type === "enclave") {
189-
this.wallet = new EnclaveWallet({
191+
return new EnclaveWallet({
190192
client: this.client,
191193
ecosystem: this.ecosystem,
192194
address: user.wallets[0].address,
193195
storage: this.storage,
194196
});
195-
return;
196197
}
197198

198-
this.wallet = new IFrameWallet({
199+
return new IFrameWallet({
199200
client: this.client,
200201
ecosystem: this.ecosystem,
201202
querier: this.querier,
@@ -233,7 +234,7 @@ export class InAppWebConnector implements InAppConnector {
233234
if (!localAuthToken) {
234235
return { status: "Logged Out" };
235236
}
236-
await this.initializeWallet(localAuthToken);
237+
this.wallet = await this.initializeWallet(localAuthToken);
237238
}
238239
if (!this.wallet) {
239240
throw new Error("Wallet not initialized");

pnpm-lock.yaml

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)