Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions apps/events/src/routes/playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,11 @@ const App = ({
encryptionPrivateKey: string;
encryptionPublicKey: string;
}) => {
const storeState = useSelector(store, (state) => state.context);
const spaces = storeState.spaces;
const updatesInFlight = storeState.updatesInFlight;
const {
createSpace,
listSpaces,
listInvitations,
invitations,
acceptInvitation,
subscribeToSpace,
inviteToSpace,
repo,
automergeHandle,
} = useGraphFramework();
const repo = useSelector(store, (state) => state.context.repo);
const spaces = useSelector(store, (state) => state.context.spaces);
const updatesInFlight = useSelector(store, (state) => state.context.updatesInFlight);
const { createSpace, listSpaces, listInvitations, invitations, acceptInvitation, subscribeToSpace, inviteToSpace } =
useGraphFramework();

return (
<>
Expand Down Expand Up @@ -131,7 +122,7 @@ const App = ({
})}
<h3>Updates</h3>
<RepoContext.Provider value={repo}>
<AutomergeApp url={automergeHandle.url} />
{space.automergeDocHandle && <AutomergeApp url={space.automergeDocHandle.url} />}
</RepoContext.Provider>
<h3>Last update clock: {space.lastUpdateClock}</h3>
<h3>Updates in flight</h3>
Expand Down
4 changes: 4 additions & 0 deletions packages/graph-framework-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@
"check:fix": "pnpm biome check --write src/*"
},
"devDependencies": {
"@automerge/automerge-repo": "^1.2.1",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only used for testing - initially my code creating an automerge ID out of our IDs was broken

"@types/uuid": "^10.0.0",
"uuid": "^11.0.3"
},
"peerDependencies": {
"uuid": "^11"
},
"dependencies": {
"bs58check": "^4.0.0"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not excited to have this dependency, but will be included anyway when we use Automerge since they use it internally

}
}
19 changes: 19 additions & 0 deletions packages/graph-framework-utils/src/automergeId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import bs58check from 'bs58check';

import { decodeBase58, encodeBase58 } from './internal/base58Utils.js';

/**
* Converts a raw Base58-encoded UUID into Base58Check
*/
export function idToAutomergeId(rawBase58Uuid: string, _versionByte = 0x00) {
const payload = decodeBase58(rawBase58Uuid);
return bs58check.encode(payload);
}

/**
* Converts a Base58Check-encoded UUID back to raw Base58
*/
export function automergeIdToId(base58CheckUuid: string) {
const versionedPayload = bs58check.decode(base58CheckUuid);
return encodeBase58(versionedPayload);
}
2 changes: 1 addition & 1 deletion packages/graph-framework-utils/src/base58.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const BASE58_ALLOWED_CHARS = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
import { BASE58_ALLOWED_CHARS } from './internal/base58Utils.js';

export type Base58 = string;

Expand Down
1 change: 1 addition & 0 deletions packages/graph-framework-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './automergeId.js';
export * from './base58.js';
export * from './generateId.js';
export * from './jsc.js';
Expand Down
47 changes: 47 additions & 0 deletions packages/graph-framework-utils/src/internal/base58Utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export const BASE58_ALLOWED_CHARS = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';

export function decodeBase58(str: string) {
let x = BigInt(0);
for (let i = 0; i < str.length; i++) {
const charIndex = BASE58_ALLOWED_CHARS.indexOf(str[i]);
if (charIndex < 0) {
throw new Error('Invalid Base58 character');
}
x = x * 58n + BigInt(charIndex);
}

const bytes: number[] = [];
while (x > 0) {
bytes.push(Number(x % 256n));
x = x >> 8n;
}

bytes.reverse();
// Pad to 16 bytes for a UUID
while (bytes.length < 16) {
bytes.unshift(0);
}

return new Uint8Array(bytes);
}

export function encodeBase58(data: Uint8Array) {
let x = BigInt(0);
for (const byte of data) {
x = (x << 8n) + BigInt(byte);
}

let encoded = '';
while (x > 0) {
const remainder = x % 58n;
x = x / 58n;
encoded = BASE58_ALLOWED_CHARS[Number(remainder)] + encoded;
}

// deal with leading zeros (0x00 bytes)
for (let i = 0; i < data.length && data[i] === 0; i++) {
encoded = `1${encoded}`;
}

return encoded;
}
23 changes: 23 additions & 0 deletions packages/graph-framework-utils/test/automergeId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isValidDocumentId } from '@automerge/automerge-repo';
import { describe, expect, it } from 'vitest';

import { automergeIdToId, idToAutomergeId } from '../src/automergeId';
import { generateId } from '../src/generateId';

describe('id <> automergeId conversion', () => {
it('converts an id to an automergeId and back', () => {
const id = generateId();
const automergeId = idToAutomergeId(id);
const id2 = automergeIdToId(automergeId);
expect(id).toBe(id2);
expect(isValidDocumentId(automergeId)).toBe(true);
});

it('throws an error for invalid Base58 characters', () => {
expect(() => idToAutomergeId('!@#$%^&*()')).toThrowError();
});

it('throws an error for invalid Base58Check strings', () => {
expect(() => automergeIdToId('11111111111111111111111111111111')).toThrowError();
});
});
100 changes: 41 additions & 59 deletions packages/graph-framework/src/core.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as automerge from '@automerge/automerge';
import { uuid } from '@automerge/automerge';
import { type AutomergeUrl, type DocHandle, Repo } from '@automerge/automerge-repo';
import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
import { useSelector as useSelectorStore } from '@xstate/store/react';
import { Effect, Exit } from 'effect';
Expand Down Expand Up @@ -28,8 +27,6 @@ import { assertExhaustive } from './assertExhaustive.js';
import type { SpaceStorageEntry } from './store.js';
import { store } from './store.js';

