Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add React SDK #20

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions client/example/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// api.ts
import { userResponseSchema } from './userResponseSchema';

export const getUser = async (id: number) => {
const response = await fetch(`/api/user/${id}`);
const data = await response.json();

// Validate using Zod
const result = userResponseSchema.parse(data);

return result;
};
26 changes: 26 additions & 0 deletions client/example/useUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// useUser.ts
import { useState, useEffect } from 'react';
import { getUser } from './api';

export const useUser = (id: number) => {
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);

useEffect(() => {
const fetchUser = async () => {
try {
const result = await getUser(id);
setUser(result);
} catch (e) {
setError(e);
} finally {
setLoading(false);
}
};

fetchUser();
}, [id]);

return { user, loading, error };
};
8 changes: 8 additions & 0 deletions client/example/userResponseSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// userResponseSchema.ts
import { z } from 'zod';

export const userResponseSchema = z.object({
id: z.number(),
name: z.string(),
email: z.string(),
});
27 changes: 27 additions & 0 deletions client/maci-coordinator-react/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-env node */

module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
'plugin:react-hooks/recommended',
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: true,
tsconfigRootDir: __dirname,
},
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
'@typescript-eslint/no-non-null-assertion': 'off',
},
}
24 changes: 24 additions & 0 deletions client/maci-coordinator-react/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
64 changes: 64 additions & 0 deletions client/maci-coordinator-react/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<h2>API Design</h2>
<pre>
API: /generateProof
- Request (POST):
{
"circuitName": "ProcessMessages",
"circuitInput": {}
}

- Response:
{
"status": "ok",
"data": {}
}

API: /getResult
- Request (GET):

- Response:
{
"circuit": {
"name": "ProcessMessages",
"status": "Finished",
"result": {
"proof": {},
"publicInput": {}
}
}
}
</pre>

### /api/generateProof

Send a POST request to the `/api/generateProof` endpoint with a JSON body containing the cicuit input of the desired circuit:

ProcessMessages:
```
curl \
-X POST \
-H "Content-Type: application/json" \
-d @demo/request_generateProof_ProcessMessages.json \
http://localhost:8080/api/generateProof
```

TallyVotes:
```
curl \
-X POST \
-H "Content-Type: application/json" \
-d @demo/request_generateProof_TallyVotes.json \
http://localhost:8080/api/generateProof
```

### /api/getResult

```
curl http://localhost:8080/api/getResult
```

## Generate Proof using snarkjs

```
snarkjs groth16 prove instruments/TallyVotes_6-2-3_test.0.zkey data/TallyVotes/6-2-3/witness_wasm.wtns outputs/proof_TallyVotes_6-2-3.json outputs/public_TallyVotes_6-2-3.json
```
13 changes: 13 additions & 0 deletions client/maci-coordinator-react/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>QFI Admin</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
39 changes: 39 additions & 0 deletions client/maci-coordinator-react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "maci-coordinator-react",
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"@chakra-ui/react": "^2.7.1",
"@fontsource/poppins": "^5.0.5",
"@rainbow-me/rainbowkit": "^1.0.6",
"axios": "^1.4.0",
"ethers": "^5.7.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^4.10.1",
"react-router-dom": "^6.14.1",
"viem": "^1.1.8",
"wagmi": "^1.3.0",
"zod": "^3.21.4"
},
"devDependencies": {
"@originjs/vite-plugin-commonjs": "^1.0.3",
"@types/react": "^18.2.14",
"@types/react-dom": "^18.2.6",
"@typescript-eslint/eslint-plugin": "^5.61.0",
"@typescript-eslint/parser": "^5.61.0",
"@vitejs/plugin-react": "^4.0.0",
"eslint": "^8.44.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.1",
"typescript": "^5.0.4",
"vite": "^4.4.0",
"vite-plugin-node-polyfills": "^0.8.2"
}
}
Loading