Skip to content

Commit

Permalink
chore: remove salt and requirement for additional api request
Browse files Browse the repository at this point in the history
  • Loading branch information
chambaz committed Nov 15, 2024
1 parent 8e37c50 commit e168cfc
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 31 deletions.
5 changes: 1 addition & 4 deletions apps/marginfi-v2-ui/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") ?? "";
Expand Down
10 changes: 2 additions & 8 deletions apps/marginfi-v2-ui/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -76,20 +76,14 @@ 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);
}, [isMrgnlendStoreInitialized, isRefreshingMrgnlendStore, setIsFetchingData]);

React.useEffect(() => {
const init = async () => {
const rpcEndpoint = await fetchRpcEndpoint();
const rpcEndpoint = await generateEndpoint(config.rpcEndpoint);
setRpcEndpoint(rpcEndpoint);
setReady(true);
initAnalytics();
Expand Down
17 changes: 0 additions & 17 deletions apps/marginfi-v2-ui/src/pages/api/endpoint.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/mrgn-utils/src/mrgnUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down

0 comments on commit e168cfc

Please sign in to comment.