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/silver-candles-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Support smart account options for ecosystem wallets
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import type { Chain } from "../../../../chains/types.js";
import type { ThirdwebClient } from "../../../../client/client.js";
import { webLocalStorage } from "../../../../utils/storage/webStorage.js";
import { getEcosystemWalletAuthOptions } from "../../../../wallets/ecosystem/get-ecosystem-wallet-auth-options.js";
import { getEcosystemOptions } from "../../../../wallets/ecosystem/get-ecosystem-wallet-auth-options.js";
import { isEcosystemWallet } from "../../../../wallets/ecosystem/is-ecosystem-wallet.js";
import type { Profile } from "../../../../wallets/in-app/core/authentication/types.js";
import { linkProfile } from "../../../../wallets/in-app/web/lib/auth/index.js";
Expand Down Expand Up @@ -121,7 +121,8 @@
queryKey: ["auth-options", wallet.id],
queryFn: async () => {
if (isEcosystemWallet(wallet)) {
return getEcosystemWalletAuthOptions(wallet.id);
const options = await getEcosystemOptions(wallet.id);
return options?.authOptions ?? null;

Check warning on line 125 in packages/thirdweb/src/react/web/wallets/shared/ConnectWalletSocialOptions.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/wallets/shared/ConnectWalletSocialOptions.tsx#L124-L125

Added lines #L124 - L125 were not covered by tests
}
return null;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@
import type { AuthOption } from "../types.js";
import type { EcosystemWalletId } from "../wallet-types.js";

export type EcosystemOptions = {
authOptions: AuthOption[];
smartAccountOptions: SmartAccountOptions;
};

type SmartAccountOptions = {
chainIds: number[];
sponsorGas: boolean;
accountFactoryAddress: string;
};

/**
* Retrieves the specified auth options for a given ecosystem wallet, if any.
* @param walletId The ecosystem wallet ID.
* @returns {AuthOption[] | undefined} The auth options for the ecosystem wallet.
* @internal
*/
export async function getEcosystemWalletAuthOptions(
export async function getEcosystemOptions(

Check warning on line 22 in packages/thirdweb/src/wallets/ecosystem/get-ecosystem-wallet-auth-options.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/ecosystem/get-ecosystem-wallet-auth-options.ts#L22

Added line #L22 was not covered by tests
walletId: EcosystemWalletId,
): Promise<AuthOption[] | undefined> {
): Promise<EcosystemOptions | null> {

Check warning on line 24 in packages/thirdweb/src/wallets/ecosystem/get-ecosystem-wallet-auth-options.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/ecosystem/get-ecosystem-wallet-auth-options.ts#L24

Added line #L24 was not covered by tests
const res = await fetch(
`${getThirdwebBaseUrl("inAppWallet")}/api/2024-05-05/ecosystem-wallet`,
{
Expand All @@ -29,5 +40,5 @@
);
}

return data.authOptions ?? undefined;
return data ?? null;

Check warning on line 43 in packages/thirdweb/src/wallets/ecosystem/get-ecosystem-wallet-auth-options.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/ecosystem/get-ecosystem-wallet-auth-options.ts#L43

Added line #L43 was not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
options,
createOptions,
connector,
ecosystem,

Check warning on line 73 in packages/thirdweb/src/wallets/in-app/core/wallet/in-app-core.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/in-app/core/wallet/in-app-core.ts#L73

Added line #L73 was not covered by tests
);

// set the states
Expand All @@ -96,6 +97,7 @@
options,
createOptions,
connector,
ecosystem,

Check warning on line 100 in packages/thirdweb/src/wallets/in-app/core/wallet/in-app-core.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/in-app/core/wallet/in-app-core.ts#L100

Added line #L100 was not covered by tests
);
// set the states
client = options.client;
Expand Down Expand Up @@ -142,6 +144,7 @@
},
createOptions,
connector,
ecosystem,

Check warning on line 147 in packages/thirdweb/src/wallets/in-app/core/wallet/in-app-core.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/in-app/core/wallet/in-app-core.ts#L147

