-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
4 changed files
with
124 additions
and
11 deletions.
There are no files selected for viewing
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,53 @@ | ||
import { NextResponse } from "next/server"; | ||
import { recoverTypedDataAddress } from "viem"; | ||
import { createUser, findUserByAddress } from "~~/services/database/repositories/users"; | ||
import { EIP_712_DOMAIN, EIP_712_TYPES__REGISTER } from "~~/utils/eip712"; | ||
|
||
type RegisterPayload = { | ||
signerAddress?: string; | ||
signature?: `0x${string}`; | ||
}; | ||
|
||
export async function POST(req: Request) { | ||
try { | ||
const { signerAddress, signature } = (await req.json()) as RegisterPayload; | ||
|
||
if (!signerAddress || !signature) { | ||
return new Response("Missing signer or signature", { status: 400 }); | ||
} | ||
|
||
const signerData = await findUserByAddress(signerAddress); | ||
if (signerData.length > 0) { | ||
console.error("Unauthorized", signerAddress); | ||
return NextResponse.json({ error: "User already registered" }, { status: 401 }); | ||
} | ||
|
||
let isValidSignature = false; | ||
|
||
const typedData = { | ||
domain: EIP_712_DOMAIN, | ||
types: EIP_712_TYPES__REGISTER, | ||
primaryType: "Message", | ||
message: { | ||
action: "Register", | ||
description: "I want to register my account into Start Ethereum signing this offchain message", | ||
}, | ||
signature, | ||
} as const; | ||
|
||
const recoveredAddress = await recoverTypedDataAddress(typedData); | ||
isValidSignature = recoveredAddress === signerAddress; | ||
|
||
if (!isValidSignature) { | ||
console.error("Signer and Recovered address does not match"); | ||
return NextResponse.json({ error: "Unauthorized in batch" }, { status: 401 }); | ||
} | ||
|
||
await createUser({ id: signerAddress }); | ||
|
||
return NextResponse.json({ message: "User registered successfully" }, { status: 200 }); | ||
} catch (error) { | ||
console.error("Error when registering", error); | ||
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 }); | ||
} | ||
} |
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
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
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,11 @@ | ||
export const EIP_712_DOMAIN = { | ||
name: "Start Ethereum", | ||
version: "1", | ||
} as const; | ||
|
||
export const EIP_712_TYPES__REGISTER = { | ||
Message: [ | ||
{ name: "action", type: "string" }, | ||
{ name: "description", type: "string" }, | ||
], | ||
}; |