Support includes import for circom wasm build

pull/3996/head
ioedeveloper 2 years ago
parent d4da5bc063
commit 75b011964f
  1. 107
      apps/circuit-compiler/src/app/services/circomPluginClient.ts

@ -23,81 +23,86 @@ export class CircomPluginClient extends PluginClient {
} }
async compile (path: string) { async compile (path: string) {
this.parse(path) const fileContent = await this.call('fileManager', 'readFile', path)
// const fileContent = await this.call('fileManager', 'readFile', path) let buildFiles = {
// let buildFiles = await this.resolveDependencies(path, fileContent) [path]: fileContent
}
// buildFiles = buildFiles.flat(Infinity) buildFiles = await this.resolveDependencies(path, fileContent, buildFiles)
// const compilationResult = main_browser(path, buildFiles, { prime: "bn128" }) const compilationResult = main_browser(path, buildFiles, { prime: "bn128" })
// console.log('compilation result: ' + compilationResult) console.log('compilation result: ' + compilationResult)
// // generate_witness(compilationResult, '{ "a": "3", "b": "11" }') generate_witness(compilationResult, '{ "a": "3", "b": "11" }')
generate_witness(compilationResult, '{ "a": "5", "b": "77" }')
} }
async parse (path: string) { async parse (path: string) {
const fileContent = await this.call('fileManager', 'readFile', path) const fileContent = await this.call('fileManager', 'readFile', path)
let buildFiles = await this.resolveDependencies(path, fileContent) let buildFiles = {
[path]: fileContent
}
buildFiles = buildFiles.flat(Infinity) buildFiles = await this.resolveDependencies(path, fileContent, buildFiles)
buildFiles.reverse()
const parsingResult = parse_circuit_browser(path, buildFiles, { prime: "bn128" }) const parsingResult = parse_circuit_browser(path, buildFiles, { prime: "bn128" })
console.log('parsing result: ' + parsingResult) console.log('parsing result: ' + parsingResult)
} }
async resolveDependencies (filePath: string, fileContent: string, depPath: string = '') { async resolveDependencies (filePath: string, fileContent: string, output = {}, depPath: string = '', blackPath: string[] = []) {
const includes = (fileContent.match(/include ['"].*['"]/g) || []).map(include => include.replace(/include ['"]/g, '').replace(/['"]/g, '')) const includes = (fileContent.match(/include ['"].*['"]/g) || []).map(include => include.replace(/include ['"]/g, '').replace(/['"]/g, ''))
if (includes.length === 0) { await Promise.all(includes.map(async include => {
return [fileContent] // fix for endless recursive includes
} else { if (blackPath.includes(include)) return
const depFiles = await Promise.all(includes.map(async include => { let dependencyContent = ''
let dependencyContent = '' let path = include
let path = include // @ts-ignore
const pathExists = await this.call('fileManager', 'exists', path)
if (pathExists) {
dependencyContent = await this.call('fileManager', 'readFile', path)
} else {
let relativePath = pathModule.resolve(filePath.slice(0, filePath.lastIndexOf('/')), include)
if (relativePath.indexOf('/') === 0) relativePath = relativePath.slice(1)
// @ts-ignore // @ts-ignore
const pathExists = await this.call('fileManager', 'exists', path) const relativePathExists = await this.call('fileManager', 'exists', relativePath)
if (pathExists) { if (relativePathExists) {
dependencyContent = await this.call('fileManager', 'readFile', path) dependencyContent = await this.call('fileManager', 'readFile', relativePath)
} else { } else {
let relativePath = pathModule.resolve(filePath.slice(0, filePath.lastIndexOf('/')), include) if (depPath) {
if (relativePath.indexOf('/') === 0) relativePath = relativePath.slice(1) path = pathModule.resolve(depPath.slice(0, depPath.lastIndexOf('/')), include)
// @ts-ignore if (path.indexOf('/') === 0) path = path.slice(1)
const relativePathExists = await this.call('fileManager', 'exists', relativePath) dependencyContent = await this.call('contentImport', 'resolveAndSave', path, null)
if (relativePathExists) {
dependencyContent = await this.call('fileManager', 'readFile', relativePath)
} else { } else {
if (depPath) { if (include.startsWith('circomlib')) {
path = pathModule.resolve(depPath.slice(0, depPath.lastIndexOf('/')), include) const splitInclude = include.split('/')
if (path.indexOf('/') === 0) path = path.slice(1) const version = splitInclude[1].match(/v[0-9]+.[0-9]+.[0-9]+/g)
dependencyContent = await this.call('contentImport', 'resolveAndSave', path, null)
} else { if (version && version[0]) {
if (include.startsWith('circomlib')) { path = `https://raw.githubusercontent.com/iden3/circomlib/${version[0]}/circuits/${splitInclude.slice(2).join('/')}`
const splitInclude = include.split('/') dependencyContent = await this.call('contentImport', 'resolveAndSave', path, null)
const version = splitInclude[1].match(/v[0-9]+.[0-9]+.[0-9]+/g)
if (version && version[0]) {
path = `https://raw.githubusercontent.com/iden3/circomlib/${version[0]}/circuits/${splitInclude.slice(2).join('/')}`
dependencyContent = await this.call('contentImport', 'resolveAndSave', path, null)
} else {
path = `https://raw.githubusercontent.com/iden3/circomlib/master/circuits/${splitInclude.slice(1).join('/')}`
dependencyContent = await this.call('contentImport', 'resolveAndSave', path, null)
}
} else { } else {
path = `https://raw.githubusercontent.com/iden3/circomlib/master/circuits/${splitInclude.slice(1).join('/')}`
dependencyContent = await this.call('contentImport', 'resolveAndSave', path, null) dependencyContent = await this.call('contentImport', 'resolveAndSave', path, null)
} }
} else {
dependencyContent = await this.call('contentImport', 'resolveAndSave', path, null)
} }
} }
} }
const dependencyIncludes = (dependencyContent.match(/include ['"].*['"]/g) || []).map(include => include.replace(/include ['"]/g, '').replace(/['"]/g, '')) }
const dependencyIncludes = (dependencyContent.match(/include ['"].*['"]/g) || []).map(include => include.replace(/include ['"]/g, '').replace(/['"]/g, ''))
if (dependencyIncludes.length > 0) return await this.resolveDependencies(filePath, dependencyContent, path)
else return dependencyContent blackPath.push(include)
})) if (dependencyIncludes.length > 0) {
depFiles.push(fileContent) await this.resolveDependencies(filePath, dependencyContent, output, path, blackPath)
return depFiles output[include] = dependencyContent
} } else {
output[include] = dependencyContent
}
}))
return output
} }
} }

Loading…
Cancel
Save