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 { addFile } = useContext(DecryptionContext);
|
||||||
const onDrop = useCallback(acceptedFiles => {
|
const onDrop = useCallback(acceptedFiles => {
|
||||||
acceptedFiles.forEach(addFile);
|
acceptedFiles.forEach(addFile);
|
||||||
}, [])
|
}, [addFile])
|
||||||
const {getRootProps, getInputProps} = useDropzone({ onDrop });
|
const {getRootProps, getInputProps} = useDropzone({ onDrop });
|
||||||
return (
|
return (
|
||||||
<DropWrapper {...getRootProps()}>
|
<DropWrapper {...getRootProps()}>
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { FileOutlined, FileTextOutlined } from '@ant-design/icons';
|
|||||||
import AddText from './AddText';
|
import AddText from './AddText';
|
||||||
import AddFile from './AddFile';
|
import AddFile from './AddFile';
|
||||||
|
|
||||||
const DEFAULT_VALUE = 'text';
|
const DEFAULT_VALUE = 'file';
|
||||||
|
|
||||||
const Add: React.FC = () => {
|
const Add: React.FC = () => {
|
||||||
const [type, setType] = useState<'file' | 'text'>(DEFAULT_VALUE);
|
const [type, setType] = useState<'file' | 'text'>(DEFAULT_VALUE);
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ const AddFile: React.FC = () => {
|
|||||||
const { addFile } = useContext(EncryptionContext);
|
const { addFile } = useContext(EncryptionContext);
|
||||||
const onDrop = useCallback(acceptedFiles => {
|
const onDrop = useCallback(acceptedFiles => {
|
||||||
acceptedFiles.forEach(addFile);
|
acceptedFiles.forEach(addFile);
|
||||||
}, [])
|
}, [addFile])
|
||||||
const {getRootProps, getInputProps} = useDropzone({ onDrop });
|
const {getRootProps, getInputProps} = useDropzone({ onDrop });
|
||||||
return (
|
return (
|
||||||
<DropWrapper {...getRootProps()}>
|
<DropWrapper {...getRootProps()}>
|
||||||
|
|||||||
@@ -6,14 +6,22 @@ import { createFile } from '../helpers/files';
|
|||||||
|
|
||||||
interface DecryptionContextType {
|
interface DecryptionContextType {
|
||||||
publicKey: string | undefined;
|
publicKey: string | undefined;
|
||||||
|
privateKey: string | undefined;
|
||||||
createKey: (name: string, email: string) => void;
|
createKey: (name: string, email: string) => void;
|
||||||
files: {[id: string]: FileType};
|
files: {[id: string]: FileType};
|
||||||
addFile: (file: File) => Promise<void>;
|
addFile: (file: File) => Promise<void>;
|
||||||
deleteFile: (id: string) => void;
|
deleteFile: (id: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const removeExtension = (name: string) => {
|
||||||
|
const parts = name.split('.');
|
||||||
|
parts.pop();
|
||||||
|
return parts.join('.');
|
||||||
|
};
|
||||||
|
|
||||||
const DecryptionContext = createContext<DecryptionContextType>({
|
const DecryptionContext = createContext<DecryptionContextType>({
|
||||||
publicKey: undefined,
|
publicKey: undefined,
|
||||||
|
privateKey: undefined,
|
||||||
files: {},
|
files: {},
|
||||||
createKey: async () => { throw new Error('Not using provider'); },
|
createKey: async () => { throw new Error('Not using provider'); },
|
||||||
addFile: 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 decrypt = async (privateKey: string, keys: string[], content: string) => {
|
||||||
const armoredKeys = await Promise.all(keys.map(openpgp.key.readArmored));
|
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({
|
const encrypted = await openpgp.decrypt({
|
||||||
message,
|
message,
|
||||||
privateKeys: [...(await openpgp.key.readArmored(privateKey)).keys],
|
privateKeys: [...(await openpgp.key.readArmored(privateKey)).keys],
|
||||||
@@ -38,7 +46,7 @@ const decrypt = async (privateKey: string, keys: string[], content: string) => {
|
|||||||
const DecryptionProvider: React.FC = ({
|
const DecryptionProvider: React.FC = ({
|
||||||
children,
|
children,
|
||||||
}) => {
|
}) => {
|
||||||
const { username, keys } = useContext(GithubContext);
|
const { keys } = useContext(GithubContext);
|
||||||
const [privateKey, setPrivateKey] = useState<string | undefined>(undefined);
|
const [privateKey, setPrivateKey] = useState<string | undefined>(undefined);
|
||||||
const [publicKey, setPublicKey] = useState<string | undefined>(undefined);
|
const [publicKey, setPublicKey] = useState<string | undefined>(undefined);
|
||||||
const [files, setFiles] = useState<DecryptionContextType['files']>({});
|
const [files, setFiles] = useState<DecryptionContextType['files']>({});
|
||||||
@@ -76,24 +84,24 @@ const DecryptionProvider: React.FC = ({
|
|||||||
|
|
||||||
const addFile = useCallback(async (file: File) => {
|
const addFile = useCallback(async (file: File) => {
|
||||||
if (!keys || !privateKey) return;
|
if (!keys || !privateKey) return;
|
||||||
const addedFile = createFile(setFiles, file.name);
|
const addedFile = createFile(setFiles, removeExtension(file.name));
|
||||||
const reader = new FileReader()
|
const reader = new FileReader()
|
||||||
|
|
||||||
reader.onabort = addedFile.setFailed,
|
reader.onabort = addedFile.setFailed,
|
||||||
reader.onerror = addedFile.setFailed,
|
reader.onerror = addedFile.setFailed,
|
||||||
reader.onload = () => {
|
reader.onload = () => {
|
||||||
console.log('read');
|
|
||||||
addedFile.setContent(
|
addedFile.setContent(
|
||||||
decrypt(privateKey, keys, reader.result as string),
|
decrypt(privateKey, keys, reader.result as string),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
reader.readAsBinaryString(file);
|
reader.readAsText(file);
|
||||||
}, [keys, username]);
|
}, [keys, privateKey]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DecryptionContext.Provider
|
<DecryptionContext.Provider
|
||||||
value={{
|
value={{
|
||||||
publicKey,
|
publicKey,
|
||||||
|
privateKey,
|
||||||
createKey,
|
createKey,
|
||||||
files,
|
files,
|
||||||
addFile,
|
addFile,
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ const EncryptionContext = createContext<EncryptionContextType>({
|
|||||||
|
|
||||||
const encrypt = async (keys: string[], content: string) => {
|
const encrypt = async (keys: string[], content: string) => {
|
||||||
const armoredKeys = await Promise.all(keys.map(openpgp.key.readArmored));
|
const armoredKeys = await Promise.all(keys.map(openpgp.key.readArmored));
|
||||||
console.log(armoredKeys);
|
|
||||||
const message = openpgp.message.fromText(content);
|
const message = openpgp.message.fromText(content);
|
||||||
const encrypted = await openpgp.encrypt({
|
const encrypted = await openpgp.encrypt({
|
||||||
message,
|
message,
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ if ('serviceWorker' in navigator) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const root = document.createElement('div');
|
const root = document.createElement('div');
|
||||||
root.style.height = '100%';
|
root.style.height = '100%';
|
||||||
document.body.appendChild(root);
|
document.body.appendChild(root);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import React, { useContext } from 'react';
|
import React, { useContext } from 'react';
|
||||||
import { Divider, Button } from 'antd';
|
import { Divider } from 'antd';
|
||||||
import FileList from '../components/FileList';
|
import FileList from '../components/FileList';
|
||||||
import Add from '../components/decrypt/AddFile';
|
import Add from '../components/decrypt/AddFile';
|
||||||
import DecryptionContext from '../contexts/Decryption';
|
import DecryptionContext from '../contexts/Decryption';
|
||||||
|
|||||||
Reference in New Issue
Block a user