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: implement signup with hats #243

Merged
merged 1 commit into from
Aug 12, 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
3 changes: 2 additions & 1 deletion packages/interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"dependencies": {
"@ethereum-attestation-service/eas-sdk": "^1.5.0",
"@hatsprotocol/sdk-v1-core": "^0.10.0",
"@hookform/resolvers": "^3.3.4",
"@nivo/boxplot": "^0.84.0",
"@nivo/line": "^0.84.0",
Expand Down Expand Up @@ -64,9 +65,9 @@
"siwe": "^2.1.4",
"sonner": "^1.4.0",
"superjson": "^2.2.1",
"tailwindcss": "^3.4.1",
"tailwind-merge": "^2.2.1",
"tailwind-variants": "^0.1.20",
"tailwindcss": "^3.4.1",
"viem": "^2.7.15",
"wagmi": "^2.9.8",
"zod": "3.22.4"
Expand Down
23 changes: 23 additions & 0 deletions packages/interface/src/contexts/Maci.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import {
genKeyPair,
GatekeeperTrait,
getGatekeeperTrait,
getHatsSingleGatekeeperData,
} from "maci-cli/sdk";
import React, { createContext, useContext, useCallback, useEffect, useMemo, useState } from "react";
import { useAccount, useSignMessage } from "wagmi";

import { config } from "~/config";
import { useEthersSigner } from "~/hooks/useEthersSigner";
import { api } from "~/utils/api";
import { getHatsClient } from "~/utils/hatsProtocol";
import { getSemaphoreProof } from "~/utils/semaphore";

import type { IVoteArgs, MaciContextType, MaciProviderProps } from "./types";
Expand Down Expand Up @@ -122,6 +124,27 @@ export const MaciProvider: React.FC<MaciProviderProps> = ({ children }: MaciProv
setSgData(attestationId);
setIsLoading(false);
break;
case GatekeeperTrait.Hats:
if (!signer) {
return;
}
getHatsSingleGatekeeperData({
maciAddress: config.maciAddress!,
signer,
}).then(({ criterionHat }) => {
// we need to check if we are allowed to signup
const hatsClient = getHatsClient();
hatsClient.isWearerOfHat({ wearer: address!, hatId: BigInt(criterionHat.at(0)!) }).then((res) => {
if (res) {
// we don't need any specific value here, as it will check based on the address
setSgData("0x0000000000000000000000000000000000000000000000000000000000000000");
kittybest marked this conversation as resolved.
Show resolved Hide resolved
} else {
setSgData(undefined);
}
});
});
setIsLoading(false);
break;
default:
break;
}
Expand Down
22 changes: 22 additions & 0 deletions packages/interface/src/utils/hatsProtocol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { HatsClient } from "@hatsprotocol/sdk-v1-core";
import { createPublicClient, http, type PublicClient } from "viem";

import { config } from "~/config";

/**
* Get an HatsClient instance to check whether the connected wallet owns the required hat
* @returns An HatsClient instance
*/
export const getHatsClient = (): HatsClient => {
const client = createPublicClient({
chain: config.network,
transport: http(),
});

const hatsClient = new HatsClient({
chainId: client.chain.id,
publicClient: client as unknown as PublicClient,
});

return hatsClient;
};
28 changes: 28 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading