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
30 changes: 12 additions & 18 deletions packages/hypergraph/src/space-events/apply-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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());
}

Expand All @@ -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');
Expand Down
6 changes: 0 additions & 6 deletions packages/hypergraph/src/space-events/create-invitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ type Params = {
previousEventHash: string;
invitee: {
accountId: string;
signaturePublicKey: string;
encryptionPublicKey: string;
};
};

Expand All @@ -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));
Expand Down
2 changes: 0 additions & 2 deletions packages/hypergraph/src/space-events/create-space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export const createSpace = ({ author }: Params): Effect.Effect<CreateSpaceEvent,
type: 'create-space' as const,
id: generateId(),
creatorAccountId: author.accountId,
creatorSignaturePublicKey: author.signaturePublicKey,
creatorEncryptionPublicKey: author.encryptionPublicKey,
};
const encodedTransaction = stringToUint8Array(canonicalize(transaction));
const signature = secp256k1
Expand Down
44 changes: 14 additions & 30 deletions packages/hypergraph/src/space-events/types.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import * as Schema from 'effect/Schema';

export const EventAuthor = Schema.Struct({
accountId: Schema.String,
// must be validated if it belongs to the accountId before being used
// Note: could be removed, but also might be useful to keep around in case accounts rotate their keys
publicKey: Schema.String,
signature: Schema.String,
});

export type EventAuthor = Schema.Schema.Type<typeof Author>;

export const SpaceMember = Schema.Struct({
accountId: Schema.String,
signaturePublicKey: Schema.String,
encryptionPublicKey: Schema.String,
role: Schema.Union(Schema.Literal('admin'), Schema.Literal('member')),
});

export type SpaceMember = Schema.Schema.Type<typeof SpaceMember>;

export const SpaceInvitation = Schema.Struct({
inviteeAccountId: Schema.String,
signaturePublicKey: Schema.String,
encryptionPublicKey: Schema.String,
});

export type SpaceInvitation = Schema.Schema.Type<typeof SpaceInvitation>;
Expand All @@ -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<typeof CreateSpaceEvent>;
Expand All @@ -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<typeof DeleteSpaceEvent>;
Expand All @@ -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<typeof CreateInvitationEvent>;
Expand All @@ -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<typeof AcceptInvitationEvent>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
});
Expand Down
4 changes: 1 addition & 3 deletions packages/hypergraph/test/space-events/create-space.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
});
Expand Down
4 changes: 1 addition & 3 deletions packages/hypergraph/test/space-events/delete-space.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
});
Expand Down
Loading