Skip to content

Commit

Permalink
feat(merkleproof-gatekeeper): added support for the merkleproof gatek…
Browse files Browse the repository at this point in the history
…eeper
  • Loading branch information
Crisgarner committed Sep 30, 2024
1 parent d8b4d1c commit af86164
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 47 deletions.
5 changes: 3 additions & 2 deletions packages/interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@hatsprotocol/sdk-v1-core": "^0.10.0",
"@hookform/resolvers": "^3.3.4",
"@nivo/line": "^0.84.0",
"@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 @@ -39,8 +40,8 @@
"graphql-request": "^6.1.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
42 changes: 41 additions & 1 deletion packages/interface/src/contexts/Maci.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable no-console */
import { StandardMerkleTree } from "@openzeppelin/merkle-tree";
import { type StandardMerkleTreeData } from "@openzeppelin/merkle-tree/dist/standard";
import { Identity } from "@semaphore-protocol/core";
import { isAfter } from "date-fns";
import { type Signer, BrowserProvider } from "ethers";
import { type Signer, BrowserProvider, AbiCoder } from "ethers";
import {
signup,
isRegisteredUser,
Expand Down Expand Up @@ -45,6 +47,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 [maciPrivKey, setMaciPrivKey] = useState<string | undefined>();
Expand Down Expand Up @@ -150,6 +153,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 @@ -421,6 +443,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 @@ -437,6 +475,7 @@ export const MaciProvider: React.FC<MaciProviderProps> = ({ children }: MaciProv
onSignup,
onVote,
gatekeeperTrait,
treeData,
}),
[
isLoading,
Expand All @@ -452,6 +491,7 @@ export const MaciProvider: React.FC<MaciProviderProps> = ({ children }: MaciProv
onSignup,
onVote,
gatekeeperTrait,
treeData,
],
);

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 af86164

Please sign in to comment.