Skip to content

Commit

Permalink
chore(artifacts|cli): move cli into artifacts (#118)
Browse files Browse the repository at this point in the history
* chore: setup `lefthook` (#137)

chore: setup lefthook

Manages git hooks. Replacement for a typical husky+lint-staged setup
Used for formatting, lint check and typecheck

* chore: shorten and sort imports

* chore: add node:fs/promises to external deps

* chore: update changeset config

* changeset

* docs: update READMEs

* chore: bump lockfile

* revert changes in tsconfig.build.json

* fix: update default and types exports

* chore: add baseUrl to tsconfig.build

* refactor: reuse `banner` to define `bannerCli`
  • Loading branch information
sripwoud authored Aug 7, 2024
1 parent fe3fb9b commit d3c650a
Show file tree
Hide file tree
Showing 40 changed files with 3,973 additions and 3,533 deletions.
9 changes: 6 additions & 3 deletions .biome.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
},
"overrides": [
{
"include": ["packages/cli/src/spinner.ts", "packages/cli/src/commands/generate/action.ts"],
"include": ["packages/artifacts/src/cli/spinner.ts", "packages/artifacts/src/cli/commands/generate/action.ts"],
"linter": { "rules": { "suspicious": { "noExplicitAny": "off" } } },
},
{
"include": ["packages/cli/src/commands/download/index.ts", "packages/cli/src/commands/generate/action.ts"],
"include": [
"packages/artifacts/src/cli/commands/download/index.ts",
"packages/artifacts/src/cli/commands/generate/action.ts",
],
"linter": { "rules": { "style": { "noParameterAssign": "off" } } },
},
{
"include": ["apps/web/src/main.tsx", "packages/cli/src/commands/generate/action.ts"],
"include": ["apps/web/src/main.tsx", "packages/artifacts/src/cli/commands/generate/action.ts"],
"linter": { "rules": { "style": { "noNonNullAssertion": "off" } } },
},
],
Expand Down
10 changes: 4 additions & 6 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": ["@changesets/changelog-github", { "repo": "privacy-scaling-explorations/snark-artifacts" }],
"commit": false,
"fixed": [],
"linked": [],
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
"bumpVersionsWithWorkspaceProtocolOnly": true,
"changelog": ["@changesets/changelog-github", { "repo": "privacy-scaling-explorations/snark-artifacts" }],
"commit": false,
"updateInternalDependencies": "patch"
}
5 changes: 5 additions & 0 deletions .changeset/hip-avocados-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zk-kit/artifacts": major
---

Move CLI into `@zk-kit/artifacts`
51 changes: 19 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
🗄️ SNARK Artifacts
</h1>
<p align="center">A streamlined mechanism for distributing SNARK artifacts.</p>
<p align="center">
<a href="https://snark-artifacts.pse.dev"><b>snark-artifacts.pse.dev</b></p></a>
</p>
</p>

