From caede763be64abae42e5bbe0bb2092577b6baf20 Mon Sep 17 00:00:00 2001 From: daniecon Date: Sun, 25 Sep 2022 12:23:49 +0200 Subject: [PATCH] Fix bug load --- dapp/components/login/LogInBox.tsx | 15 +++-- dapp/components/login/TotpSetup.tsx | 89 ++++++++++------------------- 2 files changed, 40 insertions(+), 64 deletions(-) diff --git a/dapp/components/login/LogInBox.tsx b/dapp/components/login/LogInBox.tsx index 10155f0..3e6a29c 100644 --- a/dapp/components/login/LogInBox.tsx +++ b/dapp/components/login/LogInBox.tsx @@ -16,12 +16,15 @@ const LogInBox = () => { useEffect(() => { const loadInfo = async () => { if (provider && account) { - const zkWalletFactory = connectFactory(provider) - const walletAddress = await zkWalletFactory.userAddressToWalletAddress( - account - ) - if (walletAddress !== ethers.constants.AddressZero) { - router.push('./dashboard') + try { + const zkWalletFactory = connectFactory(provider) + const walletAddress = + await zkWalletFactory.userAddressToWalletAddress(account) + if (walletAddress !== ethers.constants.AddressZero) { + router.push('./dashboard') + } + } catch (e) { + console.log(e) } } } diff --git a/dapp/components/login/TotpSetup.tsx b/dapp/components/login/TotpSetup.tsx index d2bdd33..a42c06b 100644 --- a/dapp/components/login/TotpSetup.tsx +++ b/dapp/components/login/TotpSetup.tsx @@ -44,70 +44,30 @@ const TotpSetup = (props: TotpSetupProps) => { }) // Manage ceramic connection - const [addressCeramic, setAddressCeramic] = useState(null) - const { ceramicData, ceramicStatus } = useCeramic(addressCeramic) + const { ceramicData, ceramicStatus } = useCeramic(account ? account : null) // Create Wallet - useEffect(() => { - const createWallet = async () => { - if (ceramicData && ceramicData.set && provider && account) { - const tree = await prepareMerkleTree(account) - - if (tree) { - const [URI, _, root, encrypted] = tree - ceramicData.set({ MerkleTree: encrypted }) - connectFactory(provider) - const otpValidator = await deployZkOTPValidator(root, provider) - await deployZkWallet(otpValidator, root, provider) - - setUri(URI) - setBlur('') - } else { - console.log('CONNECTION ERROR') - } - } - } - if (ceramicStatus == 'connected' && provider && account) { - createWallet() - } - }, [ceramicStatus, ceramicData, provider, account]) - const createTOTPWallet = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) - if (account) { - setAddressCeramic(account) - } - } - useEffect(() => { - const zkProof = async () => { - if (ceramicData && ceramicData.content && provider && account) { - console.log('Read the hashes: ', ceramicData.content.MerkleTree) - const encryptedHashes = ceramicData.content.MerkleTree - const totpObject = await generateInput(pin.join(''), encryptedHashes) - if (totpObject) { - connectTOTPVerifier(provider, account) - try { - const tx = await zkTimestampProof(totpObject) - await tx.wait() - setVerified(true) - } catch (e) { - console.log(e) - setVerified(false) - } - } + if (ceramicData && ceramicData.set && provider && account) { + const tree = await prepareMerkleTree(account) + + if (tree) { + const [URI, _, root, encrypted] = tree + ceramicData.set({ MerkleTree: encrypted }) + connectFactory(provider) + const otpValidator = await deployZkOTPValidator(root, provider) + await deployZkWallet(otpValidator, root, provider) + + setUri(URI) + setBlur('') + } else { + console.log('CONNECTION ERROR') } } - if ( - ceramicStatus == 'connected' && - ceramicData.content.MerkleTree && - provider && - account - ) { - zkProof() - } - }, [ceramicStatus, ceramicData, provider, account]) + } const verifyCode = async (e: React.FormEvent) => { e.preventDefault() @@ -119,8 +79,21 @@ const TotpSetup = (props: TotpSetupProps) => { actionRef.current?.focus() return } - if (account) { - setAddressCeramic(account) + if (ceramicData && ceramicData.content && provider && account) { + console.log('Read the hashes: ', ceramicData.content.MerkleTree) + const encryptedHashes = ceramicData.content.MerkleTree + const totpObject = await generateInput(pin.join(''), encryptedHashes) + if (totpObject) { + connectTOTPVerifier(provider, account) + try { + const tx = await zkTimestampProof(totpObject) + await tx.wait() + setVerified(true) + } catch (e) { + console.log(e) + setVerified(false) + } + } } }