Added line #L147 was not covered by tests
);
account = connectedAccount;
chain = connectedChain;
Expand Down
59 changes: 59 additions & 0 deletions packages/thirdweb/src/wallets/in-app/core/wallet/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { ethereum } from "../../../../chains/chain-definitions/ethereum.js";
import type { Chain } from "../../../../chains/types.js";
import { getCachedChain } from "../../../../chains/utils.js";
import type { ThirdwebClient } from "../../../../client/client.js";
import {
type SocialAuthOption,
socialAuthOptions,
} from "../../../../wallets/types.js";
import { getEcosystemOptions } from "../../../ecosystem/get-ecosystem-wallet-auth-options.js";
import type { Account, Wallet } from "../../../interfaces/wallet.js";
import type { EcosystemWalletId, WalletId } from "../../../wallet-types.js";
import type {
Expand All @@ -13,6 +15,7 @@
WalletConnectionOption,
} from "../../../wallet-types.js";
import type { InAppConnector } from "../interfaces/connector.js";
import type { Ecosystem } from "./types.js";

/**
* Checks if the provided wallet is an in-app wallet.
Expand All @@ -37,6 +40,7 @@
| CreateWalletArgs<"inApp">[1]
| CreateWalletArgs<EcosystemWalletId>[1],
connector: InAppConnector,
ecosystem: Ecosystem | undefined,

Check warning on line 43 in packages/thirdweb/src/wallets/in-app/core/wallet/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/in-app/core/wallet/index.ts#L43

Added line #L43 was not covered by tests
): Promise<[Account, Chain]> {
if (
// if auth mode is not specified, the default is popup
Expand Down Expand Up @@ -73,6 +77,33 @@
});
}

if (ecosystem) {
const ecosystemOptions = await getEcosystemOptions(ecosystem.id);
const smartAccountOptions = ecosystemOptions?.smartAccountOptions;
if (smartAccountOptions) {
const allowedChains = smartAccountOptions.chainIds;
const firstAllowedChain = allowedChains[0];
if (!firstAllowedChain) {
throw new Error(
"At least one chain must be allowed for ecosystem smart account",
);
}
const preferredChain =
options.chain && allowedChains.includes(options.chain.id)
? options.chain
: getCachedChain(firstAllowedChain);
return convertToSmartAccount({
client: options.client,
authAccount,
smartAccountOptions: {
chain: preferredChain,
sponsorGas: smartAccountOptions.sponsorGas,
factoryAddress: smartAccountOptions.accountFactoryAddress,
},
});
}
}

Check warning on line 105 in packages/thirdweb/src/wallets/in-app/core/wallet/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/in-app/core/wallet/index.ts#L80-L105

Added lines #L80 - L105 were not covered by tests

return [authAccount, options.chain || ethereum] as const;
}

Expand All @@ -87,6 +118,7 @@
| CreateWalletArgs<"inApp">[1]
| CreateWalletArgs<EcosystemWalletId>[1],
connector: InAppConnector,
ecosystem: Ecosystem | undefined,

Check warning on line 121 in packages/thirdweb/src/wallets/in-app/core/wallet/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/in-app/core/wallet/index.ts#L121

Added line #L121 was not covered by tests
): Promise<[Account, Chain]> {
if (options.authResult && connector.loginWithAuthToken) {
await connector.loginWithAuthToken(options.authResult);
Expand All @@ -112,6 +144,33 @@
});
}

if (ecosystem) {
const ecosystemOptions = await getEcosystemOptions(ecosystem.id);
const smartAccountOptions = ecosystemOptions?.smartAccountOptions;
if (smartAccountOptions) {
const allowedChains = smartAccountOptions.chainIds;
const firstAllowedChain = allowedChains[0];
if (!firstAllowedChain) {
throw new Error(
"At least one chain must be allowed for ecosystem smart account",
);
}
const preferredChain =
options.chain && allowedChains.includes(options.chain.id)
? options.chain
: getCachedChain(firstAllowedChain);
return convertToSmartAccount({
client: options.client,
authAccount,
smartAccountOptions: {
chain: preferredChain,
sponsorGas: smartAccountOptions.sponsorGas,
factoryAddress: smartAccountOptions.accountFactoryAddress,
},
});
}
}

Check warning on line 172 in packages/thirdweb/src/wallets/in-app/core/wallet/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/in-app/core/wallet/index.ts#L147-L172

Added lines #L147 - L172 were not covered by tests

return [authAccount, options.chain || ethereum] as const;
}

Expand Down