-
Notifications
You must be signed in to change notification settings - Fork 3
/
YlideCore.ts
704 lines (652 loc) · 22.7 KB
/
YlideCore.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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
import { MessageBlob } from '../content/MessageBlob';
import { MessageSecureContext } from '../content/MessageSecureContext';
import { IndexerHub } from '../indexer/IndexerHub';
import { IndexerMessagesSource } from '../indexer/IndexerMessagesSource';
import { PuppetListSource } from '../messages-list/mid-level/PuppetListSource';
import { uint256ToUint8Array, uint8ArrayToUint256 } from '../primitives/Uint256';
import { sha256 } from '../crypto/sha256';
import { RemotePublicKey } from '../keystore/RemotePublicKey';
import { PublicKeyType } from '../primitives/PublicKeyType';
import { PublicKey } from '../primitives/PublicKey';
import { ListSource } from '../messages-list/mid-level/ListSource';
import { ServiceCode } from '../primitives/ServiceCode';
import { DynamicEncryptionRouter } from '../cross-chain/DynamicEncryptionRouter';
import { YLIDE_MAIN_FEED_ID } from '../utils/constants';
import { asyncDelay } from '../utils/asyncDelay';
import { MessageContainer } from '../content/MessageContainer';
import { MessageKey } from '../content/MessageKey';
import { YlideError, YlideErrorType } from '../errors/YlideError';
import { YlideMisusageError } from '../errors/YlideMisusageError';
import type { YlidePrivateKey, YlidePrivateKeyHandlers } from '../keystore';
import type { WalletAccount } from '../primitives/WalletAccount';
import type { IUnpackedMessageContainer } from '../content/MessageContainer';
import type { Uint256 } from '../primitives/Uint256';
import type { YlideKeyRegistry } from '../keystore/YlideKeysRegistry';
import type { IMessage, IMessageContent, IMessageCorruptedContent } from '../primitives/IMessage';
import type { IListSource } from '../messages-list/types/IListSource';
import type { SourceReadingSession } from '../messages-list/SourceReadingSession';
import type { ISourceSubject } from '../messages-list/types/IBlockchainSourceSubject';
import type { AbstractBlockchainController } from '../abstracts/AbstractBlockchainController';
import type { BlockchainControllerFactory } from '../abstracts/BlockchainControllerFactory';
import type { AbstractWalletController } from '../abstracts/AbstractWalletController';
import type { YlideControllers } from './YlideControllers';
import type { MessageContentV3 } from '../content/MessageContentV3';
import type { MessageContentV4 } from '../content/MessageContentV4';
import type { MessageContentV5 } from './../content/MessageContentV5';
import type { MessageContent } from '../content/MessageContent';
import type { Ylide } from '../Ylide';
export interface SendMessageArgs {
wallet: AbstractWalletController;
sender: WalletAccount;
content: MessageContent;
recipients: string[];
secureContext?: MessageSecureContext;
feedId?: Uint256;
serviceCode?: number;
copyOfSent?: boolean;
}
export interface BroadcastMessageArgs {
wallet: AbstractWalletController;
sender: WalletAccount;
feedId: Uint256;
content: MessageContent;
serviceCode?: number;
}
export class YlideCore {
readonly indexer: IndexerHub;
constructor(
public readonly ylide: Ylide,
public readonly controllers: YlideControllers,
public readonly keyRegistry: YlideKeyRegistry,
private readonly indexerBlockchains: string[] = [],
useWebSocketPulling?: boolean,
) {
this.indexer = new IndexerHub(useWebSocketPulling);
}
static getSentAddress(recipient: Uint256): Uint256 {
return uint8ArrayToUint256(sha256(uint256ToUint8Array(recipient)));
}
async waitForPublicKey(blockchain: string, address: string, key: Uint8Array, timeout = 60000) {
const start = Date.now();
while (Date.now() - start < timeout) {
const keys = await this.ylide.core.getAddressKeys(address);
const keyInChain = keys.remoteKeys[blockchain];
if (keyInChain && keyInChain.publicKey.keyEquals(key)) {
return keyInChain;
}
await asyncDelay(2000);
}
return null;
}
async getAddressesKeys(addresses: string[]): Promise<
Record<
string,
{
freshestKey: RemotePublicKey | null;
remoteKeys: Record<string, RemotePublicKey | null>;
}
>
> {
const blockchains = this.ylide.blockchainsList.map(({ factory }) => ({
factory,
controller: this.controllers.blockchainsMap[factory.blockchain],
}));
let indexerResult: Record<string, Record<string, RemotePublicKey | null>> = {};
if (this.indexerBlockchains.length) {
const indexerAddresses = addresses.filter(a =>
blockchains
.filter(b => this.indexerBlockchains.includes(b.factory.blockchain))
.some(b => b.controller.isAddressValid(a)),
);
if (indexerAddresses.length) {
indexerResult = await this.indexer.retryingOperation(
async () => {
const rawResult = await this.indexer.requestMultipleKeys(indexerAddresses);
const result: Record<string, Record<string, RemotePublicKey | null>> = {};
for (const address of Object.keys(rawResult)) {
for (const bc of Object.keys(rawResult[address])) {
if (!result[address]) {
result[address] = {};
}
const kkey = rawResult[address][bc];
const bcGroup = this.getBlockchainGroupByBlockchain(bc);
if (bcGroup) {
result[address][bc] = kkey
? new RemotePublicKey(
bcGroup,
bc,
address,
new PublicKey(PublicKeyType.YLIDE, kkey.keyVersion, kkey.publicKey),
kkey.timestamp,
kkey.registrar,
)
: null;
}
}
}
return result;
},
async () => {
return {};
},
);
for (const address of indexerAddresses) {
if (!indexerResult[address]) {
indexerResult[address] = {};
}
}
} else {
for (const address of indexerAddresses) {
if (!indexerResult[address]) {
indexerResult[address] = {};
}
}
}
}
const toReadFromBlockchain = blockchains.filter(b => !this.indexerBlockchains.includes(b.factory.blockchain));
const readFromBlockchains = async (
fromBlockchains: { factory: BlockchainControllerFactory; controller: AbstractBlockchainController }[],
) => {
const blockchainRemoteKeys: Record<string, Record<string, RemotePublicKey | null>> = {};
await Promise.all(
addresses.map(async address => {
const remoteKeys: Record<string, RemotePublicKey | null> = {};
await Promise.all(
fromBlockchains.map(async ({ factory, controller }) => {
try {
if (controller.isAddressValid(address)) {
const key = (await controller.extractPublicKeyFromAddress(address)) || null;
if (key) {
remoteKeys[factory.blockchain] = key;
}
}
} catch (err) {
// so sad :(
}
}),
);
blockchainRemoteKeys[address] = remoteKeys;
}),
);
return blockchainRemoteKeys;
};
const blockchainsResult = await readFromBlockchains(toReadFromBlockchain);
const globalResult: Record<string, Record<string, RemotePublicKey | null>> = {};
// merge results:
for (const address of addresses) {
globalResult[address] = {
...indexerResult[address],
...blockchainsResult[address],
};
}
const processedResult: Record<
string,
{
freshestKey: RemotePublicKey | null;
remoteKeys: Record<string, RemotePublicKey | null>;
}
> = {};
for (const address of addresses) {
const remoteKeys = globalResult[address];
const bcs = Object.keys(remoteKeys);
let freshestKey: RemotePublicKey | null = null;
for (const bc of bcs) {
const kkey = remoteKeys[bc];
if (kkey && (!freshestKey || freshestKey.timestamp < kkey.timestamp)) {
freshestKey = kkey;
}
}
processedResult[address] = {
freshestKey,
remoteKeys,
};
}
return processedResult;
}
getBlockchainGroupByBlockchain(blockchain: string): string | undefined {
return this.ylide.blockchainsList.find(b => b.factory.blockchain === blockchain)?.factory.blockchainGroup;
}
async getAddressKeys(address: string): Promise<{
freshestKey: RemotePublicKey | null;
remoteKeys: Record<string, RemotePublicKey | null>;
}> {
const blockchains = this.ylide.blockchainsList.map(({ factory }) => ({
factory,
controller: this.controllers.blockchainsMap[factory.blockchain],
}));
const toReadFromBlockchain = blockchains.filter(b => !this.indexerBlockchains.includes(b.factory.blockchain));
const toReadFromIndexer = blockchains.filter(b => this.indexerBlockchains.includes(b.factory.blockchain));
const readFromBlockchains = async (
fromBlockchains: { factory: BlockchainControllerFactory; controller: AbstractBlockchainController }[],
) => {
const remoteKeys: Record<string, RemotePublicKey | null> = {};
await Promise.all(
fromBlockchains.map(async ({ factory, controller }) => {
try {
if (controller.isAddressValid(address)) {
const key = (await controller.extractPublicKeyFromAddress(address)) || null;
if (key) {
remoteKeys[factory.blockchain] = key;
}
}
} catch (err) {
// so sad :(
}
}),
);
return remoteKeys;
};
const readFromIndexer = async () => {
if (
blockchains
.filter(b => this.indexerBlockchains.includes(b.factory.blockchain))
.some(b => b.controller.isAddressValid(address))
) {
const remoteKeys: Record<string, RemotePublicKey | null> = {};
const rawRemoteKeys = await this.indexer.requestKeys(address);
const bcs = Object.keys(rawRemoteKeys);
for (const bc of bcs) {
const aggr = {
...rawRemoteKeys[bc],
publicKey: new PublicKey(
PublicKeyType.YLIDE,
rawRemoteKeys[bc].keyVersion,
rawRemoteKeys[bc].publicKey,
),
};
const bcGroup = this.getBlockchainGroupByBlockchain(bc);
if (bcGroup) {
remoteKeys[bc] = new RemotePublicKey(
bcGroup,
bc,
address.toLowerCase(),
aggr.publicKey,
aggr.timestamp,
aggr.registrar,
);
}
}
return remoteKeys;
} else {
return {};
}
};
const indexerRemoteKeys = toReadFromIndexer.length
? await this.indexer.retryingOperation(
async () => {
return readFromIndexer();
},
async () => {
return readFromBlockchains(toReadFromIndexer);
},
)
: {};
const blockchainRemoteKeys = toReadFromBlockchain.length ? await readFromBlockchains(toReadFromBlockchain) : {};
const finalRemoteKeys = { ...indexerRemoteKeys, ...blockchainRemoteKeys };
let freshestKey: RemotePublicKey | null = null;
for (const bc of Object.keys(finalRemoteKeys)) {
const key = finalRemoteKeys[bc];
if (key) {
if (!freshestKey) {
freshestKey = key;
} else if (key.timestamp > freshestKey.timestamp) {
freshestKey = key;
}
}
}
return {
freshestKey,
remoteKeys: finalRemoteKeys,
};
}
async getAddressesKeysHistory(addresses: string[]): Promise<Record<string, RemotePublicKey[]>> {
const blockchains = this.ylide.blockchainsList.map(({ factory }) => ({
factory,
controller: this.controllers.blockchainsMap[factory.blockchain],
}));
const indexerResult: Record<string, RemotePublicKey[]> = {};
// if (this.indexerBlockchains.length) {
// indexerResult = await this.indexer.retryingOperation(
// async () => {
// const rawResult = await this.indexer.requestKeysHistory(addresses);
// const result: Record<string, { key: RemotePublicKey; blockchain: string }[]> = {};
// for (const address of Object.keys(rawResult)) {
// if (!result[address]) {
// result[address] = [];
// }
// for (const kkey of rawResult[address]) {
// result[address].push({
// blockchain: kkey.blockchain,
// key: {
// keyVersion: kkey.keyVersion,
// publicKey: PublicKey.fromBytes(PublicKeyType.YLIDE, kkey.publicKey),
// timestamp: kkey.timestamp,
// registrar: kkey.registrar,
// },
// });
// }
// }
// return result;
// },
// async () => {
// return {};
// },
// );
// }
const toReadFromBlockchain = blockchains; // .filter(b => !this.indexerBlockchains.includes(b.factory.blockchain));
const readFromBlockchains = async (
fromBlockchains: { factory: BlockchainControllerFactory; controller: AbstractBlockchainController }[],
) => {
const blockchainRemoteKeys: Record<string, RemotePublicKey[]> = {};
await Promise.all(
addresses.map(async address => {
const remoteKeys: RemotePublicKey[] = [];
await Promise.all(
fromBlockchains.map(async ({ controller }) => {
try {
if (controller.isAddressValid(address)) {
const keys = await controller.extractPublicKeysHistoryByAddress(address);
remoteKeys.push(...keys);
}
} catch (err) {
// so sad :(
}
}),
);
blockchainRemoteKeys[address] = remoteKeys;
}),
);
return blockchainRemoteKeys;
};
const blockchainsResult = await readFromBlockchains(toReadFromBlockchain);
const globalResult: Record<string, RemotePublicKey[]> = {};
// merge results:
for (const address of addresses) {
globalResult[address] = [...(indexerResult[address] || []), ...(blockchainsResult[address] || [])];
globalResult[address].sort((a, b) => b.timestamp - a.timestamp);
}
return globalResult;
}
getListSources(readingSession: SourceReadingSession, subjects: ISourceSubject[]): IListSource[] {
const sources: IListSource[] = [];
for (const blockchainController of this.controllers.blockchains) {
for (const subject of subjects) {
const blockchainSubjects = blockchainController.getBlockchainSourceSubjects(subject);
for (const blockchainSubject of blockchainSubjects) {
const source = readingSession.getListSource(blockchainSubject);
if (source) {
sources.push(source);
} else {
if (blockchainSubject.sender && !blockchainController.isReadingBySenderAvailable()) {
const shrinkedSubject = {
...blockchainSubject,
sender: null,
};
let originalList = readingSession.getListSource(shrinkedSubject);
if (!originalList) {
const originalSource = blockchainController.ininiateMessagesSource(shrinkedSubject);
originalList = new ListSource(readingSession, shrinkedSubject, originalSource);
readingSession.setListSource(shrinkedSubject, originalList);
}
const filteredList = new PuppetListSource(readingSession, blockchainSubject, originalList);
readingSession.setListSource(blockchainSubject, filteredList);
sources.push(filteredList);
} else {
const originalSource = blockchainController.ininiateMessagesSource(blockchainSubject);
if (this.indexerBlockchains.includes(blockchainController.blockchain())) {
const indexerLowLevel = new IndexerMessagesSource(
originalSource,
this.indexer,
blockchainController.compareMessagesTime.bind(blockchainController),
blockchainSubject,
);
const indexerListSource = new ListSource(
readingSession,
blockchainSubject,
indexerLowLevel,
);
readingSession.setListSource(blockchainSubject, indexerListSource);
sources.push(indexerListSource);
} else {
const listSource = new ListSource(readingSession, blockchainSubject, originalSource);
readingSession.setListSource(blockchainSubject, listSource);
sources.push(listSource);
}
}
}
}
}
}
return sources;
}
async sendMessage(
{
wallet,
sender,
content,
recipients,
secureContext,
feedId,
serviceCode = ServiceCode.SDK,
copyOfSent = true,
}: SendMessageArgs,
walletOptions?: any,
) {
if (!secureContext) {
secureContext = MessageSecureContext.create();
}
const encryptedMessageBlob = MessageBlob.encodeAndPackAndEncrypt(secureContext, content);
const actualRecipients = recipients.map(r => {
const bc = this.controllers.blockchains.find(b => b.isAddressValid(r));
if (!bc) {
throw new YlideError(
YlideErrorType.INVALID_PARAM,
`Address ${r} is not valid for any registered blockchain`,
);
}
return {
keyAddress: bc.addressToUint256(r),
keyAddressOriginal: r,
address: bc.addressToUint256(r),
};
});
if (copyOfSent) {
actualRecipients.push({
keyAddress: wallet.addressToUint256(sender.address),
keyAddressOriginal: sender.address,
address: YlideCore.getSentAddress(wallet.addressToUint256(sender.address)),
});
}
const route = await DynamicEncryptionRouter.findEncyptionRoute(
this,
actualRecipients,
this.controllers.blockchains,
'ylide',
);
const { publicKeys, processedRecipients } = await DynamicEncryptionRouter.executeEncryption(
route,
secureContext,
);
const container = MessageContainer.packContainer(serviceCode, true, publicKeys, encryptedMessageBlob);
return wallet.sendMail(sender, feedId || YLIDE_MAIN_FEED_ID, container, processedRecipients, walletOptions);
}
async broadcastMessage(
{ feedId, wallet, sender, content, serviceCode = ServiceCode.SDK }: BroadcastMessageArgs,
walletOptions?: any,
) {
const encodedMessageBlob = MessageBlob.encodeAndPack(content);
const container = MessageContainer.packContainer(serviceCode, false, [], encodedMessageBlob);
return wallet.sendBroadcast(sender, feedId, container, walletOptions || {});
}
async getMessageByMsgId(msgId: string): Promise<IMessage | null> {
for (const controller of this.controllers.blockchains) {
if (controller.isValidMsgId(msgId)) {
try {
return await controller.getMessageByMsgId(msgId);
} catch (err) {
console.error('Error getting message by msgId', err);
return null;
}
}
}
return null;
}
async getMessageContent(msg: IMessage): Promise<IMessageContent | IMessageCorruptedContent | null> {
const blockchain = this.getMessageBlockchainController(msg);
if (!blockchain) {
return null;
}
if (this.indexerBlockchains.includes(blockchain.blockchain())) {
return await this.indexer.retryingOperation(
async () => {
return await this.indexer.requestContent({ blockchain: msg.blockchain, msgId: msg.msgId });
},
async () => {
return await blockchain.retrieveMessageContent(msg);
},
);
} else {
return await blockchain.retrieveMessageContent(msg);
}
}
getMessageBlockchainController(msg: IMessage): AbstractBlockchainController | null {
const blockchain = this.controllers.blockchainsMap[msg.blockchain];
if (!blockchain) {
return null;
} else {
return blockchain;
}
}
async getMessageWalletController(msg: IMessage, keyAddress?: Uint256): Promise<AbstractWalletController | null> {
const blockchainGroup = this.ylide.blockchainToGroupMap[msg.blockchain];
const walletsMap = this.controllers.walletsMap[blockchainGroup];
if (!walletsMap) {
return null;
}
const address = keyAddress || msg.recipientAddress;
const wallets = Object.values(walletsMap);
for (const wallet of wallets) {
const acc = await wallet.getAuthenticatedAccount();
if (!acc) {
continue;
}
const rec = wallet.addressToUint256(acc.address);
if (rec === address) {
return wallet;
}
}
return null;
}
async getMessageSecureContext(
recipient: WalletAccount,
msg: IMessage,
unpackedContainer: IUnpackedMessageContainer,
handlers?: YlidePrivateKeyHandlers,
) {
const receipientKeyAddress = recipient.address;
const msgKey = MessageKey.fromBytes(msg.key);
const senderPublicKey = unpackedContainer.senderPublicKeys[msgKey.publicKeyIndex];
let symmKey: Uint8Array | null = null;
if (senderPublicKey.type === PublicKeyType.YLIDE) {
const keySignature = msgKey.decryptingPublicKeySignature;
const keysToCheck: YlidePrivateKey[] = [];
if (keySignature) {
const key = this.keyRegistry.getLocalPrivateKeyForPublicKeySignature(
receipientKeyAddress,
keySignature,
);
if (key) {
keysToCheck.push(key);
}
} else {
keysToCheck.push(...this.keyRegistry.getLocalPrivateKeys(receipientKeyAddress));
}
if (!keysToCheck.length) {
throw new YlideError(YlideErrorType.DECRYPTION_KEY_UNAVAILABLE, 'Unable to find decryption key', {
signature: keySignature,
});
}
for (const ylideKey of keysToCheck) {
try {
await ylideKey.execute(async keypair => {
symmKey = keypair.decrypt(msgKey.encryptedMessageKey, senderPublicKey.keyBytes);
}, handlers);
if (symmKey) {
break;
}
} catch (err) {
console.warn('Error decrypting message key', err);
// wrong key, try next
}
}
} else {
const walletAccounts = await Promise.all(
this.controllers.wallets.map(async w => ({
wallet: w,
account: await w.getAuthenticatedAccount(),
})),
);
const accountIdx = walletAccounts.findIndex(a => a.account && a.account.address === receipientKeyAddress);
if (accountIdx === -1) {
throw new YlideError(
YlideErrorType.UNAVAILABLE,
`Native decription: Wallet of this message recipient ${receipientKeyAddress} is not registered`,
);
}
const account = walletAccounts[accountIdx].account;
if (!account) {
throw new YlideError(
YlideErrorType.UNAVAILABLE,
`Native decription: Account of this message recipient ${receipientKeyAddress} is not available`,
);
}
const wallet = walletAccounts[accountIdx].wallet;
symmKey = await wallet.decryptMessageKey(account, senderPublicKey, msgKey.encryptedMessageKey);
}
if (!symmKey) {
throw new YlideError(
YlideErrorType.DECRYPTION_KEY_UNAVAILABLE,
'No decryption key is able to decrypt this message key',
);
}
return new MessageSecureContext(symmKey);
}
async decryptMessageContent(
recipient: WalletAccount,
msg: IMessage,
content: IMessageContent,
secureContext?: MessageSecureContext,
handlers?: YlidePrivateKeyHandlers,
) {
const unpackedContainer = MessageContainer.unpackContainter(content.content);
let decryptedContent: MessageContentV3 | MessageContentV4 | MessageContentV5;
if (unpackedContainer.isEncoded) {
if (!secureContext) {
secureContext = await this.getMessageSecureContext(recipient, msg, unpackedContainer, handlers);
}
decryptedContent = MessageBlob.decryptAndUnpackAndDecode(secureContext, unpackedContainer.messageBlob);
} else {
decryptedContent = MessageBlob.unpackAndDecode(unpackedContainer.messageBlob);
}
return {
content: decryptedContent,
serviceCode: unpackedContainer.serviceCode,
container: unpackedContainer,
};
}
decryptBroadcastContent(msg: IMessage, content: IMessageContent) {
const unpackedContainer = MessageContainer.unpackContainter(content.content);
if (unpackedContainer.isEncoded) {
throw new YlideMisusageError(
'YlideCore',
`Encrypted broadcast content is not supported in YlideCore. You still can decrypt it using raw Ylide crypto primitives.`,
);
}
const decodedContent = MessageBlob.unpackAndDecode(unpackedContainer.messageBlob);
return {
content: decodedContent,
serviceCode: unpackedContainer.serviceCode,
container: unpackedContainer,
};
}
}