mirror of
https://github.com/morten-olsen/parcel.git
synced 2026-02-08 01:36:24 +01:00
Fix decrypt
This commit is contained in:
@@ -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 (
|
||||
<DropWrapper {...getRootProps()}>
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 (
|
||||
<DropWrapper {...getRootProps()}>
|
||||
|
||||
@@ -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<void>;
|
||||
deleteFile: (id: string) => void;
|
||||
}
|
||||
|
||||
const removeExtension = (name: string) => {
|
||||
const parts = name.split('.');
|
||||
parts.pop();
|
||||
return parts.join('.');
|
||||
};
|
||||
|
||||
const DecryptionContext = createContext<DecryptionContextType>({
|
||||
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<DecryptionContextType>({
|
||||
|
||||
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<string | undefined>(undefined);
|
||||
const [publicKey, setPublicKey] = useState<string | undefined>(undefined);
|
||||
const [files, setFiles] = useState<DecryptionContextType['files']>({});
|
||||
@@ -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 (
|
||||
<DecryptionContext.Provider
|
||||
value={{
|
||||
publicKey,
|
||||
privateKey,
|
||||
createKey,
|
||||
files,
|
||||
addFile,
|
||||
|
||||
@@ -20,7 +20,6 @@ const EncryptionContext = createContext<EncryptionContextType>({
|
||||
|
||||
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,
|
||||
|
||||
@@ -9,7 +9,6 @@ if ('serviceWorker' in navigator) {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const root = document.createElement('div');
|
||||
root.style.height = '100%';
|
||||
document.body.appendChild(root);
|
||||
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user