From 156ee1d5179cd65a76a1b344c8da63a4b4814888 Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 7 Nov 2023 00:00:38 +0100 Subject: [PATCH] add one button for registering creds --- src/app/home/components/Header.tsx | 1 - src/app/home/components/Keystore.tsx | 15 ++++-------- src/hooks/useKeystore.ts | 36 +++++++++++++++++++++++++--- src/hooks/useWallet.ts | 25 +------------------ 4 files changed, 38 insertions(+), 39 deletions(-) diff --git a/src/app/home/components/Header.tsx b/src/app/home/components/Header.tsx index 44338a9..258ae48 100644 --- a/src/app/home/components/Header.tsx +++ b/src/app/home/components/Header.tsx @@ -17,7 +17,6 @@ export const Header: React.FunctionComponent<{}> = () => { - ); }; diff --git a/src/app/home/components/Keystore.tsx b/src/app/home/components/Keystore.tsx index 3a62c6f..41bd76f 100644 --- a/src/app/home/components/Keystore.tsx +++ b/src/app/home/components/Keystore.tsx @@ -2,12 +2,11 @@ import React from "react"; import { Block, BlockTypes } from "@/components/Block"; import { Button } from "@/components/Button"; import { Subtitle } from "@/components/Subtitle"; -import { useRLN, useStore, useWallet } from "@/hooks"; +import { useRLN, useStore } from "@/hooks"; import { useKeystore } from "@/hooks/useKeystore"; export const Keystore: React.FunctionComponent<{}> = () => { const { keystoreCredentials } = useStore(); - const { onGenerateCredentials } = useWallet(); const { onReadCredentials, onRegisterCredentials } = useKeystore(); const { password, onPasswordChanged } = usePassword(); @@ -64,15 +63,9 @@ export const Keystore: React.FunctionComponent<{}> = () => { -

Generate new credentials from wallet

- -
diff --git a/src/hooks/useKeystore.ts b/src/hooks/useKeystore.ts index ed22d0b..d56e8fa 100644 --- a/src/hooks/useKeystore.ts +++ b/src/hooks/useKeystore.ts @@ -3,6 +3,7 @@ import { useStore } from "./useStore"; import { useRLN } from "./useRLN"; import { SEPOLIA_CONTRACT } from "@waku/rln"; import { StatusEventPayload } from "@/services/rln"; +import { SIGNATURE_MESSAGE } from "@/constants"; type UseKeystoreResult = { onReadCredentials: (hash: string, password: string) => void; @@ -12,20 +13,43 @@ type UseKeystoreResult = { export const useKeystore = (): UseKeystoreResult => { const { rln } = useRLN(); const { - credentials, setActiveCredential, setActiveMembershipID, setAppStatus, setCredentials, } = useStore(); + const generateCredentials = async () => { + if (!rln?.ethProvider) { + console.log("Cannot generate credentials, no provider found."); + return; + } + + const signer = rln.ethProvider.getSigner(); + const signature = await signer.signMessage( + `${SIGNATURE_MESSAGE}. Nonce: ${randomNumber()}` + ); + const credentials = await rln.rlnInstance?.generateSeededIdentityCredential( + signature + ); + return credentials; + }; + const onRegisterCredentials = React.useCallback( async (password: string) => { - if (!credentials || !rln?.rlnContract || !password) { + if (!rln?.rlnContract || !password) { + console.log(`Not registering - missing dependencies: contract-${!!rln?.rlnContract}, password-${!!password}`); return; } try { + const credentials = await generateCredentials(); + + if (!credentials) { + console.log("No credentials registered."); + return; + } + setAppStatus(StatusEventPayload.CREDENTIALS_REGISTERING); const membershipInfo = await rln.rlnContract.registerWithKey( credentials @@ -42,7 +66,9 @@ export const useKeystore = (): UseKeystoreResult => { }, password ); + setActiveCredential(keystoreHash); + setCredentials(credentials); setActiveMembershipID(membershipID); rln.saveKeystore(); setAppStatus(StatusEventPayload.CREDENTIALS_REGISTERED); @@ -52,7 +78,7 @@ export const useKeystore = (): UseKeystoreResult => { return; } }, - [credentials, rln, setActiveCredential, setActiveMembershipID, setAppStatus] + [rln, setActiveCredential, setActiveMembershipID, setAppStatus] ); const onReadCredentials = React.useCallback( @@ -81,3 +107,7 @@ export const useKeystore = (): UseKeystoreResult => { onReadCredentials, }; }; + +function randomNumber(): number { + return Math.ceil(Math.random() * 1000); +} diff --git a/src/hooks/useWallet.ts b/src/hooks/useWallet.ts index 8539a84..920b3fe 100644 --- a/src/hooks/useWallet.ts +++ b/src/hooks/useWallet.ts @@ -2,16 +2,14 @@ import React from "react"; import { useStore } from "./useStore"; import { isEthereumEvenEmitterValid } from "@/utils/ethereum"; import { useRLN } from "./useRLN"; -import { SIGNATURE_MESSAGE } from "@/constants"; type UseWalletResult = { onWalletConnect: () => void; - onGenerateCredentials: () => void; }; export const useWallet = (): UseWalletResult => { const { rln } = useRLN(); - const { setEthAccount, setChainID, setCredentials } = useStore(); + const { setEthAccount, setChainID } = useStore(); React.useEffect(() => { const ethereum = window.ethereum; @@ -58,28 +56,7 @@ export const useWallet = (): UseWalletResult => { } }; - const onGenerateCredentials = React.useCallback(async () => { - if (!rln?.ethProvider) { - console.log("Cannot generate credentials, no provider found."); - return; - } - - const signer = rln.ethProvider.getSigner(); - const signature = await signer.signMessage( - `${SIGNATURE_MESSAGE}. Nonce: ${randomNumber()}` - ); - const credentials = await rln.rlnInstance?.generateSeededIdentityCredential( - signature - ); - setCredentials(credentials); - }, [rln, setCredentials]); - return { onWalletConnect, - onGenerateCredentials, }; }; - -function randomNumber(): number { - return Math.ceil(Math.random() * 1000); -}