Skip to content

Commit

Permalink
ecosystem wallet refactor (part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquim-verges committed Sep 24, 2024
1 parent c0d81dc commit 822a11a
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 171 deletions.
12 changes: 7 additions & 5 deletions packages/thirdweb/src/wallets/create-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ export function createWallet<const ID extends WalletId>(
creationOptions as CreateWalletArgs<"inApp">[1],
) as Wallet<ID>;
}
/**
* ECOSYSTEM WALLET
*/
case isEcosystemWallet(id):
return ecosystemWallet(
...(args as CreateWalletArgs<EcosystemWalletId>),
) as Wallet<ID>;

/**
* COINBASE WALLET VIA SDK
Expand All @@ -160,11 +167,6 @@ export function createWallet<const ID extends WalletId>(
},
}) as Wallet<ID>;
}
case isEcosystemWallet(id):
return ecosystemWallet(
...(args as CreateWalletArgs<EcosystemWalletId>),
) as Wallet<ID>;

/**
* WALLET CONNECT AND INJECTED WALLETS + walletConnect standalone
*/
Expand Down
7 changes: 2 additions & 5 deletions packages/thirdweb/src/wallets/ecosystem/types.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
121 changes: 0 additions & 121 deletions packages/thirdweb/src/wallets/in-app/core/wallet/ecosystem-core.ts

This file was deleted.

23 changes: 16 additions & 7 deletions packages/thirdweb/src/wallets/in-app/core/wallet/in-app-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -33,15 +36,17 @@ export async function getOrCreateInAppWalletConnector(
export function createInAppWallet(args: {
createOptions?: CreateWalletArgs<"inApp">[1];
connectorFactory: (client: ThirdwebClient) => Promise<InAppConnector>;
}): 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) {
Expand All @@ -59,6 +64,7 @@ export function createInAppWallet(args: {
const connector = await getOrCreateInAppWalletConnector(
options.client,
connectorFactory,
ecosystem,
);
const [connectedAccount, connectedChain] = await autoConnectInAppWallet(
options,
Expand All @@ -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
Expand All @@ -83,6 +89,7 @@ export function createInAppWallet(args: {
const connector = await getOrCreateInAppWalletConnector(
options.client,
connectorFactory,
ecosystem,
);

const [connectedAccount, connectedChain] = await connectInAppWallet(
Expand All @@ -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
Expand All @@ -108,6 +115,7 @@ export function createInAppWallet(args: {
const connector = await getOrCreateInAppWalletConnector(
client,
connectorFactory,
ecosystem,
);
const result = await connector.logout();
if (!result.success) {
Expand All @@ -125,6 +133,7 @@ export function createInAppWallet(args: {
const connector = await getOrCreateInAppWalletConnector(
client,
connectorFactory,
ecosystem,
);
const [connectedAccount, connectedChain] = await autoConnectInAppWallet(
{
Expand All @@ -142,5 +151,5 @@ export function createInAppWallet(args: {
}
emitter.emit("chainChanged", newChain);
},
} as Wallet<"inApp">;
};
}
2 changes: 1 addition & 1 deletion packages/thirdweb/src/wallets/in-app/native/in-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ export function inAppWallet(
passkeyDomain: createOptions?.auth?.passkeyDomain,
});
},
});
}) as Wallet<"inApp">;
}
11 changes: 7 additions & 4 deletions packages/thirdweb/src/wallets/in-app/web/ecosystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -65,8 +65,11 @@ export function ecosystemWallet(
...args: CreateWalletArgs<EcosystemWalletId>
): Wallet<EcosystemWalletId> {
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");
Expand All @@ -78,5 +81,5 @@ export function ecosystemWallet(
},
});
},
});
}) as Wallet<EcosystemWalletId>;
}
2 changes: 1 addition & 1 deletion packages/thirdweb/src/wallets/in-app/web/in-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,5 @@ export function inAppWallet(
passkeyDomain: createOptions?.auth?.passkeyDomain,
});
},
});
}) as Wallet<"inApp">;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@ 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;
payload: {
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`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Hex | number | undefined>;
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`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>,
Expand All @@ -14,13 +14,15 @@ export async function signTypedData<
client,
ecosystem,
payload,
storage,
}: {
client: ThirdwebClient;
ecosystem?: Ecosystem;
payload: TypedDataDefinition<typedData, primaryType>;
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`,
Expand Down
Loading

0 comments on commit 822a11a

Please sign in to comment.