Skip to content

Commit

Permalink
init from zk-email-verify starter repo
Browse files Browse the repository at this point in the history
  • Loading branch information
kenyiu committed Apr 28, 2024
0 parents commit 83b6656
Show file tree
Hide file tree
Showing 70 changed files with 30,842 additions and 0 deletions.
77 changes: 77 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
#
#

dist

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage
junit.xml

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

*.r1cs
*.sym
*.ptau
*.zkey

.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

packages/circuits/proofs/
out/
circuits/circuit*zkey*
circuits/email/
circuits/proofs/
cache/
test.log
packages/contracts/out/
packages/contracts/lib/
packages/contracts/broadcast
.next
node_modules.nosync
*Wallet*
*wallet*
circuits/regexes/subject_regex*

*_email.txt
*email_cache.json
*input.json
*email_proof.json
*.debug
*.env
.vscode

.vite
**/.vite

tsbuildinfo

**/node_modules
**/build
**/dist
tsconfig.tsbuildinfo

.idea/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 zk-email

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
156 changes: 156 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# Proof of Twitter

Prove ownership of a X (Twitter) account using an email from Twitter, and mint an NFT with your Twitter username.

This project is a demonstration of ZK Email. You can fork it to build other projects using ZK Email.

Try it here: https://twitter.prove.email/

## How it works

You can use an email from Twitter that contains `email was meant for @username` to generate a ZK proof that you own the Twitter account `@username`.

This ZK proof can be used to mint an NFT corresponding to your username in the `ProofOfTwitter` contract.

## Running locally

#### Install dependencies

```bash
yarn
```

#### Start the web app. In `packages/app` directory, run

```bash
yarn start
```

This will start the UI at `http://localhost:3000/` where you can paste the email, generate proof and mint the NFT.

The UI works against the generated zkeys downloaded from AWS and the deployed contract on Sepolia.

## Manual Proof Generation

If you want to generate the proof locally outside browser, follow the instructions below.

### Circuits

