@@ -28,8 +28,12 @@ import {
2828 createKey ,
2929 createSpace ,
3030 decryptKey ,
31+ decryptMessage ,
32+ deserialize ,
3133 encryptKey ,
34+ encryptMessage ,
3235 generateId ,
36+ serialize ,
3337} from 'graph-framework' ;
3438import { useEffect , useState } from 'react' ;
3539
@@ -59,7 +63,7 @@ type SpaceStorageEntry = {
5963 events : SpaceEvent [ ] ;
6064 state : SpaceState | undefined ;
6165 keys : { id : string ; key : string } [ ] ;
62- updates : string [ ] ;
66+ updates : Uint8Array [ ] ;
6367 lastUpdateClock : number ;
6468} ;
6569
@@ -119,7 +123,7 @@ const App = ({
119123 if ( ! websocketConnection ) return ;
120124
121125 const onMessage = async ( event : MessageEvent ) => {
122- const data = JSON . parse ( event . data ) ;
126+ const data = deserialize ( event . data ) ;
123127 const message = decodeResponseMessage ( data ) ;
124128 if ( message . _tag === 'Right' ) {
125129 const response = message . right ;
@@ -141,7 +145,7 @@ const App = ({
141145 // fetch all spaces (for debugging purposes)
142146 for ( const space of response . spaces ) {
143147 const message : RequestSubscribeToSpace = { type : 'subscribe-space' , id : space . id } ;
144- websocketConnection ?. send ( JSON . stringify ( message ) ) ;
148+ websocketConnection ?. send ( serialize ( message ) ) ;
145149 }
146150 break ;
147151 }
@@ -176,10 +180,17 @@ const App = ({
176180 updates . push ( ...space . updates ) ;
177181 }
178182 if ( response . updates ) {
179- console . log ( 'response.updates' , response . updates , lastUpdateClock ) ;
180183 if ( response . updates . firstUpdateClock === lastUpdateClock + 1 ) {
181184 lastUpdateClock = response . updates . lastUpdateClock ;
182- updates . push ( ...response . updates . updates ) ;
185+
186+ const newUpdates = ( response . updates ? response . updates . updates : [ ] ) . map ( ( encryptedUpdate ) => {
187+ return decryptMessage ( {
188+ nonceAndCiphertext : encryptedUpdate ,
189+ secretKey : hexToBytes ( keys [ 0 ] . key ) ,
190+ } ) ;
191+ } ) ;
192+
193+ updates . push ( ...newUpdates ) ;
183194 } else {
184195 // TODO request missing updates from server
185196 }
@@ -254,9 +265,16 @@ const App = ({
254265 // TODO request missing updates from server
255266 }
256267
268+ const newUpdates = ( response . updates ? response . updates . updates : [ ] ) . map ( ( encryptedUpdate ) => {
269+ return decryptMessage ( {
270+ nonceAndCiphertext : encryptedUpdate ,
271+ secretKey : hexToBytes ( space . keys [ 0 ] . key ) ,
272+ } ) ;
273+ } ) ;
274+
257275 return {
258276 ...space ,
259- updates : [ ...space . updates , ...response . updates . updates ] ,
277+ updates : [ ...space . updates , ...newUpdates ] ,
260278 lastUpdateClock,
261279 } ;
262280 }
@@ -308,7 +326,7 @@ const App = ({
308326 authorPublicKey : encryptionPublicKey ,
309327 } ,
310328 } ;
311- websocketConnection ?. send ( JSON . stringify ( message ) ) ;
329+ websocketConnection ?. send ( serialize ( message ) ) ;
312330 } }
313331 >
314332 Create space
@@ -317,7 +335,7 @@ const App = ({
317335 < Button
318336 onClick = { ( ) => {
319337 const message : RequestListSpaces = { type : 'list-spaces' } ;
320- websocketConnection ?. send ( JSON . stringify ( message ) ) ;
338+ websocketConnection ?. send ( serialize ( message ) ) ;
321339 } }
322340 >
323341 List Spaces
@@ -326,7 +344,7 @@ const App = ({
326344 < Button
327345 onClick = { ( ) => {
328346 const message : RequestListInvitations = { type : 'list-invitations' } ;
329- websocketConnection ?. send ( JSON . stringify ( message ) ) ;
347+ websocketConnection ?. send ( serialize ( message ) ) ;
330348 } }
331349 >
332350 List Invitations
@@ -355,12 +373,12 @@ const App = ({
355373 event : spaceEvent . value ,
356374 spaceId : invitation . spaceId ,
357375 } ;
358- websocketConnection ?. send ( JSON . stringify ( message ) ) ;
376+ websocketConnection ?. send ( serialize ( message ) ) ;
359377
360378 // temporary until we have define a strategy for accepting invitations response
361379 setTimeout ( ( ) => {
362380 const message2 : RequestListInvitations = { type : 'list-invitations' } ;
363- websocketConnection ?. send ( JSON . stringify ( message2 ) ) ;
381+ websocketConnection ?. send ( serialize ( message2 ) ) ;
364382 } , 1000 ) ;
365383 } }
366384 />
@@ -375,7 +393,7 @@ const App = ({
375393 < Button
376394 onClick = { ( ) => {
377395 const message : RequestSubscribeToSpace = { type : 'subscribe-space' , id : space . id } ;
378- websocketConnection ?. send ( JSON . stringify ( message ) ) ;
396+ websocketConnection ?. send ( serialize ( message ) ) ;
379397 } }
380398 >
381399 Get data and subscribe to Space
@@ -430,7 +448,7 @@ const App = ({
430448 spaceId : space . id ,
431449 keyBoxes,
432450 } ;
433- websocketConnection ?. send ( JSON . stringify ( message ) ) ;
451+ websocketConnection ?. send ( serialize ( message ) ) ;
434452 } }
435453 >
436454 Invite { invitee . accountId . substring ( 0 , 4 ) }
@@ -445,18 +463,24 @@ const App = ({
445463 setSpaces ( ( currentSpaces ) =>
446464 currentSpaces . map ( ( currentSpace ) => {
447465 if ( space . id === currentSpace . id ) {
448- return { ...currentSpace , updates : [ ...currentSpace . updates , 'a' ] } ;
466+ return { ...currentSpace , updates : [ ...currentSpace . updates , new Uint8Array ( [ 0 ] ) ] } ;
449467 }
450468 return currentSpace ;
451469 } ) ,
452470 ) ;
471+
472+ const nonceAndCiphertext = encryptMessage ( {
473+ message : new Uint8Array ( [ 0 ] ) ,
474+ secretKey : hexToBytes ( space . keys [ 0 ] . key ) ,
475+ } ) ;
476+
453477 const message : RequestCreateUpdate = {
454478 type : 'create-update' ,
455479 ephemeralId,
456- update : 'a' ,
480+ update : nonceAndCiphertext ,
457481 spaceId : space . id ,
458482 } ;
459- websocketConnection ?. send ( JSON . stringify ( message ) ) ;
483+ websocketConnection ?. send ( serialize ( message ) ) ;
460484 } }
461485 >
462486 Create an update
0 commit comments