-
Notifications
You must be signed in to change notification settings - Fork 3
Add support for leaving comments in reflections #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ajweeks
wants to merge
2
commits into
Greenheart:main
Choose a base branch
from
ajweeks:feature/add-comment-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<script lang="ts" module> | ||
import type { HTMLAttributes } from 'svelte/elements' | ||
</script> | ||
|
||
<script lang="ts"> | ||
interface Props extends HTMLAttributes<HTMLAnchorElement> { | ||
textData: string | ||
} | ||
|
||
let { textData, class: className }: Props = $props() | ||
</script> | ||
|
||
<p class={['', className]}> | ||
{textData} | ||
</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<script lang="ts" module> | ||
import type { CommentState } from '$lib/types' | ||
</script> | ||
|
||
<script lang="ts"> | ||
type Props = { | ||
commentState: CommentState | ||
} | ||
|
||
let { commentState = $bindable() }: Props = $props() | ||
</script> | ||
|
||
<textarea rows="3" cols="50" class={['']} style="color: black; outline: black; outline-width: 1px; outline-style: dotted;" | ||
bind:value={commentState}> | ||
</textarea> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import type { UserKey } from '$lib/types' | ||
import { decodeInt32, encodeInt32 } from '$lib/utils' | ||
|
||
export const ITERATIONS = 2e6 | ||
|
||
export async function deriveKey( | ||
salt: Uint8Array, | ||
password: string, | ||
iterations: number = ITERATIONS, | ||
keyUsages: Iterable<KeyUsage> = ['encrypt', 'decrypt'], | ||
): Promise<UserKey> { | ||
const encoder = new TextEncoder() | ||
const baseKey = await crypto.subtle.importKey( | ||
'raw', | ||
encoder.encode(password), | ||
'PBKDF2', | ||
false, | ||
['deriveKey'], | ||
) | ||
return { | ||
key: await crypto.subtle.deriveKey( | ||
{ name: 'PBKDF2', salt, iterations, hash: 'SHA-256' }, | ||
baseKey, | ||
{ name: 'AES-GCM', length: 256 }, | ||
false, | ||
keyUsages, | ||
), | ||
salt, | ||
} | ||
} | ||
|
||
export async function deriveKeyFromData( | ||
data: Uint8Array, | ||
password: string, | ||
keyUsages: Iterable<KeyUsage> = ['encrypt', 'decrypt'], | ||
) { | ||
const salt = data.slice(0, 32) | ||
const iterations = data.slice(32 + 16, 32 + 16 + 4) | ||
|
||
return deriveKey(salt, password, decodeInt32(iterations), keyUsages) | ||
} | ||
|
||
/** | ||
* Encrypt a string and turn it into an encrypted payload. | ||
* | ||
* @param content The data to encrypt | ||
* @param key The key used to encrypt the content. | ||
* @param iterations The number of iterations to derive the key from the password. | ||
*/ | ||
export async function getEncryptedPayload( | ||
content: Uint8Array, | ||
key: UserKey, | ||
iterations: number = ITERATIONS, | ||
) { | ||
const salt = key.salt | ||
const iv = crypto.getRandomValues(new Uint8Array(16)) | ||
const iterationsBytes = encodeInt32(iterations) | ||
const ciphertext = new Uint8Array( | ||
await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key.key, content), | ||
) | ||
|
||
const totalLength = salt.length + iv.length + iterationsBytes.length + ciphertext.length | ||
const mergedData = new Uint8Array(totalLength) | ||
mergedData.set(salt) | ||
mergedData.set(iv, salt.length) | ||
mergedData.set(iterationsBytes, salt.length + iv.length) | ||
mergedData.set(ciphertext, salt.length + iv.length + iterationsBytes.length) | ||
|
||
return mergedData | ||
} | ||
|
||
/** | ||
* Decrypt a payload and return the contents. | ||
* | ||
* @param bytes The payload to decrypt. | ||
* @param key The key used for decryption. | ||
*/ | ||
export async function getDecryptedPayload(bytes: Uint8Array, key: UserKey) { | ||
const iv = bytes.slice(32, 32 + 16) | ||
const ciphertext = bytes.slice(32 + 16 + 4) | ||
|
||
const content = new Uint8Array( | ||
await window.crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key.key, ciphertext), | ||
) | ||
if (!content) throw new Error('Malformed content') | ||
|
||
return content | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { deflate } from 'pako' | ||
import { base64url } from 'rfc4648' | ||
|
||
import type { ProtocolVersion, ReflectionEntry } from '$lib/types' | ||
import { encodeInt32, encodeString, mergeTypedArrays } from '$lib/utils' | ||
import { PROTOCOL_VERSION } from './protocol' | ||
|
||
function encodeTime(date: Date) { | ||
const timestamp = date.getTime() / 1000 | ||
return encodeInt32(timestamp) | ||
} | ||
|
||
function encodeEntry(entry: ReflectionEntry) { | ||
return mergeTypedArrays( | ||
encodeTime(entry.time), | ||
new Uint8Array(encodeEntryData(entry.data)), | ||
encodeInt32(entry.comment != null ? entry.comment.length : 0), | ||
encodeString(entry.comment != null ? entry.comment : 'null')) | ||
ajweeks marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
export function encodeReflectionEntries(reflections: ReflectionEntry[]) { | ||
console.log('encodeReflectionEntries', reflections) | ||
const encodedEntries = reflections.map(encodeEntry) | ||
const data = mergeTypedArrays(encodeInt32(reflections.length), ...encodedEntries) | ||
return deflate(data, { level: 9 }) | ||
} | ||
|
||
/** | ||
* Encode every pair of numbers into a one byte to compress data. | ||
*/ | ||
function encodeEntryData(data: ReflectionEntry['data']) { | ||
return [data[0] << 4 | data[1], | ||
data[2] << 4 | data[3], | ||
data[4] << 4 | data[5], | ||
data[6] << 4 | data[7]] | ||
|
||
//const bin = data.map((number) => number.toString(2).padStart(4, '0')) | ||
//return [bin[0] + bin[1], bin[2] + bin[3], bin[4] + bin[5], bin[6] + bin[7]].map((n) => | ||
// parseInt(n, 2), | ||
//) | ||
ajweeks marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
|
||
const formatHeader = ({ | ||
encrypted, | ||
protocolVersion, | ||
}: { | ||
encrypted: boolean | ||
protocolVersion: ProtocolVersion | ||
}) => `${encrypted ? '1' : '0'}e${protocolVersion}p` | ||
|
||
/** | ||
* Generate a URI fragment (hash) representing user data. | ||
* Also adds a header to make it possible to know how to parse different links. | ||
* For example the header "0e2p" means "0e" = no encryption, and "2p" = protocol version 2. | ||
* Similarly "1e2p" means "1e" = the data is encrypted, and "2p" = protocol version 2. | ||
*/ | ||
export const formatLink = ({ | ||
data, | ||
encrypted = false, | ||
}: { | ||
data: Uint8Array | ||
encrypted?: boolean | ||
}) => formatHeader({ encrypted, protocolVersion: PROTOCOL_VERSION }) + base64url.stringify(data) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.