Circom circuits are located in `packages/circuits`, the main circuit being [twitter.circom](packages/circuits/twitter.circom). TwitterVerifier circuit use [EmailVerifier](https://github.com/zkemail/zk-email-verify/blob/main/packages/circuits/email-verifier.circom) circuit from `@zk-email/circuits`.

The regex circuit required to parse/extract Twitter username can be generated using [https://github.com/zkemail/zk-regex](zk-regex) package.

#### » Generate Twitter Regex Circuit

```bash
# CWD = packages/circuits
yarn generate-regex
```

This will generate `components/twitter_reset.circom` using the config in `components/twitter_reset.json`. This `twitter_reset.circom` is imported in `twitter.circom`.

Note that `twitter_reset.circom` is already in repo, so this step is optional.

#### » Build the circuit

```bash
# CWD = packages/circuits
yarn build
```

This will create `twitter.wasm` and other files in `packages/circuits/build` directory.

You can test the circuit using

```bash
# CWD = packages/circuits
yarn test
```

#### » Generating Zkey

You can generate proving and verification keys using

```bash
# CWD = packages/circuits/scripts
ZKEY_ENTROPY=<random-number> ZKEY_BEACON=<random-hex> ts-node dev-setup.ts
```

This will generate `zkey` files, `vkey.json` in `build` directory, and Solidity verifier in `packages/contracts/src/Verifier.sol` (You might need to manually update the solidity pragma version).

> Note: We are using a custom fork of `snarkjs` which generated **chunked zkeys**. Chunked zkeys make it easier to use in browser, especially since we have large circuit. You can switch to regular `snarkjs` in `package.json` if you don't want to use chunked zkeys.

For browser use, the script also compresses the chunked zkeys.

**The compressed zkeys, vkey, wasm are copied to /build/artifacts` directory. This directory can be served using a local server or uploaded to S3 for use in the browser.

To upload to S3, the below script can be used.
```bash
python3 upload_to_s3.py --build-dir <project-path>/proof-of-twitter/packages/circuits/build --circuit-name twitter
```

There are helper functions in `@zk-email/helpers` package to download and decompress the zkeys in the browser.


#### » Generate Input and Proof

```bash
# CWD = packages/circuits/scripts
ts-node generate-proof.ts --email-file ../tests/emls/twitter-test.eml --ethereum-address <your-eth-address>
```

This will generate input + witness using the given email file and Ethereum address, and prove using the generated zkey.

The script will save `inputs.json`, `input.wtns`, `proof.json`, and `public.json` in `proof` directory.

The script also verify the generated proof are correct. You can use the proof and public inputs to verify in the Solidity verifier as well.

### Contracts

The solidity contracts can be found in `packages/contracts`. The main contract is [ProofOfTwitter.sol](packages/contracts/src/ProofOfTwitter.sol).

#### You can build the contracts using

```bash
# CWD = packages/contracts
yarn build # Assume you have foundry installed
```

#### Run tests

```bash
# CWD = packages/contracts
yarn test
```

Note that the tests will not pass if you have generated your own zkeys and `Verifier.sol` as you would have used a different Entropy.

To fix, update the `publicSignals` and `proof` in `test/TestTwitter.t.sol` with the values from `input.json` and `public.json` generated from the above steps. (Remember that you need to flip items in the nested array of `pi_b`).

#### Deploy contracts

```bash
# CWD = packages/contracts
PRIVATE_KEY=<pk-hex> forge script script/DeployTwitter.s.sol:Deploy -vvvv --rpc-url https://rpc2.sepolia.org --broadcast
```

Currently deployed contracts on Sepolia:

```
Deployed Verifier at address: 0x6096601EB33d636b0e21593469920d06647FA955
Deployed DKIMRegistry at address: 0x993873c1b46c756b60089cBbE3baEEC9Fa292e9f
Deployed ProofOfTwitter at address: 0x86D390fDed54447fD244eD0718dbFCFCcbbA7edc
```

### UI

If you want to update the UI based on your own zkeys and contracts, please make the below changes:

- Set the `VITE_CONTRACT_ADDRESS` in `packages/app/.env`. This is the address of the `ProofOfTwitter` contract.
- Set `VITE_CIRCUIT_ARTIFACTS_URL` in `packages/app/.env` to the URL of the directory containing circuit artifacts (compressed partial zkeys, wasm, verifier, etc). You can run a local server in `circuits/build/artifacts` directory and use that URL or upload to S3 (or similar) and use that public URL/


## History

This repo was originally part of the [zk-email-verify](https://github.com/zkemail/zk-email-verify). Please refer there for the commit history and original contributors.
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "4.0.0",
"description": "Proof of twitter username using ZK Email",
"private": true,
"workspaces": [
"packages/*"
],
"keywords": [],
"author": "",
"license": "MIT",
"packageManager": "[email protected]"
}
19 changes: 19 additions & 0 deletions packages/app/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
plugins: [
function () {
return {
visitor: {
MetaProperty(path) {
path.replaceWithSourceString('process')
},
},
}
},
],
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
['@babel/preset-react', { "runtime": "automatic" }],
['jest'],
],
};
17 changes: 17 additions & 0 deletions packages/app/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
49 changes: 49 additions & 0 deletions packages/app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Use ZK Email to mint a proof of twitter account on-chain."
/>

<meta property="og:title" content="Proof of Twitter: ZK Email" />
<meta property="og:description" content="Mint a proof of your Twitter account on-chain using ZK Email." />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://twitter.prove.email" />
<!-- <meta property="og:image" content="https://yourwebsite.com/image.jpg" /> -->
<meta property="og:site_name" content="Proof of Twitter: ZK Email" />
<meta property="og:locale" content="en_US" />
<!-- Twitter Card data -->
<meta name="twitter:site" content="@zkemail" />
<meta name="twitter:title" content="Proof of Twitter: ZK Email" />
<meta name="twitter:description" content="Mint a proof of your Twitter account on-chain using ZK Email." />
<meta name="twitter:creator" content="@zkemail" />

<link rel="manifest" href="/manifest.json" />
<title>Proof of Twitter: ZK Email</title>

<script>
window.global = window;
</script>

<script type="module">
import { Buffer } from "buffer";
import process from "process";
import { setImmediate } from "timers";

window.Buffer = Buffer;
window.process = process;
window.setImmediate = setImmediate;
</script>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>

<script type="module" src="./src/index.tsx"></script>
</body>
</html>
Loading

0 comments on commit 83b6656

Please sign in to comment.