From 6716c3da636f9c0a1b168cb2cb0a98da97a6c5c3 Mon Sep 17 00:00:00 2001 From: Stixx Date: Tue, 10 Dec 2024 14:42:12 +0100 Subject: [PATCH] fix: including signatures in both export and encrypted export --- src/pages/sign/index.tsx | 197 +++++++++++++++---------------------- src/pages/verify/index.tsx | 151 +++++++++++++--------------- 2 files changed, 151 insertions(+), 197 deletions(-) diff --git a/src/pages/sign/index.tsx b/src/pages/sign/index.tsx index ab95318..16fa3c4 100644 --- a/src/pages/sign/index.tsx +++ b/src/pages/sign/index.tsx @@ -32,7 +32,8 @@ import { updateUsersAppData, findOtherUserMarks, timeout, - processMarks + processMarks, + ARRAY_BUFFER } from '../../utils' import { CurrentUserMark, Mark } from '../../types/mark.ts' import { @@ -46,9 +47,10 @@ import { getZipWithFiles, SigitFile } from '../../utils/file.ts' -import { ARRAY_BUFFER, DEFLATE } from '../../utils' +import { DEFLATE } from '../../utils' import { generateTimestamp } from '../../utils/opentimestamps.ts' import { MARK_TYPE_CONFIG } from '../../components/MarkTypeStrategy/MarkStrategy.tsx' +import { getLastSignersSig } from '../../utils/sign.ts' export const SignPage = () => { const navigate = useNavigate() @@ -696,124 +698,19 @@ export const SignPage = () => { } const handleExport = async () => { - if (Object.entries(files).length === 0 || !meta || !usersPubkey) return - setLoadingSpinnerDesc('Generating file') - try { - const zip = await getZipWithFiles(meta, files) - const arrayBuffer = await zip.generateAsync({ - type: ARRAY_BUFFER, - compression: DEFLATE, - compressionOptions: { - level: 6 - } - }) - if (!arrayBuffer) return - const blob = new Blob([arrayBuffer]) - saveAs(blob, `exported-${unixNow()}.sigit.zip`) - } catch (error) { - console.log('error in zip:>> ', error) - if (error instanceof Error) { - toast.error(error.message || 'Error occurred in generating zip file') - } - } + const arrayBuffer = await prepareZipExport() + if (!arrayBuffer) return + + const blob = new Blob([arrayBuffer]) + saveAs(blob, `exported-${unixNow()}.sigit.zip`) + + setIsLoading(false) + + navigate(appPublicRoutes.verify) } - /** - * @deprecated - * It's the same as {@link handleExport} but the code is not the same - * leaving it for the reference - */ - // const handleExport = async () => { - // if (Object.entries(files).length === 0 || !meta || !usersPubkey) return - // - // const usersNpub = hexToNpub(usersPubkey) - // if ( - // !signers.includes(usersNpub) && - // !viewers.includes(usersNpub) && - // submittedBy !== usersNpub - // ) - // return - // - // setIsLoading(true) - // setLoadingSpinnerDesc('Signing nostr event') - // - // if (!meta) return - // - // const prevSig = getLastSignersSig(meta, signers) - // if (!prevSig) return - // - // const signedEvent = await signEventForMetaFile( - // JSON.stringify({ - // prevSig - // }), - // nostrController, - // setIsLoading - // ) - // - // if (!signedEvent) return - // - // const exportSignature = JSON.stringify(signedEvent, null, 2) - // - // const stringifiedMeta = JSON.stringify( - // { - // ...meta, - // exportSignature - // }, - // null, - // 2 - // ) - // - // const zip = await getZipWithFiles(meta, files) - // zip.file('meta.json', stringifiedMeta) - // - // const arrayBuffer = await zip - // .generateAsync({ - // type: 'arraybuffer', - // compression: 'DEFLATE', - // compressionOptions: { - // level: 6 - // } - // }) - // .catch((err) => { - // console.log('err in zip:>> ', err) - // setIsLoading(false) - // toast.error(err.message || 'Error occurred in generating zip file') - // return null - // }) - // - // if (!arrayBuffer) return - // - // const blob = new Blob([arrayBuffer]) - // saveAs(blob, `exported-${unixNow()}.sigit.zip`) - // - // setIsLoading(false) - // - // navigate(appPublicRoutes.verify) - // } - const handleEncryptedExport = async () => { - if (Object.entries(files).length === 0 || !meta) return - - const stringifiedMeta = JSON.stringify(meta, null, 2) - const zip = await getZipWithFiles(meta, files) - - zip.file('meta.json', stringifiedMeta) - - const arrayBuffer = await zip - .generateAsync({ - type: 'arraybuffer', - compression: 'DEFLATE', - compressionOptions: { - level: 6 - } - }) - .catch((err) => { - console.log('err in zip:>> ', err) - setIsLoading(false) - toast.error(err.message || 'Error occurred in generating zip file') - return null - }) - + const arrayBuffer = await prepareZipExport() if (!arrayBuffer) return const key = await generateEncryptionKey() @@ -825,6 +722,72 @@ export const SignPage = () => { if (!finalZipFile) return saveAs(finalZipFile, `exported-${unixNow()}.sigit.zip`) + + setIsLoading(false) + } + + const prepareZipExport = async (): Promise => { + if (Object.entries(files).length === 0 || !meta || !usersPubkey) + return Promise.resolve(null) + + const usersNpub = hexToNpub(usersPubkey) + if ( + !signers.includes(usersNpub) && + !viewers.includes(usersNpub) && + submittedBy !== usersNpub + ) + return Promise.resolve(null) + + setIsLoading(true) + setLoadingSpinnerDesc('Signing nostr event') + + if (!meta) return Promise.resolve(null) + + const prevSig = getLastSignersSig(meta, signers) + if (!prevSig) return Promise.resolve(null) + + const signedEvent = await signEventForMetaFile( + JSON.stringify({ + prevSig + }), + nostrController, + setIsLoading + ) + + if (!signedEvent) return Promise.resolve(null) + + const exportSignature = JSON.stringify(signedEvent, null, 2) + + const stringifiedMeta = JSON.stringify( + { + ...meta, + exportSignature + }, + null, + 2 + ) + + const zip = await getZipWithFiles(meta, files) + zip.file('meta.json', stringifiedMeta) + + const arrayBuffer = await zip + .generateAsync({ + type: ARRAY_BUFFER, + compression: DEFLATE, + compressionOptions: { + level: 6 + } + }) + .catch((err) => { + console.log('err in zip:>> ', err) + setIsLoading(false) + toast.error(err.message || 'Error occurred in generating zip file') + return null + }) + + if (!arrayBuffer) return Promise.resolve(null) + + return Promise.resolve(arrayBuffer) } /** diff --git a/src/pages/verify/index.tsx b/src/pages/verify/index.tsx index 1d91fa4..1aedc4d 100644 --- a/src/pages/verify/index.tsx +++ b/src/pages/verify/index.tsx @@ -26,7 +26,9 @@ import { sendNotification, generateEncryptionKey, encryptArrayBuffer, - generateKeysFile + generateKeysFile, + ARRAY_BUFFER, + DEFLATE } from '../../utils' import styles from './style.module.scss' import { useLocation, useParams } from 'react-router-dom' @@ -544,62 +546,6 @@ export const VerifyPage = () => { setIsLoading(false) } - const handleMarkedExport = async () => { - if (Object.entries(files).length === 0 || !meta || !usersPubkey) return - - const usersNpub = hexToNpub(usersPubkey) - if ( - !signers.includes(usersNpub) && - !viewers.includes(usersNpub) && - submittedBy !== usersNpub - ) { - return - } - - setIsLoading(true) - setLoadingSpinnerDesc('Signing nostr event') - - const prevSig = getLastSignersSig(meta, signers) - if (!prevSig) return - - const signedEvent = await signEventForMetaFile( - JSON.stringify({ prevSig }), - nostrController, - setIsLoading - ) - - if (!signedEvent) return - - const exportSignature = JSON.stringify(signedEvent, null, 2) - const updatedMeta = { ...meta, exportSignature } - const stringifiedMeta = JSON.stringify(updatedMeta, null, 2) - - const zip = await getZipWithFiles(updatedMeta, files) - zip.file('meta.json', stringifiedMeta) - - const arrayBuffer = await zip - .generateAsync({ - type: 'arraybuffer', - compression: 'DEFLATE', - compressionOptions: { - level: 6 - } - }) - .catch((err) => { - console.log('err in zip:>> ', err) - setIsLoading(false) - toast.error(err.message || 'Error occurred in generating zip file') - return null - }) - - if (!arrayBuffer) return - - const blob = new Blob([arrayBuffer]) - saveAs(blob, `exported-${unixNow()}.sigit.zip`) - - setIsLoading(false) - } - // Handle errors during zip file generation const handleZipError = (err: unknown) => { console.log('Error in zip:>> ', err) @@ -678,29 +624,18 @@ export const VerifyPage = () => { }) } + const handleExport = async () => { + const arrayBuffer = await prepareZipExport() + if (!arrayBuffer) return + + const blob = new Blob([arrayBuffer]) + saveAs(blob, `exported-${unixNow()}.sigit.zip`) + + setIsLoading(false) + } + const handleEncryptedExport = async () => { - if (Object.entries(files).length === 0 || !meta) return - - const stringifiedMeta = JSON.stringify(meta, null, 2) - const zip = await getZipWithFiles(meta, files) - - zip.file('meta.json', stringifiedMeta) - - const arrayBuffer = await zip - .generateAsync({ - type: 'arraybuffer', - compression: 'DEFLATE', - compressionOptions: { - level: 6 - } - }) - .catch((err) => { - console.log('err in zip:>> ', err) - setIsLoading(false) - toast.error(err.message || 'Error occurred in generating zip file') - return null - }) - + const arrayBuffer = await prepareZipExport() if (!arrayBuffer) return const key = await generateEncryptionKey() @@ -712,6 +647,62 @@ export const VerifyPage = () => { if (!finalZipFile) return saveAs(finalZipFile, `exported-${unixNow()}.sigit.zip`) + + setIsLoading(false) + } + + const prepareZipExport = async (): Promise => { + if (Object.entries(files).length === 0 || !meta || !usersPubkey) + return Promise.resolve(null) + + const usersNpub = hexToNpub(usersPubkey) + if ( + !signers.includes(usersNpub) && + !viewers.includes(usersNpub) && + submittedBy !== usersNpub + ) { + return Promise.resolve(null) + } + + setIsLoading(true) + setLoadingSpinnerDesc('Signing nostr event') + + const prevSig = getLastSignersSig(meta, signers) + if (!prevSig) return Promise.resolve(null) + + const signedEvent = await signEventForMetaFile( + JSON.stringify({ prevSig }), + nostrController, + setIsLoading + ) + + if (!signedEvent) return Promise.resolve(null) + + const exportSignature = JSON.stringify(signedEvent, null, 2) + const updatedMeta = { ...meta, exportSignature } + const stringifiedMeta = JSON.stringify(updatedMeta, null, 2) + + const zip = await getZipWithFiles(updatedMeta, files) + zip.file('meta.json', stringifiedMeta) + + const arrayBuffer = await zip + .generateAsync({ + type: ARRAY_BUFFER, + compression: DEFLATE, + compressionOptions: { + level: 6 + } + }) + .catch((err) => { + console.log('err in zip:>> ', err) + setIsLoading(false) + toast.error(err.message || 'Error occurred in generating zip file') + return null + }) + + if (!arrayBuffer) return Promise.resolve(null) + + return Promise.resolve(arrayBuffer) } return ( @@ -757,7 +748,7 @@ export const VerifyPage = () => { )} currentFile={currentFile} setCurrentFile={setCurrentFile} - handleExport={handleMarkedExport} + handleExport={handleExport} handleEncryptedExport={handleEncryptedExport} /> )