Skip to content

Commit

Permalink
feat: implement signup with hats
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 committed Aug 8, 2024
1 parent f99ccf8 commit e25c42d
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 40 deletions.
7 changes: 4 additions & 3 deletions 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 @@ -46,8 +47,8 @@
"graphql-request": "^6.1.0",
"lowdb": "^1.0.0",
"lucide-react": "^0.316.0",
"maci-cli": "0.0.0-ci.1032f7a",
"maci-domainobjs": "0.0.0-ci.1032f7a",
"maci-cli": "0.0.0-ci.d644eae",
"maci-domainobjs": "0.0.0-ci.d644eae",
"next": "^14.1.0",
"next-auth": "^4.24.5",
"next-themes": "^0.2.1",
Expand All @@ -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
19 changes: 19 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,23 @@ export const MaciProvider: React.FC<MaciProviderProps> = ({ children }: MaciProv
setSgData(attestationId);
setIsLoading(false);
break;
case GatekeeperTrait.Hats:
getHatsSingleGatekeeperData({
maciAddress: config.maciAddress!,
signer: 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) {
setSgData(criterionHat.at(0));
} 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;
};
102 changes: 65 additions & 37 deletions pnpm-lock.yaml

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

0 comments on commit e25c42d

Please sign in to comment.