const hardcodedUrl = 'automerge:2JWupfYZBBm7s2NCy1VnvQa4Vdvf' as AutomergeUrl;

const decodeResponseMessage = Schema.decodeUnknownEither(ResponseMessage);

type Props = {
Expand Down Expand Up @@ -63,8 +60,6 @@ const GraphFrameworkContext = createContext<{
encryptionPublicKey: string;
};
}) => Promise<unknown>;
repo: Repo;
automergeHandle: DocHandle<unknown>;
}>({
invitations: [],
createSpace: async () => {},
Expand All @@ -73,59 +68,16 @@ const GraphFrameworkContext = createContext<{
acceptInvitation: async () => {},
subscribeToSpace: () => {},
inviteToSpace: async () => {},
// @ts-expect-error repo is always set
repo: undefined,
// @ts-expect-error automergeHandle is always set
automergeHandle: undefined,
});

export function GraphFramework({ children, accountId }: Props) {
const [websocketConnection, setWebsocketConnection] = useState<WebSocket>();
const [repo] = useState<Repo>(() => new Repo({}));
const [automergeHandle] = useState<DocHandle<unknown>>(() => repo.find(hardcodedUrl));
const spaces = useSelectorStore(store, (state) => state.context.spaces);
const invitations = useSelectorStore(store, (state) => state.context.invitations);
// Create a stable WebSocket connection that only depends on accountId
useEffect(() => {
const websocketConnection = new WebSocket(`ws://localhost:3030/?accountId=${accountId}`);

const docHandle = automergeHandle;
// set it to ready to interact with the document
docHandle.doneLoading();

docHandle.on('change', (result) => {
const lastLocalChange = automerge.getLastLocalChange(result.doc);
if (!lastLocalChange) {
return;
}

try {
const storeState = store.getSnapshot();
const space = storeState.context.spaces[0];

const ephemeralId = uuid();

const nonceAndCiphertext = encryptMessage({
message: lastLocalChange,
secretKey: hexToBytes(space.keys[0].key),
});

const messageToSend: RequestCreateUpdate = {
type: 'create-update',
ephemeralId,
update: nonceAndCiphertext,
spaceId: space.id,
};
websocketConnection.send(serialize(messageToSend));
} catch (error) {
console.error('Error sending message', error);
}
});

store.send({
type: 'setAutomergeDocumentId',
automergeDocumentId: docHandle.url.slice(10),
});
setWebsocketConnection(websocketConnection);

const onOpen = () => {
Expand All @@ -150,10 +102,9 @@ export function GraphFramework({ children, accountId }: Props) {
websocketConnection.removeEventListener('close', onClose);
websocketConnection.close();
};
}, [accountId, automergeHandle]); // Only recreate when accountId changes
}, [accountId]); // Only recreate when accountId changes

// Handle WebSocket messages in a separate effect
// biome-ignore lint/correctness/useExhaustiveDependencies: automergeHandle is a mutable object
useEffect(() => {
if (!websocketConnection) return;

Expand Down Expand Up @@ -189,7 +140,7 @@ export function GraphFramework({ children, accountId }: Props) {

const newState = state as SpaceState;

const storeState = store.getSnapshot();
let storeState = store.getSnapshot();

const keys = response.keyBoxes.map((keyBox) => {
const key = decryptKey({
Expand All @@ -210,6 +161,13 @@ export function GraphFramework({ children, accountId }: Props) {
keys,
});

storeState = store.getSnapshot();
const automergeDocHandle = storeState.context.spaces.find((s) => s.id === response.id)?.automergeDocHandle;
if (!automergeDocHandle) {
console.error('No automergeDocHandle found', response.id);
return;
}

if (response.updates) {
const updates = response.updates?.updates.map((update) => {
return decryptMessage({
Expand All @@ -219,11 +177,7 @@ export function GraphFramework({ children, accountId }: Props) {
});

for (const update of updates) {
if (!automergeHandle) {
return;
}

automergeHandle.update((existingDoc) => {
automergeDocHandle.update((existingDoc) => {
const [newDoc] = automerge.applyChanges(existingDoc, [update]);
return newDoc;
});
Expand All @@ -236,6 +190,36 @@ export function GraphFramework({ children, accountId }: Props) {
lastUpdateClock: response.updates?.lastUpdateClock,
});
}

automergeDocHandle.on('change', (result) => {
const lastLocalChange = automerge.getLastLocalChange(result.doc);
if (!lastLocalChange) {
return;
}

try {
const storeState = store.getSnapshot();
const space = storeState.context.spaces[0];

const ephemeralId = uuid();

const nonceAndCiphertext = encryptMessage({
message: lastLocalChange,
secretKey: hexToBytes(space.keys[0].key),
});

const messageToSend: RequestCreateUpdate = {
type: 'create-update',
ephemeralId,
update: nonceAndCiphertext,
spaceId: space.id,
};
websocketConnection.send(serialize(messageToSend));
} catch (error) {
console.error('Error sending message', error);
}
});

break;
}
case 'space-event': {
Expand Down Expand Up @@ -298,7 +282,7 @@ export function GraphFramework({ children, accountId }: Props) {
});
});

automergeHandle?.update((existingDoc) => {
space?.automergeDocHandle?.update((existingDoc) => {
const [newDoc] = automerge.applyChanges(existingDoc, automergeUpdates);
return newDoc;
});
Expand Down Expand Up @@ -488,8 +472,6 @@ export function GraphFramework({ children, accountId }: Props) {
acceptInvitation: acceptInvitationForContext,
subscribeToSpace,
inviteToSpace,
repo,
automergeHandle,
}}
>
{children}
Expand Down
Loading
Loading