Skip to content

Commit

Permalink
finish the zk part
Browse files Browse the repository at this point in the history
  • Loading branch information
backmeupplz committed Aug 30, 2023
1 parent 6cf194c commit b735510
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
VITE_ALCHEMY_KEY=00000000000000000000000000000000
VITE_WALLETCONNECT_PROJECT_ID=00000000000000000000000000000000
VITE_WALLETCONNECT_PROJECT_ID=00000000000000000000000000000000
VITE_TOKEN_ADDRESS=0x000000
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<title>SealHub | memorabilia token</title>
<meta name="description" content="SealHub | memorabilia token" />
</head>
<!-- SnarkJS -->
<script src="/scripts/snarkjs.min.js"></script>
<body>
<noscript>Please, turn on JavaScript to see this page.</noscript>
<div id="root" />
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
},
"dependencies": {
"@big-whale-labs/seal-hub-kit": "^0.0.36",
"@big-whale-labs/seal-hub-memorabilia-token-contracts": "^0.0.3",
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
"@ethersproject/providers": "^5.7.2",
"@rainbow-me/rainbowkit": "^1.0.9",
"envalid": "^7.3.1",
Expand Down
Binary file added public/NullifierCreator.wasm
Binary file not shown.
Binary file added public/NullifierCreator_final.zkey
Binary file not shown.
99 changes: 99 additions & 0 deletions public/NullifierCreator_verification_key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"protocol": "groth16",
"curve": "bn128",
"nPublic": 2,
"vk_alpha_1": [
"20491192805390485299153009773594534940189261866228447918068658471970481763042",
"9383485363053290200918347156157836566562967994039712273449902621266178545958",
"1"
],
"vk_beta_2": [
[
"6375614351688725206403948262868962793625744043794305715222011528459656738731",
"4252822878758300859123897981450591353533073413197771768651442665752259397132"
],
[
"10505242626370262277552901082094356697409835680220590971873171140371331206856",
"21847035105528745403288232691147584728191162732299865338377159692350059136679"
],
[
"1",
"0"
]
],
"vk_gamma_2": [
[
"10857046999023057135944570762232829481370756359578518086990519993285655852781",
"11559732032986387107991004021392285783925812861821192530917403151452391805634"
],
[
"8495653923123431417604973247489272438418190587263600148770280649306958101930",
"4082367875863433681332203403145435568316851327593401208105741076214120093531"
],
[
"1",
"0"
]
],
"vk_delta_2": [
[
"7912208710313447447762395792098481825752520616755888860068004689933335666613",
"12599857379517512478445603412764121041984228075771497593287716170335433683702"
],
[
"21679208693936337484429571887537508926366191105267550375038502782696042114705",
"11502426145685875357967720478366491326865907869902181704031346886834786027007"
],
[
"1",
"0"
]
],
"vk_alphabeta_12": [
[
[
"2029413683389138792403550203267699914886160938906632433982220835551125967885",
"21072700047562757817161031222997517981543347628379360635925549008442030252106"
],
[
"5940354580057074848093997050200682056184807770593307860589430076672439820312",
"12156638873931618554171829126792193045421052652279363021382169897324752428276"
],
[
"7898200236362823042373859371574133993780991612861777490112507062703164551277",
"7074218545237549455313236346927434013100842096812539264420499035217050630853"
]
],
[
[
"7077479683546002997211712695946002074877511277312570035766170199895071832130",
"10093483419865920389913245021038182291233451549023025229112148274109565435465"
],
[
"4595479056700221319381530156280926371456704509942304414423590385166031118820",
"19831328484489333784475432780421641293929726139240675179672856274388269393268"
],
[
"11934129596455521040620786944827826205713621633706285934057045369193958244500",
"8037395052364110730298837004334506829870972346962140206007064471173334027475"
]
]
],
"IC": [
[
"17633541629360788820369992664734051681254530438326342225046207022602858499744",
"13512347773299353284844286656287449216447187799503795154008216520070672897390",
"1"
],
[
"3029788139028659728827829882091506971741368271725634887798125570389539401568",
"7200001514979069610981531628598901261105255834567192527119785810760792132212",
"1"
],
[
"6685413157919187202914055487614692654000187980943257183283494926966064576579",
"15441509088074862215290190106159727814184068577876509048115483853131120012920",
"1"
]
]
}
9 changes: 9 additions & 0 deletions public/scripts/snarkjs.min.js

