-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add remote proof generation using zk-regex-sdk
- Loading branch information
Showing
17 changed files
with
2,099 additions
and
1,346 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 |
---|---|---|
|
@@ -8,5 +8,8 @@ | |
"keywords": [], | ||
"author": "", | ||
"license": "MIT", | ||
"packageManager": "[email protected]" | ||
"packageManager": "[email protected]", | ||
"resolutions": { | ||
"zk-regex-sdk": "portal:/Users/savitar/Projects/opensource/proof-of-twitter/packages/app/zk-regex-sdk" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +0,0 @@ | ||
VITE_CONTRACT_ADDRESS=0x21d42CC2EcDb6db2c3D9b494D8CCcb6674360912 | ||
VITE_CIRCUIT_ARTIFACTS_URL=https://storage.googleapis.com/proof-of-twitter-artifacts/b6ea02e/ | ||
VITE_GOOGLE_CLIENT_ID=274960270901-ciaimkfeslrae1955ghq8bv7n8n3n71f.apps.googleusercontent.com | ||
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,2 @@ | ||
/node_modules | ||
/dist |
Binary file not shown.
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,110 @@ | ||
'use client'; | ||
import GoogleAuthProvider from "./src/providers/GoogleAuthProvider"; | ||
import GoogleAuthContext from "./src/contexts/GoogleAuth"; | ||
import ZkRegexContext, { ProofStatus } from "./src/contexts/ZkRegex"; | ||
import useGoogleAuth from "./src/hooks/useGoogleAuth"; | ||
import { fetchEmailList, fetchEmailsRaw, fetchProfile } from "./src/hooks/useGmailClient"; | ||
import { ReactNode, useState } from "react"; | ||
import { GoogleOAuthProvider } from '@react-oauth/google'; | ||
// import React from "react"; | ||
import useZkRegex from './src/hooks/useZkRegex'; | ||
import { encode } from 'js-base64'; | ||
|
||
interface ProvidersProps { | ||
children: ReactNode; | ||
clientId: string; | ||
zkRegexRegistryUrl: string; | ||
} | ||
|
||
function ZkRegexProvider({children, clientId, zkRegexRegistryUrl}: ProvidersProps) { | ||
|
||
const [inputWorkers, setInputWorkers] = useState<Record<string, Worker>>({}); | ||
const [proofStatus, setProofStatus] = useState<Record<string, ProofStatus>>({}); | ||
|
||
function createInputWorker(name: string): void { | ||
fetch(`${zkRegexRegistryUrl}/api/script/circuit_input/${name}`, {headers: { | ||
'Accept': 'text/javascript' | ||
}}).then(async r => { | ||
const js = await r.text(); | ||
const w = new Worker(`data:text/javascript;base64,${encode(js)}`) | ||
setInputWorkers({...inputWorkers, [name]: w}); | ||
}) | ||
} | ||
|
||
async function generateInputFromEmail(name: string, email: string) { | ||
const worker = inputWorkers[name]; | ||
return new Promise((resolve, reject) => { | ||
worker.onmessage = (event: any) => { | ||
if (event.data.error) { | ||
reject(event.data.error); | ||
} else { | ||
resolve(event.data); | ||
} | ||
} | ||
worker.postMessage(email); | ||
}); | ||
} | ||
|
||
async function generateProofRemotely(name: string, input: any) { | ||
const res = await fetch(`${zkRegexRegistryUrl}/api/proof/${name}`, { | ||
method: 'POST', | ||
body: JSON.stringify(input), | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
const data = await res.json(); | ||
setProofStatus((prev) => ({...prev, [data.id]:data})); | ||
if (data.pollUrl) { | ||
poolForProofStatus(data.pollUrl) | ||
} | ||
return data; | ||
} | ||
|
||
async function poolForProofStatus(url: string) { | ||
const res = await fetch(url); | ||
const data = await res.json(); | ||
setProofStatus((prev) => ({...prev, [data.id]:data})); | ||
if (data.status !== 'COMPLETED') { | ||
setTimeout(() => poolForProofStatus(url), 5000); | ||
} | ||
} | ||
|
||
const contextValues = { | ||
zkRegexRegistryUrl, | ||
customInputGenWorkerSrc: {}, | ||
inputWorkers, | ||
createInputWorker, | ||
deleteInputWorker: function (name: string): void { | ||
inputWorkers[name].terminate(); | ||
delete inputWorkers[name]; | ||
}, | ||
generateInputFromEmail, | ||
customProofGenWorkerSrc: {}, | ||
proofWorkers: {}, | ||
createProofWorker: function (_name: string): void { | ||
throw new Error("Function not implemented."); | ||
}, | ||
deleteProofWorker: function (_name: string): void { | ||
throw new Error("Function not implemented."); | ||
}, | ||
generateProofLocally: async function (_name: string, _input: any): Promise<any> { | ||
throw new Error("Function not implemented."); | ||
}, | ||
proofStatus, | ||
generateProofRemotely, | ||
} | ||
|
||
|
||
return ( | ||
<ZkRegexContext.Provider value={contextValues}> | ||
<GoogleOAuthProvider clientId={clientId}> | ||
<GoogleAuthProvider> | ||
{children} | ||
</GoogleAuthProvider> | ||
</GoogleOAuthProvider> | ||
</ZkRegexContext.Provider> | ||
); | ||
} | ||
|
||
export { ZkRegexProvider, GoogleAuthContext, useGoogleAuth, fetchEmailList, fetchEmailsRaw, fetchProfile, useZkRegex } |
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,22 @@ | ||
{ | ||
"name": "zk-regex-sdk", | ||
"version": "1.0.0", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "tsc" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "^18.3.3", | ||
"react": "^18.3.1", | ||
"typescript": "^5" | ||
}, | ||
"dependencies": { | ||
"@react-oauth/google": "^0.12.1", | ||
"js-base64": "^3.7.7" | ||
}, | ||
"peerDependencies": { | ||
"react": "^18.3.1" | ||
} | ||
} |
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,23 @@ | ||
import { createContext } from 'react'; | ||
|
||
interface GoogleAuthValues { | ||
googleAuthToken: any | null; | ||
isGoogleAuthed: boolean; | ||
loggedInGmail: string | null; | ||
scopesApproved: boolean; | ||
googleLogIn: () => void; | ||
googleLogOut: () => void; | ||
} | ||
|
||
const defaultValues: GoogleAuthValues = { | ||
googleAuthToken: null, | ||
isGoogleAuthed: false, | ||
loggedInGmail: null, | ||
scopesApproved: false, | ||
googleLogIn: () => { console.log("context not setup")}, | ||
googleLogOut: () => {}, | ||
}; | ||
|
||
const GoogleAuthContext = createContext<GoogleAuthValues>(defaultValues) | ||
|
||
export default GoogleAuthContext |
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,65 @@ | ||
import { createContext } from 'react'; | ||
|
||
export interface ProofStatus { | ||
status: string, | ||
id: string, | ||
pollUrl: string, | ||
estimatedTimeLeft: number | ||
publicOutput: any, | ||
proof: any, | ||
} | ||
|
||
interface ZkRegexValues { | ||
zkRegexRegistryUrl: string | ||
|
||
// The worker source code for generating the circuit inputs | ||
// If not provided, the default worker scripts from zk regex registry will be used | ||
// Format: { "zk-email/twitter-proof": "https://example.com/generate_inputs_worker_bundled.js" } | ||
customInputGenWorkerSrc: { | ||
[key: string]: string | ||
} | ||
inputWorkers: { | ||
[key: string]: Worker | ||
} | ||
createInputWorker: (name: string) => void; | ||
deleteInputWorker: (name: string) => void; | ||
generateInputFromEmail: (name: string, email: string) => Promise<any>; | ||
|
||
customProofGenWorkerSrc: { | ||
[key: string]: string | ||
} | ||
proofWorkers: { | ||
[key: string]: Worker | ||
} | ||
createProofWorker: (name: string) => void; | ||
deleteProofWorker: (name: string) => void; | ||
generateProofLocally: (name: string, input: any) => Promise<any>; | ||
|
||
proofStatus: { | ||
[key: string]: ProofStatus | ||
} | ||
generateProofRemotely: (name: string, input: any) => Promise<any>; | ||
} | ||
|
||
const defaultValues: ZkRegexValues = { | ||
zkRegexRegistryUrl: "", | ||
|
||
customInputGenWorkerSrc: {}, | ||
inputWorkers: {}, | ||
createInputWorker: () => { console.log("context not setup")}, | ||
deleteInputWorker: () => { console.log("context not setup")}, | ||
generateInputFromEmail: async () => { console.log("context not setup");}, | ||
|
||
customProofGenWorkerSrc: {}, | ||
proofWorkers: {}, | ||
createProofWorker: () => { console.log("context not setup")}, | ||
deleteProofWorker: () => { console.log("context not setup")}, | ||
generateProofLocally: async () => { console.log("context not setup");}, | ||
|
||
proofStatus: {}, | ||
generateProofRemotely: async () => { console.log("context not setup");} | ||
} | ||
|
||
const ZkRegexContext = createContext<ZkRegexValues>(defaultValues) | ||
|
||
export default ZkRegexContext |
Oops, something went wrong.