| [P0tion](https://github.com/privacy-scaling-explorations/p0tion) has made conducting SNARK phase 2 trusted setup ceremonies easier for many zero-knowledge projects. However, there still seems to be no simple mechanism for distributing the artifacts generated in ceremonies. This project aims to build step-by-step a simple and easily accessible mechanism for distributing zero-knowledge artifacts (`wasm`/`zkey` files). |
Expand Down Expand Up @@ -135,20 +138,20 @@ Interested in contributing to this project? See the [board](https://github.com/o
</tr>
<tr>
<td>
<a href="https://github.com/privacy-scaling-explorations/snark-artifacts/tree/main/packages/cli">
@zk-kit/artifacts-cli
<a href="https://github.com/privacy-scaling-explorations/snark-artifacts/tree/main/packages/artifacts">
@zk-kit/artifacts
</a>
</td>
<td>
<!-- NPM version -->
<a href="https://npmjs.org/package/@zk-kit/artifacts-cli">
<img src="https://img.shields.io/npm/v/@zk-kit/artifacts-cli.svg?style=flat-square" alt="NPM version" />
<a href="https://npmjs.org/package/@zk-kit/artifacts">
<img src="https://img.shields.io/npm/v/@zk-kit/artifacts.svg?style=flat-square" alt="NPM version" />
</a>
</td>
<td>
<!-- Downloads -->
<a href="https://npmjs.org/package/@zk-kit/artifacts-cli">
<img src="https://img.shields.io/npm/dm/@zk-kit/artifacts-cli.svg?style=flat-square" alt="Downloads" />
<a href="https://npmjs.org/package/@zk-kit/artifacts">
<img src="https://img.shields.io/npm/dm/@zk-kit/artifacts.svg?style=flat-square" alt="Downloads" />
</a>
</td>
<td>
Expand All @@ -167,10 +170,9 @@ Interested in contributing to this project? See the [board](https://github.com/o
-**File integrity**: A system that guarantees the integrity of the artifacts.
-**Automated Distribution**: A system that automatically distributes artifacts after the ceremony (possibly through [P0tion](https://github.com/privacy-scaling-explorations/p0tion)).

## 📜 Getting Started

To add a new set of artifacts for your project, simply add your NPM package to the [`packages`](https://github.com/privacy-scaling-explorations/snark-artifacts/tree/main/packages) folder. The packages are published on NPM and made available on your preferred CDN (e.g. https://unpkg.com).
## 📜 Develop

To add a new set of artifacts for your project, simply add your NPM package to the [`packages`](https://github.com/privacy-scaling-explorations/snark-artifacts/tree/main/packages) folder.\
You can also create [issue](https://github.com/privacy-scaling-explorations/snark-artifacts/issues/new/choose) and let the core contributors add your files.

### Partial clone
Expand Down Expand Up @@ -201,28 +203,13 @@ After the first time clone, you can use the following npm scripts:

### [Contribute](./CONTRIBUTING.md)

### Downloading artifacts

ZK-Kit provides a set of functions to automatically download your artifacts. For example:

```ts
import { maybeGetSnarkArtifacts, Project } from '@zk-kit/artifacts'
## Downloading artifacts

// It will return the artifacts' paths.
const { wasm, zkey } = await maybeGetSnarkArtifacts(Project.POSEIDON, {
parameters: [2],
version: '1.0.0',
})
You can download all artifacts either:

console.log(wasm) // "/tmp/@zk-kit/[email protected]/poseidon-2.wasm"
console.log(zkey) // "/tmp/@zk-kit/[email protected]/poseidon-2.zkey"

// Paths on browsers will be the `unpkg` URLs directly.
// e.g. https://unpkg.com/@zk-kit/[email protected]/poseidon-2.zkey
```

You can download all artifacts from this repository with:

```bash
bash <(curl -sSL https://raw.githubusercontent.com/privacy-scaling-explorations/snark-artifacts/main/scripts/bin/dowload-artifacts.bash) -h
```
- directly from this repository with:
```bash
bash <(curl -sSL https://raw.githubusercontent.com/privacy-scaling-explorations/snark-artifacts/main/scripts/bin/dowload-artifacts.bash) -h
```
- with functions exported in [`@zk-kit/artifacts`](./packages/artifacts/README.md#downloading-artifacts)
- with the [`snarkli` CLI](./packages/artifacts/README.md#cli)
3 changes: 1 addition & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { join } from 'node:path'
import type { JestConfigWithTsJest } from 'ts-jest'
// import preset from 'ts-jest/'

const projects = ['artifacts', 'cli'].map((name) => `packages/${name}/test`)
const config: JestConfigWithTsJest = {
preset: 'ts-jest',
collectCoverage: true,
Expand All @@ -13,7 +12,7 @@ const config: JestConfigWithTsJest = {
},
moduleDirectories: ['node_modules', '<rootDir>/node_modules', '<rootDir>/src'],
moduleFileExtensions: ['js', 'ts'],
projects,
projects: ['packages/artifacts/test'],
}

export default config
43 changes: 42 additions & 1 deletion packages/artifacts/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
TODO
<p align="center">
<h1 align="center">
@zk-kit/artifacts
</h1>
</p>
<p align="center">
<a href="https://npmjs.org/package/@zk-kit/artifacts">
<img src="https://img.shields.io/npm/v/@zk-kit/artifacts.svg?style=flat-square" alt="NPM version" />
</a>
<a href="https://npmjs.org/package/@zk-kit/artifacts">
<img src="https://img.shields.io/npm/dm/@zk-kit/artifacts.svg?style=flat-square" alt="Downloads" />
</a>
</p>

## Downloading artifacts

`@zk-kit/artifacts` provides a set of functions to automatically download your artifacts. For example:

```ts
import { maybeGetSnarkArtifacts, Project } from '@zk-kit/artifacts'

// It will return the artifacts' paths.
const { wasm, zkey } = await maybeGetSnarkArtifacts(Project.POSEIDON, {
parameters: [2],
version: '1.0.0',
})

console.log(wasm) // "/tmp/@zk-kit/[email protected]/poseidon-2.wasm"
console.log(zkey) // "/tmp/@zk-kit/[email protected]/poseidon-2.zkey"

// Paths on browsers will be the `unpkg` URLs directly.
// e.g. https://unpkg.com/@zk-kit/[email protected]/poseidon-2.zkey
```

## CLI

Or you can install our `snarkli` CLI to download artifacts, list available packages or generate artifacts (`.zkey`, `.wasm`) from `.circom` source files.

```commandline
pnpm add -g @zk-kit/artifacts
snarkli
```
21 changes: 16 additions & 5 deletions packages/artifacts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
"name": "@zk-kit/artifacts",
"version": "1.9.0",
"type": "module",
"bin": {
"snarkli": "./dist/cli/index.js"
},
"description": "Utilities for downloading snark artifacts",
"scripts": {
"build": "pnpm run clean && rollup -c rollup.config.ts --configPlugin typescript",
"clean": "rimraf dist",
"build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
"test": "jest -c test/jest.config.ts",
"prepublishOnly": "pnpm run build",
"start.cli": "[ ! -f dist/cli/index.js ] && pnpm run build; node dist/cli",
"typecheck": "tsc"
},
"repository": {
Expand Down Expand Up @@ -36,15 +39,15 @@
"types": "./dist/types/index.node.d.ts",
"exports": {
".": {
"types": "./dist/types/index.node.d.ts",
"types": "./dist/types/index.browser.d.ts",
"node": {
"types": "./dist/types/index.node.d.ts",
"import": "./dist/index.node.js",
"require": "./dist/index.node.cjs",
"default": "./dist/index.node.cjs"
},
"browser": "./dist/index.browser.js",
"default": "./dist/index.node.js"
"default": "./dist/index.browser.js"
}
},
"files": [
Expand All @@ -55,6 +58,14 @@
"@rollup/plugin-typescript": "^11.1.6",
"@types/node": "^20.12.10",
"rimraf": "^5.0.5",
"rollup": "^4.12.0"
"rollup": "^4.18.1"
},
"dependencies": {
"@commander-js/extra-typings": "^12.1.0",
"@inquirer/input": "^2.1.8",
"@inquirer/select": "^2.3.4",
"circomkit": "^0.2.1",
"ora": "^5.4.1",
"snarkjs": "^0.7.4"
}
}
17 changes: 17 additions & 0 deletions packages/artifacts/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const banner = `/**
* @license ${pkg.license}
* @see [Github]{@link ${pkg.homepage}}
*/`
const shebang = '#!/usr/bin/env node'
const bannerCli = [shebang, banner].join('\n')

const config: RollupOptions[] = [
{
Expand All @@ -37,6 +39,21 @@ const config: RollupOptions[] = [
output: [{ file: pkg.exports['.'].browser, format: 'es', banner }],
plugins,
},
{
input: 'src/cli/index.ts',
output: { file: pkg.bin.snarkli, format: 'es', banner: bannerCli },
plugins,
external: [
...Object.keys(pkg.dependencies),
'node:console',
'node:fs',
'node:fs/promises',
'node:os',
'node:path',
'node:process',
'node:stream',
],
},
]

export default config
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Argument, Command } from '@commander-js/extra-typings'
import { maybeGetSnarkArtifacts, Project, projects } from '@zk-kit/artifacts'
import { spinner } from '../../spinner'
import { validateNonEmptyInput, validateOrThrow, validateProject } from '../../validators'
import { spinner } from 'cli/spinner'
import { validateNonEmptyInput, validateOrThrow, validateProject } from 'cli/validators'
import { maybeGetSnarkArtifacts } from 'index.node'
import { Project, projects } from 'projects'
import { getParametersInput, getProjectInput } from './prompts'

export const download = new Command('download').alias('d').description(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import input from '@inquirer/input'
import select from '@inquirer/select'
import { projects as choices } from '@zk-kit/artifacts'
import { validateNonEmptyInput } from '../../validators'
import { validateNonEmptyInput } from 'cli/validators'
import { projects as choices } from 'projects'

export const getProjectInput = async () =>
select({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { spinner } from 'cli/spinner'
import { readFileSync } from 'node:fs'
import { exit } from 'node:process'
import { spinner } from '../../spinner.ts'
import { generateActionNoExit } from '../generate/action.ts'
import { generateActionNoExit } from '../generate/action'

export default async function generateBatch(optionsPath: string, destination: string) {
const options = JSON.parse(readFileSync(optionsPath, 'utf8')) as Record<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Circomkit, type CircomkitConfig, type CircuitConfig } from 'circomkit'
import { spinner } from 'cli/spinner'
import { validateJsonFileInput, validateOrThrow } from 'cli/validators'
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { dirname } from 'node:path'
import { chdir, cwd, exit } from 'node:process'
import { Writable } from 'node:stream'
import { spinner } from '../../spinner.ts'
import { validateJsonFileInput, validateOrThrow } from '../../validators.ts'
import { getCircomkitConfigInput, getDestinationInput, selectCircuit } from './prompts.ts'
import { getCircomkitConfigInput, getDestinationInput, selectCircuit } from './prompts'

class SilentStream extends Writable {
_write(_chunk: any, _encoding: string, callback: () => void) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command } from '@commander-js/extra-typings'
import generateAction from './action.ts'
import generateAction from './action'

export const generate = new Command('generate').alias('g').description(
'Generate snark artifacts for a given source circom circuit',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import input from '@inquirer/input'
import select from '@inquirer/select'
import { validateJsonFileInput } from '../../validators.ts'
import { validateJsonFileInput } from 'cli/validators'

export const getCircomkitConfigInput = async () =>
input(
Expand Down
6 changes: 6 additions & 0 deletions packages/artifacts/src/cli/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { download } from './download'
import { generate } from './generate'
import { generateBatch } from './generate-batch'
import { list } from './list'

export default [download, generate, generateBatch, list]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Command } from '@commander-js/extra-typings'
import { getAvailableVersions, projects } from '@zk-kit/artifacts'
import { exit } from 'node:process'
import { spinner } from '../spinner.ts'
import { getAvailableVersions, projects } from 'projects'
import { spinner } from '../spinner'

export const list = new Command('list').alias('l').description(
'List all projects and their available packages versions',
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Project, projects } from '@zk-kit/artifacts'
import { existsSync } from 'node:fs'
import { extname } from 'node:path'
import { CliError } from './errors.ts'
import { type Project, projects } from 'projects'
import { CliError } from './errors'

export function validateFilePath(input: string) {
if (!existsSync(input)) return CliError.FILE_DOES_NOT_EXIST
Expand Down
2 changes: 1 addition & 1 deletion packages/artifacts/src/download/index.browser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Project, projects } from '../projects'
import { type Project, projects } from 'projects'
import type { SnarkArtifacts, Version } from './types'
import { getBaseUrl } from './urls'

Expand Down
2 changes: 1 addition & 1 deletion packages/artifacts/src/download/urls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Project } from '../projects'
import type { Project } from 'projects'
import type { Version } from './types'

const BASE_URL = 'https://snark-artifacts.pse.dev'
Expand Down
4 changes: 2 additions & 2 deletions packages/artifacts/src/index.browser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './index.shared'
import maybeGetSnarkArtifacts from './download/index.browser'
export * from 'index.shared'
import maybeGetSnarkArtifacts from 'download/index.browser'
export { maybeGetSnarkArtifacts }
6 changes: 3 additions & 3 deletions packages/artifacts/src/index.node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { download, maybeDownload } from './download/download'
export * from './index.shared'
import maybeGetSnarkArtifacts from './download/index.node'
export { download, maybeDownload } from 'download/download'
export * from 'index.shared'
import maybeGetSnarkArtifacts from 'download/index.node'
export { maybeGetSnarkArtifacts }
4 changes: 2 additions & 2 deletions packages/artifacts/src/index.shared.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './download/types'
export * from './projects'
export * from 'download/types'
export { Project, projects } from 'projects'
Loading

0 comments on commit d3c650a

Please sign in to comment.