sigit.io/src/pages/verify/index.tsx

642 lines
19 KiB
TypeScript
Raw Normal View History

2024-09-13 17:53:22 +02:00
import { Box, Button, Typography } from '@mui/material'
2024-05-14 14:27:05 +05:00
import JSZip from 'jszip'
import { MuiFileInput } from 'mui-file-input'
import { useEffect, useRef, useState } from 'react'
2024-05-14 14:27:05 +05:00
import { toast } from 'react-toastify'
import { LoadingSpinner } from '../../components/LoadingSpinner'
import { NostrController } from '../../controllers'
import {
DocSignatureEvent,
Meta,
SignedEvent,
OpenTimestamp,
OpenTimestampUpgradeVerifyResponse
} from '../../types'
2024-05-14 14:27:05 +05:00
import {
decryptArrayBuffer,
2024-05-22 11:19:40 +05:00
getHash,
hexToNpub,
unixNow,
2024-05-14 14:27:05 +05:00
parseJson,
readContentOfZipEntry,
signEventForMetaFile,
getCurrentUserFiles,
updateUsersAppData,
npubToHex,
sendNotification
2024-05-14 14:27:05 +05:00
} from '../../utils'
import styles from './style.module.scss'
import { useLocation } from 'react-router-dom'
2024-07-05 13:38:04 +05:00
import axios from 'axios'
import { FONT_SIZE, FONT_TYPE, inPx } from '../../utils/pdf.ts'
import { useAppSelector } from '../../hooks/store'
import { getLastSignersSig } from '../../utils/sign.ts'
import { saveAs } from 'file-saver'
import { Container } from '../../components/Container'
import { useSigitMeta } from '../../hooks/useSigitMeta.tsx'
import { StickySideColumns } from '../../layouts/StickySideColumns.tsx'
import { UsersDetails } from '../../components/UsersDetails.tsx'
import FileList from '../../components/FileList'
import { CurrentUserFile } from '../../types/file.ts'
import { Mark } from '../../types/mark.ts'
2024-08-21 11:26:17 +02:00
import React from 'react'
import {
convertToSigitFile,
getZipWithFiles,
SigitFile
} from '../../utils/file.ts'
import { FileDivider } from '../../components/FileDivider.tsx'
import { ExtensionFileBox } from '../../components/ExtensionFileBox.tsx'
import { useScale } from '../../hooks/useScale.tsx'
2024-09-04 14:05:36 +02:00
import {
faCircleInfo,
faFile,
faFileDownload
} from '@fortawesome/free-solid-svg-icons'
import { upgradeAndVerifyTimestamp } from '../../utils/opentimestamps.ts'
import _ from 'lodash'
2024-05-14 14:27:05 +05:00
interface PdfViewProps {
files: CurrentUserFile[]
currentFile: CurrentUserFile | null
parsedSignatureEvents: {
[signer: `npub1${string}`]: DocSignatureEvent
}
}
const SlimPdfView = ({
files,
currentFile,
parsedSignatureEvents
}: PdfViewProps) => {
const pdfRefs = useRef<(HTMLDivElement | null)[]>([])
const { from } = useScale()
useEffect(() => {
if (currentFile !== null && !!pdfRefs.current[currentFile.id]) {
pdfRefs.current[currentFile.id]?.scrollIntoView({
behavior: 'smooth'
})
}
}, [currentFile])
return (
<div className="files-wrapper">
{files.length > 0 ? (
files.map((currentUserFile, i) => {
const { hash, file, id } = currentUserFile
const signatureEvents = Object.keys(parsedSignatureEvents)
if (!hash) return
return (
<React.Fragment key={file.name}>
<div
id={file.name}
ref={(el) => (pdfRefs.current[id] = el)}
className="file-wrapper"
>
{file.isPdf &&
file.pages?.map((page, i) => {
const marks: Mark[] = []
signatureEvents.forEach((e) => {
const m = parsedSignatureEvents[
e as `npub1${string}`
].parsedContent?.marks.filter(
(m) => m.pdfFileHash == hash && m.location.page == i
)
if (m) {
marks.push(...m)
}
})
return (
<div className="image-wrapper" key={i}>
<img
draggable="false"
src={page.image}
alt={`page ${i} of ${file.name}`}
/>
{marks.map((m) => {
return (
<div
className={`file-mark ${styles.mark}`}
key={m.id}
style={{
left: inPx(from(page.width, m.location.left)),
top: inPx(from(page.width, m.location.top)),
width: inPx(from(page.width, m.location.width)),
height: inPx(
from(page.width, m.location.height)
),
fontFamily: FONT_TYPE,
fontSize: inPx(from(page.width, FONT_SIZE))
}}
>
{m.value}
</div>
)
})}
</div>
)
})}
{file.isImage && (
<img
className="file-image"
src={file.objectUrl}
alt={file.name}
/>
)}
{!(file.isPdf || file.isImage) && (
<ExtensionFileBox extension={file.extension} />
)}
</div>
{i < files.length - 1 && <FileDivider />}
</React.Fragment>
)
})
) : (
<LoadingSpinner variant="small" />
)}
</div>
2024-05-16 10:40:56 +05:00
)
}
2024-05-14 14:27:05 +05:00
export const VerifyPage = () => {
const location = useLocation()
const usersPubkey = useAppSelector((state) => state.auth.usersPubkey)
2024-09-13 17:53:22 +02:00
const nostrController = NostrController.getInstance()
const [isLoading, setIsLoading] = useState(false)
const [loadingSpinnerDesc, setLoadingSpinnerDesc] = useState('')
2024-07-09 01:16:47 +05:00
/**
* uploadedZip will be received from home page when a user uploads a sigit zip wrapper that contains meta.json
* meta will be received in navigation from create & home page in online mode
*/
2024-09-13 17:53:22 +02:00
const { uploadedZip, meta: metaInNavState } = location.state || {}
const [selectedFile, setSelectedFile] = useState<File | null>(null)
useEffect(() => {
if (uploadedZip) {
setSelectedFile(uploadedZip)
}
}, [uploadedZip])
2024-09-13 17:53:22 +02:00
const [meta, setMeta] = useState<Meta>(metaInNavState)
const {
submittedBy,
zipUrl,
encryptionKey,
signers,
viewers,
fileHashes,
parsedSignatureEvents,
timestamps
} = useSigitMeta(meta)
2024-09-13 17:53:22 +02:00
const [files, setFiles] = useState<{ [filename: string]: SigitFile }>({})
2024-05-22 11:19:40 +05:00
2024-09-13 17:53:22 +02:00
const [currentFile, setCurrentFile] = useState<CurrentUserFile | null>(null)
2024-05-22 11:19:40 +05:00
const [currentFileHashes, setCurrentFileHashes] = useState<{
[key: string]: string | null
}>({})
const signTimestampEvent = async (signerContent: {
timestamps: OpenTimestamp[]
}): Promise<SignedEvent | null> => {
return await signEventForMetaFile(
JSON.stringify(signerContent),
nostrController,
setIsLoading
)
}
useEffect(() => {
if (Object.entries(files).length > 0) {
2024-09-13 17:53:22 +02:00
const tmp = getCurrentUserFiles(files, currentFileHashes, fileHashes)
setCurrentFile(tmp[0])
}
2024-09-13 17:53:22 +02:00
}, [currentFileHashes, fileHashes, files])
2024-05-14 14:27:05 +05:00
useEffect(() => {
if (
timestamps &&
timestamps.length > 0 &&
usersPubkey &&
submittedBy &&
parsedSignatureEvents
) {
if (timestamps.every((t) => !!t.verification)) {
2024-10-08 17:04:07 +02:00
console.log('Timestamps are fully verified')
return
}
const upgradeT = async (timestamps: OpenTimestamp[]) => {
try {
setLoadingSpinnerDesc('Upgrading your timestamps.')
const findCreatorTimestamp = (timestamps: OpenTimestamp[]) => {
if (usersPubkey === submittedBy) {
return timestamps[0]
}
}
const findSignerTimestamp = (timestamps: OpenTimestamp[]) => {
const parsedEvent = parsedSignatureEvents[hexToNpub(usersPubkey)]
if (parsedEvent?.id) {
return timestamps.find((t) => t.nostrId === parsedEvent.id)
}
}
/**
* Checks if timestamp verification has been achieved for the first time.
* Note that the upgrade flag is separate from verification. It is possible for a timestamp
* to not be upgraded, but to be verified for the first time.
* @param upgradedTimestamp
* @param timestamps
*/
const isNewlyVerified = (
upgradedTimestamp: OpenTimestampUpgradeVerifyResponse,
timestamps: OpenTimestamp[]
) => {
if (!upgradedTimestamp.verified) return false
const oldT = timestamps.find(
(t) => t.nostrId === upgradedTimestamp.timestamp.nostrId
)
if (!oldT) return false
if (!oldT.verification && upgradedTimestamp.verified) return true
}
const userTimestamps: OpenTimestamp[] = []
const creatorTimestamp = findCreatorTimestamp(timestamps)
if (creatorTimestamp) {
userTimestamps.push(creatorTimestamp)
}
const signerTimestamp = findSignerTimestamp(timestamps)
if (signerTimestamp) {
userTimestamps.push(signerTimestamp)
}
if (userTimestamps.every((t) => !!t.verification)) {
console.log('user timestamps are verified')
return
}
const upgradedUserTimestamps = await Promise.all(
userTimestamps.map(upgradeAndVerifyTimestamp)
)
const upgradedTimestamps = upgradedUserTimestamps
.filter((t) => t.upgraded || isNewlyVerified(t, userTimestamps))
.map((t) => {
const timestamp: OpenTimestamp = { ...t.timestamp }
if (t.verified) {
timestamp.verification = t.verification
}
return timestamp
})
if (upgradedUserTimestamps.length > 0) {
return
}
setLoadingSpinnerDesc('Signing a timestamp upgrade event.')
const signedEvent = await signTimestampEvent({
timestamps: upgradedTimestamps
})
if (!signedEvent) return
const finalTimestamps = timestamps.map((t) => {
const upgraded = upgradedTimestamps.find(
(tu) => tu.nostrId === t.nostrId
)
if (upgraded) {
return {
...upgraded,
signature: JSON.stringify(signedEvent, null, 2)
}
}
return t
})
const updatedMeta = _.cloneDeep(meta)
updatedMeta.timestamps = [...finalTimestamps]
updatedMeta.modifiedAt = unixNow()
const updatedEvent = await updateUsersAppData(updatedMeta)
if (!updatedEvent) return
const userSet = new Set<`npub1${string}`>()
signers.forEach((signer) => {
if (signer !== usersPubkey) {
userSet.add(signer)
}
})
viewers.forEach((viewer) => {
userSet.add(viewer)
})
const users = Array.from(userSet)
const promises = users.map((user) =>
sendNotification(npubToHex(user)!, updatedMeta)
)
await Promise.all(promises)
toast.success('Timestamp updates have been sent successfully.')
setMeta(meta)
} catch (err) {
console.error(err)
toast.error(
'There was an error upgrading or verifying your timestamps!'
)
}
}
upgradeT(timestamps)
}
}, [timestamps, submittedBy, parsedSignatureEvents])
useEffect(() => {
2024-09-13 17:53:22 +02:00
if (metaInNavState && encryptionKey) {
2024-07-05 13:38:04 +05:00
const processSigit = async () => {
setIsLoading(true)
setLoadingSpinnerDesc('Fetching file from file server')
axios
.get(zipUrl, {
responseType: 'arraybuffer'
})
.then(async (res) => {
const fileName = zipUrl.split('/').pop()
const file = new File([res.data], fileName!)
const encryptedArrayBuffer = await file.arrayBuffer()
const arrayBuffer = await decryptArrayBuffer(
encryptedArrayBuffer,
encryptionKey
).catch((err) => {
console.log('err in decryption:>> ', err)
toast.error(
err.message || 'An error occurred in decrypting file.'
)
return null
})
if (arrayBuffer) {
const zip = await JSZip.loadAsync(arrayBuffer).catch((err) => {
console.log('err in loading zip file :>> ', err)
toast.error(
err.message || 'An error occurred in loading zip file.'
)
return null
})
if (!zip) return
const files: { [fileName: string]: SigitFile } = {}
2024-07-05 13:38:04 +05:00
const fileHashes: { [key: string]: string | null } = {}
const fileNames = Object.values(zip.files).map(
(entry) => entry.name
)
// generate hashes for all entries in files folder of zipArchive
// these hashes can be used to verify the originality of files
for (const fileName of fileNames) {
const arrayBuffer = await readContentOfZipEntry(
zip,
fileName,
'arraybuffer'
)
if (arrayBuffer) {
files[fileName] = await convertToSigitFile(
arrayBuffer,
fileName!
)
2024-07-05 13:38:04 +05:00
const hash = await getHash(arrayBuffer)
if (hash) {
fileHashes[fileName.replace(/^files\//, '')] = hash
}
} else {
fileHashes[fileName.replace(/^files\//, '')] = null
}
}
setCurrentFileHashes(fileHashes)
setFiles(files)
2024-07-05 13:38:04 +05:00
setIsLoading(false)
}
})
.catch((err) => {
console.error(`error occurred in getting file from ${zipUrl}`, err)
toast.error(
err.message || `error occurred in getting file from ${zipUrl}`
)
})
.finally(() => {
setIsLoading(false)
})
2024-05-22 11:19:40 +05:00
}
2024-07-05 13:38:04 +05:00
processSigit()
2024-05-22 11:19:40 +05:00
}
2024-09-13 17:53:22 +02:00
}, [encryptionKey, metaInNavState, zipUrl])
2024-05-14 14:27:05 +05:00
2024-05-16 10:40:56 +05:00
const handleVerify = async () => {
if (!selectedFile) return
setIsLoading(true)
2024-05-14 14:27:05 +05:00
2024-05-16 10:40:56 +05:00
const zip = await JSZip.loadAsync(selectedFile).catch((err) => {
2024-05-14 14:27:05 +05:00
console.log('err in loading zip file :>> ', err)
toast.error(err.message || 'An error occurred in loading zip file.')
return null
})
if (!zip) return
2024-07-05 13:38:04 +05:00
2024-09-13 17:53:22 +02:00
const files: { [filename: string]: SigitFile } = {}
2024-07-05 13:38:04 +05:00
const fileHashes: { [key: string]: string | null } = {}
const fileNames = Object.values(zip.files)
.filter((entry) => entry.name.startsWith('files/') && !entry.dir)
.map((entry) => entry.name)
// generate hashes for all entries in files folder of zipArchive
// these hashes can be used to verify the originality of files
2024-09-13 17:53:22 +02:00
for (const zipFilePath of fileNames) {
2024-07-05 13:38:04 +05:00
const arrayBuffer = await readContentOfZipEntry(
zip,
2024-09-13 17:53:22 +02:00
zipFilePath,
2024-07-05 13:38:04 +05:00
'arraybuffer'
)
2024-09-13 17:53:22 +02:00
const fileName = zipFilePath.replace(/^files\//, '')
2024-07-05 13:38:04 +05:00
if (arrayBuffer) {
2024-09-13 17:53:22 +02:00
files[fileName] = await convertToSigitFile(arrayBuffer, fileName)
2024-07-05 13:38:04 +05:00
const hash = await getHash(arrayBuffer)
if (hash) {
2024-09-13 17:53:22 +02:00
fileHashes[fileName] = hash
2024-07-05 13:38:04 +05:00
}
} else {
2024-09-13 17:53:22 +02:00
fileHashes[fileName] = null
2024-07-05 13:38:04 +05:00
}
}
2024-09-13 17:53:22 +02:00
setFiles(files)
2024-07-05 13:38:04 +05:00
setCurrentFileHashes(fileHashes)
2024-05-14 14:27:05 +05:00
setLoadingSpinnerDesc('Parsing meta.json')
const metaFileContent = await readContentOfZipEntry(
zip,
'meta.json',
'string'
)
if (!metaFileContent) {
setIsLoading(false)
return
}
const parsedMetaJson = await parseJson<Meta>(metaFileContent).catch(
(err) => {
console.log('err in parsing the content of meta.json :>> ', err)
toast.error(
err.message || 'error occurred in parsing the content of meta.json'
)
setIsLoading(false)
return null
}
)
2024-05-22 11:19:40 +05:00
if (!parsedMetaJson) return
2024-09-13 17:53:22 +02:00
setMeta(parsedMetaJson)
2024-05-22 11:19:40 +05:00
2024-05-16 10:40:56 +05:00
setIsLoading(false)
2024-05-14 14:27:05 +05:00
}
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)
}
2024-05-14 14:27:05 +05:00
return (
<>
{isLoading && <LoadingSpinner desc={loadingSpinnerDesc} />}
<Container className={styles.container}>
2024-05-16 10:40:56 +05:00
{!meta && (
2024-05-14 14:27:05 +05:00
<>
2024-05-15 11:50:21 +03:00
<Typography component="label" variant="h6">
2024-05-16 10:40:56 +05:00
Select exported zip file
2024-05-14 14:27:05 +05:00
</Typography>
2024-05-16 10:40:56 +05:00
<MuiFileInput
2024-05-16 11:25:30 +05:00
placeholder="Select file"
2024-05-16 10:40:56 +05:00
value={selectedFile}
onChange={(value) => setSelectedFile(value)}
InputProps={{
inputProps: {
accept: '.sigit.zip'
2024-05-16 10:40:56 +05:00
}
}}
/>
2024-05-14 14:27:05 +05:00
2024-05-16 10:40:56 +05:00
{selectedFile && (
2024-05-15 11:19:28 +05:00
<Box sx={{ mt: 2, display: 'flex', justifyContent: 'center' }}>
2024-05-16 11:25:30 +05:00
<Button onClick={handleVerify} variant="contained">
2024-05-16 10:40:56 +05:00
Verify
2024-05-14 14:27:05 +05:00
</Button>
</Box>
)}
</>
)}
2024-05-16 10:40:56 +05:00
{meta && (
<StickySideColumns
left={
2024-09-13 17:53:22 +02:00
currentFile !== null && (
<FileList
files={getCurrentUserFiles(
files,
currentFileHashes,
fileHashes
)}
currentFile={currentFile}
setCurrentFile={setCurrentFile}
handleDownload={handleMarkedExport}
2024-09-13 17:53:22 +02:00
downloadLabel="Download Sigit"
/>
)
}
right={<UsersDetails meta={meta} />}
2024-09-04 14:05:36 +02:00
leftIcon={faFileDownload}
centerIcon={faFile}
rightIcon={faCircleInfo}
>
<SlimPdfView
currentFile={currentFile}
2024-09-13 17:53:22 +02:00
files={getCurrentUserFiles(files, currentFileHashes, fileHashes)}
parsedSignatureEvents={parsedSignatureEvents}
/>
</StickySideColumns>
2024-05-14 14:27:05 +05:00
)}
</Container>
2024-05-14 14:27:05 +05:00
</>
)
}