DEV Community

Vanshika Srivastava
Vanshika Srivastava

Posted on

How to build your first zkApp with Mina Protocol

Today we'll be doing a deep dive to understand to zkApps and how you can build one using Mina Protocol.

What is a zkApp ?

A zkApp is an app which is based on zero knowledge proofs. In the context of Mina, these zkApps utilise zk-SNARKs and can perform complex off-chain computations with a fixed fee to verify the zero knowledge proof on the chain.

Mina's recursive zero-knowledge proofs, which are composable, let developers reuse proofs of already verified information across different blockchains and applications. This approach reduces verification costs, supports composable builds, and enhances privacy for both user and company data. Moreover, various zkBridges are in development to allow decentralized applications from other blockchains to benefit from Mina's features.

The best part about zkApps is that you can make these apps using typescript. Simply a zkApp consists of smart contract and a UI (frontend).

Image description
Img Source : Mina Protocol

Understanding o1js for Writing zk Smart Contracts

o1js is a versatile zk framework that provides the tools necessary to create zero-knowledge proofs. It allows you to develop various zk programs using a comprehensive set of built-in provable operations, such as basic arithmetic, hashing, signatures, boolean operations, comparisons, and more. The o1js framework is designed to facilitate the creation of zkApps on Mina—smart contracts that run client-side and handle private inputs.

Recursion
Kimchi, the custom proof system that powers o1js, enables the construction of infinite recursive proofs for circuits through its integration with the Pickles recursive system. Mina Protocol is unique in offering infinite recursion capabilities.

Recursion is an immensely powerful tool with numerous applications. For example:

  • Mina employs linear recursive proofs to compress its blockchain, which continuously grows, into a constant size.
  • Mina also utilizes "rollup-like" tree-based recursive proofs to compress transactions within blocks down to a constant size in parallel.
  • An app-specific rollup, such as a Mastermind game, can use linear recursive proofs to advance the application's state machine without needing to sync back to the game.
  • App-specific rollups can communicate with each other using recursion, similar to how app chains utilize Inter-Blockchain Communication (IBC) in Cosmos or Cross-Chain Virtual Machine (XVM) in parachains to send messages.

I also shared a video over twitter explaining how typescript developers can now start building zkApps. Check it out here

Setting up zkApp CLI to scaffold, write, test, and deploy zkApps (zero knowledge apps) for Mina Protocol

We will start off with dependencies :

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

If you have a later version of a dependency, install the required version using the package manager for your system:

  • MacOs Homebrew
  • Windows Chocolatey
  • Linux (apt, yum, and others)

As recommended by the Node.js project, you might need to install a recent Node.js version using NodeSource binary distributions: Debian, rpm.

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

Usage
To see usage information for all of the zkApp CLI commands:

$ zk --help

Install the zkApp CLI

To install the latest version:

npm install -g zkapp-cli

To confirm successful installation:

$ zk --version

Building zkApps using zkapp-cli

1.Create a project

$ zk project <project-name>

o1js is automatically installed when you generate a project using the zkApp CLI.

To proceed without an accompanying UI project, select none when prompted.

See Option B: Start your own project.

To create a UI, select a framework and follow the prompts.

2.Implementing test code for the project

When you use the zkApp CLI to create a project, it comes with built-in tests and examples.

o1js provides an array of 10 test accounts for the simulated LocalBlockchain instance, which are used to pay fees and sign transactions. These can be accessed via `Local.testAccounts.

The example utilizes the public/private key pairs of two of these accounts, named as follows (though you can rename them as needed):

  • deployerAccount: Deploys the smart contract
  • senderAccount: Pays transaction fees
  • Deploy the smart contract to the simulated LocalBlockchain instance, which acts as a test network.

Refer to the localDeploy function,for example, in the [Add.test.ts](https://github.com/o1-labs/zkapp-cli/blob/main/templates/project-ts/src/Add.test.ts#L38-L46) example file for details.

3. Adding the implementation for smart contract

Start experimenting with iterative development to build and test one method at a time. Add functionality to the smart contract by implementing a @method.

Build the smart contract:

npm run build

Invoke the @method you added or use new functionality in the test file.

To test zkApp locally you can npm run test in your project's root repository which will allow you to run and test application on a simulated local blockchain.

4. Test with Lightnet
Use Lightnet to accurately test your zkApp in a Mina blockchain-like environment.

Start Lightnet with the following command:

zk lightnet start
By default, this launches a single node, which should meet most testing needs.

Check the status of your local blockchain:

zk lightnet status
Communicate with the Mina Accounts-Manager service to retrieve account details. This service is available at:

http://localhost:8181/

Use the HTTP endpoints to acquire, release, list, lock, and unlock accounts.

Configure your zkApp to interact with the Lightnet blockchain using the endpoints provided by the zk lightnet status command.

Set the Mina GraphQL API URL for deployment to:

http://localhost:8080/graphql

Set the transaction fee for deployment (in MINA) to 0.1

5. Deploy your zkApp to Lightnet:
zk deploy

You can check out the example application here.

Here are necessary links that will help you better guide and build zkApps.

  1. zkApp development framework

  2. Deploying zkApp

You can join Mina's discord channel to learn more and connect with Mina developers as well as the community.

Top comments (2)

Collapse
 
sabrinaesaquino profile image
Sabrina

great tutorial! the illustration really helps with the visualization

Collapse
 
talhab62 profile image
talhaB62 (httpz)

Congrats and thanks for the amazing article!