From e168cfc541973daaa743889c4cb03eb98773dc3d Mon Sep 17 00:00:00 2001 From: chambaz Date: Fri, 15 Nov 2024 10:56:07 -0500 Subject: [PATCH] chore: remove salt and requirement for additional api request --- apps/marginfi-v2-ui/src/middleware.ts | 5 +---- apps/marginfi-v2-ui/src/pages/_app.tsx | 10 ++-------- apps/marginfi-v2-ui/src/pages/api/endpoint.ts | 17 ----------------- packages/mrgn-utils/src/mrgnUtils.ts | 4 ++-- 4 files changed, 5 insertions(+), 31 deletions(-) delete mode 100644 apps/marginfi-v2-ui/src/pages/api/endpoint.ts diff --git a/apps/marginfi-v2-ui/src/middleware.ts b/apps/marginfi-v2-ui/src/middleware.ts index 3c2faf260..c69c75afb 100644 --- a/apps/marginfi-v2-ui/src/middleware.ts +++ b/apps/marginfi-v2-ui/src/middleware.ts @@ -16,10 +16,7 @@ const allowedOrigins = [ ]; export async function middleware(req: NextRequest) { - const fullRpcProxy = await generateEndpoint( - process.env.NEXT_PUBLIC_MARGINFI_RPC_ENDPOINT_OVERRIDE ?? "", - process.env.RPC_PROXY_SALT ?? "" - ); + const fullRpcProxy = await generateEndpoint(process.env.NEXT_PUBLIC_MARGINFI_RPC_ENDPOINT_OVERRIDE ?? ""); if (req.nextUrl.toString() === fullRpcProxy) { const origin = req.headers.get("origin") ?? ""; diff --git a/apps/marginfi-v2-ui/src/pages/_app.tsx b/apps/marginfi-v2-ui/src/pages/_app.tsx index 352ca8f94..2eabce0ca 100644 --- a/apps/marginfi-v2-ui/src/pages/_app.tsx +++ b/apps/marginfi-v2-ui/src/pages/_app.tsx @@ -11,7 +11,7 @@ import { ToastContainer } from "react-toastify"; import { Analytics } from "@vercel/analytics/react"; import { registerMoonGateWallet } from "@moongate/moongate-adapter"; -import { cn, DEFAULT_MAX_CAP, Desktop, Mobile, init as initAnalytics } from "@mrgnlabs/mrgn-utils"; +import { cn, generateEndpoint, DEFAULT_MAX_CAP, Desktop, Mobile, init as initAnalytics } from "@mrgnlabs/mrgn-utils"; import { ActionBoxProvider, ActionProvider, AuthDialog } from "@mrgnlabs/mrgn-ui"; import config from "~/config"; @@ -76,12 +76,6 @@ export default function MrgnApp({ Component, pageProps, path }: AppProps & MrgnA const [ready, setReady] = React.useState(false); const [rpcEndpoint, setRpcEndpoint] = React.useState(""); - const fetchRpcEndpoint = async () => { - const res = await fetch(`/api/endpoint?url=${config.rpcEndpoint}`); - const data = await res.json(); - return data.url; - }; - React.useEffect(() => { const isFetchingData = isRefreshingMrgnlendStore; setIsFetchingData(isFetchingData); @@ -89,7 +83,7 @@ export default function MrgnApp({ Component, pageProps, path }: AppProps & MrgnA React.useEffect(() => { const init = async () => { - const rpcEndpoint = await fetchRpcEndpoint(); + const rpcEndpoint = await generateEndpoint(config.rpcEndpoint); setRpcEndpoint(rpcEndpoint); setReady(true); initAnalytics(); diff --git a/apps/marginfi-v2-ui/src/pages/api/endpoint.ts b/apps/marginfi-v2-ui/src/pages/api/endpoint.ts deleted file mode 100644 index 6d2e60ac9..000000000 --- a/apps/marginfi-v2-ui/src/pages/api/endpoint.ts +++ /dev/null @@ -1,17 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; -import { generateEndpoint } from "@mrgnlabs/mrgn-utils"; - -type EndpointResponse = { - url: string; -}; - -export default async function handler(req: NextApiRequest, res: NextApiResponse) { - const endpoint = req.query.url as string; - - const url = await generateEndpoint(endpoint, process.env.RPC_PROXY_SALT ?? ""); - - res.setHeader("Cache-Control", "public, max-age=14400"); - return res.status(200).json({ - url, - }); -} diff --git a/packages/mrgn-utils/src/mrgnUtils.ts b/packages/mrgn-utils/src/mrgnUtils.ts index 2de795a90..63304e4d4 100644 --- a/packages/mrgn-utils/src/mrgnUtils.ts +++ b/packages/mrgn-utils/src/mrgnUtils.ts @@ -147,11 +147,11 @@ export function isBankOracleStale(bank: ExtendedBankInfo) { return isStale; } -export async function generateEndpoint(endpoint: string, salt: string) { +export async function generateEndpoint(endpoint: string) { const now = new Date(); const midnight = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() + 1)); const timestamp = Math.floor(midnight.getTime() / 1000); - const msgUint8 = new TextEncoder().encode(`${endpoint}-${timestamp}-${salt}`); + const msgUint8 = new TextEncoder().encode(`${endpoint}-${timestamp}`); const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8); const hashArray = Array.from(new Uint8Array(hashBuffer)); const hash = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");