Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldev20 committed Nov 16, 2024
1 parent 4e2b2b4 commit 27c38c6
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 13 deletions.
13 changes: 13 additions & 0 deletions packages/foundry/scripts-js/generateTsAbis.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@ function main() {
}
)
);

if (!existsSync("../nextjs-app/contracts/")) {
mkdirSync("../nextjs-app/contracts/");
}
writeFileSync(
`../nextjs-app/contracts/deployedContracts.ts`,
format(
`${generatedContractComment} \n\nconst deployedContracts = {${fileContent}} as const;\n`,
{
parser: "typescript",
}
)
);
}

try {
Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion packages/nextjs-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@
"@react-aria/visually-hidden": "3.8.12",
"@worldcoin/minikit-react": "^1.2.0",
"clsx": "2.1.1",
"eruda": "^3.4.1",
"framer-motion": "~11.1.1",
"intl-messageformat": "^10.5.0",
"next": "14.2.4",
"next-themes": "^0.2.1",
"react": "18.3.1",
"react-dom": "18.3.1"
"react-dom": "18.3.1",
"viem": "^2.21.45"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
Expand Down
15 changes: 15 additions & 0 deletions packages/nextjs-app/src/app/api/nonce/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";
import crypto from "crypto";

export function GET(request: NextRequest) {
const uuid = crypto.randomUUID().replace(/-/g, "");
const nonce = crypto
.createHmac("sha256", process.env.NONCE_SECRET || "")
.update(uuid)
.digest("hex");

cookies().set("siwe", nonce, { secure: true });

return NextResponse.json({ nonce });
}
32 changes: 32 additions & 0 deletions packages/nextjs-app/src/app/api/siwe/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";
import { MiniAppWalletAuthSuccessPayload, verifySiweMessage } from "@worldcoin/minikit-js";

interface IRequestPayload {
payload: MiniAppWalletAuthSuccessPayload;
nonce: string;
}

export async function POST(req: NextRequest) {
const { payload, nonce } = (await req.json()) as IRequestPayload;
if (nonce != cookies().get("siwe")?.value) {
return NextResponse.json({
status: "error",
isValid: false,
message: "Invalid nonce",
});
}
try {
const validMessage = await verifySiweMessage(payload, nonce);
return NextResponse.json({
status: "success",
isValid: validMessage.isValid,
});
} catch (error: any) {
return NextResponse.json({
status: "error",
isValid: false,
message: error.message,
});
}
}
9 changes: 6 additions & 3 deletions packages/nextjs-app/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Inter } from "next/font/google";
import { NextProviders } from "../providers/nextUiProvider";
import { appConfig } from "@/appConfig";
import { ErudaProvider } from "@/components/eruda";
import { MiniKitProvider } from "@/providers/miniKitProvider";
import "@/styles/globals.css";
import clsx from "clsx";
Expand Down Expand Up @@ -30,9 +31,11 @@ export default function RootLayout({ children }: { children: React.ReactNode })
return (
<html suppressHydrationWarning lang="en">
<head />
<body className={clsx("min-h-screen bg-background", inter.className)}>
<NextProviders themeProps={{ attribute: "class", defaultTheme: "dark" }}>
<MiniKitProvider>{children}</MiniKitProvider>
<body className={clsx("min-h-dvh bg-background touch-none", inter.className)}>
<NextProviders themeProps={{ attribute: "class", defaultTheme: "light" }}>
<ErudaProvider>
<MiniKitProvider>{children}</MiniKitProvider>
</ErudaProvider>
</NextProviders>
</body>
</html>
Expand Down
85 changes: 79 additions & 6 deletions packages/nextjs-app/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,92 @@
"use client";

import { useState } from "react";
import { useRouter } from "next/navigation";
import { Button } from "@nextui-org/button";
import { MiniKit, ResponseEvent } from "@worldcoin/minikit-js";
import { createPublicClient, http, getContract } from "viem";
import { worldchain } from "viem/chains";
import deployedContracts from "../../../nextjs/contracts/deployedContracts";

const subscribeToWalletAuth = async (nonce: string) => {
return new Promise((resolve, reject) => {
MiniKit.subscribe(ResponseEvent.MiniAppWalletAuth, async payload => {
if (payload.status === "error") {
reject(new Error("Payload status is error"));
} else {
try {
const response = await fetch("/api/siwe", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
payload: payload,
nonce,
}),
});

if (!response.ok) {
reject(new Error("Failed to complete SIWE"));
}

const responseData = await response.json();
resolve(responseData);
} catch (error) {
reject(error);
}
}
});
});
};

