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

feat: make sure freeforall and semaphore gatekeeping strategy works #251

Merged
merged 1 commit into from
Aug 9, 2024
Merged
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
7 changes: 7 additions & 0 deletions packages/interface/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ NEXT_PUBLIC_APPROVAL_SCHEMA=0x858e0bc94997c072d762d90440966759b57c8bca892d4c9447
# (optional)
NEXT_PUBLIC_METADATA_SCHEMA=0xd00c966351896bd3dc37d22017bf1ef23165f859d7546a2aba12a01623dec912

# -----------------
# Semaphore CONFIGURATION
# -----------------

# If you use Semaphore as gatekeeper, please add your semaphore subgraph url below
NEXT_PUBLIC_SEMAPHORE_SUBGRAPH=

# ----------------------
# Advanced Configuration
# ----------------------
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 @@ -90,6 +90,7 @@ export const config = {
roundOrganizer: process.env.NEXT_PUBLIC_ROUND_ORGANIZER ?? "PSE",
pollMode: process.env.NEXT_PUBLIC_POLL_MODE ?? "non-qv",
roundLogo: process.env.NEXT_PUBLIC_ROUND_LOGO,
semaphoreSubgraphUrl: process.env.NEXT_PUBLIC_SEMAPHORE_SUBGRAPH,
};

export const theme = {
Expand Down
7 changes: 5 additions & 2 deletions packages/interface/src/contexts/Maci.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ export const MaciProvider: React.FC<MaciProviderProps> = ({ children }: MaciProv
// just by fetching the attestation. On the other hand, with other
// gatekeepers it might be more difficult to determine it
// for instance with semaphore
const isEligibleToVote = useMemo(() => Boolean(sgData) && Boolean(address), [sgData, address]);
const isEligibleToVote = useMemo(
() => gatekeeperTrait && (gatekeeperTrait === GatekeeperTrait.FreeForAll || Boolean(sgData)) && Boolean(address),
[sgData, address],
);

// on load get the key pair from local storage and set the signature message
useEffect(() => {
Expand Down Expand Up @@ -187,7 +190,7 @@ export const MaciProvider: React.FC<MaciProviderProps> = ({ children }: MaciProv
// function to be used to signup to MACI
const onSignup = useCallback(
async (onError: () => void) => {
if (!signer || !maciPubKey || !sgData) {
if (!signer || !maciPubKey || (gatekeeperTrait && gatekeeperTrait !== GatekeeperTrait.FreeForAll && !sgData)) {
return;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/interface/src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ module.exports = createEnv({

NEXT_PUBLIC_POLL_MODE: z.enum(["qv", "non-qv"]).default("non-qv"),
NEXT_PUBLIC_ROUND_LOGO: z.string().optional(),

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

/**
Expand Down Expand Up @@ -108,6 +110,8 @@ module.exports = createEnv({

NEXT_PUBLIC_POLL_MODE: process.env.NEXT_PUBLIC_POLL_MODE,
NEXT_PUBLIC_ROUND_LOGO: process.env.NEXT_PUBLIC_ROUND_LOGO,

NEXT_PUBLIC_SEMAPHORE_SUBGRAPH: process.env.NEXT_PUBLIC_SEMAPHORE_SUBGRAPH,
},
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
Expand Down
10 changes: 6 additions & 4 deletions packages/interface/src/utils/semaphore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Group, Identity, generateProof, type SemaphoreProof } from "@semaphore-protocol/core";
import { SemaphoreEthers } from "@semaphore-protocol/data";
import { SemaphoreEthers, SemaphoreSubgraph } from "@semaphore-protocol/data";
import { AbiCoder, JsonRpcSigner } from "ethers";
import { getSemaphoreGatekeeperData } from "maci-cli/sdk";

Expand Down Expand Up @@ -33,9 +33,11 @@ export const getSemaphoreProof = async (signer: JsonRpcSigner, identity: Identit
maciAddress: config.maciAddress!,
});

const semaphore = new SemaphoreEthers(getRPCURL() ?? semaphoreEthersChain(), {
address: semaphoreContractAddress,
});
const semaphore = config.semaphoreSubgraphUrl
? new SemaphoreSubgraph(config.semaphoreSubgraphUrl)
: new SemaphoreEthers(getRPCURL() ?? semaphoreEthersChain(), {
address: semaphoreContractAddress,
});

const groupResponse = await semaphore.getGroupMembers(groupId);

Expand Down
Loading