From 9fa3df3850935b4798489e9980de4520f00c2b20 Mon Sep 17 00:00:00 2001 From: SwiftHawk Date: Fri, 19 Apr 2024 15:57:44 +0500 Subject: [PATCH] fix: update the url in DM to contain fileUrl and encryption key --- src/pages/decrypt/index.tsx | 42 +++++++++++++++++++++++++++++++++++-- src/routes/index.tsx | 2 +- src/utils/misc.ts | 6 +++++- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/pages/decrypt/index.tsx b/src/pages/decrypt/index.tsx index 1181ef7..6a25cab 100644 --- a/src/pages/decrypt/index.tsx +++ b/src/pages/decrypt/index.tsx @@ -1,13 +1,17 @@ import { Box, Button, TextField, Typography } from '@mui/material' import saveAs from 'file-saver' import { MuiFileInput } from 'mui-file-input' -import { useState } from 'react' +import { useEffect, useState } from 'react' import { LoadingSpinner } from '../../components/LoadingSpinner' import { decryptArrayBuffer } from '../../utils' import styles from './style.module.scss' import { toast } from 'react-toastify' +import { useSearchParams } from 'react-router-dom' +import axios from 'axios' export const DecryptZip = () => { + const [searchParams] = useSearchParams() + const [selectedFile, setSelectedFile] = useState(null) const [encryptionKey, setEncryptionKey] = useState('') @@ -15,6 +19,36 @@ export const DecryptZip = () => { const [loadingSpinnerDesc, setLoadingSpinnerDesc] = useState('') const [isDraggingOver, setIsDraggingOver] = useState(false) + useEffect(() => { + const fileUrl = searchParams.get('file') + + if (fileUrl) { + setIsLoading(true) + setLoadingSpinnerDesc('Fetching zip file') + axios + .get(fileUrl, { + responseType: 'arraybuffer' + }) + .then((res) => { + const fileName = fileUrl.split('/').pop() + const file = new File([res.data], fileName!) + setSelectedFile(file) + }) + .catch((err) => { + console.error( + `error occurred in getting zip file from ${fileUrl}`, + err + ) + }) + .finally(() => { + setIsLoading(false) + }) + } + + const key = searchParams.get('key') + if (key) setEncryptionKey(key) + }, [searchParams]) + const handleDecrypt = async () => { if (!selectedFile || !encryptionKey) return @@ -91,7 +125,11 @@ export const DecryptZip = () => { - diff --git a/src/routes/index.tsx b/src/routes/index.tsx index 76f9b4c..0428e85 100644 --- a/src/routes/index.tsx +++ b/src/routes/index.tsx @@ -8,7 +8,7 @@ import { SignDocument } from '../pages/sign' export const appPrivateRoutes = { homePage: '/', - decryptZip: '/decrypt-zip', + decryptZip: '/decrypt', sign: '/sign' } diff --git a/src/utils/misc.ts b/src/utils/misc.ts index f2f2357..dea5e0e 100644 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -2,6 +2,7 @@ import axios from 'axios' import { EventTemplate } from 'nostr-tools' import { MetadataController, NostrController } from '../controllers' import { toast } from 'react-toastify' +import { appPrivateRoutes } from '../routes' /** * Uploads a file to a file storage service. @@ -73,7 +74,10 @@ export const sendDM = async ( const initialLine = isSigner ? 'You have been requested for a signature.' : 'You have received a signed document.' - const content = `${initialLine}\nHere is the URL for the zip file that you can download.\n${fileUrl}\nHowever, this zip file is encrypted and you need to decrypt it using https://app.sigit.io \nEncryption key: ${encryptionKey}` + + const decryptionUrl = `http://app.sigit.io${appPrivateRoutes.decryptZip}?file=${fileUrl}&key=${encryptionKey}` + + const content = `${initialLine}\nHere is the URL for the zip file that you can download.\n${fileUrl}\nHowever, this zip file is encrypted and you need to decrypt it using ${decryptionUrl}` // Set up event listener for authentication event nostrController.on('nsecbunker-auth', (url) => {