Skip to content

Commit f4b08bd

Browse files
committed
フロントエンドの資材を追加
1 parent 5f8166e commit f4b08bd

32 files changed

+10766
-2144
lines changed

pkgs/circuit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"generateInput": "node ./scripts/generateInput.js",
1313
"test": "node ./test/verify.test.js",
1414
"cp:verifier": "cp PasswordHashVerifier.sol ../backend/contracts",
15-
"cp:zk": "cp PasswordHash_js/PasswordHash.wasm ../backend/zk && cp zkey/PasswordHash_final.zkey ../backend/zk && cp PasswordHash_js/PasswordHash.wasm ../frontend/src/zk && cp zkey/PasswordHash_final.zkey ../frontend/src/zk"
15+
"cp:zk": "cp PasswordHash_js/PasswordHash.wasm ../backend/zk && cp zkey/PasswordHash_final.zkey ../backend/zk && cp PasswordHash_js/PasswordHash.wasm ../frontend/zk && cp zkey/PasswordHash_final.zkey ../frontend/zk"
1616
},
1717
"keywords": [],
1818
"author": "",

pkgs/frontend/.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Privy Auth Settings
2+
NEXT_PUBLIC_PRIVY_APP_ID=your_privy_app_id_here
3+
4+
# PASSWORD_HASH Settings
5+
PASSWORD_HASH=

pkgs/frontend/.eslintrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": ["next/core-web-vitals"],
3+
"rules": {
4+
"prefer-const": "error",
5+
"no-var": "error"
6+
}
7+
}

pkgs/frontend/.gitignore

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Dependencies
2+
node_modules/
3+
.pnpm-debug.log*
4+
5+
# Build outputs
6+
.next/
7+
out/
8+
dist/
9+
build/
10+
11+
# Environment variables
12+
.env
13+
.env.local
14+
.env.production
15+
.env.test
16+
17+
# IDE and OS files
18+
.DS_Store
19+
.vscode/
20+
.idea/
21+
*.swp
22+
*.swo
23+
*~
24+
25+
# Logs
26+
npm-debug.log*
27+
yarn-debug.log*
28+
yarn-error.log*
29+
.pnpm-debug.log*
30+
31+
# Cache
32+
.eslintcache
33+
.cache/
34+
*.tsbuildinfo
35+
36+
# TypeScript
37+
typings/
38+
39+
# Coverage
40+
coverage/
41+
.nyc_output
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import type { NextRequest } from "next/server";
2+
import { NextResponse } from "next/server";
3+
import path from "node:path";
4+
// @ts-ignore
5+
const snarkjs = require("snarkjs");
6+
7+
/**
8+
* ZK Proofを生成するAPIエンドポイント
9+
*/
10+
export async function GET() {
11+
return NextResponse.json({
12+
message: "ZK Proof Generation API is running",
13+
endpoints: {
14+
POST: "/api/generate-proof",
15+
description: "Generate ZK proof from password",
16+
},
17+
});
18+
}
19+
20+
export async function POST(request: NextRequest) {
21+
try {
22+
const { passwordNumber } = await request.json();
23+
24+
if (!passwordNumber || typeof passwordNumber !== "string") {
25+
return NextResponse.json(
26+
{ error: "パスワードが無効です" },
27+
{ status: 400 },
28+
);
29+
}
30+
31+
// 回路の入力データを作成
32+
const input = {
33+
password: passwordNumber,
34+
passwordHash: process.env.PASSWORD_HASH,
35+
};
36+
37+
console.log("Input for proof generation:", input);
38+
39+
// WAsmファイルとzkeyファイルのパスを設定
40+
const wasmPath = path.join(process.cwd(), "zk", "PasswordHash.wasm");
41+
const zkeyPath = path.join(process.cwd(), "zk", "PasswordHash_final.zkey");
42+
43+
console.log("wasmPath:", wasmPath);
44+
45+
// プルーフを生成
46+
const { proof, publicSignals } = await snarkjs.groth16.fullProve(
47+
input,
48+
wasmPath,
49+
zkeyPath,
50+
);
51+
52+
console.log("Proof generated successfully:", proof);
53+
54+
// プルーフをフォーマット(コントラクト用)
55+
const solidityProof = {
56+
a: [proof.pi_a[0], proof.pi_a[1]],
57+
b: [
58+
[proof.pi_b[0][1], proof.pi_b[0][0]],
59+
[proof.pi_b[1][1], proof.pi_b[1][0]],
60+
],
61+
c: [proof.pi_c[0], proof.pi_c[1]],
62+
};
63+
64+
return NextResponse.json({
65+
success: true,
66+
data: {
67+
proof: solidityProof,
68+
publicSignals: publicSignals,
69+
},
70+
});
71+
} catch (error) {
72+
console.error("プルーフ生成エラー:", error);
73+
return NextResponse.json(
74+
{ error: "プルーフの生成に失敗しました" },
75+
{ status: 500 },
76+
);
77+
}
78+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import type { NextRequest } from "next/server";
2+
import { NextResponse } from "next/server";
3+
// @ts-ignore
4+
const circomlibjs = require("circomlibjs");
5+
6+
/**
7+
* パスワードをハッシュ化するAPIエンドポイント
8+
*/
9+
export async function GET() {
10+
return NextResponse.json({
11+
message: "Password Hash API is running",
12+
endpoints: {
13+
POST: "/api/hash-password",
14+
description: "Hash password using Poseidon",
15+
},
16+
});
17+
}
18+
19+
export async function POST(request: NextRequest) {
20+
try {
21+
const { password } = await request.json();
22+
23+
if (!password || typeof password !== "string") {
24+
return NextResponse.json(
25+
{ error: "パスワードが無効です" },
26+
{ status: 400 },
27+
);
28+
}
29+
30+
// Poseidonハッシュ関数をビルド
31+
const poseidon = await circomlibjs.buildPoseidon();
32+
33+
// パスワードをバイト配列に変換してからBigIntに変換
34+
const passwordBytes = Buffer.from(password, "utf8");
35+
const passwordNumber = BigInt(`0x${passwordBytes.toString("hex")}`);
36+
37+
// Poseidonハッシュを計算
38+
const hash = poseidon.F.toString(poseidon([passwordNumber]));
39+
40+
return NextResponse.json({
41+
success: true,
42+
data: {
43+
originalPassword: password,
44+
passwordNumber: passwordNumber.toString(),
45+
hash: hash,
46+
},
47+
});
48+
} catch (error) {
49+
console.error("パスワードハッシュ化エラー:", error);
50+
return NextResponse.json(
51+
{ error: "パスワードのハッシュ化に失敗しました" },
52+
{ status: 500 },
53+
);
54+
}
55+
}

0 commit comments

Comments
 (0)