Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show user info for ecosystem wallets #4715

Merged
merged 1 commit into from
Sep 20, 2024
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/quick-hotels-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Fix showing user info for ecosystem wallets
33 changes: 30 additions & 3 deletions packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@
import { isContractDeployed } from "../../../../utils/bytecode/is-contract-deployed.js";
import { formatNumber } from "../../../../utils/formatNumber.js";
import { webLocalStorage } from "../../../../utils/storage/webStorage.js";
import { isEcosystemWallet } from "../../../../wallets/ecosystem/is-ecosystem-wallet.js";
import type { Ecosystem } from "../../../../wallets/in-app/web/types.js";
import type { Account, Wallet } from "../../../../wallets/interfaces/wallet.js";
import type { SmartWalletOptions } from "../../../../wallets/smart/types.js";
import {
type AppMetadata,
type SocialAuthOption,
socialAuthOptions,
} from "../../../../wallets/types.js";
import type { WalletId } from "../../../../wallets/wallet-types.js";
import type {
EcosystemWalletId,
WalletId,
} from "../../../../wallets/wallet-types.js";
import {
CustomThemeProvider,
parseTheme,
Expand Down Expand Up @@ -53,6 +58,7 @@
import { useActiveAccount } from "../../../core/hooks/wallets/useActiveAccount.js";
import { useActiveWallet } from "../../../core/hooks/wallets/useActiveWallet.js";
import { useActiveWalletChain } from "../../../core/hooks/wallets/useActiveWalletChain.js";
import { useAdminWallet } from "../../../core/hooks/wallets/useAdminAccount.js";
import { useDisconnect } from "../../../core/hooks/wallets/useDisconnect.js";
import { useSwitchActiveWalletChain } from "../../../core/hooks/wallets/useSwitchActiveWalletChain.js";
import { SetRootElementContext } from "../../../core/providers/RootElementContext.js";
Expand Down Expand Up @@ -1066,6 +1072,7 @@
const { client, locale } = props;
const account = useActiveAccount();
const activeWallet = useActiveWallet();
const adminWallet = useAdminWallet();

Check warning on line 1075 in packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx#L1075

Added line #L1075 was not covered by tests
const { data: walletInfo } = useWalletInfo(activeWallet?.id);
const isSmartWallet = hasSmartAccount(activeWallet);
const { data: walletName } = useQuery({
Expand All @@ -1092,25 +1099,45 @@
const userInfoQuery = useQuery({
queryKey: ["in-app-wallet-user", client, account?.address],
queryFn: async () => {
const isInAppWallet =
adminWallet &&
joaquim-verges marked this conversation as resolved.
Show resolved Hide resolved
(adminWallet.id === "inApp" || adminWallet.id.startsWith("ecosystem."));

Check warning on line 1104 in packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx#L1102-L1104

Added lines #L1102 - L1104 were not covered by tests

if (!isInAppWallet) {
return null;
}

Check warning on line 1108 in packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx#L1106-L1108

Added lines #L1106 - L1108 were not covered by tests

let ecosystem: Ecosystem | undefined;
if (isEcosystemWallet(adminWallet)) {
const ecosystemWallet = adminWallet as Wallet<EcosystemWalletId>;
const partnerId = ecosystemWallet.getConfig()?.partnerId;
ecosystem = {
id: ecosystemWallet.id,
partnerId,
};
}

Check warning on line 1118 in packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx#L1110-L1118

Added lines #L1110 - L1118 were not covered by tests

const { getUserEmail, getUserPhoneNumber } = await import(
"../../../../wallets/in-app/web/lib/auth/index.js"
);

const [email, phone] = await Promise.all([
getUserEmail({
client: client,
ecosystem,

Check warning on line 1127 in packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx#L1127

Added line #L1127 was not covered by tests
}),
getUserPhoneNumber({
client: client,
ecosystem,

Check warning on line 1131 in packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx#L1131

Added line #L1131 was not covered by tests
}),
]);

return email || phone || null;
},
enabled: !isSmartWallet,
enabled: !!adminWallet,

Check warning on line 1137 in packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx#L1137

Added line #L1137 was not covered by tests
});

if (isSmartWallet) {
if (!userInfoQuery.data && isSmartWallet) {

Check warning on line 1140 in packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/Details.tsx#L1140

Added line #L1140 was not covered by tests
return <ConnectedToSmartWallet client={client} connectLocale={locale} />;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,5 @@ export type GetUser =

export type GetAuthenticatedUserParams = {
client: ThirdwebClient;
ecosystem?: Ecosystem;
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
} from "../authentication/types.js";
import type { InAppConnector } from "../interfaces/connector.js";

const connectorCache = new WeakMap<
{ client: ThirdwebClient; ecosystem?: Ecosystem },
InAppConnector
>();
const connectorCache = new Map<string, InAppConnector>();

/**
* @internal
Expand All @@ -30,7 +27,7 @@
connectorFactory: (client: ThirdwebClient) => Promise<InAppConnector>,
ecosystem?: Ecosystem,
) {
const key = { client, ecosystem };
const key = JSON.stringify({ clientId: client.clientId, ecosystem });

Check warning on line 30 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#L30

Added line #L30 was not covered by tests
if (connectorCache.has(key)) {
return connectorCache.get(key) as InAppConnector;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { Ecosystem } from "../../types.js";
*
* @internal
*/
export async function getEnclaveUserStatus({
export async function getUserStatus({
authToken,
client,
ecosystem,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
import type { ClientIdWithQuerierType, Ecosystem } from "../../types.js";
import type { InAppWalletIframeCommunicator } from "../../utils/iFrameCommunication/InAppWalletIframeCommunicator.js";
import { generateWallet } from "../actions/generate-wallet.enclave.js";
import { getEnclaveUserStatus } from "../actions/get-enclave-user-status.js";
import { getUserStatus } from "../actions/get-enclave-user-status.js";
import { BaseLogin } from "./base-login.js";

export type AuthQuerierTypes = {
Expand Down Expand Up @@ -111,7 +111,7 @@ export class Auth {
): Promise<AuthLoginReturnType> {
await this.preLogin();

const user = await getEnclaveUserStatus({
const user = await getUserStatus({
authToken: authToken.storedToken.cookieString,
client: this.client,
ecosystem: this.ecosystem,
Expand Down
4 changes: 2 additions & 2 deletions packages/thirdweb/src/wallets/in-app/web/lib/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ async function getInAppWalletConnector(
export async function getAuthenticatedUser(
options: GetAuthenticatedUserParams,
) {
const { client } = options;
const connector = await getInAppWalletConnector(client);
const { client, ecosystem } = options;
const connector = await getInAppWalletConnector(client, ecosystem);
const user = await connector.getUser();
switch (user.status) {
case UserWalletStatus.LOGGED_IN_WALLET_INITIALIZED: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
type WalletAddressObjectType,
} from "../../core/authentication/types.js";
import type { Ecosystem } from "../types.js";
import { getEnclaveUserStatus } from "./actions/get-enclave-user-status.js";
import { getUserStatus } from "./actions/get-enclave-user-status.js";
import { signMessage as signEnclaveMessage } from "./actions/sign-message.enclave.js";
import { signTransaction as signEnclaveTransaction } from "./actions/sign-transaction.enclave.js";
import { signTypedData as signEnclaveTypedData } from "./actions/sign-typed-data.enclave.js";
Expand Down Expand Up @@ -96,22 +96,23 @@ export class EnclaveWallet implements IWebWallet {
return { status: UserWalletStatus.LOGGED_OUT };
}

const userStatus = await getEnclaveUserStatus({
const userStatus = await getUserStatus({
authToken: token,
client: this.client,
ecosystem: this.ecosystem,
});

if (!userStatus) {
return { status: UserWalletStatus.LOGGED_OUT };
}
const wallet = userStatus.wallets[0];

const authDetails = {
email: userStatus.linkedAccounts.find(
(account) => account.type === "email",
(account) => account.details.email !== undefined,
)?.details.email,
phoneNumber: userStatus.linkedAccounts.find(
(account) => account.type === "phone",
(account) => account.details.phone !== undefined,
)?.details.phone,
userWalletId: userStatus.id || "",
recoveryShareManagement: RecoveryShareManagement.ENCLAVE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
UserWalletStatus,
} from "../../core/authentication/types.js";
import type { InAppConnector } from "../../core/interfaces/connector.js";
import { getEnclaveUserStatus } from "../lib/actions/get-enclave-user-status.js";
import { getUserStatus } from "../lib/actions/get-enclave-user-status.js";
import type { Ecosystem, InAppWalletConstructorType } from "../types.js";
import { InAppWalletIframeCommunicator } from "../utils/iFrameCommunication/InAppWalletIframeCommunicator.js";
import { Auth, type AuthQuerierTypes } from "./auth/iframe-auth.js";
Expand Down Expand Up @@ -152,7 +152,7 @@
);
}

const user = await getEnclaveUserStatus({
const user = await getUserStatus({

Check warning on line 155 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#L155

Added line #L155 was not covered by tests
authToken: authToken || (storedAuthToken as string),
client: this.client,
ecosystem: this.ecosystem,
Expand Down
Loading