From 8cc811fa3f3d5594aca4756a2d966c7d99d921bc Mon Sep 17 00:00:00 2001 From: Morten Olsen Date: Sat, 22 Aug 2020 19:17:53 +0200 Subject: [PATCH] Fix decrypt --- src/components/decrypt/AddFile.tsx | 2 +- src/components/encrypt/Add.tsx | 2 +- src/components/encrypt/AddFile.tsx | 2 +- src/contexts/Decryption.tsx | 20 ++++++++++++++------ src/contexts/Encryption.tsx | 1 - src/index.tsx | 1 - src/screens/Decrypt.tsx | 2 +- 7 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/components/decrypt/AddFile.tsx b/src/components/decrypt/AddFile.tsx index 81b137a..17577b7 100644 --- a/src/components/decrypt/AddFile.tsx +++ b/src/components/decrypt/AddFile.tsx @@ -22,7 +22,7 @@ const AddFile: React.FC = () => { const { addFile } = useContext(DecryptionContext); const onDrop = useCallback(acceptedFiles => { acceptedFiles.forEach(addFile); - }, []) + }, [addFile]) const {getRootProps, getInputProps} = useDropzone({ onDrop }); return ( diff --git a/src/components/encrypt/Add.tsx b/src/components/encrypt/Add.tsx index 541e37b..8df44b4 100644 --- a/src/components/encrypt/Add.tsx +++ b/src/components/encrypt/Add.tsx @@ -4,7 +4,7 @@ import { FileOutlined, FileTextOutlined } from '@ant-design/icons'; import AddText from './AddText'; import AddFile from './AddFile'; -const DEFAULT_VALUE = 'text'; +const DEFAULT_VALUE = 'file'; const Add: React.FC = () => { const [type, setType] = useState<'file' | 'text'>(DEFAULT_VALUE); diff --git a/src/components/encrypt/AddFile.tsx b/src/components/encrypt/AddFile.tsx index 38609b9..9e92eaf 100644 --- a/src/components/encrypt/AddFile.tsx +++ b/src/components/encrypt/AddFile.tsx @@ -22,7 +22,7 @@ const AddFile: React.FC = () => { const { addFile } = useContext(EncryptionContext); const onDrop = useCallback(acceptedFiles => { acceptedFiles.forEach(addFile); - }, []) + }, [addFile]) const {getRootProps, getInputProps} = useDropzone({ onDrop }); return ( diff --git a/src/contexts/Decryption.tsx b/src/contexts/Decryption.tsx index 2875593..e7232ea 100644 --- a/src/contexts/Decryption.tsx +++ b/src/contexts/Decryption.tsx @@ -6,14 +6,22 @@ import { createFile } from '../helpers/files'; interface DecryptionContextType { publicKey: string | undefined; + privateKey: string | undefined; createKey: (name: string, email: string) => void; files: {[id: string]: FileType}; addFile: (file: File) => Promise; deleteFile: (id: string) => void; } +const removeExtension = (name: string) => { + const parts = name.split('.'); + parts.pop(); + return parts.join('.'); +}; + const DecryptionContext = createContext({ publicKey: undefined, + privateKey: undefined, files: {}, createKey: async () => { throw new Error('Not using provider'); }, addFile: async () => { throw new Error('Not using provider'); }, @@ -22,7 +30,7 @@ const DecryptionContext = createContext({ const decrypt = async (privateKey: string, keys: string[], content: string) => { const armoredKeys = await Promise.all(keys.map(openpgp.key.readArmored)); - const message = openpgp.message.fromBinary(content); + const message = await openpgp.message.readArmored(content); const encrypted = await openpgp.decrypt({ message, privateKeys: [...(await openpgp.key.readArmored(privateKey)).keys], @@ -38,7 +46,7 @@ const decrypt = async (privateKey: string, keys: string[], content: string) => { const DecryptionProvider: React.FC = ({ children, }) => { - const { username, keys } = useContext(GithubContext); + const { keys } = useContext(GithubContext); const [privateKey, setPrivateKey] = useState(undefined); const [publicKey, setPublicKey] = useState(undefined); const [files, setFiles] = useState({}); @@ -76,24 +84,24 @@ const DecryptionProvider: React.FC = ({ const addFile = useCallback(async (file: File) => { if (!keys || !privateKey) return; - const addedFile = createFile(setFiles, file.name); + const addedFile = createFile(setFiles, removeExtension(file.name)); const reader = new FileReader() reader.onabort = addedFile.setFailed, reader.onerror = addedFile.setFailed, reader.onload = () => { - console.log('read'); addedFile.setContent( decrypt(privateKey, keys, reader.result as string), ); } - reader.readAsBinaryString(file); - }, [keys, username]); + reader.readAsText(file); + }, [keys, privateKey]); return ( ({ const encrypt = async (keys: string[], content: string) => { const armoredKeys = await Promise.all(keys.map(openpgp.key.readArmored)); - console.log(armoredKeys); const message = openpgp.message.fromText(content); const encrypted = await openpgp.encrypt({ message, diff --git a/src/index.tsx b/src/index.tsx index c4e0210..ae3c346 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -9,7 +9,6 @@ if ('serviceWorker' in navigator) { }); } - const root = document.createElement('div'); root.style.height = '100%'; document.body.appendChild(root); diff --git a/src/screens/Decrypt.tsx b/src/screens/Decrypt.tsx index 5f3ed52..70d58cf 100644 --- a/src/screens/Decrypt.tsx +++ b/src/screens/Decrypt.tsx @@ -1,5 +1,5 @@ import React, { useContext } from 'react'; -import { Divider, Button } from 'antd'; +import { Divider } from 'antd'; import FileList from '../components/FileList'; import Add from '../components/decrypt/AddFile'; import DecryptionContext from '../contexts/Decryption';