|
| 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 | +} |
0 commit comments