Skip to content

Commit

Permalink
refactor(proof): create 0-dependency download script
Browse files Browse the repository at this point in the history
re #635
  • Loading branch information
cedoor committed Feb 15, 2024
1 parent 22d391d commit 1dfb6d6
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 490 deletions.
2 changes: 1 addition & 1 deletion packages/data/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default {
{ file: pkg.exports.require, format: "cjs", banner, exports: "auto" },
{ file: pkg.exports.default, format: "es", banner }
],
external: Object.keys(pkg.dependencies),
external: [...Object.keys(pkg.dependencies), "ethers/contract", "ethers/constants", "ethers/providers"],
plugins: [
json(),
typescript({ tsconfig: "./build.tsconfig.json", useTsconfigDeclarationDir: true }),
Expand Down
4 changes: 1 addition & 3 deletions packages/proof/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@
},
"dependencies": {
"@types/snarkjs": "0.7.8",
"download": "8.0.0",
"ethers": "6.10.0",
"snarkjs": "0.7.3",
"tmp": "0.2.1"
"snarkjs": "0.7.3"
}
}
13 changes: 12 additions & 1 deletion packages/proof/rollup.browser.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,18 @@ export default {
banner
}
],
external: Object.keys(pkg.dependencies),
external: [
...Object.keys(pkg.dependencies),
"node:fs",
"node:fs/promises",
"node:os",
"node:path",
"node:stream",
"node:stream/promises",
"ethers/crypto",
"ethers/utils",
"ethers/abi"
],
plugins: [
alias({
entries: [{ find: "./get-snark-artifacts.node", replacement: "./get-snark-artifacts.browser" }]
Expand Down
13 changes: 12 additions & 1 deletion packages/proof/rollup.node.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@ export default {
banner
}
],
external: [...Object.keys(pkg.dependencies), "fs"],
external: [
...Object.keys(pkg.dependencies),
"node:fs",
"node:fs/promises",
"node:os",
"node:path",
"node:stream",
"node:stream/promises",
"ethers/crypto",
"ethers/utils",
"ethers/abi"
],
plugins: [
typescript({
tsconfig: "./build.tsconfig.json",
Expand Down
38 changes: 31 additions & 7 deletions packages/proof/src/get-snark-artifacts.node.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
/* istanbul ignore file */
import download from "download"
import fs from "fs"
import tmp from "tmp"
import { createWriteStream, existsSync, readdirSync } from "node:fs"
import { mkdir } from "node:fs/promises"
import os from "node:os"
import { dirname } from "node:path"
import { Readable } from "node:stream"
import { finished } from "node:stream/promises"
import { SnarkArtifacts } from "./types"

async function download(url: string, outputPath: string) {
const response = await fetch(url)

if (!response.ok) {
throw new Error(`Failed to fetch ${url}: ${response.statusText}`)
}

// Ensure the directory exists.
const dir = dirname(outputPath)
await mkdir(dir, { recursive: true })

const fileStream = createWriteStream(outputPath)
await finished(Readable.fromWeb(response.body as any).pipe(fileStream))
}

export default async function getSnarkArtifacts(treeDepth: number): Promise<SnarkArtifacts> {
const tmpDir = "semaphore-proof"
const tmpPath = `${tmp.tmpdir}/${tmpDir}-${treeDepth}`
const tmpPath = `${os.tmpdir()}/${tmpDir}-${treeDepth}`

if (!fs.existsSync(tmpPath) || fs.readdirSync(tmpPath).length !== 2) {
await download(`https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.wasm`, tmpPath)
await download(`https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.zkey`, tmpPath)
if (!existsSync(tmpPath) || readdirSync(tmpPath).length !== 2) {
await download(
`https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.wasm`,
`${tmpPath}/semaphore.wasm`
)
await download(
`https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.zkey`,
`${tmpPath}/semaphore.zkey`
)
}

return {
Expand Down
Loading

0 comments on commit 1dfb6d6

Please sign in to comment.