Skip to content

Commit

Permalink
Merge pull request #156 from alvinlee001/fix/#150-make-zkp-generic
Browse files Browse the repository at this point in the history
pass LOAD_URL as param, and put LOAD_URL into .env
  • Loading branch information
saleel authored Feb 1, 2024
2 parents ccdb8a2 + 3aa063b commit 0a18d49
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 26 deletions.
9 changes: 4 additions & 5 deletions packages/helpers/dist/zkp.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
export declare const loadURL = "https://twitter-verifier-zkeys.s3.amazonaws.com/751fae9012c8a36543f60a2d2ec528d088ed6df0/";
export declare function downloadFromFilename(filename: string, compressed?: boolean): Promise<void>;
export declare const downloadProofFiles: (filename: string, onFileDownloaded: () => void) => Promise<void>;
export declare const uncompressProofFiles: (filename: string) => Promise<void>;
export declare function generateProof(input: any, filename: string): Promise<{
export declare function downloadFromFilename(loadURL: string, filename: string, compressed?: boolean): Promise<void>;
export declare const downloadProofFiles: (loadURL: string, filename: string, onFileDownloaded: () => void) => Promise<void>;
export declare const uncompressProofFiles: (loadURL: string, filename: string) => Promise<void>;
export declare function generateProof(loadURL: string, input: any, filename: string): Promise<{
proof: any;
publicSignals: any;
}>;
Expand Down
20 changes: 10 additions & 10 deletions packages/helpers/dist/zkp.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildInput = exports.verifyProof = exports.generateProof = exports.uncompressProofFiles = exports.downloadProofFiles = exports.downloadFromFilename = exports.loadURL = void 0;
exports.buildInput = exports.verifyProof = exports.generateProof = exports.uncompressProofFiles = exports.downloadProofFiles = exports.downloadFromFilename = void 0;
const vkey_1 = require("./vkey");
const localforage_1 = __importDefault(require("localforage"));
const uncompress_1 = require("./uncompress");
// @ts-ignore
const snarkjs = __importStar(require("snarkjs"));
exports.loadURL = "https://twitter-verifier-zkeys.s3.amazonaws.com/751fae9012c8a36543f60a2d2ec528d088ed6df0/";
// export const loadURL = "https://twitter-verifier-zkeys.s3.amazonaws.com/751fae9012c8a36543f60a2d2ec528d088ed6df0/";
// export const loadURL = "http://localhost:3001/";
const compressed = true;
// const loadURL = "/zkemail-zkey-chunks/";
Expand All @@ -56,8 +56,8 @@ async function downloadWithRetries(link, downloadAttempts) {
// GET the compressed file from the remote server, then store it with localforage
// Note that it must be stored as an uncompressed ArrayBuffer
// and named such that filename===`${name}.zkey${a}` in order for it to be found by snarkjs.
async function downloadFromFilename(filename, compressed = false) {
const link = exports.loadURL + filename;
async function downloadFromFilename(loadURL, filename, compressed = false) {
const link = loadURL + filename;
const zkeyResp = await downloadWithRetries(link, 3);
const zkeyBuff = await zkeyResp.arrayBuffer();
if (!compressed) {
Expand All @@ -76,7 +76,7 @@ async function downloadFromFilename(filename, compressed = false) {
console.log(`Storage of ${filename} successful!`);
}
exports.downloadFromFilename = downloadFromFilename;
const downloadProofFiles = async function (filename, onFileDownloaded) {
const downloadProofFiles = async function (loadURL, filename, onFileDownloaded) {
const filePromises = [];
for (const c of zkeySuffix) {
const targzFilename = `${filename}.zkey${c}${zkeyExtension}`;
Expand All @@ -89,13 +89,13 @@ const downloadProofFiles = async function (filename, onFileDownloaded) {
}
filePromises.push(
// downloadFromFilename(targzFilename, true).then(
downloadFromFilename(targzFilename, compressed).then(() => onFileDownloaded()));
downloadFromFilename(loadURL, targzFilename, compressed).then(() => onFileDownloaded()));
}
console.log(filePromises);
await Promise.all(filePromises);
};
exports.downloadProofFiles = downloadProofFiles;
const uncompressProofFiles = async function (filename) {
const uncompressProofFiles = async function (loadURL, filename) {
const filePromises = [];
for (const c of zkeySuffix) {
const targzFilename = `${filename}.zkey${c}${zkeyExtension}`;
Expand All @@ -108,17 +108,17 @@ const uncompressProofFiles = async function (filename) {
console.log(`${filename}.zkey${c}${item ? "" : zkeyExtension} already found in localforage!`);
continue;
}
filePromises.push(downloadFromFilename(targzFilename));
filePromises.push(downloadFromFilename(loadURL, targzFilename));
}
console.log(filePromises);
await Promise.all(filePromises);
};
exports.uncompressProofFiles = uncompressProofFiles;
async function generateProof(input, filename) {
async function generateProof(loadURL, input, filename) {
// TODO: figure out how to generate this s.t. it passes build
console.log("generating proof for input");
console.log(input);
const { proof, publicSignals } = await snarkjs.groth16.fullProve(input, `${exports.loadURL}${filename}.wasm`, `${filename}.zkey`);
const { proof, publicSignals } = await snarkjs.groth16.fullProve(input, `${loadURL}${filename}.wasm`, `${filename}.zkey`);
console.log(`Generated proof ${JSON.stringify(proof)}`);
return {
proof,
Expand Down
14 changes: 7 additions & 7 deletions packages/helpers/src/zkp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { uncompressGz as uncompress } from "./uncompress";
// @ts-ignore
import * as snarkjs from "snarkjs";

export const loadURL = "https://twitter-verifier-zkeys.s3.amazonaws.com/751fae9012c8a36543f60a2d2ec528d088ed6df0/";
// export const loadURL = "https://twitter-verifier-zkeys.s3.amazonaws.com/751fae9012c8a36543f60a2d2ec528d088ed6df0/";
// export const loadURL = "http://localhost:3001/";
const compressed = true;
// const loadURL = "/zkemail-zkey-chunks/";
Expand Down Expand Up @@ -34,7 +34,7 @@ async function downloadWithRetries(link: string, downloadAttempts: number) {
// GET the compressed file from the remote server, then store it with localforage
// Note that it must be stored as an uncompressed ArrayBuffer
// and named such that filename===`${name}.zkey${a}` in order for it to be found by snarkjs.
export async function downloadFromFilename(filename: string, compressed = false) {
export async function downloadFromFilename(loadURL: string, filename: string, compressed = false) {
const link = loadURL + filename;

const zkeyResp = await downloadWithRetries(link, 3);
Expand All @@ -55,7 +55,7 @@ export async function downloadFromFilename(filename: string, compressed = false)
console.log(`Storage of ${filename} successful!`);
}

export const downloadProofFiles = async function (filename: string, onFileDownloaded: () => void) {
export const downloadProofFiles = async function (loadURL: string, filename: string, onFileDownloaded: () => void) {
const filePromises = [];
for (const c of zkeySuffix) {
const targzFilename = `${filename}.zkey${c}${zkeyExtension}`;
Expand All @@ -68,14 +68,14 @@ export const downloadProofFiles = async function (filename: string, onFileDownlo
}
filePromises.push(
// downloadFromFilename(targzFilename, true).then(
downloadFromFilename(targzFilename, compressed).then(() => onFileDownloaded())
downloadFromFilename(loadURL, targzFilename, compressed).then(() => onFileDownloaded())
);
}
console.log(filePromises);
await Promise.all(filePromises);
};

export const uncompressProofFiles = async function (filename: string) {
export const uncompressProofFiles = async function (loadURL: string, filename: string) {
const filePromises = [];
for (const c of zkeySuffix) {
const targzFilename = `${filename}.zkey${c}${zkeyExtension}`;
Expand All @@ -87,13 +87,13 @@ export const uncompressProofFiles = async function (filename: string) {
console.log(`${filename}.zkey${c}${item ? "" : zkeyExtension} already found in localforage!`);
continue;
}
filePromises.push(downloadFromFilename(targzFilename));
filePromises.push(downloadFromFilename(loadURL, targzFilename));
}
console.log(filePromises);
await Promise.all(filePromises);
};

export async function generateProof(input: any, filename: string) {
export async function generateProof(loadURL: string, input: any, filename: string) {
// TODO: figure out how to generate this s.t. it passes build
console.log("generating proof for input");
console.log(input);
Expand Down
3 changes: 2 additions & 1 deletion packages/helpers/tests/mocks/handlers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { rest } from 'msw'
import { loadURL } from '../../src/zkp'
import fs from 'fs';
import path from 'path';

export const loadURL = "https://test/endpoint/";

const getCompressedTestFile = () => {
const buffer = fs.readFileSync(path.join(__dirname, `../test-data/compressed-files/compressed.txt.gz`));
return buffer;
Expand Down
6 changes: 4 additions & 2 deletions packages/helpers/tests/zkp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { StringDecoder } from "string_decoder";
import _localforage from "localforage";
import { downloadFromFilename, downloadProofFiles } from "../src/zkp";
import { server } from './mocks/server.js'
import {loadURL} from "./mocks/handlers";

// this is mocked in __mocks__/localforage.ts
jest.mock("localforage");
Expand Down Expand Up @@ -37,7 +38,8 @@ describe('Test zkp fetch and store', () => {
// downloadFileFromFilename requests the file from the server, which we mocked with msw.
// The server returns a gz file of a file containing "not compressed 👍",
// which is defined in __fixtures__/compressed-files/compressed.txt.gz
await downloadFromFilename(filename, true);

await downloadFromFilename(loadURL, filename, true);
// check that localforage.setItem was called once to save the zkey file.
expect(localforage.setItem).toBeCalledTimes(1);
const filenameRaw = localforage.setItem.mock.calls[0][0];
Expand All @@ -53,7 +55,7 @@ describe('Test zkp fetch and store', () => {
test('should should download all the zkeys and save them in local storage for snarkjs to access.', async () => {
// downloadProofFiles calls downloadFromFilename 10 times, one for each zkey, b-k.
const onDownloaded = jest.fn();
await downloadProofFiles("email", onDownloaded);
await downloadProofFiles(loadURL,"email", onDownloaded);
expect(localforage.setItem).toBeCalledTimes(10);

// check the first one
Expand Down
1 change: 1 addition & 0 deletions packages/twitter-verifier-app/.env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
VITE_CONTRACT_ADDRESS=0x881d3F6af2ED60c7391f637008310Ce3CCF24ca5
LOAD_URL=https://twitter-verification.vercel.app/
4 changes: 3 additions & 1 deletion packages/twitter-verifier-app/pages/MainPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { isSetIterator } from "util/types";
export const MainPage: React.FC<{}> = (props) => {
// raw user inputs
const filename = "twitter";
const LOAD_URL = import.meta.env.LOAD_URL

const [emailSignals, setEmailSignals] = useState<string>("");
const [emailFull, setEmailFull] = useState<string>(
Expand Down Expand Up @@ -348,7 +349,7 @@ export const MainPage: React.FC<{}> = (props) => {
);
setStatus("downloading-proof-files");
try {
await downloadProofFiles(filename, () => {
await downloadProofFiles(LOAD_URL, filename, () => {
setDownloadProgress((p) => p + 1);
});
}
Expand All @@ -371,6 +372,7 @@ export const MainPage: React.FC<{}> = (props) => {
console.log("Starting proof generation");
// alert("Generating proof, will fail due to input");
const { proof, publicSignals } = await generateProof(
LOAD_URL,
input,
filename
);
Expand Down

0 comments on commit 0a18d49

Please sign in to comment.