Include all workspace files when creating circuits.

pull/4512/head
Evan Sangaline 10 months ago committed by Aniket
parent 3f7fdccd21
commit af9d4ae74a
  1. 101
      libs/remix-ws-templates/src/script-templates/sindri/sindri.ts

@ -1,62 +1,75 @@
import client from 'sindri' import client from 'sindri'
const getSindriManifest = async () => {
const sindriJson = await remix.call('fileManager', 'readFile', `sindri.json`)
return JSON.parse(sindriJson)
}
const normalizePath = (path: string): string => {
while (path.startsWith('/') || path.startsWith('./')) {
path = path.replace(/^(\.\/|\/)/, '')
}
return path
}
/** /**
* Compile the given circuit * Compile the given circuit
* @param {string} entryPoint - path to the circuit to compile
* @param {string} apiKey - sindri API key * @param {string} apiKey - sindri API key
* @returns {Circuit} compiled circuit * @returns {Circuit} compiled circuit
*/ */
export const createCircuit = async (entryPoint: string, apiKey: string) => { export const createCircuit = async (apiKey: string) => {
client.authorize({ apiKey }) const sindriManifest = await getSindriManifest()
client.authorize({apiKey})
const sindriConf = await remix.call('fileManager', 'readFile', `sindri.json`)
const circuit = await remix.call('fileManager', 'readFile', entryPoint) // Create a map from file paths to `File` objects for all files in the workspace.
const deps = await remix.call('circuit-compiler' as any, 'resolveDependencies', entryPoint, circuit) const filesByPath: {[path: string]: File} = {}
interface Workspace {
const files = [] children?: Workspace
files.push(new File([circuit], 'circuit.circom')) content?: string
files.push(new File([sindriConf], 'sindri.json')) }
const workspace: Workspace = await remix.call('fileManager', 'copyFolderToJson', '/')
for (const file in deps) { const childQueue: Array<[string, Workspace]> = Object.entries(workspace)
if (file === entryPoint) continue while (childQueue.length > 0) {
files.push(new File([deps[file]], file)) const [path, child] = childQueue.pop()
if ('content' in child) {
filesByPath[path] = new File([child.content], path)
} }
if ('children' in child) {
childQueue.push(...Object.entries(child.children))
}
}
// Merge any of the circuit's resolved dependencies into the files at their expected import paths.
if (sindriManifest.circuitType === 'circom') {
const circuitPath = normalizePath(sindriManifest.circuitPath || 'circuit.circom')
const circuitContent = await remix.call('fileManager', 'readFile', circuitPath)
const dependencies: {[path: string]: string} = await remix.call('circuit-compiler' as any, 'resolveDependencies', circuitPath, circuitContent)
Object.entries(dependencies).forEach(([rawPath, content]) => {
const path = normalizePath(rawPath)
filesByPath[path] = new File([content], path)
})
}
console.log(`creating circuit "${entryPoint}"...`) console.log(`creating circuit "${entryPoint}"...`)
const circuitProject = await client.createCircuit(files) const files = Object.values(filesByPath)
console.log(`circuit created ${circuitProject.circuit_id}`) const circuitProject = await client.createCircuit(files)
return circuitProject console.log(`circuit created ${circuitProject.circuit_id}`)
return circuitProject
} }
/** /**
* Generate a proof against the given circuit * Generate a proof against the given circuit
* @param {string} circuitId - id of the circuit
* @param {Object} signals - input signals * @param {Object} signals - input signals
* @param {string} apiKey - sindri API key
* @returns {Proof} generated proof * @returns {Proof} generated proof
*/ */
export const proveCircuit = async (circuitId: string, signals: { [id: string]: string }, apiKey: string) => { export const proveCircuit = async (signals: {[id: string]: string}, apiKey: string) => {
client.authorize({ apiKey }) const sindriManifest = await getSindriManifest()
console.log(`proving circuit ${circuitId}...`) client.authorize({apiKey})
const proof = await client.proveCircuit(circuitId, JSON.stringify(signals))
console.log(`proof id: ${proof.proof_id}`)
return proof
}
/**
* Save the circuit
* @param {Circuit} circuitProject - compiled circuit
*/
export const saveCircuit = async (circuitProject) => {
await remix.call('fileManager', 'writeFile', `.sindri/${circuitProject.circuit_id}.json`, JSON.stringify(circuitProject, null, '\t'))
}
/** const circuitName = sindriManifest.name
* Load the circuit console.log(`proving circuit "${circuitName}"...`)
* @param {string} circuitId - id of the circuit const proof = await client.proveCircuit(circuitName, JSON.stringify(signals))
* @param {Object} signals - input signals console.log(`proof id: ${proof.proof_id}`)
* @returns {Proof} generated proof return proof
*/
export const loadCircuit = async (circuitId: string) => {
return JSON.parse(await remix.call('fileManager', 'readFile', `.sindri/${circuitId}.json`))
} }

Loading…
Cancel
Save