Git Product home page Git Product logo

zkapp-cli's Introduction

zkApp CLI

The zkApp CLI allows you to scaffold, write, test, and deploy zkApps (zero knowledge apps) for Mina Protocol using recommended best practices.

Apps are written using o1js, a TypeScript framework for writing zero knowledge proof-based smart contracts. o1js is automatically included when you create a project using the zkApp CLI.

Getting Started

Read through this README file and the Mina Protocol zkApp Developer docs.

Build foundational knowledge by completing the guided steps in the zkApp Developer Tutorials.

Get Involved

To learn about ways to participate and interact with community members, see the Mina Online Communities docs.

Contributions are always appreciated. See the zkApp CLI CONTRIBUTING guidelines.

Install the zkApp CLI

To install the zkApp CLI:

$ npm install -g zkapp-cli

To confirm successful installation:

$ zk --version

Update the zkApp CLI

You are prompted to install the new version if you are running an earlier zkApp CLI minor version. For example, if you are running version 0.9.8, but the current version is 0.10.2, you are prompted to update.

You are not prompted to update if you are using an earlier patch version. For example, you are not notified to upgrade when you are running 0.10.1, and the current version is 0.10.2.

To update to the latest version of the zkApp CLI:

$ npm update -g zkapp-cli

Dependencies

To use the zkApp CLI and o1js, your environment requires:

  • NodeJS v18 and later
  • NPM v10 and later
  • git v2 and later

Use a package manager to install the required versions and upgrade older versions if needed. Package managers for the supported environments are:

  • MacOS Homebrew

  • Windows Chocolatey

  • Linux

    • apt, yum, and others

    On Linux, you might need to install a recent Node.js version by using NodeSource. Use deb or rpm as recommended by the Node.js project.

To verify your installed versions, use npm -v, node -v, and git -v.

Usage

To see all of the zkApp CLI commands:

zk --help

Create a project

zk project my-proj  # or path/to/my-proj

✔ Fetch project template
✔ Initialize Git repo
✔ NPM install
✔ Set project name
✔ Git init commit

Success!

Next steps:
  cd my-proj
  git remote add origin <your-repo-url>
  git push -u origin main

This command creates a directory containing a new project template, fully set up and ready for local development.

  • See the included README for usage instructions. All of the usual commands are available: npm run build, npm run test, npm run coverage, and so on.
  • A GitHub repo is automatically initialized in the project directory. For consistency and by convention, we use main as the default development branch.
  • A GitHub Actions CI workflow is also included. If you push your project to GitHub, GitHub Actions run your tests (named as *.test.js) automatically whenever you push a commit or open a pull request.
  • Code style consistency (using Prettier) and linting (using ES Lint) is automatically enforced using Git pre-commit hooks. This requires no configuration and occurs automatically when you commit a change, for example, git commit -m 'feat: add awesome feature'.

Create an example project

zk example <name>

where name is one of the names found in the zkApps examples directory.

All examples are based on the standard project template created by the zkApp CLI and contain only changes within the src directory, so feel free to use one of these examples as your project base.

When inside an example folder in your terminal, you can run the example:

npm run build && node ./build/src/index.js

Create a new file

zk file <name>  # or path/to/name

This command creates name.js and name.test.js.

For convenience, when you run this command in your project's root directory creates the files inside your project's src dir automatically, even if you don't specify src/ as part of your file path.

When you run this command when you are not in your project's root dir, files are created at the path you specify relative to your terminal's current working directory.

Show system info

zk system

This command outputs system info such as your NodeJS version, NPM version, zkapp-cli version, and so on. For easier troubleshooting, be sure to include this information if submitting a bug report for zkapp-cli. See the CONTRIBUTING guidelines.

Update your config.json

zk config

The auto-generated config.json file contains your deployment-related configurations.

Run the zk config command to add a new deployment configuration to this file.

Respond to the interactive command prompts to build or update a deploy alias.

A deploy alias consists of:

  • A self-describing name. This tutorial uses berkeley. The deploy alias name can be anything and does not have to match the network name.

  • The Mina GraphQL API URL that defines the network that receives your deploy transaction and broadcasts it to the appropriate Mina network (Testnet, Devnet, Mainnet, and so on)

  • The transaction fee (in MINA) to use when deploying

  • Two key pairs:

    • A key pair for the zkApp account. Public and private keys to use in your application are automatically generated in keys/berkeley.json.

    • A key pair to use as a fee payer account for updates and deployments. Public and private keys are stored on your local computer and can be used across multiple projects.

  • Fee payer account alias

    A fee payer account is required. If you don't have a fee payer account, you are prompted to select one of these options:

    • Recover a fee payer account from an existing base58 private key
    • Create a new fee payer key pair