Large diffs are not rendered by default.

104 changes: 98 additions & 6 deletions src/components/MintingFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@ import { AlchemyProvider } from '@ethersproject/providers'
import {
getCommitmentFromSignature,
getMessage,
getSealHubValidatorInputs,
isCommitmentRegistered,
} from '@big-whale-labs/seal-hub-kit'
import { useAccount, useSignMessage } from 'wagmi'
import { useEffect, useState } from 'preact/hooks'
import env from 'helpers/env'
import tokenContract from 'helpers/tokenContract'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare const snarkjs: any

enum Step {
empty = '',
awaitingSignature = 'Awaiting signature...',
checkingCommitment = 'Checking commitment...',
commitmentNotRegistered = 'Commitment not registered',
error = 'Error',
checkingBalance = 'Checking balance...',
zk = 'Generating a ZK proof...',
minting = 'Minting...',
minted = 'Minted!',
error = 'Error',
}

export default function () {
Expand Down Expand Up @@ -55,6 +62,81 @@ export default function () {
}
}

async function checkBalance() {
if (!address) {
setStep(Step.error)
return
}
const balance = await tokenContract.balanceOf(address)
if (balance.eq(0)) {
setStep(Step.zk)
} else {
setStep(Step.minted)
}
}

async function generateZKProof() {
if (!signatureData) {
setStep(Step.error)
return
}
try {
const inputs = await getSealHubValidatorInputs(
signatureData,
getMessage(),
new AlchemyProvider('goerli', env.VITE_ALCHEMY_KEY)
)
const proof = await snarkjs.groth16.fullProve(
inputs,
'/NullifierCreator.wasm',
'/NullifierCreator_final.zkey'
)
// Proof:
// {
// "proof": {
// "pi_a": [
// "13541247944574745837868314136253945230129375627573736642345051466959842601061",
// "3588149924236996797503708002256605763337276068528050848888072819304817687339",
// "1"
// ],
// "pi_b": [
// [
// "19883355938065097053964661316346448763027368280670040341962896212349060368271",
// "10713585228785001441206714591981378860450768221664841618412581272805710395175"
// ],
// [
// "11972044746202682585064583620652507442622231786501473104038139224507458887252",
// "5679184047081825787551941111220465546469612120177194289047377528490743216712"
// ],
// [
// "1",
// "0"
// ]
// ],
// "pi_c": [
// "4537811364823272105530426961838425872763410312483499906183411799653562375712",
// "11338596102929199422174554879682317856625804503776499592452258244335560283508",
// "1"
// ],
// "protocol": "groth16",
// "curve": "bn128"
// },
// "publicSignals": [
// "1606557549283573144709933128830539461483138555532643293876135124027209928089",
// "16830880825080895546328487425414699910730890478699539177647338495355167294806"
// ]
// }
setStep(Step.minting)
} catch (error) {
console.error(error)
setStep(Step.error)
}
}

async function mint() {
// TODO: mint
}

