@@ -34,11 +34,13 @@ export const applyEvent = ({
3434 return Effect . fail ( new InvalidEventError ( ) ) ;
3535 }
3636 if ( event . transaction . previousEventHash !== state . lastEventHash ) {
37+ console . log ( 'WEEEEE' , event . transaction . previousEventHash , state . lastEventHash ) ;
3738 return Effect . fail ( new InvalidEventError ( ) ) ;
3839 }
3940 }
4041
4142 const encodedTransaction = stringToUint8Array ( canonicalize ( event . transaction ) ) ;
43+
4244 const isValidSignature = secp256k1 . verify ( event . author . signature , encodedTransaction , event . author . publicKey , {
4345 prehash : true ,
4446 } ) ;
@@ -65,27 +67,58 @@ export const applyEvent = ({
6567 removedMembers = { ...state . removedMembers } ;
6668 invitations = { ...state . invitations } ;
6769
68- if ( event . transaction . type === 'delete-space' ) {
69- removedMembers = { ...members } ;
70- members = { } ;
71- invitations = { } ;
72- } else if ( event . transaction . type === 'create-invitation' ) {
73- if ( members [ event . transaction . signaturePublicKey ] !== undefined ) {
70+ if ( event . transaction . type === 'accept-invitation' ) {
71+ // is already a member
72+ if ( members [ event . author . publicKey ] !== undefined ) {
7473 return Effect . fail ( new InvalidEventError ( ) ) ;
7574 }
76- for ( const invitation of Object . values ( invitations ) ) {
77- if ( invitation . signaturePublicKey === event . transaction . signaturePublicKey ) {
78- return Effect . fail ( new InvalidEventError ( ) ) ;
79- }
75+
76+ // find the invitation
77+ const result = Object . entries ( invitations ) . find (
78+ ( [ , invitation ] ) => invitation . signaturePublicKey === event . author . publicKey ,
79+ ) ;
80+ if ( ! result ) {
81+ return Effect . fail ( new InvalidEventError ( ) ) ;
8082 }
83+ const [ id , invitation ] = result ;
8184
82- invitations [ event . transaction . id ] = {
83- signaturePublicKey : event . transaction . signaturePublicKey ,
84- encryptionPublicKey : event . transaction . encryptionPublicKey ,
85+ members [ event . author . publicKey ] = {
86+ signaturePublicKey : event . author . publicKey ,
87+ encryptionPublicKey : invitation . encryptionPublicKey ,
88+ role : 'member' ,
8589 } ;
90+ delete invitations [ id ] ;
91+ if ( removedMembers [ event . author . publicKey ] !== undefined ) {
92+ delete removedMembers [ event . author . publicKey ] ;
93+ }
94+ } else {
95+ // check if the author is an admin
96+ if ( members [ event . author . publicKey ] ?. role !== 'admin' ) {
97+ return Effect . fail ( new InvalidEventError ( ) ) ;
98+ }
99+
100+ if ( event . transaction . type === 'delete-space' ) {
101+ removedMembers = { ...members } ;
102+ members = { } ;
103+ invitations = { } ;
104+ } else if ( event . transaction . type === 'create-invitation' ) {
105+ if ( members [ event . transaction . signaturePublicKey ] !== undefined ) {
106+ return Effect . fail ( new InvalidEventError ( ) ) ;
107+ }
108+ for ( const invitation of Object . values ( invitations ) ) {
109+ if ( invitation . signaturePublicKey === event . transaction . signaturePublicKey ) {
110+ return Effect . fail ( new InvalidEventError ( ) ) ;
111+ }
112+ }
113+
114+ invitations [ event . transaction . id ] = {
115+ signaturePublicKey : event . transaction . signaturePublicKey ,
116+ encryptionPublicKey : event . transaction . encryptionPublicKey ,
117+ } ;
118+ } else {
119+ throw new Error ( 'State is required for all events except create-space' ) ;
120+ }
86121 }
87- } else {
88- throw new Error ( 'State is required for all events except create-space' ) ;
89122 }
90123
91124 return Effect . succeed ( {
0 commit comments