-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49f2090
commit da21df2
Showing
3 changed files
with
141 additions
and
24 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
packages/nextjs/pages/api/devcon/check-eligibility/[builderAddress].ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import { isAddress } from "viem"; | ||
|
||
type Data = { | ||
isEligible: boolean; | ||
type: "builder" | "batch" | null; | ||
}; | ||
|
||
type ErrorResponse = { | ||
error: string; | ||
}; | ||
|
||
export default function handler(req: NextApiRequest, res: NextApiResponse<Data | ErrorResponse>) { | ||
const { builderAddress } = req.query; | ||
|
||
if (typeof builderAddress !== "string") { | ||
return res.status(400).json({ error: "Invalid builder address" }); | ||
} | ||
|
||
// Validate the builder address | ||
if (!isAddress(builderAddress)) { | ||
return res.status(400).json({ error: "Invalid Ethereum address" }); | ||
} | ||
|
||
const isEligible = Math.random() > 0.5; | ||
const type = isEligible ? (Math.random() > 0.5 ? "builder" : "batch") : null; | ||
|
||
res.status(200).json({ isEligible, type }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import { isAddress, verifyMessage } from "viem"; | ||
|
||
type RequestBody = { | ||
signature: `0x${string}`; | ||
message: string; | ||
builderAddress: string; | ||
}; | ||
|
||
type SuccessResponse = { | ||
voucher: string; | ||
}; | ||
|
||
type ErrorResponse = { | ||
error: string; | ||
}; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse<SuccessResponse | ErrorResponse>) { | ||
if (req.method !== "POST") { | ||
return res.status(405).json({ error: "Method Not Allowed" }); | ||
} | ||
|
||
const { signature, message, builderAddress } = req.body as RequestBody; | ||
|
||
if (!signature || !message || !builderAddress) { | ||
return res.status(400).json({ error: "Missing required fields" }); | ||
} | ||
|
||
if (!isAddress(builderAddress)) { | ||
return res.status(400).json({ error: "Invalid Ethereum address" }); | ||
} | ||
|
||
try { | ||
const isValid = await verifyMessage({ | ||
address: builderAddress, | ||
message, | ||
signature, | ||
}); | ||
|
||
if (!isValid) { | ||
return res.status(401).json({ error: "Invalid signature" }); | ||
} | ||
} catch (error) { | ||
return res.status(400).json({ error: "Signature verification failed" }); | ||
} | ||
const voucher = Math.random().toString(36).substring(2, 8).toUpperCase(); | ||
|
||
res.status(200).json({ voucher }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters