From 1297acc7dbfab4e2f6a49fc3e6aa224131b269fd Mon Sep 17 00:00:00 2001 From: enes Date: Wed, 7 Aug 2024 14:51:44 +0200 Subject: [PATCH] refactor: PR review changes and lint fixes --- src/pages/sign/index.tsx | 154 ++++++++++++++++++++------------------- 1 file changed, 79 insertions(+), 75 deletions(-) diff --git a/src/pages/sign/index.tsx b/src/pages/sign/index.tsx index 6f3231b..3798863 100644 --- a/src/pages/sign/index.tsx +++ b/src/pages/sign/index.tsx @@ -5,7 +5,7 @@ import JSZip from 'jszip' import _ from 'lodash' import { MuiFileInput } from 'mui-file-input' import { Event, verifyEvent } from 'nostr-tools' -import { useEffect, useState } from 'react' +import { useCallback, useEffect, useState } from 'react' import { useSelector } from 'react-redux' import { useLocation, useNavigate } from 'react-router-dom' import { toast } from 'react-toastify' @@ -208,7 +208,6 @@ export const SignPage = () => { const signedMarks = extractMarksFromSignedMeta(meta) const currentUserMarks = getCurrentUserMarks(metaMarks, signedMarks) setCurrentUserMarks(currentUserMarks) - // setCurrentUserMark(findNextCurrentUserMark(currentUserMarks) || null) setIsReadyToSign(isCurrentUserMarksComplete(currentUserMarks)) } @@ -218,7 +217,79 @@ export const SignPage = () => { if (meta) { handleUpdatedMeta(meta) } - }, [meta]) + }, [meta, usersPubkey]) + + const decrypt = useCallback( + async (file: File) => { + setLoadingSpinnerDesc('Decrypting file') + + const zip = await loadZip(file) + if (!zip) return + + const parsedKeysJson = await parseKeysJson(zip) + if (!parsedKeysJson) return + + const encryptedArrayBuffer = await readContentOfZipEntry( + zip, + 'compressed.sigit', + 'arraybuffer' + ) + + if (!encryptedArrayBuffer) return + + const { keys, sender } = parsedKeysJson + + for (const key of keys) { + // Set up event listener for authentication event + nostrController.on('nsecbunker-auth', (url) => { + setAuthUrl(url) + }) + + // Set up timeout promise to handle encryption timeout + const timeoutPromise = new Promise((_, reject) => { + setTimeout(() => { + reject(new Error('Timeout occurred')) + }, 60000) // Timeout duration = 60 seconds + }) + + // decrypt the encryptionKey, with timeout + const encryptionKey = await Promise.race([ + nostrController.nip04Decrypt(sender, key), + timeoutPromise + ]) + .then((res) => { + return res + }) + .catch((err) => { + console.log('err :>> ', err) + return null + }) + .finally(() => { + setAuthUrl(undefined) // Clear authentication URL + }) + + // Return if encryption failed + if (!encryptionKey) continue + + const arrayBuffer = await decryptArrayBuffer( + encryptedArrayBuffer, + encryptionKey + ) + .catch((err) => { + console.log('err in decryption:>> ', err) + return null + }) + .finally(() => { + setIsLoading(false) + }) + + if (arrayBuffer) return arrayBuffer + } + + return null + }, + [nostrController] + ) useEffect(() => { // online mode - from create and home page views @@ -276,7 +347,7 @@ export const SignPage = () => { setIsLoading(false) setDisplayInput(true) } - }, [decryptedArrayBuffer, uploadedZip, metaInNavState]) + }, [decryptedArrayBuffer, uploadedZip, metaInNavState, decrypt]) const handleArrayBufferFromBlossom = async ( arrayBuffer: ArrayBuffer, @@ -354,75 +425,6 @@ export const SignPage = () => { }) } - const decrypt = async (file: File) => { - setLoadingSpinnerDesc('Decrypting file') - - const zip = await loadZip(file) - if (!zip) return - - const parsedKeysJson = await parseKeysJson(zip) - if (!parsedKeysJson) return - - const encryptedArrayBuffer = await readContentOfZipEntry( - zip, - 'compressed.sigit', - 'arraybuffer' - ) - - if (!encryptedArrayBuffer) return - - const { keys, sender } = parsedKeysJson - - for (const key of keys) { - // Set up event listener for authentication event - nostrController.on('nsecbunker-auth', (url) => { - setAuthUrl(url) - }) - - // Set up timeout promise to handle encryption timeout - const timeoutPromise = new Promise((_, reject) => { - setTimeout(() => { - reject(new Error('Timeout occurred')) - }, 60000) // Timeout duration = 60 seconds - }) - - // decrypt the encryptionKey, with timeout - const encryptionKey = await Promise.race([ - nostrController.nip04Decrypt(sender, key), - timeoutPromise - ]) - .then((res) => { - return res - }) - .catch((err) => { - console.log('err :>> ', err) - return null - }) - .finally(() => { - setAuthUrl(undefined) // Clear authentication URL - }) - - // Return if encryption failed - if (!encryptionKey) continue - - const arrayBuffer = await decryptArrayBuffer( - encryptedArrayBuffer, - encryptionKey - ) - .catch((err) => { - console.log('err in decryption:>> ', err) - return null - }) - .finally(() => { - setIsLoading(false) - }) - - if (arrayBuffer) return arrayBuffer - } - - return null - } - const handleDecryptedArrayBuffer = async (arrayBuffer: ArrayBuffer) => { const decryptedZipFile = new File([arrayBuffer], 'decrypted.zip') @@ -618,10 +620,12 @@ export const SignPage = () => { } // Handle errors during zip file generation - const handleZipError = (err: any) => { + const handleZipError = (err: unknown) => { console.log('Error in zip:>> ', err) setIsLoading(false) - toast.error(err.message || 'Error occurred in generating zip file') + if (err instanceof Error) { + toast.error(err.message || 'Error occurred in generating zip file') + } return null }