diff --git a/packages/thirdweb/src/wallets/create-wallet.ts b/packages/thirdweb/src/wallets/create-wallet.ts index 4b9c7c9462e..5f2bedddaf5 100644 --- a/packages/thirdweb/src/wallets/create-wallet.ts +++ b/packages/thirdweb/src/wallets/create-wallet.ts @@ -140,6 +140,13 @@ export function createWallet( creationOptions as CreateWalletArgs<"inApp">[1], ) as Wallet; } + /** + * ECOSYSTEM WALLET + */ + case isEcosystemWallet(id): + return ecosystemWallet( + ...(args as CreateWalletArgs), + ) as Wallet; /** * COINBASE WALLET VIA SDK @@ -160,11 +167,6 @@ export function createWallet( }, }) as Wallet; } - case isEcosystemWallet(id): - return ecosystemWallet( - ...(args as CreateWalletArgs), - ) as Wallet; - /** * WALLET CONNECT AND INJECTED WALLETS + walletConnect standalone */ diff --git a/packages/thirdweb/src/wallets/ecosystem/types.ts b/packages/thirdweb/src/wallets/ecosystem/types.ts index 7355e080874..3d91f50a758 100644 --- a/packages/thirdweb/src/wallets/ecosystem/types.ts +++ b/packages/thirdweb/src/wallets/ecosystem/types.ts @@ -1,14 +1,11 @@ import type { InAppWalletAutoConnectOptions, InAppWalletConnectionOptions, + InAppWalletCreationOptions, } from "../in-app/core/wallet/types.js"; -export type EcosystemWalletCreationOptions = { +export type EcosystemWalletCreationOptions = InAppWalletCreationOptions & { partnerId?: string; - auth?: { - mode?: "popup" | "redirect" | "window"; - redirectUrl?: string; - }; }; export type EcosystemWalletConnectionOptions = InAppWalletConnectionOptions; diff --git a/packages/thirdweb/src/wallets/in-app/core/wallet/ecosystem-core.ts b/packages/thirdweb/src/wallets/in-app/core/wallet/ecosystem-core.ts deleted file mode 100644 index 0ffbe3c7142..00000000000 --- a/packages/thirdweb/src/wallets/in-app/core/wallet/ecosystem-core.ts +++ /dev/null @@ -1,121 +0,0 @@ -import { trackConnect } from "../../../../analytics/track.js"; -import type { Chain } from "../../../../chains/types.js"; -import { getCachedChainIfExists } from "../../../../chains/utils.js"; -import type { ThirdwebClient } from "../../../../client/client.js"; -import type { Account, Wallet } from "../../../interfaces/wallet.js"; -import { createWalletEmitter } from "../../../wallet-emitter.js"; -import type { - CreateWalletArgs, - EcosystemWalletId, -} from "../../../wallet-types.js"; -import type { InAppConnector } from "../interfaces/connector.js"; -import { getOrCreateInAppWalletConnector } from "./in-app-core.js"; -import type { Ecosystem } from "./types.js"; - -/** - * @internal - */ -export function createEcosystemWallet(args: { - id: EcosystemWalletId; - createOptions: CreateWalletArgs[1]; - connectorFactory: (client: ThirdwebClient) => Promise; -}): Wallet { - // Under the hood, an ecosystem wallet wraps an in-app wallet - const { id, createOptions, connectorFactory } = args; - const emitter = createWalletEmitter(); - let account: Account | undefined = undefined; - let chain: Chain | undefined = undefined; - let client: ThirdwebClient | undefined; - const ecosystem: Ecosystem = { - id, - partnerId: createOptions?.partnerId, - }; - - return { - id, - subscribe: emitter.subscribe, - getChain() { - if (!chain) { - return undefined; - } - - chain = getCachedChainIfExists(chain.id) || chain; - return chain; - }, - getConfig: () => createOptions, - getAccount: () => account, - autoConnect: async (options) => { - const { autoConnectInAppWallet } = await import("./index.js"); - - const connector = await getOrCreateInAppWalletConnector( - options.client, - connectorFactory, - ecosystem, - ); - - const [connectedAccount, connectedChain] = await autoConnectInAppWallet( - options, - createOptions, - connector, - ); - // set the states - client = options.client; - account = connectedAccount; - chain = connectedChain; - trackConnect({ - client: options.client, - walletType: id, - walletAddress: account.address, - }); - // return only the account - return account; - }, - connect: async (options) => { - const { connectInAppWallet } = await import("./index.js"); - - const connector = await getOrCreateInAppWalletConnector( - options.client, - connectorFactory, - ecosystem, - ); - - const [connectedAccount, connectedChain] = await connectInAppWallet( - options, - createOptions, - connector, - ); - // set the states - client = options.client; - account = connectedAccount; - chain = connectedChain; - trackConnect({ - client: options.client, - walletType: id, - walletAddress: account.address, - }); - // return only the account - return account; - }, - disconnect: async () => { - // If no client is assigned, we should be fine just unsetting the states - if (client) { - const connector = await getOrCreateInAppWalletConnector( - client, - connectorFactory, - ecosystem, - ); - const result = await connector.logout(); - if (!result.success) { - throw new Error("Failed to logout"); - } - } - account = undefined; - chain = undefined; - emitter.emit("disconnect", undefined); - }, - switchChain: async (newChain) => { - chain = newChain; - emitter.emit("chainChanged", newChain); - }, - } as Wallet; -} diff --git a/packages/thirdweb/src/wallets/in-app/core/wallet/in-app-core.ts b/packages/thirdweb/src/wallets/in-app/core/wallet/in-app-core.ts index 9ae50860de1..8cce2b03fac 100644 --- a/packages/thirdweb/src/wallets/in-app/core/wallet/in-app-core.ts +++ b/packages/thirdweb/src/wallets/in-app/core/wallet/in-app-core.ts @@ -4,7 +4,10 @@ import { getCachedChainIfExists } from "../../../../chains/utils.js"; import type { ThirdwebClient } from "../../../../client/client.js"; import type { Account, Wallet } from "../../../interfaces/wallet.js"; import { createWalletEmitter } from "../../../wallet-emitter.js"; -import type { CreateWalletArgs } from "../../../wallet-types.js"; +import type { + CreateWalletArgs, + EcosystemWalletId, +} from "../../../wallet-types.js"; import type { InAppConnector } from "../interfaces/connector.js"; import type { Ecosystem } from "./types.js"; @@ -33,15 +36,17 @@ export async function getOrCreateInAppWalletConnector( export function createInAppWallet(args: { createOptions?: CreateWalletArgs<"inApp">[1]; connectorFactory: (client: ThirdwebClient) => Promise; -}): Wallet<"inApp"> { - const { createOptions, connectorFactory } = args; + ecosystem?: Ecosystem; +}): Wallet<"inApp" | EcosystemWalletId> { + const { createOptions, connectorFactory, ecosystem } = args; + const walletId = ecosystem ? ecosystem.id : "inApp"; const emitter = createWalletEmitter<"inApp">(); let account: Account | undefined = undefined; let chain: Chain | undefined = undefined; let client: ThirdwebClient | undefined; return { - id: "inApp", + id: walletId, subscribe: emitter.subscribe, getChain() { if (!chain) { @@ -59,6 +64,7 @@ export function createInAppWallet(args: { const connector = await getOrCreateInAppWalletConnector( options.client, connectorFactory, + ecosystem, ); const [connectedAccount, connectedChain] = await autoConnectInAppWallet( options, @@ -72,7 +78,7 @@ export function createInAppWallet(args: { chain = connectedChain; trackConnect({ client: options.client, - walletType: "inApp", + walletType: walletId, walletAddress: account.address, }); // return only the account @@ -83,6 +89,7 @@ export function createInAppWallet(args: { const connector = await getOrCreateInAppWalletConnector( options.client, connectorFactory, + ecosystem, ); const [connectedAccount, connectedChain] = await connectInAppWallet( @@ -96,7 +103,7 @@ export function createInAppWallet(args: { chain = connectedChain; trackConnect({ client: options.client, - walletType: "inApp", + walletType: walletId, walletAddress: account.address, }); // return only the account @@ -108,6 +115,7 @@ export function createInAppWallet(args: { const connector = await getOrCreateInAppWalletConnector( client, connectorFactory, + ecosystem, ); const result = await connector.logout(); if (!result.success) { @@ -125,6 +133,7 @@ export function createInAppWallet(args: { const connector = await getOrCreateInAppWalletConnector( client, connectorFactory, + ecosystem, ); const [connectedAccount, connectedChain] = await autoConnectInAppWallet( { @@ -142,5 +151,5 @@ export function createInAppWallet(args: { } emitter.emit("chainChanged", newChain); }, - } as Wallet<"inApp">; + }; } diff --git a/packages/thirdweb/src/wallets/in-app/native/in-app.ts b/packages/thirdweb/src/wallets/in-app/native/in-app.ts index 78fae0f65a0..aba02bb5e20 100644 --- a/packages/thirdweb/src/wallets/in-app/native/in-app.ts +++ b/packages/thirdweb/src/wallets/in-app/native/in-app.ts @@ -67,5 +67,5 @@ export function inAppWallet( passkeyDomain: createOptions?.auth?.passkeyDomain, }); }, - }); + }) as Wallet<"inApp">; } diff --git a/packages/thirdweb/src/wallets/in-app/web/ecosystem.ts b/packages/thirdweb/src/wallets/in-app/web/ecosystem.ts index 770bd259a04..0b494787290 100644 --- a/packages/thirdweb/src/wallets/in-app/web/ecosystem.ts +++ b/packages/thirdweb/src/wallets/in-app/web/ecosystem.ts @@ -4,7 +4,7 @@ import type { CreateWalletArgs, EcosystemWalletId, } from "../../wallet-types.js"; -import { createEcosystemWallet } from "../core/wallet/ecosystem-core.js"; +import { createInAppWallet } from "../core/wallet/in-app-core.js"; /** * Creates an [Ecosystem Wallet](https://portal.thirdweb.com/connect/ecosystems/overview) based on various authentication methods. @@ -65,8 +65,11 @@ export function ecosystemWallet( ...args: CreateWalletArgs ): Wallet { const [ecosystemId, createOptions] = args; - return createEcosystemWallet({ - id: ecosystemId, + return createInAppWallet({ + ecosystem: { + id: ecosystemId, + partnerId: createOptions?.partnerId, + }, createOptions, connectorFactory: async (client: ThirdwebClient) => { const { InAppWebConnector } = await import("./lib/web-connector.js"); @@ -78,5 +81,5 @@ export function ecosystemWallet( }, }); }, - }); + }) as Wallet; } diff --git a/packages/thirdweb/src/wallets/in-app/web/in-app.ts b/packages/thirdweb/src/wallets/in-app/web/in-app.ts index 2f947c3fe67..d0507ff5cd7 100644 --- a/packages/thirdweb/src/wallets/in-app/web/in-app.ts +++ b/packages/thirdweb/src/wallets/in-app/web/in-app.ts @@ -206,5 +206,5 @@ export function inAppWallet( passkeyDomain: createOptions?.auth?.passkeyDomain, }); }, - }); + }) as Wallet<"inApp">; } diff --git a/packages/thirdweb/src/wallets/in-app/web/lib/actions/sign-message.enclave.ts b/packages/thirdweb/src/wallets/in-app/web/lib/actions/sign-message.enclave.ts index 4821dde3516..c490a6813ca 100644 --- a/packages/thirdweb/src/wallets/in-app/web/lib/actions/sign-message.enclave.ts +++ b/packages/thirdweb/src/wallets/in-app/web/lib/actions/sign-message.enclave.ts @@ -2,13 +2,14 @@ import type { ThirdwebClient } from "../../../../../client/client.js"; import { getThirdwebBaseUrl } from "../../../../../utils/domains.js"; import { getClientFetch } from "../../../../../utils/fetch.js"; import { stringify } from "../../../../../utils/json.js"; +import type { ClientScopedStorage } from "../../../core/authentication/client-scoped-storage.js"; import type { Ecosystem } from "../../../core/wallet/types.js"; -import { getAuthToken } from "../get-auth-token.js"; export async function signMessage({ client, ecosystem, payload: { message, isRaw }, + storage, }: { client: ThirdwebClient; ecosystem?: Ecosystem; @@ -16,9 +17,10 @@ export async function signMessage({ message: string; isRaw: boolean; }; + storage: ClientScopedStorage; }) { const clientFetch = getClientFetch(client, ecosystem); - const authToken = await getAuthToken(client, ecosystem); // TODO (enclave): pass storage from web/native + const authToken = await storage.getAuthCookie(); const response = await clientFetch( `${getThirdwebBaseUrl("inAppWallet")}/api/v1/enclave-wallet/sign-message`, diff --git a/packages/thirdweb/src/wallets/in-app/web/lib/actions/sign-transaction.enclave.ts b/packages/thirdweb/src/wallets/in-app/web/lib/actions/sign-transaction.enclave.ts index c79c9fe86e9..beed149a09d 100644 --- a/packages/thirdweb/src/wallets/in-app/web/lib/actions/sign-transaction.enclave.ts +++ b/packages/thirdweb/src/wallets/in-app/web/lib/actions/sign-transaction.enclave.ts @@ -3,21 +3,23 @@ import { getThirdwebBaseUrl } from "../../../../../utils/domains.js"; import type { Hex } from "../../../../../utils/encoding/hex.js"; import { getClientFetch } from "../../../../../utils/fetch.js"; import { stringify } from "../../../../../utils/json.js"; +import type { ClientScopedStorage } from "../../../core/authentication/client-scoped-storage.js"; import type { Ecosystem } from "../../../core/wallet/types.js"; -import { getAuthToken } from "../get-auth-token.js"; export async function signTransaction({ client, ecosystem, payload, + storage, }: { client: ThirdwebClient; ecosystem?: Ecosystem; payload: Record; + storage: ClientScopedStorage; }) { console.log("payload", payload); const clientFetch = getClientFetch(client, ecosystem); - const authToken = await getAuthToken(client, ecosystem); // TODO (enclave): pass storage from web/native + const authToken = await storage.getAuthCookie(); const response = await clientFetch( `${getThirdwebBaseUrl("inAppWallet")}/api/v1/enclave-wallet/sign-transaction`, diff --git a/packages/thirdweb/src/wallets/in-app/web/lib/actions/sign-typed-data.enclave.ts b/packages/thirdweb/src/wallets/in-app/web/lib/actions/sign-typed-data.enclave.ts index bf48cd12a9d..a91931d4bd3 100644 --- a/packages/thirdweb/src/wallets/in-app/web/lib/actions/sign-typed-data.enclave.ts +++ b/packages/thirdweb/src/wallets/in-app/web/lib/actions/sign-typed-data.enclave.ts @@ -4,8 +4,8 @@ import type { ThirdwebClient } from "../../../../../client/client.js"; import { getThirdwebBaseUrl } from "../../../../../utils/domains.js"; import { getClientFetch } from "../../../../../utils/fetch.js"; import { stringify } from "../../../../../utils/json.js"; +import type { ClientScopedStorage } from "../../../core/authentication/client-scoped-storage.js"; import type { Ecosystem } from "../../../core/wallet/types.js"; -import { getAuthToken } from "../get-auth-token.js"; export async function signTypedData< const typedData extends TypedData | Record, @@ -14,13 +14,15 @@ export async function signTypedData< client, ecosystem, payload, + storage, }: { client: ThirdwebClient; ecosystem?: Ecosystem; payload: TypedDataDefinition; + storage: ClientScopedStorage; }) { const clientFetch = getClientFetch(client, ecosystem); - const authToken = await getAuthToken(client, ecosystem); // TODO (enclave): pass storage from web/native + const authToken = await storage.getAuthCookie(); const response = await clientFetch( `${getThirdwebBaseUrl("inAppWallet")}/api/v1/enclave-wallet/sign-typed-data`, diff --git a/packages/thirdweb/src/wallets/in-app/web/lib/enclave-wallet.ts b/packages/thirdweb/src/wallets/in-app/web/lib/enclave-wallet.ts index 4f4a07abefc..4e623367bb4 100644 --- a/packages/thirdweb/src/wallets/in-app/web/lib/enclave-wallet.ts +++ b/packages/thirdweb/src/wallets/in-app/web/lib/enclave-wallet.ts @@ -136,6 +136,7 @@ export class EnclaveWallet implements IWebWallet { async getAccount(): Promise { const client = this.client; const ecosystem = this.ecosystem; + const storage = this.localStorage; const _signTransaction = async (tx: SendTransactionOption) => { const rpcRequest = getRpcClient({ @@ -176,6 +177,7 @@ export class EnclaveWallet implements IWebWallet { return signEnclaveTransaction({ client, ecosystem, + storage, payload: transaction, }); }; @@ -223,6 +225,7 @@ export class EnclaveWallet implements IWebWallet { client, ecosystem, payload: messagePayload, + storage, }); return signature as Hex; }, @@ -232,6 +235,7 @@ export class EnclaveWallet implements IWebWallet { client, ecosystem, payload: parsedTypedData, + storage, }); return signature as Hex; diff --git a/packages/thirdweb/src/wallets/in-app/web/lib/get-auth-token.ts b/packages/thirdweb/src/wallets/in-app/web/lib/get-auth-token.ts deleted file mode 100644 index c1e12a1f903..00000000000 --- a/packages/thirdweb/src/wallets/in-app/web/lib/get-auth-token.ts +++ /dev/null @@ -1,18 +0,0 @@ -import type { ThirdwebClient } from "../../../../client/client.js"; -import { webLocalStorage } from "../../../../utils/storage/webStorage.js"; -import { ClientScopedStorage } from "../../core/authentication/client-scoped-storage.js"; -import type { Ecosystem } from "../../core/wallet/types.js"; - -// TODO: Remove this and properly pass storage from web / native -export async function getAuthToken( - client: ThirdwebClient, - ecosystem?: Ecosystem, -) { - const localStorage = new ClientScopedStorage({ - storage: webLocalStorage, - clientId: client.clientId, - ecosystemId: ecosystem?.id, - }); - - return localStorage.getAuthCookie(); -} diff --git a/packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts b/packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts index 01eb9d8a76d..9142fbd6874 100644 --- a/packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts +++ b/packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts @@ -34,7 +34,6 @@ import { Auth, type AuthQuerierTypes } from "./auth/iframe-auth.js"; import { loginWithOauth, loginWithOauthRedirect } from "./auth/oauth.js"; import { sendOtp, verifyOtp } from "./auth/otp.js"; import { EnclaveWallet } from "./enclave-wallet.js"; -import { getAuthToken } from "./get-auth-token.js"; import { IFrameWallet } from "./iframe-wallet.js"; /** @@ -159,7 +158,7 @@ export class InAppWebConnector implements InAppConnector { } async initializeWallet(authToken?: string) { - const storedAuthToken = await getAuthToken(this.client, this.ecosystem); + const storedAuthToken = await this.localStorage.getAuthCookie(); if (!authToken && storedAuthToken === null) { throw new Error( "No auth token provided and no stored auth token found to initialize the wallet", @@ -224,7 +223,7 @@ export class InAppWebConnector implements InAppConnector { async getUser(): Promise { // If we don't have a wallet yet we'll create one if (!this.wallet) { - const maybeAuthToken = await getAuthToken(this.client, this.ecosystem); + const maybeAuthToken = await this.localStorage.getAuthCookie(); if (!maybeAuthToken) { return { status: UserWalletStatus.LOGGED_OUT }; }