|
1 | 1 | /* istanbul ignore file */
|
2 |
| -import download from "download" |
3 |
| -import fs from "fs" |
4 |
| -import tmp from "tmp" |
| 2 | +import { createWriteStream, existsSync, readdirSync } from "node:fs" |
| 3 | +import { mkdir } from "node:fs/promises" |
| 4 | +import os from "node:os" |
| 5 | +import { dirname } from "node:path" |
| 6 | +import { Readable } from "node:stream" |
| 7 | +import { finished } from "node:stream/promises" |
5 | 8 | import { SnarkArtifacts } from "./types"
|
6 | 9 |
|
| 10 | +async function download(url: string, outputPath: string) { |
| 11 | + const response = await fetch(url) |
| 12 | + |
| 13 | + if (!response.ok) { |
| 14 | + throw new Error(`Failed to fetch ${url}: ${response.statusText}`) |
| 15 | + } |
| 16 | + |
| 17 | + // Ensure the directory exists. |
| 18 | + const dir = dirname(outputPath) |
| 19 | + await mkdir(dir, { recursive: true }) |
| 20 | + |
| 21 | + const fileStream = createWriteStream(outputPath) |
| 22 | + await finished(Readable.fromWeb(response.body as any).pipe(fileStream)) |
| 23 | +} |
| 24 | + |
7 | 25 | export default async function getSnarkArtifacts(treeDepth: number): Promise<SnarkArtifacts> {
|
8 | 26 | const tmpDir = "semaphore-proof"
|
9 |
| - const tmpPath = `${tmp.tmpdir}/${tmpDir}-${treeDepth}` |
| 27 | + const tmpPath = `${os.tmpdir()}/${tmpDir}-${treeDepth}` |
10 | 28 |
|
11 |
| - if (!fs.existsSync(tmpPath) || fs.readdirSync(tmpPath).length !== 2) { |
12 |
| - await download(`https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.wasm`, tmpPath) |
13 |
| - await download(`https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.zkey`, tmpPath) |
| 29 | + if (!existsSync(tmpPath) || readdirSync(tmpPath).length !== 2) { |
| 30 | + await download( |
| 31 | + `https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.wasm`, |
| 32 | + `${tmpPath}/semaphore.wasm` |
| 33 | + ) |
| 34 | + await download( |
| 35 | + `https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.zkey`, |
| 36 | + `${tmpPath}/semaphore.zkey` |
| 37 | + ) |
14 | 38 | }
|
15 | 39 |
|
16 | 40 | return {
|
|
0 commit comments