zkExpenseTracker: A Privacy-Preserving Financial Tracker on Mina

Introduction

This article provides an in-depth guide to building a zkExpenseTracker using Mina’s privacy-focused blockchain technology. The zkExpenseTracker ensures users can track their expenses while maintaining privacy using zero-knowledge proofs (ZKPs).

Setting Up the Development Environment

1. Install Required Dependencies

To begin, install the necessary dependencies:

Install Git

Ensure Git is installed by running:

 git --version

If Git is not installed, download and install it from Git's official website.

Install Node.js and npm

Mina’s tooling requires Node.js. Download and install the latest stable version from Node.js. Verify installation:

 node -v
 npm -v

Install Vite (for frontend development)

 npm create vite@latest zkExpenseTracker --template vanilla

2. Project Setup

 cd zkExpenseTracker
 npm install

Developing the zkExpenseTracker

3. Creating the Smart Contract

The zkExpenseTracker will use a smart contract to verify budget constraints privately.

Install Mina’s zkApp CLI

 npm install -g snarkyjs

Define the Smart Contract

Create a contracts/zkExpenseTracker.ts file and add:

import { Field, SmartContract, state, method } from 'snarkyjs';

class zkExpenseTracker extends SmartContract {
  @state(Field) budget = Field(0);

  @method setBudget(amount: Field) {
    this.budget.set(amount);
  }

  @method validateExpense(expenses: Field[]) {
    let total = expenses.reduce((acc, val) => acc.add(val), Field(0));
    total.assertLessThanOrEqual(this.budget.get());
  }
}

4. Integrating the Frontend

Install Frontend Libraries

 npm install snarkyjs

Modify src/main.js:

import { zkExpenseTracker } from '../contracts/zkExpenseTracker';

async function setup() {
  const contract = new zkExpenseTracker();
  await contract.deploy();
}
setup();

5. Backend for Proof Generation

Create server/index.js:

const express = require('express');
const { zkExpenseTracker } = require('../contracts/zkExpenseTracker');

const app = express();
app.use(express.json());

app.post('/validate-expense', async (req, res) => {
  const { expenses } = req.body;
  const contract = new zkExpenseTracker();
  try {
    await contract.validateExpense(expenses);
    res.send({ success: true });
  } catch (error) {
    res.send({ success: false, error: error.message });
  }
});

app.listen(3000, () => console.log('Server running on port 3000'));

6. Deploying the zkExpenseTracker on Mina Testnet

 mina deploy --network testnet --app zkExpenseTracker

Wave 5: Completing the zkExpenseTracker

The next phase will focus on:

  • Enhancing the smart contract with categorical proofs

  • Improving frontend UI/UX

  • Extensive testing and optimization

  • Deploying the final version to Mina Mainnet

Conclusion

This article covered the step-by-step guide to setting up, building, and deploying a zkExpenseTracker on Mina. Future iterations will refine the project for real-world usability. Stay tuned for Wave 5 developments!

for more information about Mina, check out their official website. https://minaprotocol.com/