Skip to content

Commit

Permalink
feat(merkleroot-gatekeeper): added support for the merkleproof gateke…
Browse files Browse the repository at this point in the history
…eper
  • Loading branch information
Crisgarner authored and crisgarner committed Sep 30, 2024
1 parent 8c04308 commit 14f3310
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 48 deletions.
5 changes: 3 additions & 2 deletions packages/interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@pcd/util": "^0.5.4",
"@pcd/zk-eddsa-event-ticket-pcd": "^0.6.6",
"@pcd/zuauth": "^1.4.5",
"@openzeppelin/merkle-tree": "^1.0.7",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
"@rainbow-me/rainbowkit": "^2.0.1",
Expand All @@ -45,8 +46,8 @@
"js-sha256": "^0.11.0",
"lowdb": "^1.0.0",
"lucide-react": "^0.316.0",
"maci-cli": "^2.3.0",
"maci-domainobjs": "^2.2.0",
"maci-cli": "^2.4.0",
"maci-domainobjs": "^2.4.0",
"next": "^14.1.0",
"next-auth": "^4.24.5",
"next-themes": "^0.2.1",
Expand Down
1 change: 1 addition & 0 deletions packages/interface/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const config = {
roundOrganizer: process.env.NEXT_PUBLIC_ROUND_ORGANIZER ?? "PSE",
roundLogo: process.env.NEXT_PUBLIC_ROUND_LOGO,
semaphoreSubgraphUrl: process.env.NEXT_PUBLIC_SEMAPHORE_SUBGRAPH,
treeUrl: process.env.NEXT_PUBLIC_TREE_URL,
};

export const theme = {
Expand Down
40 changes: 40 additions & 0 deletions packages/interface/src/contexts/Maci.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable no-console */
import { StandardMerkleTree } from "@openzeppelin/merkle-tree";
import { type StandardMerkleTreeData } from "@openzeppelin/merkle-tree/dist/standard";
import { type ZKEdDSAEventTicketPCD } from "@pcd/zk-eddsa-event-ticket-pcd/ZKEdDSAEventTicketPCD";
import { Identity } from "@semaphore-protocol/core";
import { isAfter } from "date-fns";
Expand Down Expand Up @@ -48,6 +50,7 @@ export const MaciProvider: React.FC<MaciProviderProps> = ({ children }: MaciProv
const [error, setError] = useState<string>();
const [pollData, setPollData] = useState<IGetPollData>();
const [tallyData, setTallyData] = useState<TallyData>();
const [treeData, setTreeData] = useState<StandardMerkleTreeData<string[]>>();

const [semaphoreIdentity, setSemaphoreIdentity] = useState<Identity | undefined>();
const [zupassProof, setZupassProof] = useState<PCD>();
Expand Down Expand Up @@ -170,6 +173,25 @@ export const MaciProvider: React.FC<MaciProviderProps> = ({ children }: MaciProv
case GatekeeperTrait.FreeForAll:
setIsLoading(false);
break;
case GatekeeperTrait.MerkleProof:
if (!signer) {
setIsLoading(false);
return;
}
if (!treeData) {
setIsLoading(false);
return;
}
try {
const merkleTree = StandardMerkleTree.load(treeData);
const proof = merkleTree.getProof([signer.address]);
const encodedProof = AbiCoder.defaultAbiCoder().encode(["bytes32[]"], [proof]);
setSgData(encodedProof);
} catch (e) {
setSgData(undefined);
}
setIsLoading(false);
break;
default:
break;
}
Expand Down Expand Up @@ -449,6 +471,22 @@ export const MaciProvider: React.FC<MaciProviderProps> = ({ children }: MaciProv
}
}, [signer, votingEndsAt, setIsLoading, setTallyData, setPollData, poll.data]);

/// check the tree data
useEffect(() => {
// if we have the tree url then it means we can get the tree data through there
if (config.treeUrl) {
setIsLoading(true);
fetch(config.treeUrl)
.then((res) => res.json())
.then((res: StandardMerkleTreeData<string[]>) => {
setTreeData(res);
})
.catch(() => undefined);

setIsLoading(false);
}
}, [setIsLoading]);

const value = useMemo(
() => ({
isLoading,
Expand All @@ -466,6 +504,7 @@ export const MaciProvider: React.FC<MaciProviderProps> = ({ children }: MaciProv
onVote,
gatekeeperTrait,
storeZupassProof,
treeData,
}),
[
isLoading,
Expand All @@ -482,6 +521,7 @@ export const MaciProvider: React.FC<MaciProviderProps> = ({ children }: MaciProv
onVote,
gatekeeperTrait,
storeZupassProof,
treeData,
],
);

Expand Down
2 changes: 2 additions & 0 deletions packages/interface/src/contexts/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type StandardMerkleTreeData } from "@openzeppelin/merkle-tree/dist/standard";
import { type TallyData, type IGetPollData, type GatekeeperTrait } from "maci-cli/sdk";
import { type ReactNode } from "react";

Expand All @@ -23,6 +24,7 @@ export interface MaciContextType {
maciPubKey?: string;
gatekeeperTrait?: GatekeeperTrait;
storeZupassProof: (args: PCD) => Promise<void>;
treeData?: StandardMerkleTreeData<string[]>;
onSignup: (onError: () => void) => Promise<void>;
onVote: (
args: IVoteArgs[],
Expand Down
2 changes: 2 additions & 0 deletions packages/interface/src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ module.exports = createEnv({
NEXT_PUBLIC_ROUND_LOGO: z.string().optional(),

NEXT_PUBLIC_SEMAPHORE_SUBGRAPH: z.string().url().optional(),
NEXT_PUBLIC_TREE_URL: z.string().url().optional(),
},

/**
Expand Down Expand Up @@ -107,6 +108,7 @@ module.exports = createEnv({
NEXT_PUBLIC_ROUND_LOGO: process.env.NEXT_PUBLIC_ROUND_LOGO,

NEXT_PUBLIC_SEMAPHORE_SUBGRAPH: process.env.NEXT_PUBLIC_SEMAPHORE_SUBGRAPH,
NEXT_PUBLIC_TREE_URL: process.env.NEXT_PUBLIC_TREE_URL,
},
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
Expand Down
Loading

0 comments on commit 14f3310

Please sign in to comment.