Deploy your smart contract

zk deploy <alias>
// OR
zk deploy // shows a list of aliases in your project to choose from

Deployment is supported only to Berkeley Testnet until zkApp programmability is available on Mina Mainnet.

After you run zk config, the zk deploy command allows you to deploy a smart contract to a specified deploy alias.

Note: When you deploy to an alias for the first time, you are prompted to choose which smart contract you want to deploy from those that exist as named exports in your project. The name of the smart contract that you choose is remembered by being saved into your config.json for this alias. For safety, the next time you run zk deploy <alias> this same smart contract automatically deploys to this alias. See Tutorial 3: Deploy to a Live Network.

Testing your zkApp

To test your zkApps, you first create automated tests for your smart contract and test with a simulated local blockchain. See Testing zkApps Locally.

Before you test with a live network, use Lightnet to test your zkApp locally on an accurate representation of a Mina blockchain.

zk lightnet --help

See Testing zkApps with Lightnet.

Block explorer

To launch a lightweight Mina explorer to monitor transactions in a web browser:

zk lightnet explorer

Community Support and Participation

Mina Protocol Discord is the most popular place where Mina enthusiasts and technical contributors gather to share knowledge.

Join us in these zkApps channels:

Feedback

To provide feedback, first review existing issues. You can create issues with feature requests for zkApp CLI and Lightnet in this repository.

zkApp CLI is an open source project, see the CONTRIBUTING guidelines.

License

Apache-2.0

zkapp-cli's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

zkapp-cli's Issues

[Windows] Git init fails when creating a project

Description
Install snapp-cli on Windows and issue a snapp project [name] command. The git init process will fail.

System Information

λ snapp system
Please include the following when submitting a Github issue:

  System:
    OS: Windows 10 10.0.19042
    CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
  Binaries:
    Node: 16.10.0 - C:\Program Files\nodejs\node.EXE
    Yarn: Not Found
    npm: 7.24.0 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    @o1labs/snarkyjs-web: Not Found
  npmGlobalPackages:
    snapp-cli: Not Found

Output

λ snapp project snapp-test1
√ Fetch project template
√ Initialize Git repo
- NPM install...The system cannot find the path specified.
× NPM install
√ Set project name
- Git init commit...warning: LF will be replaced by CRLF in .github/workflows/ci.yml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in .gitignore.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in .husky/_/husky.sh.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in .husky/pre-commit.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in .prettierignore.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in .prettierrc.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in LICENSE.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in jest.config.js.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in package-lock.json.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in package.json.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in src/index.test.ts.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in src/index.ts.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in tsconfig.json.
The file will have its original line endings in your working directory
error: pathspec 'commit'' did not match any file(s) known to git
× Git init commit

Success!

Next steps:
  cd snapp-test1
  git remote add origin <your-repo-url>
  git push -u origin main

file: when in the project root, create files in /src

Check if first child dir is /src. If so, create it there. If not, create file wherever we are because user could be in src/foo/..., etc).

Add to README: "If you're in the project root, it will create these in the /src dir."

project: add --verbose flag

E.g. snapp project foo --verbose

For insight into why npm install might be hanging. We don't show it all the time b/c it clutters up the UX and makes it harder for the user to see the "done" and "remaining" steps.

Remove degit, use gittar

Will fix: #31
Will allow slightly cleaner syntax.

  • Remove degit and use gittar everywhere.
  • Update postinstall.js to warm gittar's cache
  • Ensure the CLI still works offline, immediately after installation.

Add tsconfig.json to root dir that fixes tests

The problem is our dir structure is like this:

  /src JS    (this issue)
      templates/project JS   (DONE)
      templates/project-ts TS  (DONE)

Tests & CI work in both project & project-ts; no changes are needed there.

But snapp-cli (/src in the schematic above) needs to a tsconfig.json added that will allow us to run tests npm run test successfully even though JS and TS test files coexist.

Goal is to run ALL tests (for /src,project, & project-ts), rather than excluding the projects.

Mismatch between NodeJS and TypeScript Target

System:
    OS: Linux 5.11 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
  Binaries:
    Node: 12.22.6 - ~/.nvm/versions/node/v12.22.6/bin/node
    Yarn: 1.22.5 - /usr/bin/yarn
    npm: 6.14.15 - ~/.nvm/versions/node/v12.22.6/bin/npm
  npmPackages:
    snapp-cli: Not Found
    snarky-js: Not Found
