diff --git a/apps/circuit-compiler/src/app/actions/index.ts b/apps/circuit-compiler/src/app/actions/index.ts index 440d098493..55944f7bc6 100644 --- a/apps/circuit-compiler/src/app/actions/index.ts +++ b/apps/circuit-compiler/src/app/actions/index.ts @@ -1,7 +1,7 @@ -import { CircomPluginClient } from "../services/circomPluginClient" +import type { CircomPluginClient } from "../services/circomPluginClient" import { Actions, AppState } from "../types" -export const compileCircuit = async (plugin: CircomPluginClient, appState: AppState, dispatch: React.Dispatch) => { +export const compileCircuit = async (plugin: CircomPluginClient, appState: AppState) => { try { if (appState.status !== "compiling") { await plugin.compile(appState.filePath, { version: appState.version, prime: appState.primeValue }) @@ -9,12 +9,12 @@ export const compileCircuit = async (plugin: CircomPluginClient, appState: AppSt console.log('Exisiting circuit compilation in progress') } } catch (e) { + plugin.internalEvents.emit('circuit_compiling_errored', e) console.error(e) - dispatch({ type: 'SET_SIGNAL_INPUTS', payload: [] }) } } -export const generateR1cs = async (plugin: CircomPluginClient, appState: AppState, dispatch: React.Dispatch) => { +export const generateR1cs = async (plugin: CircomPluginClient, appState: AppState) => { try { if (appState.status !== "generating") { await plugin.generateR1cs(appState.filePath, { version: appState.version, prime: appState.primeValue }) @@ -22,6 +22,22 @@ export const generateR1cs = async (plugin: CircomPluginClient, appState: AppStat console.log('Exisiting r1cs generation in progress') } } catch (e) { + plugin.internalEvents.emit('circuit_generating_r1cs_errored', e) console.error('Generating R1CS failed: ', e) } -} \ No newline at end of file +} + +export const computeWitness = async (plugin: CircomPluginClient, status: string, witnessValues: Record) => { + try { + if (status !== "computing") { + const input = JSON.stringify(witnessValues) + + await plugin.computeWitness(input) + } else { + console.log('Exisiting witness computation in progress') + } + } catch (e) { + plugin.internalEvents.emit('circuit_computing_witness_errored', e) + console.error('Computing witness failed: ', e) + } +} diff --git a/apps/circuit-compiler/src/app/app.tsx b/apps/circuit-compiler/src/app/app.tsx index 3482ffc2ba..cdb36783ee 100644 --- a/apps/circuit-compiler/src/app/app.tsx +++ b/apps/circuit-compiler/src/app/app.tsx @@ -16,7 +16,6 @@ function App() { messages: null }) const [isContentChanged, setIsContentChanged] = useState(false) - const [content, setNewContent] = useState("") useEffect(() => { const plugin = new CircomPluginClient() @@ -34,25 +33,36 @@ function App() { } }) // @ts-ignore - plugin.on('editor', 'contentChanged', async (filePath, content) => { + plugin.on('editor', 'contentChanged', async () => { setIsContentChanged(true) - setNewContent(content) }) setPlugin(plugin) }) - plugin.internalEvents.on('circuit_compiling', () => dispatch({ type: 'SET_COMPILER_STATUS', payload: 'compiling' })) - plugin.internalEvents.on('circuit_done', (signalInputs) => { + + // compiling events + plugin.internalEvents.on('circuit_compiling_start', () => dispatch({ type: 'SET_COMPILER_STATUS', payload: 'compiling' })) + plugin.internalEvents.on('circuit_compiling_done', (signalInputs) => { signalInputs = (signalInputs || []).filter(input => input) dispatch({ type: 'SET_SIGNAL_INPUTS', payload: signalInputs }) dispatch({ type: 'SET_COMPILER_STATUS', payload: 'idle' }) }) - plugin.internalEvents.on('circuit_errored', (err) => dispatch({ type: 'SET_COMPILER_STATUS', payload: err.message })) + plugin.internalEvents.on('circuit_compiling_errored', (err) => dispatch({ type: 'SET_COMPILER_STATUS', payload: 'errored' })) + + // r1cs events + plugin.internalEvents.on('circuit_generating_r1cs_start', () => dispatch({ type: 'SET_COMPILER_STATUS', payload: 'generating' })) + plugin.internalEvents.on('circuit_generating_r1cs_done', () => dispatch({ type: 'SET_COMPILER_STATUS', payload: 'idle' })) + plugin.internalEvents.on('circuit_generating_r1cs_errored', (err) => dispatch({ type: 'SET_COMPILER_STATUS', payload: 'errored' })) + + // witness events + plugin.internalEvents.on('circuit_computing_witness_start', () => dispatch({ type: 'SET_COMPILER_STATUS', payload: 'computing' })) + plugin.internalEvents.on('circuit_computing_witness_done', () => dispatch({ type: 'SET_COMPILER_STATUS', payload: 'idle' })) + plugin.internalEvents.on('circuit_computing_witness_errored', (err) => dispatch({ type: 'SET_COMPILER_STATUS', payload: 'errored' })) }, []) useEffect(() => { if (isContentChanged) { (async () => { - if (appState.autoCompile) await compileCircuit(plugin, appState, dispatch) + if (appState.autoCompile) await compileCircuit(plugin, appState) })() setIsContentChanged(false) } @@ -67,7 +77,7 @@ function App() { useEffect(() => { if (appState.filePath) { (async () => { - if (appState.autoCompile) await compileCircuit(plugin, appState, dispatch) + if (appState.autoCompile) await compileCircuit(plugin, appState) })() } }, [appState.filePath]) diff --git a/apps/circuit-compiler/src/app/components/compileBtn.tsx b/apps/circuit-compiler/src/app/components/compileBtn.tsx index 3f131224e7..c778377cc1 100644 --- a/apps/circuit-compiler/src/app/components/compileBtn.tsx +++ b/apps/circuit-compiler/src/app/components/compileBtn.tsx @@ -5,14 +5,12 @@ import { FormattedMessage } from "react-intl"; import { compileCircuit } from "../actions"; export function CompileBtn () { - const { plugin, appState, dispatch } = useContext(CircuitAppContext) - - + const { plugin, appState } = useContext(CircuitAppContext) return (