Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-needles-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Automatically migrate in-app wallets to the new enclave system
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@
await setLastAuthProvider("passkey", webLocalStorage);
}
done();
} catch {
} catch (e) {
console.error("Failed to login with passkey", e);

Check warning on line 164 in packages/thirdweb/src/react/web/wallets/shared/PassKeyLogin.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/wallets/shared/PassKeyLogin.tsx#L163-L164

Added lines #L163 - L164 were not covered by tests
setStatus("error");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import type { Ecosystem } from "../wallet/types.js";
* @internal
*/
export async function generateWallet({
authToken,
client,
ecosystem,
authToken,
}: {
client: ThirdwebClient;
ecosystem: Ecosystem;
authToken: string;
ecosystem?: Ecosystem;
}) {
const clientFetch = getClientFetch(client, ecosystem);
const response = await clientFetch(
Expand Down
37 changes: 18 additions & 19 deletions packages/thirdweb/src/wallets/in-app/native/native-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,26 @@ export class InAppNativeConnector implements InAppConnector {
}
let wallet = user.wallets[0];

// TODO (enclaves): Migration to enclave wallet for in-app wallets as well
if (
authResult &&
this.storage.ecosystem &&
wallet &&
wallet.type === "sharded"
) {
const { migrateToEnclaveWallet } = await import(
"./helpers/wallet/migration.js"
);
wallet = await migrateToEnclaveWallet({
client: this.client,
storage: this.storage,
storedToken: authResult.storedToken,
encryptionKey,
});
if (authResult && wallet && wallet.type === "sharded") {
try {
const { migrateToEnclaveWallet } = await import(
"./helpers/wallet/migration.js"
);
wallet = await migrateToEnclaveWallet({
client: this.client,
storage: this.storage,
storedToken: authResult.storedToken,
encryptionKey,
});
} catch {
console.warn(
"Failed to migrate from sharded to enclave wallet, continuing with sharded wallet",
);
}
}

if (authResult && this.ecosystem && !wallet) {
// new ecosystem user, generate enclave wallet
// TODO (enclaves): same flow for in-app wallets
if (authResult && !wallet) {
// new user, generate enclave wallet
const { generateWallet } = await import(
"../core/actions/generate-wallet.enclave.js"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class Auth {
});
}

if (user.wallets.length === 0 && this.ecosystem) {
if (user.wallets.length === 0) {
// If this is a new ecosystem wallet without an enclave yet, we'll generate an enclave
const result = await generateWallet({
authToken: authToken.storedToken.cookieString,
Expand Down
25 changes: 13 additions & 12 deletions packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@
onAuthSuccess: async (authResult) => {
onAuthSuccess?.(authResult);

if (
this.ecosystem &&
authResult.storedToken.authDetails.walletType === "sharded"
) {
if (authResult.storedToken.authDetails.walletType === "sharded") {

Check warning on line 104 in packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L104 was not covered by tests
// If this is an existing sharded ecosystem wallet, we'll need to migrate
const result = await this.querier.call<boolean>({
procedureName: "migrateFromShardToEnclave",
Expand All @@ -113,11 +110,15 @@
},
});
if (!result) {
throw new Error("Failed to migrate from sharded to enclave wallet");
console.warn(
"Failed to migrate from sharded to enclave wallet, continuing with sharded wallet",
);

Check warning on line 115 in packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L113 - L115 were not covered by tests
}
}

await this.initializeWallet(authResult.storedToken.cookieString);
this.wallet = await this.initializeWallet(
authResult.storedToken.cookieString,
);

Check warning on line 121 in packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L119 - L121 were not covered by tests

if (!this.wallet) {
throw new Error("Failed to initialize wallet");
Expand All @@ -133,7 +134,7 @@
deviceShareStored,
});

if (authResult.storedToken.authDetails.walletType !== "enclave") {
if (this.wallet instanceof IFrameWallet) {

Check warning on line 137 in packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L137 was not covered by tests
await this.querier.call({
procedureName: "initIframe",
params: {
Expand Down Expand Up @@ -163,7 +164,7 @@
});
}

async initializeWallet(authToken?: string) {
async initializeWallet(authToken?: string): Promise<IWebWallet> {

Check warning on line 167 in packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L167 was not covered by tests
const storedAuthToken = await this.storage.getAuthCookie();
if (!authToken && storedAuthToken === null) {
throw new Error(
Expand All @@ -176,6 +177,7 @@
client: this.client,
ecosystem: this.ecosystem,
});

if (!user) {
throw new Error("Cannot initialize wallet, no user logged in");
}
Expand All @@ -186,16 +188,15 @@
}

if (user.wallets[0]?.type === "enclave") {
this.wallet = new EnclaveWallet({
return new EnclaveWallet({

Check warning on line 191 in packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L191 was not covered by tests
client: this.client,
ecosystem: this.ecosystem,
address: user.wallets[0].address,
storage: this.storage,
});
return;
}

this.wallet = new IFrameWallet({
return new IFrameWallet({

Check warning on line 199 in packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L199 was not covered by tests
client: this.client,
ecosystem: this.ecosystem,
querier: this.querier,
Expand Down Expand Up @@ -233,7 +234,7 @@
if (!localAuthToken) {
return { status: "Logged Out" };
}
await this.initializeWallet(localAuthToken);
this.wallet = await this.initializeWallet(localAuthToken);

Check warning on line 237 in packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L237 was not covered by tests
}
if (!this.wallet) {
throw new Error("Wallet not initialized");
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading