Skip to content

Commit

Permalink
added state index of the user key to auth context
Browse files Browse the repository at this point in the history
  • Loading branch information
yashgo0018 committed Apr 10, 2024
1 parent 64d7321 commit c945ae8
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions packages/nextjs/contexts/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
import { createContext, useContext, useEffect, useState } from "react";
import { Keypair } from "@se-2/hardhat/maci-ts/domainobjs";
import { useAccount } from "wagmi";
import { useScaffoldContractRead, useScaffoldEventSubscriber } from "~~/hooks/scaffold-eth";
import deployedContracts from "~~/contracts/deployedContracts";
import { useScaffoldContractRead, useScaffoldEventHistory, useScaffoldEventSubscriber } from "~~/hooks/scaffold-eth";
import scaffoldConfig from "~~/scaffold.config";
import { fetchOrCreateUserKeyPair } from "~~/utils/crypto";

interface IAuthContext {
isRegistered: boolean;
keypair: Keypair | null;
stateIndex: bigint | null;
}

export const AuthContext = createContext<IAuthContext>({} as IAuthContext);

export default function AuthContextProvider({ children }: { children: React.ReactNode }) {
const { address } = useAccount();
const [keypair, setKeyPair] = useState<Keypair | null>(null);
const [stateIndex, setStateIndex] = useState<bigint | null>(null);

useEffect(() => {
setKeyPair(fetchOrCreateUserKeyPair(address));
Expand All @@ -27,20 +31,52 @@ export default function AuthContextProvider({ children }: { children: React.Reac
args: keypair ? keypair.pubKey.rawPubKey : [undefined, undefined],
});

const chainId = scaffoldConfig.targetNetworks[0].id;

const {
MACI: { deploymentBlockNumber },
} = deployedContracts[chainId];

const { data: SignUpEvents } = useScaffoldEventHistory({
contractName: "MACI",
eventName: "SignUp",
filters: {
_userPubKeyX: keypair?.pubKey.asContractParam().x,
_userPubKeyY: keypair?.pubKey.asContractParam().y,
},
fromBlock: BigInt(deploymentBlockNumber),
});

useEffect(() => {
if (!keypair || !SignUpEvents || !SignUpEvents.length) {
setStateIndex(null);
return;
}

const lastSignUpEvent = SignUpEvents[SignUpEvents.length - 1];
setStateIndex(lastSignUpEvent.args._stateIndex || null);
}, [keypair, SignUpEvents]);

useScaffoldEventSubscriber({
contractName: "MACI",
eventName: "SignUp",
listener: logs => {
logs.forEach(log => {
log.args._userPubKeyX === keypair?.pubKey.asContractParam().x &&
log.args._userPubKeyY === keypair?.pubKey.asContractParam().y &&
refetchIsRegistered();
if (
log.args._userPubKeyX !== keypair?.pubKey.asContractParam().x ||
log.args._userPubKeyY !== keypair?.pubKey.asContractParam().y
)
return;
refetchIsRegistered();
setStateIndex(log.args._stateIndex || null);
});
},
});

return (
<AuthContext.Provider value={{ isRegistered: Boolean(isRegistered), keypair }}>{children}</AuthContext.Provider>
<AuthContext.Provider value={{ isRegistered: Boolean(isRegistered), keypair, stateIndex }}>
{children}
</AuthContext.Provider>
);
}

Expand Down

0 comments on commit c945ae8

Please sign in to comment.