From 8479fdd3cda433d422889703797308e042b16984 Mon Sep 17 00:00:00 2001 From: Daniel Whiffing Date: Thu, 12 Sep 2024 14:39:25 -0400 Subject: [PATCH] Shows tee wallet address --- app/api/wallet/route.ts | 22 ++++++++++++++++++++++ app/page.tsx | 17 ++++++++++++++--- components/MagicProvider.tsx | 8 ++++++++ utils/tee.ts | 2 +- 4 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 app/api/wallet/route.ts diff --git a/app/api/wallet/route.ts b/app/api/wallet/route.ts new file mode 100644 index 0000000..ad237b9 --- /dev/null +++ b/app/api/wallet/route.ts @@ -0,0 +1,22 @@ +import { getWalletUUIDandAccessKey } from "@/utils/tee"; +import { Magic } from "@magic-sdk/admin"; +import { NextRequest, NextResponse } from "next/server"; + +export const runtime = "nodejs"; +const magic = await Magic.init(process.env.MAGIC_SECRET_KEY); + +export async function GET(req: NextRequest) { + try { + console.log(req.nextUrl.searchParams.get("didToken")); + const didToken = req.nextUrl.searchParams.get("didToken") ?? ""; + + const userMetadata = await magic.users.getMetadataByToken(didToken); + const publicAddress = userMetadata.publicAddress ?? ""; + + const result = await getWalletUUIDandAccessKey(publicAddress); + + return NextResponse.json({ wallet_address: result.wallet_address }); + } catch (e: any) { + return NextResponse.json({ error: e.message }, { status: e.status ?? 500 }); + } +} diff --git a/app/page.tsx b/app/page.tsx index 3eae0e9..737a492 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -16,8 +16,14 @@ export default function Page() { const { contracts } = useContracts(); const [editContractKey, setEditContractKey] = useState(null); const [isUploadModalOpen, setIsUploadModalOpen] = useState(false); - const { magic, isLoggedIn, handleLogin, handleLogout, isLoading } = - useMagic(); + const { + magic, + teeWalletAddress, + isLoggedIn, + handleLogin, + handleLogout, + isLoading, + } = useMagic(); return (
@@ -58,7 +64,12 @@ export default function Page() {
-
+
+ {teeWalletAddress && ( +

+ TEE Wallet: {teeWalletAddress} +

+ )} diff --git a/components/MagicProvider.tsx b/components/MagicProvider.tsx index b4aebad..c4136c9 100644 --- a/components/MagicProvider.tsx +++ b/components/MagicProvider.tsx @@ -12,6 +12,7 @@ export const MagicContext = createContext<{ handleLogin: () => void; isLoggedIn: boolean; isLoading: boolean; + teeWalletAddress: string | null; address: string | null; didToken: string | null; }>({ @@ -20,6 +21,7 @@ export const MagicContext = createContext<{ handleLogout: () => {}, handleLogin: () => {}, isLoggedIn: false, + teeWalletAddress: null, isLoading: false, address: null, didToken: null, @@ -32,6 +34,7 @@ const MagicProvider = ({ children }: any) => { const [web3, setWeb3] = useState(null); const [isLoggedIn, setIsLoggedIn] = useState(false); const [isLoading, setIsLoading] = useState(true); + const [teeWalletAddress, setTEEWalletAddress] = useState(null); const [address, setAddress] = useState(null); const [didToken, setDidToken] = useState(null); @@ -65,6 +68,10 @@ const MagicProvider = ({ children }: any) => { setAddress(address); let didToken = await magic.user.getIdToken(); setDidToken(didToken); + + const response = await fetch(`/api/wallet?didToken=${didToken}`); + const json = await response.json(); + setTEEWalletAddress(json.wallet_address); } setIsLoading(false); }; @@ -107,6 +114,7 @@ const MagicProvider = ({ children }: any) => { { try {