$ npm run test

> [email protected] test /tmp/myproject3
> jest

ts-jest[config] (WARN) There is a mismatch between your NodeJs version v12.22.6 and your TypeScript target undefined. This might lead to some unexpected errors when running tests with `ts-jest`. To fix this, you can check https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping

Windows support

  • 1. Git warns about line endings Mac vs Windows when committing. Can fix via .gitattributes. Fixed in commit.
  • 2. When user types snapp project myproj, we update package.json with this name. This works on Mac & Linux. But on windows, it appears like this c:\path\to\myproj for an unknown reason.
  • 3. npm install causes an error of system cannot find the path specified for an unknown reason.

Linux support

Test CLI on Ubuntu, using Node 14 & 16. If time, test Node 12 too please.

Can easily install different Node versions using NodeSource.

Known issues

  1. Linux requires use of sudo to install a command globally. Unfortunately, this causes Gittar's directory to be created at /root/.gittar during install & then when commands are used, the CLI expects this to be at /<user>/.gittar. We need to figure out a solution to this--maybe using /root/.gittar all the time? But this might not be configurable within Gittar. Or submitting a PR to Gittar to identify the non Sudo user? TBD.
  2. ???

Add deploy command

  1. Add command snapp deploy <network>
  2. Exclude config file from git
  3. If config file does not exist yet, create it and create an address & private keypair and say: "Testnet address & private key created & added to config.js". (User can overwrite this with their own address & private key, if updating an existing Snapp.)
  4. If config exists, check if network name exists. If not, create address & private key as above. If yes, we'll use it.
  5. "Continue with deploy to y/n?"
  6. Create TX from proof.
  7. Send to proper network.
  • Alternatively, make it interactive and allow user to select tesnet or devnet
  • Add step to run tests first?

Cloing project templates sometimes fails with an unhelpful error message

System: M1 MacBook Air

Running the create project command gives me:

% snap proj myproj
x Clone project template
could not find commit hash for main
ERROR: MISSING_REF

Some research indicates that this likely a problem with degit which is used to fetch project templates:
Rich-Harris/degit#37

Note that git is installed and available on PATH on this machine. Per the suggestion in the issue linked above, setting cache: false in degit options does seem to fix this issue in a pinch. However, that might not be a good long-term solution.

Others suggest that the XCode Command Line Utilities might need to be installed on Mac for this work. This machine actually does not have those installed, so I will try that soon and see if it works with cache: true. If so, we could make this part of the installation instructions.

Alternatively, we could look into other methods of fetching project templates (gittar is one that apparently has similar functionality but doesn't depend on git).

Create a TypeScript `template/project-ts`

  • duplicate js project dir
  • set up ts & build command
  • get tests working
  • get project-ts' CI running (push it to Github to confirm)
  • update ts project's README where relevant

Rename Git repo init to use `main` instead of `master`

martin@minkov:/tmp$ snapp project myproj 
✔ Fetch project template
⠋ Initialize Git repo...error: refname refs/heads/master not found
fatal: Branch rename failed
✖ Initialize Git repo
✔ NPM install
✔ Set project name
✔ Git init commit

Success!

Next steps:
  cd myproj
  git remote add origin <your-repo-url>
  git push -u origin main

martin@minkov:/tmp/myproj$ snapp system
Please include the following when submitting a Github issue:

  System:
    OS: Linux 5.11 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
  Binaries:
    Node: 12.22.6 - ~/.nvm/versions/node/v12.22.6/bin/node
    Yarn: 1.22.5 - /usr/bin/yarn
    npm: 6.14.15 - ~/.nvm/versions/node/v12.22.6/bin/npm
  npmPackages:
    snapp-cli: Not Found
    snarky-js: Not Found

Template: Get the project template working with SnarkyJS

Project template

  • Add SnarkyJS to package.json & NPM install
  • Add files for Hello World example (e.g. verify hash) in the /src directory of the project template.
  • Push project to Github to confirm project's CI file still passes.

CLI

  • Add npm run build command, if needed.

project: enable offline use

E.g. snapp project foo --offline. Update: We won't use a flag because we can now handle all aspects to allow offline use when required.

Why? So a dev can work offline (on an airplane, etc).

  • Note that degit requires internet unless the dev has a cached project. So it will only work offline if the dev has run the snapp project <name> with internet once previously.

  • Also, npm install or npm ci require internet unless package-lock.json exists AND the packages exist in the local NPM cache.

  • 1. Skip any optional commands that require internet and show this to the user that it's being skipped. Update: not necessary anymore.

  • 2. Enable cache in degit.

  • 3. Warm degit cache during CLI installation

  • Warm NPM cache for project deps during CLI installation

Add `check` command

To check the project against current conventions and best practices. Why current and not "current for the version of SnarkyJS used"...to encourage consistency in the ecosystem.

Codify the current conventions and automatically check the current dir for:

  • expected dir structure & files
  • .prettierrc exists & follows current format (and code passes?)
  • lint config file exists & follows current rules (and code passes?)
  • package-lock.json exists in Git (b/c it's required by the npm ci command used in CI templates)
  • tests exist (and pass?)
  • test coverage above x%? or function call coverage at 100%?

Could add a CI file for this too. Write this in JS and it can be used by both the CLI & CI workflow.

For failures, we might want to give a link to a conventions.md (to the proper section header within it) which explains 1.) the expected convention, and 2.) why.

