Skip to content

Commit 84a84ab

Browse files
authored
use accountId as primary identifier (#130)
1 parent 73ef4bd commit 84a84ab

File tree

8 files changed

+31
-73
lines changed

8 files changed

+31
-73
lines changed

packages/hypergraph/src/space-events/apply-event.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,14 @@ export const applyEvent = ({
5555
}
5656

5757
let id = '';
58-
let members: { [signaturePublicKey: string]: SpaceMember } = {};
59-
let removedMembers: { [signaturePublicKey: string]: SpaceMember } = {};
58+
let members: { [accountId: string]: SpaceMember } = {};
59+
let removedMembers: { [accountId: string]: SpaceMember } = {};
6060
let invitations: { [id: string]: SpaceInvitation } = {};
6161

6262
if (event.transaction.type === 'create-space') {
6363
id = event.transaction.id;
64-
members[event.transaction.creatorSignaturePublicKey] = {
64+
members[event.transaction.creatorAccountId] = {
6565
accountId: event.transaction.creatorAccountId,
66-
signaturePublicKey: event.transaction.creatorSignaturePublicKey,
67-
encryptionPublicKey: event.transaction.creatorEncryptionPublicKey,
6866
role: 'admin',
6967
};
7068
} else if (state !== undefined) {
@@ -75,32 +73,30 @@ export const applyEvent = ({
7573

7674
if (event.transaction.type === 'accept-invitation') {
7775
// is already a member
78-
if (members[event.author.publicKey] !== undefined) {
76+
if (members[event.author.accountId] !== undefined) {
7977
return Effect.fail(new InvalidEventError());
8078
}
8179

8280
// find the invitation
8381
const result = Object.entries(invitations).find(
84-
([, invitation]) => invitation.signaturePublicKey === event.author.publicKey,
82+
([, invitation]) => invitation.inviteeAccountId === event.author.accountId,
8583
);
8684
if (!result) {
8785
return Effect.fail(new InvalidEventError());
8886
}
8987
const [id, invitation] = result;
9088

91-
members[event.author.publicKey] = {
92-
accountId: event.author.accountId,
93-
signaturePublicKey: event.author.publicKey,
94-
encryptionPublicKey: invitation.encryptionPublicKey,
89+
members[invitation.inviteeAccountId] = {
90+
accountId: invitation.inviteeAccountId,
9591
role: 'member',
9692
};
9793
delete invitations[id];
98-
if (removedMembers[event.author.publicKey] !== undefined) {
99-
delete removedMembers[event.author.publicKey];
94+
if (removedMembers[event.author.accountId] !== undefined) {
95+
delete removedMembers[event.author.accountId];
10096
}
10197
} else {
10298
// check if the author is an admin
103-
if (members[event.author.publicKey]?.role !== 'admin') {
99+
if (members[event.author.accountId]?.role !== 'admin') {
104100
return Effect.fail(new InvalidEventError());
105101
}
106102

@@ -109,19 +105,17 @@ export const applyEvent = ({
109105
members = {};
110106
invitations = {};
111107
} else if (event.transaction.type === 'create-invitation') {
112-
if (members[event.transaction.signaturePublicKey] !== undefined) {
108+
if (members[event.transaction.inviteeAccountId] !== undefined) {
113109
return Effect.fail(new InvalidEventError());
114110
}
115111
for (const invitation of Object.values(invitations)) {
116-
if (invitation.signaturePublicKey === event.transaction.signaturePublicKey) {
112+
if (invitation.inviteeAccountId === event.transaction.inviteeAccountId) {
117113
return Effect.fail(new InvalidEventError());
118114
}
119115
}
120116

121117
invitations[event.transaction.id] = {
122118
inviteeAccountId: event.transaction.inviteeAccountId,
123-
signaturePublicKey: event.transaction.signaturePublicKey,
124-
encryptionPublicKey: event.transaction.encryptionPublicKey,
125119
};
126120
} else {
127121
throw new Error('State is required for all events except create-space');

packages/hypergraph/src/space-events/create-invitation.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ type Params = {
99
previousEventHash: string;
1010
invitee: {
1111
accountId: string;
12-
signaturePublicKey: string;
13-
encryptionPublicKey: string;
1412
};
1513
};
1614

@@ -22,11 +20,7 @@ export const createInvitation = ({
2220
const transaction = {
2321
id: generateId(),
2422
type: 'create-invitation' as const,
25-
ciphertext: '',
26-
nonce: '',
2723
inviteeAccountId: invitee.accountId,
28-
signaturePublicKey: invitee.signaturePublicKey,
29-
encryptionPublicKey: invitee.encryptionPublicKey,
3024
previousEventHash,
3125
};
3226
const encodedTransaction = stringToUint8Array(canonicalize(transaction));

packages/hypergraph/src/space-events/create-space.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ export const createSpace = ({ author }: Params): Effect.Effect<CreateSpaceEvent,
1313
type: 'create-space' as const,
1414
id: generateId(),
1515
creatorAccountId: author.accountId,
16-
creatorSignaturePublicKey: author.signaturePublicKey,
17-
creatorEncryptionPublicKey: author.encryptionPublicKey,
1816
};
1917
const encodedTransaction = stringToUint8Array(canonicalize(transaction));
2018
const signature = secp256k1

packages/hypergraph/src/space-events/types.ts

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import * as Schema from 'effect/Schema';
22

3+
export const EventAuthor = Schema.Struct({
4+
accountId: Schema.String,
5+
// must be validated if it belongs to the accountId before being used
6+
// Note: could be removed, but also might be useful to keep around in case accounts rotate their keys
7+
publicKey: Schema.String,
8+
signature: Schema.String,
9+
});
10+
11+
export type EventAuthor = Schema.Schema.Type<typeof Author>;
12+
313
export const SpaceMember = Schema.Struct({
414
accountId: Schema.String,
5-
signaturePublicKey: Schema.String,
6-
encryptionPublicKey: Schema.String,
715
role: Schema.Union(Schema.Literal('admin'), Schema.Literal('member')),
816
});
917

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

1220
export const SpaceInvitation = Schema.Struct({
1321
inviteeAccountId: Schema.String,
14-
signaturePublicKey: Schema.String,
15-
encryptionPublicKey: Schema.String,
1622
});
1723

1824
export type SpaceInvitation = Schema.Schema.Type<typeof SpaceInvitation>;
@@ -32,14 +38,8 @@ export const CreateSpaceEvent = Schema.Struct({
3238
type: Schema.Literal('create-space'),
3339
id: Schema.String,
3440
creatorAccountId: Schema.String,
35-
creatorSignaturePublicKey: Schema.String,
36-
creatorEncryptionPublicKey: Schema.String,
37-
}),
38-
author: Schema.Struct({
39-
accountId: Schema.String,
40-
publicKey: Schema.String,
41-
signature: Schema.String,
4241
}),
42+
author: EventAuthor,
4343
});
4444

4545
export type CreateSpaceEvent = Schema.Schema.Type<typeof CreateSpaceEvent>;
@@ -50,11 +50,7 @@ export const DeleteSpaceEvent = Schema.Struct({
5050
id: Schema.String,
5151
previousEventHash: Schema.String,
5252
}),
53-
author: Schema.Struct({
54-
accountId: Schema.String,
55-
publicKey: Schema.String,
56-
signature: Schema.String,
57-
}),
53+
author: EventAuthor,
5854
});
5955

6056
export type DeleteSpaceEvent = Schema.Schema.Type<typeof DeleteSpaceEvent>;
@@ -63,18 +59,10 @@ export const CreateInvitationEvent = Schema.Struct({
6359
transaction: Schema.Struct({
6460
type: Schema.Literal('create-invitation'),
6561
id: Schema.String,
66-
ciphertext: Schema.String,
67-
nonce: Schema.String,
6862
inviteeAccountId: Schema.String,
69-
signaturePublicKey: Schema.String,
70-
encryptionPublicKey: Schema.String,
7163
previousEventHash: Schema.String,
7264
}),
73-
author: Schema.Struct({
74-
accountId: Schema.String,
75-
publicKey: Schema.String,
76-
signature: Schema.String,
77-
}),
65+
author: EventAuthor,
7866
});
7967

8068
export type CreateInvitationEvent = Schema.Schema.Type<typeof CreateInvitationEvent>;
@@ -85,11 +73,7 @@ export const AcceptInvitationEvent = Schema.Struct({
8573
type: Schema.Literal('accept-invitation'),
8674
previousEventHash: Schema.String,
8775
}),
88-
author: Schema.Struct({
89-
accountId: Schema.String,
90-
publicKey: Schema.String,
91-
signature: Schema.String,
92-
}),
76+
author: EventAuthor,
9377
});
9478

9579
export type AcceptInvitationEvent = Schema.Schema.Type<typeof AcceptInvitationEvent>;

packages/hypergraph/test/space-events/accept-invitation.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,12 @@ it('should accept an invitation', async () => {
4646
expect(state3.id).toBeTypeOf('string');
4747
expect(state3.invitations).toEqual({});
4848
expect(state3.members).toEqual({
49-
[author.signaturePublicKey]: {
49+
[author.accountId]: {
5050
accountId: author.accountId,
51-
signaturePublicKey: author.signaturePublicKey,
52-
encryptionPublicKey: author.encryptionPublicKey,
5351
role: 'admin',
5452
},
55-
[invitee.signaturePublicKey]: {
53+
[invitee.accountId]: {
5654
accountId: invitee.accountId,
57-
signaturePublicKey: invitee.signaturePublicKey,
58-
encryptionPublicKey: invitee.encryptionPublicKey,
5955
role: 'member',
6056
},
6157
});

packages/hypergraph/test/space-events/create-invitation.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,11 @@ it('should create an invitation', async () => {
5050
expect(state2.invitations).toEqual({
5151
[spaceEvent2.transaction.id]: {
5252
inviteeAccountId: invitee.accountId,
53-
signaturePublicKey: '0x03bf5d2a1badf15387b08a007d1a9a13a9bfd6e1c56f681e251514d9ba10b57462',
54-
encryptionPublicKey: 'encryption',
5553
},
5654
});
5755
expect(state2.members).toEqual({
58-
[author.signaturePublicKey]: {
56+
[author.accountId]: {
5957
accountId: author.accountId,
60-
signaturePublicKey: author.signaturePublicKey,
61-
encryptionPublicKey: author.encryptionPublicKey,
6258
role: 'admin',
6359
},
6460
});

packages/hypergraph/test/space-events/create-space.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ it('should create a space state', async () => {
2222
expect(state.id).toBeTypeOf('string');
2323
expect(state.invitations).toEqual({});
2424
expect(state.members).toEqual({
25-
[author.signaturePublicKey]: {
25+
[author.accountId]: {
2626
accountId: author.accountId,
27-
signaturePublicKey: author.signaturePublicKey,
28-
encryptionPublicKey: author.encryptionPublicKey,
2927
role: 'admin',
3028
},
3129
});

packages/hypergraph/test/space-events/delete-space.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@ it('should delete a space', async () => {
3636
expect(state.invitations).toEqual({});
3737
expect(state.members).toEqual({});
3838
expect(state.removedMembers).toEqual({
39-
[author.signaturePublicKey]: {
39+
[author.accountId]: {
4040
accountId: author.accountId,
41-
signaturePublicKey: author.signaturePublicKey,
42-
encryptionPublicKey: author.encryptionPublicKey,
4341
role: 'admin',
4442
},
4543
});

0 commit comments

Comments
 (0)