Skip to content

Commit

Permalink
api: add backwards compat for in-app wallet user fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
alecananian committed Oct 10, 2024
1 parent eacd784 commit c62e734
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 17 deletions.
25 changes: 9 additions & 16 deletions apps/api/src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type { FastifyPluginAsync } from "fastify";

import "../middleware/chain";
import "../middleware/swagger";
import { type GetUserResult, getUser } from "thirdweb";
import type {
LoginBody,
LoginReply,
Expand All @@ -23,7 +22,9 @@ import {
} from "../schema";
import type { TdkApiContext } from "../types";
import { USER_PROFILE_SELECT_FIELDS, USER_SELECT_FIELDS } from "../utils/db";
import { log } from "../utils/log";
import {
getThirdwebUser,
parseThirdwebUserEmail,
transformUserProfileResponseFields,
} from "../utils/user";
Expand Down Expand Up @@ -115,24 +116,16 @@ export const authRoutes =
adminAddress = result[0];
} catch {
// Ignore lookup if this fails, address may not be a smart account if user connected with Web3 wallet
log.warn("Error fetching admin wallet for smart account:", address);
}

if (adminAddress) {
// Look up any associated user details in the embedded wallet
let thirdwebUser: GetUserResult | null | undefined;
try {
thirdwebUser = await getUser({
client,
ecosystem: {
id: env.THIRDWEB_ECOSYSTEM_ID,
partnerId: env.THIRDWEB_ECOSYSTEM_PARTNER_ID,
},
walletAddress: adminAddress,
});
} catch {
// Ignore failures from the Thirdweb SDK, this info is "nice-to-have"
}

const thirdwebUser = await getThirdwebUser({
client,
ecosystemId: env.THIRDWEB_ECOSYSTEM_ID,
ecosystemPartnerId: env.THIRDWEB_ECOSYSTEM_PARTNER_ID,
walletAddress: adminAddress,
});
if (thirdwebUser) {
const email = parseThirdwebUserEmail(thirdwebUser);
if (email) {
Expand Down
53 changes: 52 additions & 1 deletion apps/api/src/utils/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { UserProfile } from "@prisma/client";
import type { GetUserResult } from "thirdweb";
import {
DEFAULT_TDK_ECOSYSTEM_ID,
type EcosystemIdString,
} from "@treasure-dev/tdk-core";
import { type GetUserResult, type ThirdwebClient, getUser } from "thirdweb";

import { log } from "./log";

export const transformUserProfileResponseFields = (
profile: Partial<UserProfile>,
Expand All @@ -12,6 +18,51 @@ export const transformUserProfileResponseFields = (
profile.testnetFaucetLastUsedAt?.toISOString() ?? null,
});

export const getThirdwebUser = async ({
client,
ecosystemId = DEFAULT_TDK_ECOSYSTEM_ID,
ecosystemPartnerId,
walletAddress,
}: {
client: ThirdwebClient;
ecosystemId?: EcosystemIdString;
ecosystemPartnerId: string;
walletAddress: string;
}) => {
try {
const ecosystemWalletUser = await getUser({
client,
ecosystem: {
id: ecosystemId,
partnerId: ecosystemPartnerId,
},
walletAddress,
});
if (ecosystemWalletUser) {
return ecosystemWalletUser;
}
} catch (err) {
// Ignore failures from the Thirdweb SDK, this info is "nice-to-have"
log.warn("Error fetching Thirdweb ecosystem wallets user:", err);
}

// Fall back to querying in-app wallets (no ecosystem ID)
try {
const inAppWalletUser = await getUser({
client,
walletAddress,
});
if (inAppWalletUser) {
return inAppWalletUser;
}
} catch (err) {
// Ignore failures from the Thirdweb SDK, this info is "nice-to-have"
log.warn("Error fetching Thirdweb in-app wallets user:", err);
}

return undefined;
};

export const parseThirdwebUserEmail = (user: GetUserResult) => {
if (user.email) {
return user.email;
Expand Down

0 comments on commit c62e734

Please sign in to comment.