feat: added hashes.json in zip
All checks were successful
Release / build_and_release (push) Successful in 56s

This commit is contained in:
SwiftHawk 2024-04-22 16:24:50 +05:00
parent 9fa3df3850
commit d879c7d45a
3 changed files with 46 additions and 7 deletions

View File

@ -214,8 +214,16 @@ export const HomePage = () => {
try { try {
const stringifiedMeta = JSON.stringify(meta, null, 2) const stringifiedMeta = JSON.stringify(meta, null, 2)
zip.file('meta.json', stringifiedMeta) 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) { } catch (err) {
toast.error('An error occurred in converting meta json to string') toast.error('An error occurred in converting meta json to string')
} }

View File

@ -78,7 +78,27 @@ export const SignDocument = () => {
return null 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') 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 current user is the last signer, then send DMs to all viewers
if (isLastSigner) { if (isLastSigner) {
for (const viewer of meta.viewers) { for (const viewer of meta.viewers) {
await signAndSendToNext(zip, meta, viewer, fileHashes, false) await signAndSendToNext(zip, meta, hashes, viewer, fileHashes, false)
} }
} else { } else {
const nextSigner = meta.signers[signerIndex + 1] const nextSigner = meta.signers[signerIndex + 1]
await signAndSendToNext(zip, meta, nextSigner, fileHashes, true) await signAndSendToNext(zip, meta, hashes, nextSigner, fileHashes, true)
} }
setIsLoading(false) setIsLoading(false)
@ -131,6 +151,7 @@ export const SignDocument = () => {
const signAndSendToNext = async ( const signAndSendToNext = async (
zip: JSZip, zip: JSZip,
meta: any, meta: any,
hashes: any,
receiver: string, receiver: string,
fileHashes: { fileHashes: {
[key: string]: string [key: string]: string
@ -155,6 +176,16 @@ export const SignDocument = () => {
const stringifiedMeta = JSON.stringify(meta, null, 2) const stringifiedMeta = JSON.stringify(meta, null, 2)
zip.file('meta.json', stringifiedMeta) 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 const arrayBuffer = await zip
.generateAsync({ .generateAsync({
type: 'arraybuffer', type: 'arraybuffer',

View File

@ -6,12 +6,12 @@ import { toast } from 'react-toastify'
* @param arrayBuffer The ArrayBuffer to hash. * @param arrayBuffer The ArrayBuffer to hash.
* @returns A Promise resolving to the SHA-256 hash as a hexadecimal string, or null if hashing fails. * @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 // 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 // Handle error if hashing fails
console.log(`error occurred in hashing arrayBuffer :>> `, err) 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 return null // Return null if hashing fails
}) })