Convert to ES6 imports

Yargs failed with ES6 imports initially. Try again to see if it has fixed the issue.

  • Convert to ES6 (note: project templates already use ES6 since they're unaffected by yargs)
  • run npm remove eslint-plugin-import
  • remove "commonjs": true, from snapp-cli's .eslintrc config.

NPM cache doesn't accept multiple arguments in NPM 6

martin@minkov:/tmp$ npm install -g snapp-cli
/home/martin/.nvm/versions/node/v12.22.6/bin/snapp -> /home/martin/.nvm/versions/node/v12.22.6/lib/node_modules/snapp-cli/src/bin/index.js
/home/martin/.nvm/versions/node/v12.22.6/bin/snapp-cli -> /home/martin/.nvm/versions/node/v12.22.6/lib/node_modules/snapp-cli/src/bin/index.js

> [email protected] postinstall /home/martin/.nvm/versions/node/v12.22.6/lib/node_modules/snapp-cli
> node src/postinstall

  Warm NPM cache for project template deps.
npm ERR! code EINVALIDTAGNAME
npm ERR! Invalid tag name "^7.0.1@jest@^27.0.6": Tags may not have any characters that encodeURIComponent encodes.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/martin/.npm/_logs/2021-09-21T20_07_58_574Z-debug.log
1
  Warm cache for project template.
+ [email protected]
added 74 packages from 55 contributors in 0.837s
martin@minkov:/tmp$ snapp system
Please include the following when submitting a Github issue:

  System:
    OS: Linux 5.11 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
  Binaries:
    Node: 12.22.6 - ~/.nvm/versions/node/v12.22.6/bin/node
    Yarn: 1.22.5 - /usr/bin/yarn
    npm: 6.14.15 - ~/.nvm/versions/node/v12.22.6/bin/npm
  npmPackages:
    snapp-cli: Not Found
    snarky-js: Not Found

Discussion: Should the project template be a clone of a repo on Github?

As opposed to a using Gittar and creating a new Git repo.

  • Pro: This would allow devs to rebase using the latest recommended project & see what has changed.
  • Cons: Dev's repos would have more history, that they may not want--though a dev could delete .git and start from scratch if they wanted too, or could squash old commits. Cloning would be slower than degit. Degit is already set up with caching to work even offline too.

[ ] To do this, git clone instead of using gittar.
[ ] Change project command to get project creation working offline again.
[ ] And rename Git remote 'origin' to 'upstream'.

Given the downsides, I lean toward "no". We will have a check and fix command that will work, even if a dev created their own repo too.

Add example command

The project command should create a mostly clean project, except for maybe a basic "Hello World" showing how files fit together. But we should make it easy to create the project using one of a few, built-in examples via an optional flag:

E.g. snapp project myproj --example sudoku

or snapp project --example sudoku

  • Add flag
  • Add examples (use git submodules based on project templates so we can keep them updated?)
  • Make sure tests exist for these examples and snapp-cli's CI template runs them, so the examples never fall out of date & break.
  • make myproj optional or ignored and just use the name of the example + incremented # if case of existing dir name conflict?

use `npm ci` instead of `npm install` for the project template?

Pro:

  • make npm install step faster during project creation via snapp project <name>. Better UX.
  • predictability -- b/c they will be using the same dep versions, though theoretically shouldn't matter if dep authors use semver correctly.

Con:

  • users won't have very latest versions of deps until they npm update at some point.

Todo:

  • commit package-lock.json
  • update code to use npm ci in src/lib/project.js

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.