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/link ethereum adress and birth year #24

Merged
merged 2 commits into from
Sep 1, 2023
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
18 changes: 13 additions & 5 deletions packages/nextjs/pages/example-zk/BirthDateSignature.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,20 @@ export const signBirthYear = async (form: TForm) => {

export const BirthDateSignature = ({ aliceDefaultAge }: { aliceDefaultAge: number }) => {
const [form, setForm] = useState<TForm>(() => getInitialFormState(aliceDefaultAge));
const ethereumAddress = useBirthYearProofsStore(state => state.ethereumAddress);
const setEthereumAddress = useBirthYearProofsStore(state => state.setEthereumAddress);
const birthYear = useBirthYearProofsStore(state => state.birthYear);
const setBirthYear = useBirthYearProofsStore(state => state.setBirthYear);
const setSignedBirthYear = useBirthYearProofsStore(state => state.setSignedBirthYear);
const setSignerPublicKey = useBirthYearProofsStore(state => state.setSignerPublicKey);

const handleSubmission = async () => {
try {
const { signedMessage, signerPublicKey } = await signBirthYear(form);
const { signedMessage, signerPublicKey } = await signBirthYear({
...form,
personEthereumAddress: ethereumAddress,
birthYear,
});
setSignedBirthYear(signedMessage);
setSignerPublicKey(signerPublicKey);
notification.success("Successfully signed birth year");
Expand Down Expand Up @@ -84,10 +92,10 @@ export const BirthDateSignature = ({ aliceDefaultAge }: { aliceDefaultAge: numbe
<span className="label-text">Enter your Ethereum address</span>
</label>
<AddressInput
value={form.personEthereumAddress}
value={ethereumAddress}
name="personEthereumAddress"
placeholder="Ethereum address"
onChange={(val: string) => setForm({ ...form, personEthereumAddress: val })}
onChange={(value: string) => setEthereumAddress(value)}
/>
</div>
<div className="form-control">
Expand All @@ -98,8 +106,8 @@ export const BirthDateSignature = ({ aliceDefaultAge }: { aliceDefaultAge: numbe
type="number"
placeholder="Birth year"
className="input input-bordered"
value={form.birthYear}
onChange={e => setForm({ ...form, birthYear: e.target.value as unknown as number })}
value={birthYear}
onChange={e => setBirthYear(e.target.value)}
/>
</div>
<div className="form-control">
Expand Down
16 changes: 10 additions & 6 deletions packages/nextjs/pages/example-zk/GenerateProof.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export const parseForm = (form: TForm) => {
};

export const GenerateProof = ({ requiredBirthYear }: { requiredBirthYear: number }) => {
const ethereumAddress = useBirthYearProofsStore(state => state.ethereumAddress);
const setEthereumAddress = useBirthYearProofsStore(state => state.setEthereumAddress);
const birthYear = useBirthYearProofsStore(state => state.birthYear);
const setBirthYear = useBirthYearProofsStore(state => state.setBirthYear);
const setProof = useBirthYearProofsStore(state => state.setProof);
const signedBirthYear = useBirthYearProofsStore(state => state.signedBirthYear);
const signerPublicKey = useBirthYearProofsStore(state => state.signerPublicKey);
Expand All @@ -73,7 +77,7 @@ export const GenerateProof = ({ requiredBirthYear }: { requiredBirthYear: number
setIsProofRunning(true);
const notifcationId = notification.loading("Generating proof...");
try {
const parsedForm = parseForm(form);
const parsedForm = parseForm({ ...form, personEthereumAddress: ethereumAddress, birthYear });
const { proof } = await generateProof("LessThanSignedAge", parsedForm as ParsedArgs);
setProof(`0x${proof}`);
notification.success("Proof generated");
Expand Down Expand Up @@ -120,8 +124,8 @@ export const GenerateProof = ({ requiredBirthYear }: { requiredBirthYear: number
type="number"
placeholder="Signed birth year"
className="input input-bordered"
value={form.birthYear}
onChange={e => setForm({ ...form, birthYear: e.target.value as unknown as number })}
value={birthYear}
onChange={e => setBirthYear(e.target.value)}
/>
</div>
<div className="form-control">
Expand All @@ -133,7 +137,7 @@ export const GenerateProof = ({ requiredBirthYear }: { requiredBirthYear: number
placeholder="Required birth year"
className="input input-bordered"
value={form.requiredBirthYear}
onChange={e => setForm({ ...form, requiredBirthYear: e.target.value as unknown as number })}
onChange={e => setForm({ ...form, requiredBirthYear: Number(e.target.value) })}
/>
</div>
</div>
Expand Down Expand Up @@ -168,10 +172,10 @@ export const GenerateProof = ({ requiredBirthYear }: { requiredBirthYear: number
<span className="label-text">*Ethereum address signature</span>
</label>
<AddressInput
value={form.personEthereumAddress}
value={ethereumAddress}
name="personEthereumAddress"
placeholder="Ethereum address in signature"
onChange={(val: string) => setForm({ ...form, personEthereumAddress: val })}
onChange={(value: string) => setEthereumAddress(value)}
/>
</div>
<div className="form-control mt-6">
Expand Down
16 changes: 9 additions & 7 deletions packages/nextjs/pages/example-zk/ZkSteps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ const ZkSteps: NextPage = () => {
>
Previous
</button>
<button
className="join-item btn btn-outline"
onClick={() => setCurrentStep(currentStep => currentStep + 1)}
disabled={currentStep === LAST_STEP}
>
Next
</button>
{currentStep !== LAST_STEP && (
<button
className="join-item btn btn-outline"
onClick={() => setCurrentStep(currentStep => currentStep + 1)}
disabled={currentStep === LAST_STEP}
>
Next
</button>
)}
</>
)}
</div>
Expand Down
8 changes: 8 additions & 0 deletions packages/nextjs/services/store/birth-year-proofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@ import { create } from "zustand";
import { HexString } from "~~/hooks/noir/useProofGenerator";

type BirthYearProofs = {
ethereumAddress: string;
birthYear: number;
signedBirthYear: string;
signerPublicKey: string;
proof: HexString;
setEthereumAddress: (input: string) => void;
setBirthYear: (input: string) => void;
setSignedBirthYear: (input: string) => void;
setSignerPublicKey: (input: string) => void;
setProof: (input: HexString) => void;
};

export const useBirthYearProofsStore = create<BirthYearProofs>(set => ({
ethereumAddress: "",
birthYear: 2014,
signedBirthYear: "",
signerPublicKey: "",
proof: "0x",
setEthereumAddress: (input: string) => set({ ethereumAddress: input }),
setBirthYear: (input: string) => set({ birthYear: Number(input) }),
setSignedBirthYear: (input: string) => set({ signedBirthYear: input }),
setSignerPublicKey: (input: string) => set({ signerPublicKey: input }),
setProof: (input: HexString) => set({ proof: input }),
Expand Down