zk402-wasm
WASM bindings for zk402 client-side proof generation. Enables browser and Node.js clients to generate zero-knowledge payment proofs.
Building
# Install wasm-pack if not already installed
# Build for web
# Build for Node.js
# Build for bundlers (webpack, etc.)
Usage
TypeScript/JavaScript
import init, { build_payload } from './pkg/zk402_wasm';
await init();
const requirements = {
scheme: "zk-settle",
network: "eip155:8453",
amount: "100",
asset: "0x0000000000000000000000000000000000000001",
payTo: "0x0000000000000000000000000000000000000002",
maxTimeoutSeconds: 3600,
extra: {
proofSystem: "groth16",
circuitId: "zk-settle-transfer-v1",
verifierAddress: "0x0000000000000000000000000000000000000003"
}
};
const witness = {
secretKey: "0x" + "00".repeat(32),
from: "0x0000000000000000000000000000000000000004",
paymentNonce: "0x" + "05".repeat(32),
provingKey: "0x...", // Serialized Groth16 proving key
validAfter: Math.floor(Date.now() / 1000),
validBefore: Math.floor(Date.now() / 1000) + 3600
};
try {
const payloadJson = build_payload(
JSON.stringify(requirements),
JSON.stringify(witness)
);
const payload = JSON.parse(payloadJson);
console.log("Generated proof:", payload.proof);
// Send to facilitator
const response = await fetch('http://localhost:3000/verify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
requirements,
payload
})
});
const result = await response.json();
console.log("Verification result:", result);
} catch (error) {
console.error("Proof generation failed:", error);
}
API
build_payload(requirements_json: string, witness_json: string): string
Generates a zero-knowledge proof and assembles a complete PaymentPayload.
Arguments:
requirements_json: JSON string ofPaymentRequirements(camelCase)witness_json: JSON string containing:secretKey: Hex-encoded private key (0x-prefixed)from: Payer address (0x-prefixed, 20 bytes)paymentNonce: Random nonce (0x-prefixed, 32 bytes)provingKey: Hex-encoded Groth16 proving keyvalidAfter: Unix timestamp (seconds)validBefore: Unix timestamp (seconds)
Returns:
- JSON string of
PaymentPayload(camelCase), or throws error
Example witness input:
Error Handling
The function throws errors in these cases:
- Invalid JSON in either input
- Invalid hex encoding
- Invalid address/nonce length
- Proof generation failure
All errors are JavaScript Error objects with descriptive messages.
Security Notes
- Never expose
secretKeyorprovingKeyin logs or error messages - Generate
paymentNoncecryptographically (e.g.,crypto.randomBytes(32)) - Always verify
validAfterandvalidBeforeare reasonable - This module runs client-side - proof generation happens in the browser/Node process
License
MIT OR Apache-2.0