diff --git a/src/pages/home/index.tsx b/src/pages/home/index.tsx index 85586b5..ebea0d4 100644 --- a/src/pages/home/index.tsx +++ b/src/pages/home/index.tsx @@ -214,8 +214,16 @@ export const HomePage = () => { try { const stringifiedMeta = JSON.stringify(meta, null, 2) - zip.file('meta.json', stringifiedMeta) + + const metaHash = await getHash(stringifiedMeta) + if (!metaHash) return + + const metaHashJson = { + [usersPubkey!]: metaHash + } + + zip.file('hashes.json', JSON.stringify(metaHashJson, null, 2)) } catch (err) { toast.error('An error occurred in converting meta json to string') } diff --git a/src/pages/sign/index.tsx b/src/pages/sign/index.tsx index 56c24b0..bfbc6af 100644 --- a/src/pages/sign/index.tsx +++ b/src/pages/sign/index.tsx @@ -78,7 +78,27 @@ export const SignDocument = () => { return null }) - if (!meta) return + const hashesFileContent = await readContentOfZipEntry( + zip, + 'hashes.json', + 'string' + ) + + if (!hashesFileContent) { + setIsLoading(false) + return + } + + const hashes = await parseJson(hashesFileContent).catch((err) => { + console.log('err in parsing the content of hashes.json :>> ', err) + toast.error( + err.message || 'error occurred in parsing the content of hashes.json' + ) + setIsLoading(false) + return null + }) + + if (!hashes) return setLoadingSpinnerDesc('Generating hashes for files') @@ -117,12 +137,12 @@ export const SignDocument = () => { // if current user is the last signer, then send DMs to all viewers if (isLastSigner) { for (const viewer of meta.viewers) { - await signAndSendToNext(zip, meta, viewer, fileHashes, false) + await signAndSendToNext(zip, meta, hashes, viewer, fileHashes, false) } } else { const nextSigner = meta.signers[signerIndex + 1] - await signAndSendToNext(zip, meta, nextSigner, fileHashes, true) + await signAndSendToNext(zip, meta, hashes, nextSigner, fileHashes, true) } setIsLoading(false) @@ -131,6 +151,7 @@ export const SignDocument = () => { const signAndSendToNext = async ( zip: JSZip, meta: any, + hashes: any, receiver: string, fileHashes: { [key: string]: string @@ -155,6 +176,16 @@ export const SignDocument = () => { const stringifiedMeta = JSON.stringify(meta, null, 2) zip.file('meta.json', stringifiedMeta) + const metaHash = await getHash(stringifiedMeta) + if (!metaHash) return + + hashes = { + ...hashes, + [usersPubkey!]: metaHash + } + + zip.file('hashes.json', JSON.stringify(hashes, null, 2)) + const arrayBuffer = await zip .generateAsync({ type: 'arraybuffer', diff --git a/src/utils/hash.ts b/src/utils/hash.ts index eb8c8dd..bd2d4e7 100644 --- a/src/utils/hash.ts +++ b/src/utils/hash.ts @@ -6,12 +6,12 @@ import { toast } from 'react-toastify' * @param arrayBuffer The ArrayBuffer to hash. * @returns A Promise resolving to the SHA-256 hash as a hexadecimal string, or null if hashing fails. */ -export const getHash = async (arrayBuffer: ArrayBuffer) => { +export const getHash = async (input: ArrayBuffer | string) => { // Compute the SHA-256 hash of the array buffer - const hash = await sha256(arrayBuffer).catch((err) => { + const hash = await sha256(input).catch((err) => { // Handle error if hashing fails console.log(`error occurred in hashing arrayBuffer :>> `, err) - toast.error(err.message || `error occurred in hashing arrayBuffer`) + toast.error(err.message || `error occurred in hashing`) return null // Return null if hashing fails })