From ebdf10fd93a7ffdf1df029410c06dee075ccbb7d Mon Sep 17 00:00:00 2001 From: Henke Adolfsson Date: Tue, 17 Dec 2019 19:42:39 +0100 Subject: [PATCH] Clean up console logs and code a little bit --- frontend/src/App.tsx | 101 +++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 61 deletions(-) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index a9e4ff2..9a65785 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -14,8 +14,6 @@ import { generateKey, } from './secrets' -const mock = false - const apiPost = (path: string, body: any) => { const url = `${window.location.origin}${path}` return fetch(url, { @@ -39,65 +37,43 @@ const createNote = async ( key: CryptoKey, burnDate: number ) => { - if (mock) { - return '744fa5c0-5511-420d-abd3-eaedf7e29e2a' - } else { - const { encryptedMessage, IV, fingerprint } = await encryptMessage( - message, - key - ) - const note = { - allowedReads: 1, - encryptedMessage: Array.prototype.slice.call( - new Uint8Array(encryptedMessage) - ), - fingerprint: Array.prototype.slice.call( - new Uint8Array(fingerprint) - ), - IV: Array.prototype.slice.call(IV), - burnDate, - } - console.log('NOTE') - console.log(note) - console.log('Creating note') - const response = await apiPost('/api/note', note) - console.log('repsponse!') - const { ID } = await response.json() - console.log('ID=' + ID) - return ID + const { encryptedMessage, IV, fingerprint } = await encryptMessage( + message, + key + ) + const note = { + allowedReads: 1, + encryptedMessage: Array.prototype.slice.call( + new Uint8Array(encryptedMessage) + ), + fingerprint: Array.prototype.slice.call(new Uint8Array(fingerprint)), + IV: Array.prototype.slice.call(IV), + burnDate, } + console.log('POSTing note to API') + const response = await apiPost('/api/note', note) + const { ID } = await response.json() + return ID } const getNote = async (id: string) => { - if (mock) { - return { - message: 'Test message', - burnDate: Date.now() + 10000, - } - } else { - console.log('ID:' + id) - const response = await apiGet(`/api/note/${id}`) - if (!response.ok) { - throw new Error(`Getting ${id} did not work!`) - } - console.log('response:') - console.log(response) - const note = await response.json() - console.log('note') - console.log(note) - note.encryptedMessage = new Uint8Array(note.encryptedMessage).buffer - note.fingerprint = new Uint8Array(note.fingerprint).buffer - note.IV = new Uint8Array(note.IV) - console.log('NOTE') - console.log(note) - console.log('HASH:', window.location.hash.substr(1)) - const key = await urldecodeKey(window.location.hash.substr(1)) - console.log('KEY:', key) - const decryptedMessage = await decryptMessage(key, note) - return { - message: decryptedMessage, - burnDate: note.burnDate, - } + const response = await apiGet(`/api/note/${id}`) + if (!response.ok) { + throw new Error(`Getting ${id} did not work!`) + } + const note = await response.json() + + note.encryptedMessage = new Uint8Array(note.encryptedMessage).buffer + note.fingerprint = new Uint8Array(note.fingerprint).buffer + note.IV = new Uint8Array(note.IV) + + const urlencodedKey = window.location.hash.substr(1) + const key = await urldecodeKey(urlencodedKey) + const decryptedMessage = await decryptMessage(key, note) + + return { + message: decryptedMessage, + burnDate: note.burnDate, } } @@ -108,15 +84,19 @@ const App = () => { const [createdNoteKey, setCreatedNoteKey] = useState('') const [noteLoading, setNoteLoading] = useState(false) const [note, setNote] = useState({ - message: 'hello world', + message: '', burnDate: Date.now() + 5000, }) const [noteFailed, setNoteFailed] = useState(false) const onKeyGenerated = (key: CryptoKey) => { + console.log('Key has been generated') urlencodeKey(key) .then(encodedString => { - console.log('encodedString:', encodedString) + console.log( + 'Encoded key length is:', + (encodedString || '').length + ) setCreatedNoteKey(encodedString) }) .catch(console.error) @@ -126,11 +106,11 @@ const App = () => { setCreatedNoteId('1234') generateKey() .then(key => { - console.log('KEU', key) onKeyGenerated(key) return createNote(message, key, burnDate) }) .then(id => { + console.log('Note generated with ID', id) setCreatedNoteId(id) }) .catch(console.error) @@ -166,7 +146,6 @@ const App = () => { } else if (creatingNoteId) { app = } else if (showLink) { - console.log('CREATED NOTE KEY IS:', createdNoteKey) app = } else { app =