-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
534 lines (463 loc) · 16.2 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import 'react-native-gesture-handler';
import React, { useState,useEffect,useContext,useCallback } from 'react';
var Buffer = require("@craftzdog/react-native-buffer").Buffer;
import {
View,
Text,
TextInput,
ScrollView,
Button,
Image,
ImageSourcePropType,
TouchableOpacity,
TouchableHighlight,
AsyncStorage}
from "react-native";
//used for id generation
import uuid from 'react-native-uuid';
//navigation
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { getHeaderTitle } from '@react-navigation/elements';
import { withNavigation } from 'react-navigation';
import Ionicons from '@expo/vector-icons/Ionicons';
import * as SecureStore from 'expo-secure-store';
//import components to include in the navigation pages
import MainScreenComponent from './MainScreenComponent';
import MainSettingScreenComponent from './MainSettingScreenComponent';
import ChatScreenComponent from './ChatScreenComponent';
import {RegisterUserComponent,ChatSettingScreenComponent} from './ChatSettingScreenComponent';
import {NewChatScreenComponent,NewGroupChatScreenComponent} from './NewChatScreenComponent';
import SettingOptionsComponent from './SettingOptionsComponent';
import * as SplashScreen from 'expo-splash-screen';
import {PasswordUnlockScreen} from './SettingOptionsComponent';
//import global style
import {GlobalStyle} from './Styles.js';
//import signal components
import {
KeyHelper,
SignedPublicPreKeyType,
SignalProtocolAddress,
SessionBuilder,
PreKeyType,
SessionCipher,
MessageType }
from '@privacyresearch/libsignal-protocol-typescript'
import { SignalProtocolStore,arrayBufferToString } from './storage-type';
//import typescript interfaces and contexts
import {
ChatContext,
ContactContext,
SignalContext,
PasswordContext,
Chat,
Contact,
ProcessedChatMessage,
MessageCreator}
from './Context';
//contact creation function
function ContactCreator(map:Map<string,Contact>,id:string,name:string,picture:ImageSourcePropType,details:string){
map.set(id,{id,name,picture,details})
}
//chat creation function
function ChatCreator(map:Map<string,Chat>,id:string,contactids:string[],messages:ProcessedChatMessage[],name:string,picture:ImageSourcePropType,details:string){
map.set(id,{id,contactids,messages,name,picture,details})
}
//message creation function
export async function save(key:string, value:any) {
await SecureStore.setItemAsync(key, value);
}
async function getValueFor(key:string) {
const result = await SecureStore.getItemAsync(key);
if (result) {
console.log("Here's your value \n" , result);
return result;
}else{
//console.log("ayy no value");
return undefined;
}
}
async function removeValue(key:string){
await SecureStore.deleteItemAsync(key);
}
//create stack navigator
const StackNav = createNativeStackNavigator();
//create maps for contact & chat storage
const contactMap = new Map<string,Contact>();
const chatMap = new Map<string,Chat>();
//this should be generated randomly at first run
const initialUserId = "47769a91-2d07-4580-8828-5913cf821623";
//debug id for testing purposes
const altId = "1d4070bf-7ada-46bd-8b7c-c8b8e0507dec"
//please don't doxx me.
const serverip = "98.14.55.86:8000"
//generate websocket connection on app start
const initialws = new WebSocket('ws://'+serverip+'/'+'loading')
//signal protocol address (currently unused)
const userAddress = new SignalProtocolAddress(initialUserId, 1);
//add user to contacts
//ContactCreator(contactMap,initialUserId,"TestUser",GlobalStyle.defaultprofile,"They/Them")
//ContactCreator(contactMap,"1111","The Fool",GlobalStyle.defaultprofile,"They/Them")
//ContactCreator(contactMap,"1112","The Magician",GlobalStyle.defaultprofile,"They/Them")
//ContactCreator(contactMap,"1113","The Emperor",GlobalStyle.defaultprofile,"They/Them")
//ChatCreator(chatMap,"1",["1111","1112","1113"],[],"",GlobalStyle.defaultprofile,"")
function debugData(){
//generate debug contacts
ContactCreator(contactMap,"2","The High Priestess",GlobalStyle.defaultprofile,"They/Them")
ContactCreator(contactMap,"3","The Empress",GlobalStyle.defaultprofile,"They/Them")
ContactCreator(contactMap,"5","The Hierophant",GlobalStyle.defaultprofile,"They/Them")
ContactCreator(contactMap,"6","The Lovers",GlobalStyle.defaultprofile,"They/Them")
ContactCreator(contactMap,"7","The Chariot",GlobalStyle.defaultprofile,"They/Them")
ContactCreator(contactMap,"8","Strength",GlobalStyle.defaultprofile,"They/Them")
ContactCreator(contactMap,"9","The Hermit",GlobalStyle.defaultprofile,"They/Them")
ContactCreator(contactMap,"10","The Wheel of Fortune",GlobalStyle.defaultprofile,"They/Them")
//generate debug chats
ChatCreator(chatMap,"0",[initialUserId,altId],[
MessageCreator("Test Message 0. Lorem Ipsum",altId,"0"),
MessageCreator("Test Message 1. Lorem Ipsum",initialUserId,"0"),
MessageCreator("Test Message 0. Lorem Ipsum",altId,"0"),
MessageCreator("Test Message 2. Lorem Ipsum",initialUserId,"0"),
MessageCreator("Test Message 0. Lorem Ipsum",altId,"0"),
MessageCreator("Test Message 0. Lorem Ipsum",altId,"0"),
MessageCreator("Test Message 3. Lorem Ipsum",initialUserId,"0"),
MessageCreator("Test Message 0. Lorem Ipsum",altId,"0"),
],"",GlobalStyle.defaultprofile,"")
ChatCreator(chatMap,"1",[initialUserId,"1","4"], [
MessageCreator("Test Message 0. Lorem Ipsum","1","1"),
MessageCreator("Test Message 1. Lorem Ipsum","4","1"),
MessageCreator("Test Message 2. Lorem Ipsum",initialUserId,"1"),
],"Test Group chat", GlobalStyle.defaultprofile,"A test Chat")
}
SplashScreen.preventAutoHideAsync();
function App() {
function makeKeyId(){
return Math.floor(10000 * Math.random());
}
//stores data to signal protool store. This is probably not the best implementation of this
const storeSomewhereSafe = (store: SignalProtocolStore) =>
(key: string, value: any) => {store.put(key, value)};
// storage.set(key, value)
//signal id creation function
const createID = async (name: string, store: SignalProtocolStore) =>
{
const registrationId = KeyHelper.generateRegistrationId()
storeSomewhereSafe(store)(`registrationID`, registrationId)
//storage.set(`registrationID`, registrationId)
const identityKeyPair = await KeyHelper.generateIdentityKeyPair()
const view = Buffer.from(identityKeyPair.privKey);
storeSomewhereSafe(store)('identityKey', identityKeyPair)
//storage.set('identityKey', JSON.stringify(identityKeyPair))
const baseKeyId = makeKeyId()
const preKey = await KeyHelper.generatePreKey(baseKeyId)
store.storePreKey(`${baseKeyId}`, preKey.keyPair)
//storage.set(`${baseKeyId}`, JSON.stringify(preKey.keyPair))
const signedPreKeyId = makeKeyId()
const signedPreKey = await KeyHelper.generateSignedPreKey(identityKeyPair, signedPreKeyId)
store.storeSignedPreKey(signedPreKeyId, signedPreKey.keyPair)
//storage.set(`${signedPreKeyId}`, JSON.stringify(signedPreKey.keyPair))
// Now we register this with the server or other directory so all users can see them.
/*
Everything here needs to be reimplemented when signal server is ready
const publicSignedPreKey: SignedPublicPreKeyType = {
keyId: signedPreKeyId,
publicKey: signedPreKey.keyPair.pubKey,
signature: signedPreKey.signature,
}
const publicPreKey: PreKeyType = {
keyId: preKey.keyId,
publicKey: preKey.keyPair.pubKey,
}
directory.storeKeyBundle(name, {
registrationId,
identityPubKey: identityKeyPair.pubKey,
signedPreKey: publicSignedPreKey,
oneTimePreKeys: [publicPreKey],
})
*/
//save('firsttimerun', false);
}
//call id creation function
function delData(){
chats.forEach((chat:Chat)=>{
SecureStore.deleteItemAsync(chat.id);
})
SecureStore.deleteItemAsync('chatids')
contacts.forEach((contact:Contact)=>{
SecureStore.deleteItemAsync(contact.id);
})
SecureStore.deleteItemAsync('contactids')
SecureStore.deleteItemAsync('userid');
SecureStore.deleteItemAsync('userstore');
console.log("Cleared Save Data");
}
//data states
const [contacts,setContacts] = useState<Map<string,Contact>>(contactMap);
const [chats,setChats] = useState<Map<string,Chat>>(new Map<string,Chat>(chatMap));
const [userid,setUserId] = useState<string>("");
const [processedmessages,setProcessedMessages] = useState<Map<string,ProcessedChatMessage>>(new Map<string,ProcessedChatMessage>());
//signal storage state
const [userStore,setUserStore] = useState(new SignalProtocolStore());
const [ws,setWs] = useState<WebSocket>(initialws)
//creates the user identity and saves it to the persistent data
async function createUserIdentity():Promise<void>{
console.log("This actually ran")
await createID(userid, userStore);
//we have to override the json function to save the array buffers in a different manner. Hopefully, they still work when loaded again
const stringifiedstore = JSON.stringify(userStore,function(k,v){
if (k == "pubKey" || k == "privKey"){
const buf = Buffer.from(v);
console.log(buf.toJSON())
return Buffer.from(v).toJSON();
}
return v;
});
await SecureStore.setItemAsync('userstore',stringifiedstore);
console.log(stringifiedstore);
};
//websocket state
const [appIsReady, setAppIsReady] = useState(false);
const [firsttimerun,setFirstTimeRun] = useState(true);
const [ispasswordlock,setLockState] = useState(false);
const [isapplock,setAppLock] = useState(false);
const [password,setPassword] = useState("");
//organize data for context providing
const chatState = {chats,setChats,ws,setWs,processedmessages,setProcessedMessages};
const contactState = {contacts,setContacts,userid,setUserId,resetContactData:delData};
const signalState = {userStore,createUserIdentity,serverip}
const passwordState = {ispasswordlock,setLockState,isapplock,setAppLock,password,setPassword}
//console.log("New Web Socket Connection: ",ws);
useEffect(() => {
async function prepare(){
try{
const userid = await SecureStore.getItemAsync('userid')
if(userid!=null && userid != ""){
setUserId(userid);
setFirstTimeRun(false);
}else if(userid == ""){
setFirstTimeRun(true);
}
else{
setFirstTimeRun(true);
}
const pass = await SecureStore.getItemAsync('password');
if(pass!=null){
setLockState(true);
setAppLock(true)
setPassword(pass);
}else{
setLockState(false);
}
const chatidjson = await SecureStore.getItemAsync('chatids')
if(chatidjson != null){
const chatids:string[] = JSON.parse(chatidjson);
//console.log('chatidsload',chatids);
for (const chatid of chatids){
const chatjson = await SecureStore.getItemAsync(chatid);
//console.log('chatjson',chatjson)
if(chatjson != null){
setChats((chats)=>{
const newChats = new Map(chats);
newChats.set(chatid,JSON.parse(chatjson));
return newChats;
})
}
}
}
const contactidjson = await SecureStore.getItemAsync('contactids');
if(contactidjson != null){
const contactids:string[] = JSON.parse(contactidjson);
//console.log('contactidsload',contactids);
for (const contactid of contactids){
const contactjson = await SecureStore.getItemAsync(contactid)
if(contactjson != null){
setContacts((contacts)=>{
const newContacts = new Map(contacts);
newContacts.set(contactid,JSON.parse(contactjson));
return newContacts;
})
}
}
}
const userstorejson = await SecureStore.getItemAsync('userstore');
//console.log(userstorejson);
if(userstorejson!=null){
//console.log(userstorejson);
const newuserstore = JSON.parse(userstorejson,(key,value)=>{
//console.log(key,value);
if (key == "pubKey" || key == "privKey"){
//console.log(value);
const arraybuf = Buffer.from(value).buffer;
//console.log(value.data)
return arraybuf;
}
return value;
})
//console.log(newuserstore);
setUserStore(newuserstore);
}
if (userid != ""){
setWs(new WebSocket('ws://'+serverip+'/'+userid))
}
}catch(e){
console.warn(e);
}finally{
console.log("Finishing")
setAppIsReady(true);
}
}
prepare();
},[])
useEffect(()=>{
if (appIsReady) {
const chatids:string[] = [];
chats.forEach((chat)=>{
chatids.push(chat.id);
SecureStore.setItemAsync(chat.id,JSON.stringify(chat,function(key,value){
console.log(key,typeof key,value,typeof value);
return value;
}));
})
SecureStore.setItemAsync('chatids',JSON.stringify(chatids))
SecureStore.getItemAsync('chatids').then((chatids)=>{
console.log("saving chatids as",chatids);
})
}
},[chats])
useEffect(()=>{
if (appIsReady) {
const contactids:string[] = [];
contacts.forEach((contact)=>{
contactids.push(contact.id);
SecureStore.setItemAsync(contact.id,JSON.stringify(contact));
})
SecureStore.setItemAsync('contactids',JSON.stringify(contactids));
if(typeof contacts.get(userid) != "undefined")
{
setFirstTimeRun(false);
}
}
}
,[contacts])
useEffect(()=>{
if (appIsReady) {
if(userid != ""){
//console.log('saving the userid');
SecureStore.setItemAsync('userid',userid);
setWs(new WebSocket('ws://'+serverip+'/'+userid))
}else{
setFirstTimeRun(true);
}
}
},[userid])
//not working?
useEffect(()=>{
if (appIsReady) {
//console.log('saving the userstore');
//console.log(JSON.stringify(userStore))
SecureStore.setItemAsync('userstore',JSON.stringify(userStore));
}
},[userStore])
//resets data probably should implemet it better
//delData();
//on recieve message from server
ws.onmessage = (e) => {
//parse json string
let msgData = JSON.parse(e.data);
console.log("Recieved: ", msgData);
//get chatid
let chatId:string = msgData.chatId
console.log(chatId)
//push message to chat map & update state
setChats((chats) =>{
const newChats = new Map(chats);
const thischat = newChats.get(chatId);
if (typeof thischat != undefined){
const tschat = thischat as Chat;
tschat.messages.push(msgData)
newChats.set(chatId,tschat)
}
return newChats;
})
};
const onLayoutRootView = useCallback(async () => {
if (appIsReady) {
if (typeof contacts.get(userid) != "undefined"){
setFirstTimeRun(false);
}
console.log("calling hideasync")
await SplashScreen.hideAsync();
}
}, [appIsReady]);
if (!appIsReady) {
return null;
}
//context providers allow pages to access all relevant information
return (
<View style={{flex:1}} onLayout={onLayoutRootView}>
<NavigationContainer>
<ChatContext.Provider value={chatState}>
<SignalContext.Provider value={signalState}>
<ContactContext.Provider value={contactState}>
<PasswordContext.Provider value={passwordState}>
<StackNav.Navigator>
{ (firsttimerun)?
<><StackNav.Screen
name="UserRegister"
component={RegisterUserComponent}
/>
</>
:
<>
{ (isapplock)?
<><StackNav.Screen
name="PasswordUnlock"
component={PasswordUnlockScreen}
/>
</>
:
<>
<StackNav.Screen
name="Home"
component={MainScreenComponent}
options={({ route }) => ({ title: (contacts.get(userid) as Contact).name })}
/>
<StackNav.Screen
name="MainSettings"
component={MainSettingScreenComponent}
/>
<StackNav.Screen
name="ChatScreen"
component={ChatScreenComponent}
options={({ route }) => ({ title: "Typescript placeholder" })}
/>
<StackNav.Screen
name="ChatSettings"
component={ChatSettingScreenComponent}
/>
<StackNav.Screen
name="NewChatScreen"
component={NewChatScreenComponent}
/>
<StackNav.Screen
name="NewGroupChatScreen"
component={NewGroupChatScreenComponent}
/>
<StackNav.Screen
name="SettingOptions"
component={SettingOptionsComponent}
/>
</>
}
</>
}
</StackNav.Navigator>
</PasswordContext.Provider>
</ContactContext.Provider>
</SignalContext.Provider>
</ChatContext.Provider>
</NavigationContainer>
</View>
);
}
//replace typescript placeholder with this once assertion problem is fixed route.params.username
export default App;