Skip to content

Commit

Permalink
Clean up console logs and code a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
CatEars committed Dec 17, 2019
1 parent 875e624 commit ebdf10f
Showing 1 changed file with 40 additions and 61 deletions.
101 changes: 40 additions & 61 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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,
}
}

Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -166,7 +146,6 @@ const App = () => {
} else if (creatingNoteId) {
app = <LinkLoading />
} else if (showLink) {
console.log('CREATED NOTE KEY IS:', createdNoteKey)
app = <Link noteId={createdNoteId} noteKey={createdNoteKey} />
} else {
app = <Form onCreateNote={onCreateNote} />
Expand Down

0 comments on commit ebdf10f

Please sign in to comment.