-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add functions to download artifacts (#45)
* feat: add functions to download artifacts * chore: add test step in `pr` gh workflow * changeset * chore: update `rollup.config.ts` * chore: export download node module * docs: add back tsdoc comments * fix `tsconfig.json` * chore: remove `@rollup/plugin-terser` * chore: check build in pr workflow * fix tsconfig.json * chore: fix remaining conf issues * chore(poseidon): add circuit npm field --------- Co-authored-by: cedoor <[email protected]>
- Loading branch information
Showing
16 changed files
with
2,252 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@zk-kit/artifacts": minor | ||
--- | ||
|
||
Add `maybeGetSnarkArtifacts` function (initally from `@zk-kit/utils`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { type Project, projects } from '../projects' | ||
import type { SnarkArtifacts, Version } from './types' | ||
|
||
export default async function maybeGetSnarkArtifacts( | ||
project: Project, | ||
options: { | ||
parameters?: (bigint | number | string)[] | ||
version?: Version | ||
cdnUrl?: string | ||
} = {}, | ||
): Promise<SnarkArtifacts> { | ||
if (!projects.includes(project)) { | ||
throw new Error(`Project '${project}' is not supported`) | ||
} | ||
|
||
options.version ??= 'latest' | ||
options.cdnUrl ??= 'https://unpkg.com' | ||
|
||
const BASE_URL = `${options.cdnUrl}/@zk-kit/${project}-artifacts@${options.version}` | ||
const parameters = options.parameters | ||
? `-${options.parameters.join('-')}` | ||
: '' | ||
|
||
return { | ||
wasm: `${BASE_URL}/${project}${parameters}.wasm`, | ||
zkey: `${BASE_URL}/${project}${parameters}.zkey`, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { createWriteStream, existsSync } from 'node:fs' | ||
import { mkdir } from 'node:fs/promises' | ||
import os from 'node:os' | ||
import { dirname } from 'node:path' | ||
import _maybeGetSnarkArtifacts from './download.browser' | ||
import type { 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}`) | ||
if (!response.body) throw new Error('Failed to get response body') | ||
|
||
const dir = dirname(outputPath) | ||
await mkdir(dir, { recursive: true }) | ||
|
||
const fileStream = createWriteStream(outputPath) | ||
const reader = response.body.getReader() | ||
|
||
try { | ||
const pump = async () => { | ||
const { done, value } = await reader.read() | ||
if (done) { | ||
fileStream.end() | ||
return | ||
} | ||
|
||
fileStream.write(Buffer.from(value)) | ||
await pump() | ||
} | ||
|
||
await pump() | ||
} catch (error) { | ||
fileStream.close() | ||
throw error | ||
} | ||
} | ||
|
||
// https://unpkg.com/@zk-kit/poseidon-artifacts@latest/poseidon.wasm -> @zk/poseidon-artifacts@latest/poseidon.wasm | ||
const extractEndPath = (url: string) => url.substring(url.indexOf('@zk')) | ||
|
||
async function maybeDownload(url: string) { | ||
const outputPath = `${os.tmpdir()}/${extractEndPath(url)}` | ||
|
||
if (!existsSync(outputPath)) await download(url, outputPath) | ||
|
||
return outputPath | ||
} | ||
|
||
/** | ||
* Downloads SNARK artifacts (`wasm` and `zkey`) files if not already present in OS tmp folder. | ||
* @example | ||
* ```ts | ||
* { | ||
* wasm: "/tmp/@zk-kit/semaphore-artifacts@latest/semaphore-3.wasm", | ||
* zkey: "/tmp/@zk-kit/semaphore-artifacts@latest/semaphore-3.zkey" | ||
* } | ||
* ``` | ||
* @returns {@link SnarkArtifacts} | ||
*/ | ||
export default async function maybeGetSnarkArtifacts( | ||
...pars: Parameters<typeof _maybeGetSnarkArtifacts> | ||
): Promise<SnarkArtifacts> { | ||
const { wasm: wasmUrl, zkey: zkeyUrl } = await _maybeGetSnarkArtifacts( | ||
...pars, | ||
) | ||
const [wasm, zkey] = await Promise.all([ | ||
maybeDownload(wasmUrl), | ||
maybeDownload(zkeyUrl), | ||
]) | ||
|
||
return { | ||
wasm, | ||
zkey, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @prop SnarkArtifacts.wasm | ||
* @prop SnarkArtifacts.zkey | ||
* @interface | ||
*/ | ||
export type SnarkArtifacts = Record<'wasm' | 'zkey', string> | ||
|
||
type Digit = `${number}` | ||
type PreRelease = 'alpha' | 'beta' | ||
|
||
/** | ||
* Semantic version. | ||
* @example | ||
* 1.0.0-beta | ||
* 2.0.0 | ||
* @example | ||
* "latest" | ||
*/ | ||
export type Version = | ||
| `${Digit}.${Digit}.${Digit}` | ||
| `${Digit}.${Digit}.${Digit}-${PreRelease}` | ||
| `${Digit}.${Digit}.${Digit}-${PreRelease}.${Digit}` | ||
| 'latest' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,2 @@ | ||
export enum Proof { | ||
SEMAPHORE_IDENTITY = 'semaphore-identity', | ||
POSEIDON = 'poseidon', | ||
//RLN = 'rln', | ||
SEMAPHORE = 'semaphore', | ||
} | ||
export * from './projects' | ||
export * from './download/download.node' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export enum Project { | ||
SEMAPHORE_IDENTITY = 'semaphore-identity', | ||
POSEIDON = 'poseidon', | ||
//RLN = 'rln', | ||
SEMAPHORE = 'semaphore', | ||
} | ||
|
||
export const projects = Object.values(Project) |
Oops, something went wrong.