switch (step) {
case Step.empty:
setGlobalLoading(false)
Expand All @@ -76,15 +158,24 @@ export default function () {
if (commitmentRegistered === null) {
void checkCommitment()
} else if (commitmentRegistered) {
setStep(Step.minting)
setStep(Step.checkingBalance)
} else {
setStep(Step.commitmentNotRegistered)
}
break
case Step.minting: // TODO: handle UI
case Step.checkingBalance:
setGlobalLoading(true)
void checkBalance()
break
case Step.zk:
setGlobalLoading(true)
void generateZKProof()
break
case Step.minting:
setGlobalLoading(true)
void mint()
break
case Step.commitmentNotRegistered: // TODO: show UI for this
case Step.commitmentNotRegistered:
setGlobalLoading(false)
break
case Step.error:
Expand All @@ -101,6 +192,7 @@ export default function () {
setCommitmentRegistered,
signMessage,
commitmentRegistered,
address,
])

return (
Expand Down Expand Up @@ -131,8 +223,8 @@ export default function () {
Commitment not registered. Please{' '}
<a href="https://hub.sealc.red" target="_blank">
register it first on SealHub
</a>
.
</a>{' '}
and then refresh this page.
</p>
)}
{step === Step.error && (
Expand Down
1 change: 1 addition & 0 deletions src/helpers/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { cleanEnv, str } from 'envalid'
export default cleanEnv(import.meta.env, {
VITE_ALCHEMY_KEY: str(),
VITE_WALLETCONNECT_PROJECT_ID: str(),
VITE_TOKEN_ADDRESS: str(),
})
8 changes: 8 additions & 0 deletions src/helpers/tokenContract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { AlchemyProvider } from '@ethersproject/providers'
import { SealHubMemorabiliaToken__factory } from '@big-whale-labs/seal-hub-memorabilia-token-contracts'
import env from 'helpers/env'

export default SealHubMemorabiliaToken__factory.connect(
env.VITE_TOKEN_ADDRESS,
new AlchemyProvider('goerli', env.VITE_ALCHEMY_KEY)
)
13 changes: 13 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineConfig, Plugin } from 'vite'
import preact from '@preact/preset-vite'
import tsconfigPaths from 'vite-tsconfig-paths'
import { visualizer } from 'rollup-plugin-visualizer'
import GlobalsPolyfills from '@esbuild-plugins/node-globals-polyfill'

export default defineConfig({
plugins: [preact(), tsconfigPaths()],
Expand All @@ -16,4 +17,16 @@ export default defineConfig({
},
outDir: 'dist',
},
optimizeDeps: {
esbuildOptions: {
define: {
global: 'globalThis',
},
plugins: [
GlobalsPolyfills({
buffer: true,
}),
] as any[],
},
},
})
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2884,6 +2884,15 @@ __metadata:
languageName: node
linkType: hard

"@big-whale-labs/seal-hub-memorabilia-token-contracts@npm:^0.0.3":
version: 0.0.3
resolution: "@big-whale-labs/seal-hub-memorabilia-token-contracts@npm:0.0.3"
dependencies:
"@big-whale-labs/seal-hub-kit": ^0.0.36
checksum: 6d91fb06850440b09ab56b597614bd2c53d4853c7231a27773f91f21ab8ae5caa40462e9d7c2d426a31baa799ee87f24d1e2e9fd4e0f4e8b1a64e5eee38ab433
languageName: node
linkType: hard

"@big-whale-labs/versioned-contract@npm:^1.0.2":
version: 1.0.2
resolution: "@big-whale-labs/versioned-contract@npm:1.0.2"
Expand Down Expand Up @@ -2930,6 +2939,15 @@ __metadata:
languageName: node
linkType: hard

"@esbuild-plugins/node-globals-polyfill@npm:^0.2.3":
version: 0.2.3
resolution: "@esbuild-plugins/node-globals-polyfill@npm:0.2.3"
peerDependencies:
esbuild: "*"
checksum: f83eeaa382680b26a3b1cf6c396450332c41d2dc0f9fd935d3f4bacf5412bef7383d2aeb4246a858781435b7c005a570dadc81051f8a038f1ef2111f17d3d8b0
languageName: node
linkType: hard

"@esbuild/android-arm64@npm:0.18.17":
version: 0.18.17
resolution: "@esbuild/android-arm64@npm:0.18.17"
Expand Down Expand Up @@ -16990,6 +17008,8 @@ __metadata:
resolution: "seal-hub-memorabilia-token@workspace:."
dependencies:
"@big-whale-labs/seal-hub-kit": ^0.0.36
"@big-whale-labs/seal-hub-memorabilia-token-contracts": ^0.0.3
"@esbuild-plugins/node-globals-polyfill": ^0.2.3
"@ethersproject/providers": ^5.7.2
"@preact/preset-vite": ^2.5.0
"@rainbow-me/rainbowkit": ^1.0.9
Expand Down

0 comments on commit b735510

Please sign in to comment.