export default function LoginPage() {
const [loading, setLoading] = useState(false);
const router = useRouter();
const onLoginSignup = () => {
console.log("Login / Sign Up");
router.replace("/statements");
const onLoginSignup = async () => {
setLoading(true);

const client = createPublicClient({
chain: worldchain,
transport: http("https://worldchain-mainnet.g.alchemy.com/public"),
});
// const contract = getContract({
// address: deployedContracts[worldchain].HumanOracle.address,
// })

// get wallet auth
try {
const res = await fetch(`/api/nonce`);
const { nonce } = await res.json();

const generateMessageResult = MiniKit.commands.walletAuth({
nonce: nonce,
expirationTime: new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000),
notBefore: new Date(new Date().getTime() - 24 * 60 * 60 * 1000),
statement: "Login or create an account",
});
console.log(generateMessageResult);
const result = await subscribeToWalletAuth(nonce);
} catch (error) {
console.error(error);
setLoading(false);
return;
}

// // check if registered?
// try {

// }

// // if not registered, register
// try {

// }
};

return (
<section className="h-screen flex flex-col items-center justify-center">
{/* <h1>Logo</h1> */}
<Button color="primary" radius="sm" onClick={onLoginSignup}>
<section className="h-dvh flex flex-col items-center justify-center">
<Button color="primary" radius="sm" onClick={onLoginSignup} isLoading={loading}>
Sign In / Sign Up
</Button>
</section>
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs-app/src/app/statements/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function HomePage() {
const [results, setResults] = useState([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);

return (
<section className="h-screen flex flex-col items-center">
<section className="h-dvh flex flex-col items-center">
<Navbar
startContent={<AddressComponent address="vitalik.eth" />}
endContent={<TotalPayout total={100} />}
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs-app/src/app/vote/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function VotePage({ params }: VotePageProps) {
const router = useRouter();

return (
<section className="h-screen flex flex-col items-center">
<section className="h-dvh flex flex-col items-center">
<Navbar
startContent={
<IconButton icon={<ChevronLeftIcon className="size-7" onClick={() => router.back()} />} />
Expand Down
18 changes: 18 additions & 0 deletions packages/nextjs-app/src/components/eruda/eruda-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use client";

import { ReactNode, useEffect } from "react";
import eruda from "eruda";

export const Eruda = (props: { children: ReactNode }) => {
useEffect(() => {
if (typeof window !== "undefined") {
try {
eruda.init();
} catch (error) {
console.log("Eruda failed to initialize", error);
}
}
}, []);

return <>{props.children}</>;
};
15 changes: 15 additions & 0 deletions packages/nextjs-app/src/components/eruda/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use client";

import { ReactNode } from "react";
import dynamic from "next/dynamic";

const Eruda = dynamic(() => import("./eruda-provider").then(c => c.Eruda), {
ssr: false,
});

export const ErudaProvider = (props: { children: ReactNode }) => {
if (process.env.NEXT_PUBLIC_APP_ENV === "production") {
return props.children;
}
return <Eruda>{props.children}</Eruda>;
};
Empty file.
11 changes: 10 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6600,6 +6600,13 @@ __metadata:
languageName: node
linkType: hard

"eruda@npm:^3.4.1":
version: 3.4.1
resolution: "eruda@npm:3.4.1"
checksum: ff6762d839877d5dcd77830718a23b3bf21e9aaa4a8f029fdb9ba9e422d6439ab1b15eaee84552c6544cfdae3a9c97b75038504f9279c1789cb1d9ba16384fe4
languageName: node
linkType: hard

"es-abstract@npm:^1.17.5, es-abstract@npm:^1.22.1, es-abstract@npm:^1.22.3, es-abstract@npm:^1.23.0, es-abstract@npm:^1.23.1, es-abstract@npm:^1.23.2, es-abstract@npm:^1.23.3":
version: 1.23.5
resolution: "es-abstract@npm:1.23.5"
Expand Down Expand Up @@ -10053,6 +10060,7 @@ __metadata:
"@worldcoin/minikit-react": ^1.2.0
autoprefixer: 10.4.19
clsx: 2.1.1
eruda: ^3.4.1
eslint: ^8.57.0
eslint-config-next: 14.2.1
eslint-config-prettier: 9.1.0
Expand All @@ -10074,6 +10082,7 @@ __metadata:
tailwind-variants: 0.1.20
tailwindcss: 3.4.3
typescript: 5.6.3
viem: ^2.21.45
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -13293,7 +13302,7 @@ __metadata:
languageName: node
linkType: hard

"viem@npm:^2.1.1, viem@npm:^2.17.0":
"viem@npm:^2.1.1, viem@npm:^2.17.0, viem@npm:^2.21.45":
version: 2.21.45
resolution: "viem@npm:2.21.45"
dependencies:
Expand Down

0 comments on commit 27c38c6

Please sign in to comment.