-
Notifications
You must be signed in to change notification settings - Fork 198
/
sdk.ts
287 lines (243 loc) · 9.55 KB
/
sdk.ts
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
import { AesGcmEncryption } from './cryptoAES';
import { setConfig } from './configContext';
import { cryptoUtils as _cryptoUtils } from './cryptoRSA';
import deleteLink from './deleteLink';
import getLink from './getLink';
import getUsersInChannel from './getUsersInChannel';
import { configType, IChatE2EE, ISendMessageReturn, LinkObjType, TypeUsersInChannel } from './public/types';
import { getPublicKey, sharePublicKey } from './publicKey';
import sendMessage from './sendMessage';
import { SocketInstance, SubscriptionType } from './socket/socket';
import { Logger } from './utils/logger';
export { setConfig } from './configContext';
import { generateUUID } from './utils/uuid';
import { WebRTCCall, E2ECall, peerConnectionEvents, PeerConnectionEventType } from './webrtc';
export { IE2ECall } from './webrtc';
export const utils = {
decryptMessage: (ciphertext: string, privateKey: string) => _cryptoUtils.decryptMessage(ciphertext, privateKey),
generateUUID
}
const logger = new Logger();
export const createChatInstance = (): IChatE2EE => {
logger.log('Creating new instance');
return new ChatE2EE();
}
export type chatJoinPayloadType = {
channelID: string,
userID: string,
publicKey: string
}
class ChatE2EE implements IChatE2EE {
private channelId?: string;
private userId?: string;
private privateKey?: string;
private publicKey?: string;
private receiverPublicKey?: string;
//To Do: Fix types
private subscriptions: Map<string, Set<Function>> = new Map();
private callSubscriptions: Map<string, Set<Function>> = new Map();
private socket: SocketInstance;
private subscriptionLogger = logger.createChild('Subscription');
private callLogger = logger.createChild('Call');
private initialized = false;
private call?: WebRTCCall;
private iceCandidates = [];
private symEncryption = new AesGcmEncryption();
private setupCallSubs(call: WebRTCCall): void {
call.on('state-changed', (state) => {
if(state === 'failed' || state === 'closed') {
this.callLogger.log(`Ending call, RTCPeerConnectionState: ${state}`);
this.endCall();
}
})
}
constructor(config?: Partial<configType>) {
config && setConfig(config);
}
public async init(): Promise<void> {
const initLogger = logger.createChild('Init');
const evetLogger = logger.createChild('Events');
initLogger.log(`Started.`);
this.createSocketSubcription();
const { privateKey, publicKey } = await _cryptoUtils.generateKeypairs();
this.privateKey = privateKey;
this.publicKey = publicKey;
this.on('on-alice-join', () => {
evetLogger.log("Receiver connected.");
this.getPublicKey(initLogger);
})
this.on("on-alice-disconnect", () => {
evetLogger.log("Receiver disconnected");
this.receiverPublicKey = null;
});
/**
* Related to webrtc connection,
* Move it to WebRTC class?
*/
this.on('webrtc-session-description', (data) => {
evetLogger.log("New session description");
if(data.type === 'offer') {
evetLogger.log("New offer");
this.call = this.getWebRtcCall();
this.callSubscriptions.get("call-added")?.forEach((cb) => cb(this.activeCall));
this.call.signal(data);
// add ICE from buffer
this.iceCandidates.forEach((ice) => {
this.call.signal(ice);
})
this.iceCandidates = [];
}else if(data.type === 'answer') {
evetLogger.log("New answer");
this.call.signal(data);
}else if(data.type === 'candidate') {
evetLogger.log('ICE Candidate received.');
if(!this.call) {
evetLogger.log("call not created yet, storing ICE candidate");
this.iceCandidates.push(data);
}else {
this.call.signal(data);
}
}
});
initLogger.log(`Initializing AES Encryption for webrtc`);
await this.symEncryption.int();
initLogger.log(`Initialized AES Encryption for webrtc`);
initLogger.log(`Finished.`);
this.initialized = true;
}
public get activeCall(): E2ECall | null {
if(!this.call) {
return null;
}
return new E2ECall(this.call);
}
public async getLink(): Promise<LinkObjType> {
logger.log('getLink()');
return getLink();
}
public async setChannel(channelId: string, userId: string, userName?: string): Promise<void> {
this.checkInitialized();
logger.log(`setChannel(), ${JSON.stringify({ channelId, userId,userName })}`);
this.channelId = channelId;
this.userId = userId;
const aesPlain = await this.symEncryption.getRawAesKeyToExport();
await sharePublicKey({ aesKey: aesPlain, publicKey: this.publicKey, sender: this.userId, channelId: this.channelId});
this.socket.joinChat({ publicKey: this.publicKey, userID: this.userId, channelID: this.channelId})
await this.getPublicKey(logger);
return;
}
public isEncrypted(): boolean {
this.checkInitialized();
logger.log(`isEncrypted()`);
return !!this.receiverPublicKey;
}
public async delete(): Promise<void> {
logger.log(`delete()`);
this.checkInitialized();
return deleteLink({ channelID: this.channelId });
}
public async getUsersInChannel(): Promise<TypeUsersInChannel> {
logger.log(`getUsersInChannel()`);
this.checkInitialized();
await this.getPublicKey(logger.createChild('getUsersInChannel'));
return getUsersInChannel({ channelID: this.channelId });
}
public async sendMessage({ image, text }): Promise<ISendMessageReturn> {
logger.log(`sendMessage()`);
this.checkInitialized();
return sendMessage({ channelID: this.channelId, userId: this.userId, image, text })
}
public encrypt({ image, text }): { send: () => Promise<ISendMessageReturn> } {
logger.log(`encrypt()`);
this.checkInitialized();
const encryptedTextPromise = _cryptoUtils.encryptMessage(text, this.receiverPublicKey);
return ({
send: async () => {
const encryptedText = await encryptedTextPromise;
return this.sendMessage({ image, text: encryptedText })
}
})
}
public on(listener: string, callback): void {
const loggerWithCount = this.subscriptionLogger.count();
let subscriptions = this.subscriptions;
if(peerConnectionEvents.includes(listener as PeerConnectionEventType)) {
subscriptions = this.callSubscriptions;
}
const sub = this.subscriptions.get(listener);
if (sub) {
if (sub.has(callback)) {
loggerWithCount.log(`Skpping, subscription: ${listener}`);
return;
}
loggerWithCount.log(`Created +1 : ${listener}`);
sub.add(callback);
} else {
loggerWithCount.log(`Created: ${listener}`);
subscriptions.set(listener, new Set([callback]));
}
}
public dispose(): void {
this.checkInitialized();
logger.log('dispose()');
this.socket.dispose();
this.subscriptions.clear();
this.initialized = false;
}
public getKeyPair(): { privateKey: string, publicKey: string } {
this.checkInitialized();
return {
privateKey: this.privateKey,
publicKey: this.publicKey
}
}
public async startCall(): Promise<E2ECall> {
if(!WebRTCCall.isSupported()) {
throw new Error('createEncodedStreams not supported.');
}
if(this.call) {
throw new Error('Call already active');
}
const webrtcCall = this.getWebRtcCall();
await webrtcCall.startCall()
const call = new E2ECall(webrtcCall);
return call;
}
public async endCall(): Promise<void> {
this.call?.endCall();
this.call = null;
this.callSubscriptions.get("call-removed")?.forEach((cb) => cb());
}
//get receiver public key
private async getPublicKey(logger: Logger): Promise<void> {
logger.log(`getPublicKey()`);
const receiverPublicKey = await getPublicKey({ userId: this.userId, channelId: this.channelId });
logger.log(`setPublicKey() - ${!!receiverPublicKey?.publicKey}`);
this.receiverPublicKey = receiverPublicKey?.publicKey;
if(receiverPublicKey.aesKey) {
await this.symEncryption.setRemoteAesKey(receiverPublicKey.aesKey)
}
return;
}
private createSocketSubcription(): void {
const subscriptionContext = () => this.subscriptions as SubscriptionType;
this.socket = new SocketInstance(subscriptionContext, logger.createChild('Socket'));
}
private checkInitialized(): void {
if(!this.initialized) {
throw new Error('ChatE2EE is not initialized, call init()');
}
}
private getWebRtcCall(): WebRTCCall {
this.checkInitialized();
this.call = new WebRTCCall(
this.symEncryption,
this.userId,
this.channelId,
this.callLogger,
);
this.setupCallSubs(this.call)
return this.call;
}
}
export * from './public/types';