Fetch compile wasm binaries

pull/4144/head
ioedeveloper 1 year ago
parent 7773a829e7
commit 8f8b56e326
  1. 137
      apps/circuit-compiler/src/app/services/circomPluginClient.ts

@ -1,8 +1,8 @@
import {PluginClient} from '@remixproject/plugin'
import {createClient} from '@remixproject/plugin-webview'
import { PluginClient } from '@remixproject/plugin'
import { createClient } from '@remixproject/plugin-webview'
import EventManager from 'events'
import pathModule from 'path'
import {parse} from 'circom_wasm'
import { parse, compile } from 'circom_wasm'
export class CircomPluginClient extends PluginClient {
public internalEvents: EventManager
@ -11,7 +11,7 @@ export class CircomPluginClient extends PluginClient {
super()
createClient(this)
this.internalEvents = new EventManager()
this.methods = ['init', 'parse']
this.methods = ['init', 'parse', 'compile']
this.onload()
}
@ -30,7 +30,7 @@ export class CircomPluginClient extends PluginClient {
async parse(path: string, fileContent: string): Promise<void> {
let buildFiles = {
[path]: fileContent
[path]: fileContent,
}
buildFiles = await this.resolveDependencies(path, fileContent, buildFiles)
@ -49,21 +49,19 @@ export class CircomPluginClient extends PluginClient {
for (const label in report.labels) {
if (report.labels[label].file_id === '0') {
// @ts-ignore
const startPosition: {lineNumber: number; column: number} =
await this.call(
'editor',
// @ts-ignore
'getPositionAt',
report.labels[label].range.start
)
const startPosition: { lineNumber: number; column: number } = await this.call(
'editor',
// @ts-ignore
'getPositionAt',
report.labels[label].range.start
)
// @ts-ignore
const endPosition: {lineNumber: number; column: number} =
await this.call(
'editor',
// @ts-ignore
'getPositionAt',
report.labels[label].range.end
)
const endPosition: { lineNumber: number; column: number } = await this.call(
'editor',
// @ts-ignore
'getPositionAt',
report.labels[label].range.end
)
markers.push({
message: report.message,
@ -71,14 +69,14 @@ export class CircomPluginClient extends PluginClient {
position: {
start: {
line: startPosition.lineNumber,
column: startPosition.column
column: startPosition.column,
},
end: {
line: endPosition.lineNumber,
column: endPosition.column
}
column: endPosition.column,
},
},
file: path
file: path,
})
}
}
@ -97,17 +95,21 @@ export class CircomPluginClient extends PluginClient {
}
}
async resolveDependencies(
filePath: string,
fileContent: string,
output = {},
depPath: string = '',
blackPath: string[] = []
): Promise<Record<string, string>> {
async compile(path: string): Promise<void> {
const fileContent = await this.call('fileManager', 'readFile', path)
let buildFiles = {
[path]: fileContent,
}
buildFiles = await this.resolveDependencies(path, fileContent, buildFiles)
const compiledOutput = compile(path, buildFiles, { prime: 'bn128' })
console.log('compiled wasm binaries: ', compiledOutput)
}
async resolveDependencies(filePath: string, fileContent: string, output = {}, depPath: string = '', blackPath: string[] = []): Promise<Record<string, string>> {
// extract all includes
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, ''))
await Promise.all(
includes.map(async (include) => {
@ -123,12 +125,8 @@ export class CircomPluginClient extends PluginClient {
dependencyContent = await this.call('fileManager', 'readFile', path)
} else {
// if include import (path) does not exist, try to construct relative path using the original file path (current file opened in editor)
let relativePath = pathModule.resolve(
filePath.slice(0, filePath.lastIndexOf('/')),
include
)
if (relativePath.indexOf('/') === 0)
relativePath = relativePath.slice(1)
let relativePath = pathModule.resolve(filePath.slice(0, filePath.lastIndexOf('/')), include)
if (relativePath.indexOf('/') === 0) relativePath = relativePath.slice(1)
const relativePathExists = await this.call(
'fileManager',
// @ts-ignore
@ -138,25 +136,13 @@ export class CircomPluginClient extends PluginClient {
if (relativePathExists) {
// fetch file content if include import exists as a relative path
dependencyContent = await this.call(
'fileManager',
'readFile',
relativePath
)
dependencyContent = await this.call('fileManager', 'readFile', relativePath)
} else {
if (depPath) {
// if depPath is provided, try to resolve include import from './deps' folder in remix
path = pathModule.resolve(
depPath.slice(0, depPath.lastIndexOf('/')),
include
)
path = pathModule.resolve(depPath.slice(0, depPath.lastIndexOf('/')), include)
if (path.indexOf('/') === 0) path = path.slice(1)
dependencyContent = await this.call(
'contentImport',
'resolveAndSave',
path,
null
)
dependencyContent = await this.call('contentImport', 'resolveAndSave', path, null)
} else {
if (include.startsWith('circomlib')) {
// try to resolve include import from github if it is a circomlib dependency
@ -164,55 +150,26 @@ export class CircomPluginClient extends PluginClient {
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
)
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
)
path = `https://raw.githubusercontent.com/iden3/circomlib/master/circuits/${splitInclude.slice(1).join('/')}`
dependencyContent = await this.call('contentImport', 'resolveAndSave', path, null)
}
} else {
// If all import cases are not true, use the default import to try fetching from node_modules and unpkg
dependencyContent = await this.call(
'contentImport',
'resolveAndSave',
path,
null
)
dependencyContent = await this.call('contentImport', 'resolveAndSave', path, null)
}
}
}
}
// extract all includes from the dependency content
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, ''))
blackPath.push(include)
// recursively resolve all dependencies of the dependency
if (dependencyIncludes.length > 0) {
await this.resolveDependencies(
filePath,
dependencyContent,
output,
path,
blackPath
)
await this.resolveDependencies(filePath, dependencyContent, output, path, blackPath)
output[include] = dependencyContent
} else {
output[include] = dependencyContent

Loading…
Cancel
Save