From 28b31d0a4c3b5bed3ce4033b9206c0a993ba714c Mon Sep 17 00:00:00 2001 From: Nik Graf Date: Tue, 28 Jan 2025 15:50:07 +0100 Subject: [PATCH] use accountId as primary identifier --- .../src/space-events/apply-event.ts | 30 +++++-------- .../src/space-events/create-invitation.ts | 6 --- .../src/space-events/create-space.ts | 2 - packages/hypergraph/src/space-events/types.ts | 44 ++++++------------- .../space-events/accept-invitation.test.ts | 8 +--- .../space-events/create-invitation.test.ts | 6 +-- .../test/space-events/create-space.test.ts | 4 +- .../test/space-events/delete-space.test.ts | 4 +- 8 files changed, 31 insertions(+), 73 deletions(-) diff --git a/packages/hypergraph/src/space-events/apply-event.ts b/packages/hypergraph/src/space-events/apply-event.ts index 08fc50d3..5214c404 100644 --- a/packages/hypergraph/src/space-events/apply-event.ts +++ b/packages/hypergraph/src/space-events/apply-event.ts @@ -55,16 +55,14 @@ export const applyEvent = ({ } let id = ''; - let members: { [signaturePublicKey: string]: SpaceMember } = {}; - let removedMembers: { [signaturePublicKey: string]: SpaceMember } = {}; + let members: { [accountId: string]: SpaceMember } = {}; + let removedMembers: { [accountId: string]: SpaceMember } = {}; let invitations: { [id: string]: SpaceInvitation } = {}; if (event.transaction.type === 'create-space') { id = event.transaction.id; - members[event.transaction.creatorSignaturePublicKey] = { + members[event.transaction.creatorAccountId] = { accountId: event.transaction.creatorAccountId, - signaturePublicKey: event.transaction.creatorSignaturePublicKey, - encryptionPublicKey: event.transaction.creatorEncryptionPublicKey, role: 'admin', }; } else if (state !== undefined) { @@ -75,32 +73,30 @@ export const applyEvent = ({ if (event.transaction.type === 'accept-invitation') { // is already a member - if (members[event.author.publicKey] !== undefined) { + if (members[event.author.accountId] !== undefined) { return Effect.fail(new InvalidEventError()); } // find the invitation const result = Object.entries(invitations).find( - ([, invitation]) => invitation.signaturePublicKey === event.author.publicKey, + ([, invitation]) => invitation.inviteeAccountId === event.author.accountId, ); if (!result) { return Effect.fail(new InvalidEventError()); } const [id, invitation] = result; - members[event.author.publicKey] = { - accountId: event.author.accountId, - signaturePublicKey: event.author.publicKey, - encryptionPublicKey: invitation.encryptionPublicKey, + members[invitation.inviteeAccountId] = { + accountId: invitation.inviteeAccountId, role: 'member', }; delete invitations[id]; - if (removedMembers[event.author.publicKey] !== undefined) { - delete removedMembers[event.author.publicKey]; + if (removedMembers[event.author.accountId] !== undefined) { + delete removedMembers[event.author.accountId]; } } else { // check if the author is an admin - if (members[event.author.publicKey]?.role !== 'admin') { + if (members[event.author.accountId]?.role !== 'admin') { return Effect.fail(new InvalidEventError()); } @@ -109,19 +105,17 @@ export const applyEvent = ({ members = {}; invitations = {}; } else if (event.transaction.type === 'create-invitation') { - if (members[event.transaction.signaturePublicKey] !== undefined) { + if (members[event.transaction.inviteeAccountId] !== undefined) { return Effect.fail(new InvalidEventError()); } for (const invitation of Object.values(invitations)) { - if (invitation.signaturePublicKey === event.transaction.signaturePublicKey) { + if (invitation.inviteeAccountId === event.transaction.inviteeAccountId) { return Effect.fail(new InvalidEventError()); } } invitations[event.transaction.id] = { inviteeAccountId: event.transaction.inviteeAccountId, - signaturePublicKey: event.transaction.signaturePublicKey, - encryptionPublicKey: event.transaction.encryptionPublicKey, }; } else { throw new Error('State is required for all events except create-space'); diff --git a/packages/hypergraph/src/space-events/create-invitation.ts b/packages/hypergraph/src/space-events/create-invitation.ts index bfbf2d35..288f65c4 100644 --- a/packages/hypergraph/src/space-events/create-invitation.ts +++ b/packages/hypergraph/src/space-events/create-invitation.ts @@ -9,8 +9,6 @@ type Params = { previousEventHash: string; invitee: { accountId: string; - signaturePublicKey: string; - encryptionPublicKey: string; }; }; @@ -22,11 +20,7 @@ export const createInvitation = ({ const transaction = { id: generateId(), type: 'create-invitation' as const, - ciphertext: '', - nonce: '', inviteeAccountId: invitee.accountId, - signaturePublicKey: invitee.signaturePublicKey, - encryptionPublicKey: invitee.encryptionPublicKey, previousEventHash, }; const encodedTransaction = stringToUint8Array(canonicalize(transaction)); diff --git a/packages/hypergraph/src/space-events/create-space.ts b/packages/hypergraph/src/space-events/create-space.ts index 6892f258..f735b127 100644 --- a/packages/hypergraph/src/space-events/create-space.ts +++ b/packages/hypergraph/src/space-events/create-space.ts @@ -13,8 +13,6 @@ export const createSpace = ({ author }: Params): Effect.Effect; + export const SpaceMember = Schema.Struct({ accountId: Schema.String, - signaturePublicKey: Schema.String, - encryptionPublicKey: Schema.String, role: Schema.Union(Schema.Literal('admin'), Schema.Literal('member')), }); @@ -11,8 +19,6 @@ export type SpaceMember = Schema.Schema.Type; export const SpaceInvitation = Schema.Struct({ inviteeAccountId: Schema.String, - signaturePublicKey: Schema.String, - encryptionPublicKey: Schema.String, }); export type SpaceInvitation = Schema.Schema.Type; @@ -32,14 +38,8 @@ export const CreateSpaceEvent = Schema.Struct({ type: Schema.Literal('create-space'), id: Schema.String, creatorAccountId: Schema.String, - creatorSignaturePublicKey: Schema.String, - creatorEncryptionPublicKey: Schema.String, - }), - author: Schema.Struct({ - accountId: Schema.String, - publicKey: Schema.String, - signature: Schema.String, }), + author: EventAuthor, }); export type CreateSpaceEvent = Schema.Schema.Type; @@ -50,11 +50,7 @@ export const DeleteSpaceEvent = Schema.Struct({ id: Schema.String, previousEventHash: Schema.String, }), - author: Schema.Struct({ - accountId: Schema.String, - publicKey: Schema.String, - signature: Schema.String, - }), + author: EventAuthor, }); export type DeleteSpaceEvent = Schema.Schema.Type; @@ -63,18 +59,10 @@ export const CreateInvitationEvent = Schema.Struct({ transaction: Schema.Struct({ type: Schema.Literal('create-invitation'), id: Schema.String, - ciphertext: Schema.String, - nonce: Schema.String, inviteeAccountId: Schema.String, - signaturePublicKey: Schema.String, - encryptionPublicKey: Schema.String, previousEventHash: Schema.String, }), - author: Schema.Struct({ - accountId: Schema.String, - publicKey: Schema.String, - signature: Schema.String, - }), + author: EventAuthor, }); export type CreateInvitationEvent = Schema.Schema.Type; @@ -85,11 +73,7 @@ export const AcceptInvitationEvent = Schema.Struct({ type: Schema.Literal('accept-invitation'), previousEventHash: Schema.String, }), - author: Schema.Struct({ - accountId: Schema.String, - publicKey: Schema.String, - signature: Schema.String, - }), + author: EventAuthor, }); export type AcceptInvitationEvent = Schema.Schema.Type; diff --git a/packages/hypergraph/test/space-events/accept-invitation.test.ts b/packages/hypergraph/test/space-events/accept-invitation.test.ts index b8730e3a..28182404 100644 --- a/packages/hypergraph/test/space-events/accept-invitation.test.ts +++ b/packages/hypergraph/test/space-events/accept-invitation.test.ts @@ -46,16 +46,12 @@ it('should accept an invitation', async () => { expect(state3.id).toBeTypeOf('string'); expect(state3.invitations).toEqual({}); expect(state3.members).toEqual({ - [author.signaturePublicKey]: { + [author.accountId]: { accountId: author.accountId, - signaturePublicKey: author.signaturePublicKey, - encryptionPublicKey: author.encryptionPublicKey, role: 'admin', }, - [invitee.signaturePublicKey]: { + [invitee.accountId]: { accountId: invitee.accountId, - signaturePublicKey: invitee.signaturePublicKey, - encryptionPublicKey: invitee.encryptionPublicKey, role: 'member', }, }); diff --git a/packages/hypergraph/test/space-events/create-invitation.test.ts b/packages/hypergraph/test/space-events/create-invitation.test.ts index 93224e7a..d3460497 100644 --- a/packages/hypergraph/test/space-events/create-invitation.test.ts +++ b/packages/hypergraph/test/space-events/create-invitation.test.ts @@ -50,15 +50,11 @@ it('should create an invitation', async () => { expect(state2.invitations).toEqual({ [spaceEvent2.transaction.id]: { inviteeAccountId: invitee.accountId, - signaturePublicKey: '0x03bf5d2a1badf15387b08a007d1a9a13a9bfd6e1c56f681e251514d9ba10b57462', - encryptionPublicKey: 'encryption', }, }); expect(state2.members).toEqual({ - [author.signaturePublicKey]: { + [author.accountId]: { accountId: author.accountId, - signaturePublicKey: author.signaturePublicKey, - encryptionPublicKey: author.encryptionPublicKey, role: 'admin', }, }); diff --git a/packages/hypergraph/test/space-events/create-space.test.ts b/packages/hypergraph/test/space-events/create-space.test.ts index dcd91cc6..dc7fff30 100644 --- a/packages/hypergraph/test/space-events/create-space.test.ts +++ b/packages/hypergraph/test/space-events/create-space.test.ts @@ -22,10 +22,8 @@ it('should create a space state', async () => { expect(state.id).toBeTypeOf('string'); expect(state.invitations).toEqual({}); expect(state.members).toEqual({ - [author.signaturePublicKey]: { + [author.accountId]: { accountId: author.accountId, - signaturePublicKey: author.signaturePublicKey, - encryptionPublicKey: author.encryptionPublicKey, role: 'admin', }, }); diff --git a/packages/hypergraph/test/space-events/delete-space.test.ts b/packages/hypergraph/test/space-events/delete-space.test.ts index b3453710..0ccea828 100644 --- a/packages/hypergraph/test/space-events/delete-space.test.ts +++ b/packages/hypergraph/test/space-events/delete-space.test.ts @@ -36,10 +36,8 @@ it('should delete a space', async () => { expect(state.invitations).toEqual({}); expect(state.members).toEqual({}); expect(state.removedMembers).toEqual({ - [author.signaturePublicKey]: { + [author.accountId]: { accountId: author.accountId, - signaturePublicKey: author.signaturePublicKey, - encryptionPublicKey: author.encryptionPublicKey, role: 'admin', }, });