@mtcute/convert 0.17.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/convert/src/dcs.cjs +119 -0
  2. package/convert/src/dcs.js +114 -0
  3. package/convert/src/gramjs/convert.cjs +30 -0
  4. package/convert/src/gramjs/convert.js +25 -0
  5. package/convert/src/gramjs/parse.cjs +33 -0
  6. package/convert/src/gramjs/parse.js +28 -0
  7. package/convert/src/gramjs/serialize.cjs +26 -0
  8. package/convert/src/gramjs/serialize.js +21 -0
  9. package/convert/src/mtkruto/convert.cjs +33 -0
  10. package/convert/src/mtkruto/convert.js +28 -0
  11. package/convert/src/mtkruto/parse.cjs +31 -0
  12. package/convert/src/mtkruto/parse.js +26 -0
  13. package/convert/src/mtkruto/serialize.cjs +18 -0
  14. package/convert/src/mtkruto/serialize.js +13 -0
  15. package/convert/src/pyrogram/convert.cjs +42 -0
  16. package/convert/src/pyrogram/convert.js +37 -0
  17. package/convert/src/pyrogram/parse.cjs +52 -0
  18. package/convert/src/pyrogram/parse.js +47 -0
  19. package/convert/src/pyrogram/serialize.cjs +40 -0
  20. package/convert/src/pyrogram/serialize.js +35 -0
  21. package/convert/src/tdesktop/convert.cjs +66 -0
  22. package/convert/src/tdesktop/convert.js +61 -0
  23. package/convert/src/tdesktop/qt-bundle.cjs +11 -0
  24. package/convert/src/tdesktop/qt-bundle.js +6 -0
  25. package/convert/src/tdesktop/qt-reader.cjs +38 -0
  26. package/convert/src/tdesktop/qt-reader.js +33 -0
  27. package/convert/src/tdesktop/qt-writer.cjs +34 -0
  28. package/convert/src/tdesktop/qt-writer.js +29 -0
  29. package/convert/src/tdesktop/tdata.cjs +359 -0
  30. package/convert/src/tdesktop/tdata.js +332 -0
  31. package/convert/src/telethon/convert.cjs +47 -0
  32. package/convert/src/telethon/convert.js +42 -0
  33. package/convert/src/telethon/parse.cjs +39 -0
  34. package/convert/src/telethon/parse.js +34 -0
  35. package/convert/src/telethon/serialize.cjs +34 -0
  36. package/convert/src/telethon/serialize.js +29 -0
  37. package/convert/src/utils/crypto.cjs +53 -0
  38. package/convert/src/utils/crypto.js +26 -0
  39. package/convert/src/utils/rle.cjs +49 -0
  40. package/convert/src/utils/rle.js +44 -0
  41. package/dcs.d.cts +4 -0
  42. package/gramjs/__fixtures__/session.d.cts +1 -0
  43. package/gramjs/convert.d.cts +4 -0
  44. package/gramjs/convert.test.d.cts +1 -0
  45. package/gramjs/index.d.cts +4 -0
  46. package/gramjs/parse.d.cts +2 -0
  47. package/gramjs/parse.test.d.cts +1 -0
  48. package/gramjs/serialize.d.cts +2 -0
  49. package/gramjs/serialize.test.d.cts +1 -0
  50. package/gramjs/types.d.cts +7 -0
  51. package/index.cjs +41 -488
  52. package/index.d.cts +1 -0
  53. package/index.d.ts +1 -0
  54. package/index.js +20 -467
  55. package/mtkruto/__fixtures__/session.d.cts +1 -0
  56. package/mtkruto/convert.d.cts +4 -0
  57. package/mtkruto/convert.test.d.cts +1 -0
  58. package/mtkruto/index.d.cts +4 -0
  59. package/mtkruto/parse.d.cts +2 -0
  60. package/mtkruto/parse.test.d.cts +1 -0
  61. package/mtkruto/serialize.d.cts +2 -0
  62. package/mtkruto/serialize.test.d.cts +1 -0
  63. package/mtkruto/types.d.cts +5 -0
  64. package/node/src/utils/crypto.cjs +75 -0
  65. package/node/src/utils/crypto.js +69 -0
  66. package/node/src/utils.cjs +34 -0
  67. package/node/src/utils.js +7 -0
  68. package/package.json +12 -9
  69. package/pyrogram/__fixtures__/session.d.cts +1 -0
  70. package/pyrogram/__fixtures__/session_old.d.cts +1 -0
  71. package/pyrogram/convert.d.cts +6 -0
  72. package/pyrogram/convert.test.d.cts +1 -0
  73. package/pyrogram/index.d.cts +4 -0
  74. package/pyrogram/parse.d.cts +2 -0
  75. package/pyrogram/parse.test.d.cts +1 -0
  76. package/pyrogram/serialize.d.cts +2 -0
  77. package/pyrogram/serialize.test.d.cts +1 -0
  78. package/pyrogram/types.d.cts +8 -0
  79. package/tdesktop/convert.d.cts +5 -0
  80. package/tdesktop/convert.d.ts +5 -0
  81. package/tdesktop/index.d.cts +5 -0
  82. package/tdesktop/index.d.ts +5 -0
  83. package/tdesktop/qt-bundle.d.cts +3 -0
  84. package/tdesktop/qt-bundle.d.ts +3 -0
  85. package/tdesktop/qt-reader.d.cts +6 -0
  86. package/tdesktop/qt-reader.d.ts +6 -0
  87. package/tdesktop/qt-writer.d.cts +6 -0
  88. package/tdesktop/qt-writer.d.ts +6 -0
  89. package/tdesktop/tdata.d.cts +65 -0
  90. package/tdesktop/tdata.d.ts +65 -0
  91. package/tdesktop/tdata.test.d.cts +1 -0
  92. package/tdesktop/tdata.test.d.ts +1 -0
  93. package/tdesktop/types.d.cts +22 -0
  94. package/tdesktop/types.d.ts +22 -0
  95. package/telethon/__fixtures__/session.d.cts +1 -0
  96. package/telethon/__fixtures__/session_v6.d.cts +1 -0
  97. package/telethon/convert.d.cts +4 -0
  98. package/telethon/convert.test.d.cts +1 -0
  99. package/telethon/index.d.cts +4 -0
  100. package/telethon/parse.d.cts +2 -0
  101. package/telethon/parse.test.d.cts +1 -0
  102. package/telethon/serialize.d.cts +2 -0
  103. package/telethon/serialize.test.d.cts +1 -0
  104. package/telethon/types.d.cts +7 -0
  105. package/types.d.cts +0 -0
  106. package/utils/crypto.d.cts +9 -0
  107. package/utils/crypto.d.ts +9 -0
  108. package/utils/fs.d.cts +11 -0
  109. package/utils/fs.d.ts +11 -0
  110. package/utils/rle.d.cts +3 -0
  111. package/wasm/src/index.cjs +64 -0
  112. package/wasm/src/index.js +59 -0
  113. package/utils/ip.d.ts +0 -3
package/index.js CHANGED
@@ -1,486 +1,39 @@
1
- import { dataViewFromBuffer, readStringSession, TlBinaryReader, TlBinaryWriter, longFromBuffer } from "@mtcute/core/utils.js";
2
- import { MtArgumentError, Long } from "@mtcute/core";
3
- import { getPlatform } from "@mtcute/core/platform.js";
4
- const DC_MAPPING_PROD = {
5
- 1: {
6
- main: {
7
- id: 1,
8
- ipAddress: "149.154.175.56",
9
- port: 443
10
- },
11
- media: {
12
- id: 1,
13
- ipAddress: "149.154.175.211",
14
- port: 443
15
- }
16
- },
17
- 2: {
18
- main: {
19
- id: 2,
20
- ipAddress: "149.154.167.41",
21
- port: 443
22
- },
23
- media: {
24
- id: 2,
25
- ipAddress: "149.154.167.35",
26
- port: 443
27
- }
28
- },
29
- 3: {
30
- main: {
31
- id: 3,
32
- ipAddress: "149.154.175.100",
33
- port: 443
34
- },
35
- media: {
36
- id: 3,
37
- ipAddress: "149.154.175.100",
38
- port: 443
39
- }
40
- },
41
- 4: {
42
- main: {
43
- id: 4,
44
- ipAddress: "149.154.167.91",
45
- port: 443
46
- },
47
- media: {
48
- id: 4,
49
- ipAddress: "149.154.167.255",
50
- port: 443
51
- }
52
- },
53
- 5: {
54
- main: {
55
- id: 5,
56
- ipAddress: "91.108.56.179",
57
- port: 443
58
- },
59
- media: {
60
- id: 5,
61
- ipAddress: "149.154.171.255",
62
- port: 443
63
- }
64
- }
65
- };
66
- const DC_MAPPING_TEST = {
67
- 1: {
68
- main: {
69
- id: 1,
70
- ipAddress: "149.154.175.10",
71
- port: 80
72
- },
73
- media: {
74
- id: 1,
75
- ipAddress: "149.154.175.10",
76
- port: 80
77
- }
78
- },
79
- 2: {
80
- main: {
81
- id: 2,
82
- ipAddress: "149.154.167.40",
83
- port: 443
84
- },
85
- media: {
86
- id: 2,
87
- ipAddress: "149.154.167.40",
88
- port: 443
89
- }
90
- },
91
- 3: {
92
- main: {
93
- id: 3,
94
- ipAddress: "149.154.175.117",
95
- port: 443
96
- },
97
- media: {
98
- id: 3,
99
- ipAddress: "149.154.175.117",
100
- port: 443
101
- }
102
- }
103
- };
104
- function isTestDc(ip) {
105
- return Object.values(DC_MAPPING_TEST).some((dc) => dc.main.ipAddress === ip || dc.media.ipAddress === ip);
106
- }
107
- function parseIpFromBytes(data) {
108
- if (data.length === 4) {
109
- return `${data[0]}.${data[1]}.${data[2]}.${data[3]}`;
110
- }
111
- if (data.length === 16) {
112
- let res = "";
113
- for (let i = 0; i < 16; i += 2) {
114
- res += data[i].toString(16).padStart(2, "0");
115
- res += data[i + 1].toString(16).padStart(2, "0");
116
- if (i < 14) res += ":";
117
- }
118
- return res;
119
- }
120
- throw new MtArgumentError("Invalid IP address length");
121
- }
122
- function serializeIpv4ToBytes(ip, buf) {
123
- const parts = ip.split(".");
124
- if (parts.length !== 4) {
125
- throw new MtArgumentError("Invalid IPv4 address");
126
- }
127
- buf[0] = Number(parts[0]);
128
- buf[1] = Number(parts[1]);
129
- buf[2] = Number(parts[2]);
130
- buf[3] = Number(parts[3]);
131
- }
132
- function serializeIpv6ToBytes(ip, buf) {
133
- const parts = ip.split(":");
134
- if (parts.length !== 8) {
135
- throw new MtArgumentError("Invalid IPv6 address");
136
- }
137
- for (let i = 0; i < 8; i++) {
138
- const val = Number.parseInt(parts[i], 16);
139
- buf[i * 2] = val >> 8;
140
- buf[i * 2 + 1] = val & 255;
141
- }
142
- }
143
- function parseTelethonSession(session) {
144
- if (session[0] !== "1") {
145
- throw new MtArgumentError(`Invalid session string (version = ${session[0]})`);
146
- }
147
- session = session.slice(1);
148
- const data = getPlatform().base64Decode(session, true);
149
- const dv = dataViewFromBuffer(data);
150
- const dcId = dv.getUint8(0);
151
- const ipSize = session.length === 352 ? 4 : 16;
152
- let pos = 1 + ipSize;
153
- const ipBytes = data.subarray(1, pos);
154
- const port = dv.getUint16(pos);
155
- pos += 2;
156
- const authKey = data.subarray(pos, pos + 256);
157
- const ip = parseIpFromBytes(ipBytes);
158
- return {
159
- dcId,
160
- ipAddress: ip,
161
- ipv6: ipSize === 16,
162
- port,
163
- authKey
164
- };
165
- }
166
- function serializeTelethonSession(session) {
167
- if (session.authKey.length !== 256) {
168
- throw new MtArgumentError("authKey must be 256 bytes long");
169
- }
170
- const ipSize = session.ipv6 ? 16 : 4;
171
- const u8 = new Uint8Array(259 + ipSize);
172
- const dv = dataViewFromBuffer(u8);
173
- dv.setUint8(0, session.dcId);
174
- let pos;
175
- if (session.ipv6) {
176
- serializeIpv6ToBytes(session.ipAddress, u8.subarray(1, 17));
177
- pos = 17;
178
- } else {
179
- serializeIpv4ToBytes(session.ipAddress, u8.subarray(1, 5));
180
- pos = 5;
181
- }
182
- dv.setUint16(pos, session.port);
183
- pos += 2;
184
- u8.set(session.authKey, pos);
185
- let b64 = getPlatform().base64Encode(u8, true);
186
- while (b64.length % 4 !== 0) b64 += "=";
187
- return `1${b64}`;
188
- }
189
- function convertFromTelethonSession(session) {
190
- if (typeof session === "string") {
191
- session = parseTelethonSession(session);
192
- }
193
- const dc = {
194
- id: session.dcId,
195
- ipAddress: session.ipAddress,
196
- port: session.port,
197
- ipv6: session.ipv6
198
- };
199
- return {
200
- version: 3,
201
- // we don't exactly have that information. try to deduce it from DC_MAPPING_TEST
202
- // todo: we should maybe check this at connect?
203
- testMode: isTestDc(session.ipAddress),
204
- primaryDcs: {
205
- main: dc,
206
- media: dc
207
- },
208
- authKey: session.authKey
209
- };
210
- }
211
- function convertToTelethonSession(session) {
212
- if (typeof session === "string") {
213
- session = readStringSession(session);
214
- }
215
- return serializeTelethonSession({
216
- dcId: session.primaryDcs.main.id,
217
- ipAddress: session.primaryDcs.main.ipAddress,
218
- port: session.primaryDcs.main.port,
219
- ipv6: session.primaryDcs.main.ipv6 ?? false,
220
- authKey: session.authKey
221
- });
222
- }
223
- function parseGramjsSession(session) {
224
- if (session[0] !== "1") {
225
- throw new MtArgumentError(`Invalid session string (version = ${session[0]})`);
226
- }
227
- session = session.slice(1);
228
- const data = getPlatform().base64Decode(session);
229
- const dv = dataViewFromBuffer(data);
230
- const dcId = dv.getUint8(0);
231
- const ipSize = dv.getUint16(1);
232
- let pos = 3 + ipSize;
233
- const ip = getPlatform().utf8Decode(data.subarray(3, pos));
234
- const port = dv.getUint16(pos);
235
- pos += 2;
236
- const authKey = data.subarray(pos, pos + 256);
237
- return {
238
- dcId,
239
- ipAddress: ip,
240
- ipv6: ip.includes(":"),
241
- // dumb check but gramjs does this
242
- port,
243
- authKey
244
- };
245
- }
246
- function serializeGramjsSession(session) {
247
- if (session.authKey.length !== 256) {
248
- throw new MtArgumentError("authKey must be 256 bytes long");
249
- }
250
- const ipEncoded = getPlatform().utf8Encode(session.ipAddress);
251
- const u8 = new Uint8Array(261 + ipEncoded.length);
252
- const dv = dataViewFromBuffer(u8);
253
- dv.setUint8(0, session.dcId);
254
- dv.setUint16(1, ipEncoded.length);
255
- u8.set(ipEncoded, 3);
256
- let pos = 3 + ipEncoded.length;
257
- dv.setUint16(pos, session.port);
258
- pos += 2;
259
- u8.set(session.authKey, pos);
260
- return `1${getPlatform().base64Encode(u8)}`;
261
- }
262
- function convertFromGramjsSession(session) {
263
- if (typeof session === "string") {
264
- session = parseGramjsSession(session);
265
- }
266
- return convertFromTelethonSession(session);
267
- }
268
- function convertToGramjsSession(session) {
269
- if (typeof session === "string") {
270
- session = readStringSession(session);
271
- }
272
- return serializeGramjsSession({
273
- dcId: session.primaryDcs.main.id,
274
- ipAddress: session.primaryDcs.main.ipAddress,
275
- port: session.primaryDcs.main.port,
276
- ipv6: session.primaryDcs.main.ipv6 ?? false,
277
- authKey: session.authKey
278
- });
279
- }
280
- function telegramRleEncode(buf) {
281
- const len = buf.length;
282
- const ret = [];
283
- let count = 0;
284
- for (let i = 0; i < len; i++) {
285
- const cur = buf[i];
286
- if (cur === 0) {
287
- count += 1;
288
- } else {
289
- if (count > 0) {
290
- ret.push(0, count);
291
- count = 0;
292
- }
293
- ret.push(cur);
294
- }
295
- }
296
- if (count > 0) {
297
- ret.push(0, count);
298
- }
299
- return new Uint8Array(ret);
300
- }
301
- function telegramRleDecode(buf) {
302
- const len = buf.length;
303
- const ret = [];
304
- let prev = -1;
305
- for (let i = 0; i < len; i++) {
306
- const cur = buf[i];
307
- if (prev === 0) {
308
- for (let j = 0; j < cur; j++) {
309
- ret.push(prev);
310
- }
311
- prev = -1;
312
- } else {
313
- if (prev !== -1) ret.push(prev);
314
- prev = cur;
315
- }
316
- }
317
- if (prev !== -1) ret.push(prev);
318
- return new Uint8Array(ret);
319
- }
320
- function parseMtkrutoSession(session) {
321
- const data = telegramRleDecode(getPlatform().base64Decode(session, true));
322
- const reader = TlBinaryReader.manual(data);
323
- let dcIdStr = reader.string();
324
- const authKey = reader.bytes();
325
- const isTest = dcIdStr.endsWith("-test");
326
- if (isTest) {
327
- dcIdStr = dcIdStr.slice(0, -5);
328
- }
329
- const dcId = Number(dcIdStr);
330
- if (Number.isNaN(dcId)) {
331
- throw new MtArgumentError(`Invalid DC ID: ${dcIdStr}`);
332
- }
333
- return {
334
- dcId,
335
- isTest,
336
- authKey
337
- };
338
- }
339
- function serializeMtkrutoSession(session) {
340
- const dcIdStr = `${session.dcId}${session.isTest ? "-test" : ""}`;
341
- const writer = TlBinaryWriter.manual(session.authKey.length + dcIdStr.length + 8);
342
- writer.string(dcIdStr);
343
- writer.bytes(session.authKey);
344
- return getPlatform().base64Encode(telegramRleEncode(writer.result()), true);
345
- }
346
- function convertFromMtkrutoSession(session) {
347
- if (typeof session === "string") {
348
- session = parseMtkrutoSession(session);
349
- }
350
- return {
351
- version: 3,
352
- testMode: session.isTest,
353
- primaryDcs: (session.isTest ? DC_MAPPING_TEST : DC_MAPPING_PROD)[session.dcId],
354
- authKey: session.authKey
355
- };
356
- }
357
- function convertToMtkrutoSession(session) {
358
- if (typeof session === "string") {
359
- session = readStringSession(session);
360
- }
361
- return serializeMtkrutoSession({
362
- dcId: session.primaryDcs.main.id,
363
- isTest: session.testMode,
364
- authKey: session.authKey
365
- });
366
- }
367
- const SESSION_STRING_SIZE$1 = 351;
368
- const SESSION_STRING_SIZE_64 = 356;
369
- function parsePyrogramSession(session) {
370
- const data = getPlatform().base64Decode(session, true);
371
- const dv = dataViewFromBuffer(data);
372
- if (session.length === SESSION_STRING_SIZE$1 || session.length === SESSION_STRING_SIZE_64) {
373
- const dcId2 = dv.getUint8(0);
374
- const isTest2 = dv.getUint8(1) !== 0;
375
- const authKey2 = data.subarray(2, 258);
376
- let userId2;
377
- if (data.length === SESSION_STRING_SIZE$1) {
378
- userId2 = dv.getUint32(258);
379
- } else {
380
- const high = dv.getUint32(258);
381
- const low = dv.getUint32(262);
382
- userId2 = Long.fromBits(low, high).toNumber();
383
- }
384
- const isBot2 = dv.getUint8(data.length - 1) !== 0;
385
- return {
386
- dcId: dcId2,
387
- isTest: isTest2,
388
- authKey: authKey2,
389
- userId: userId2,
390
- isBot: isBot2
391
- };
392
- }
393
- const dcId = dv.getUint8(0);
394
- const apiId = dv.getUint32(1);
395
- const isTest = dv.getUint8(5) !== 0;
396
- const authKey = data.subarray(6, 262);
397
- const userId = longFromBuffer(data.subarray(262, 270), true, false).toNumber();
398
- const isBot = dv.getUint8(270) !== 0;
399
- return {
400
- dcId,
401
- apiId,
402
- isTest,
403
- authKey,
404
- userId,
405
- isBot
406
- };
407
- }
408
- const SESSION_STRING_SIZE_OLD = 267;
409
- const SESSION_STRING_SIZE = 271;
410
- function serializePyrogramSession(session) {
411
- if (session.authKey.length !== 256) {
412
- throw new MtArgumentError("authKey must be 256 bytes long");
413
- }
414
- const userIdLong = Long.fromNumber(session.userId, true);
415
- let u8;
416
- if (session.apiId === void 0) {
417
- u8 = new Uint8Array(SESSION_STRING_SIZE_OLD);
418
- const dv = dataViewFromBuffer(u8);
419
- dv.setUint8(0, session.dcId);
420
- dv.setUint8(1, session.isTest ? 1 : 0);
421
- u8.set(session.authKey, 2);
422
- dv.setUint32(258, userIdLong.high);
423
- dv.setUint32(262, userIdLong.low);
424
- dv.setUint8(266, session.isBot ? 1 : 0);
425
- } else {
426
- u8 = new Uint8Array(SESSION_STRING_SIZE);
427
- const dv = dataViewFromBuffer(u8);
428
- dv.setUint8(0, session.dcId);
429
- dv.setUint32(1, session.apiId);
430
- dv.setUint8(5, session.isTest ? 1 : 0);
431
- u8.set(session.authKey, 6);
432
- dv.setUint32(262, userIdLong.high);
433
- dv.setUint32(266, userIdLong.low);
434
- dv.setUint8(270, session.isBot ? 1 : 0);
435
- }
436
- return getPlatform().base64Encode(u8, true);
437
- }
438
- function convertFromPyrogramSession(session) {
439
- if (typeof session === "string") {
440
- session = parsePyrogramSession(session);
441
- }
442
- return {
443
- version: 3,
444
- testMode: session.isTest,
445
- primaryDcs: (session.isTest ? DC_MAPPING_TEST : DC_MAPPING_PROD)[session.dcId],
446
- authKey: session.authKey,
447
- self: {
448
- userId: session.userId,
449
- isBot: session.isBot,
450
- isPremium: false,
451
- usernames: []
452
- }
453
- };
454
- }
455
- function convertToPyrogramSession(session, params) {
456
- if (typeof session === "string") {
457
- session = readStringSession(session);
458
- }
459
- return serializePyrogramSession({
460
- apiId: params?.apiId,
461
- isBot: session.self?.isBot ?? false,
462
- isTest: session.testMode,
463
- userId: session.self?.userId ?? 0,
464
- dcId: session.primaryDcs.main.id,
465
- authKey: session.authKey
466
- });
467
- }
1
+ import { DC_MAPPING_PROD, DC_MAPPING_TEST, isTestDc } from "./convert/src/dcs.js";
2
+ import { convertFromGramjsSession, convertToGramjsSession } from "./convert/src/gramjs/convert.js";
3
+ import { parseGramjsSession } from "./convert/src/gramjs/parse.js";
4
+ import { serializeGramjsSession } from "./convert/src/gramjs/serialize.js";
5
+ import { convertFromMtkrutoSession, convertToMtkrutoSession } from "./convert/src/mtkruto/convert.js";
6
+ import { parseMtkrutoSession } from "./convert/src/mtkruto/parse.js";
7
+ import { serializeMtkrutoSession } from "./convert/src/mtkruto/serialize.js";
8
+ import { convertFromPyrogramSession, convertToPyrogramSession } from "./convert/src/pyrogram/convert.js";
9
+ import { parsePyrogramSession } from "./convert/src/pyrogram/parse.js";
10
+ import { serializePyrogramSession } from "./convert/src/pyrogram/serialize.js";
11
+ import { convertFromTdata, convertToTdata } from "./convert/src/tdesktop/convert.js";
12
+ import { Tdata } from "./convert/src/tdesktop/tdata.js";
13
+ import * as qtBundle from "./convert/src/tdesktop/qt-bundle.js";
14
+ import { convertFromTelethonSession, convertToTelethonSession } from "./convert/src/telethon/convert.js";
15
+ import { parseTelethonSession } from "./convert/src/telethon/parse.js";
16
+ import { serializeTelethonSession } from "./convert/src/telethon/serialize.js";
468
17
  export {
469
18
  DC_MAPPING_PROD,
470
19
  DC_MAPPING_TEST,
20
+ Tdata,
471
21
  convertFromGramjsSession,
472
22
  convertFromMtkrutoSession,
473
23
  convertFromPyrogramSession,
24
+ convertFromTdata,
474
25
  convertFromTelethonSession,
475
26
  convertToGramjsSession,
476
27
  convertToMtkrutoSession,
477
28
  convertToPyrogramSession,
29
+ convertToTdata,
478
30
  convertToTelethonSession,
479
31
  isTestDc,
480
32
  parseGramjsSession,
481
33
  parseMtkrutoSession,
482
34
  parsePyrogramSession,
483
35
  parseTelethonSession,
36
+ qtBundle as qt,
484
37
  serializeGramjsSession,
485
38
  serializeMtkrutoSession,
486
39
  serializePyrogramSession,
@@ -0,0 +1 @@
1
+ export declare const MTKRUTO_SESSION = "BjItdGVzdAAB_gABAQABWEIKa07Ch-9zoA024mDOpsv20TW4YwuoRRROqSi41YQCbD3c4nKnz7BcFIu1mfn6f6Xm3OTVqoT0zib4p_AuZD9H-t8j5AagecRg-oSpQlmjoiUazKQSxnxWotGWf1mPNntAeOvDNa5t1NjXUxmqdB3e2AjYLF_E2jDESVgUuDBQUMBHIDc_xFBAlz6kVxCZ6iINJHbnyJ2F19tbEPFJvSM999RKaFj5lUUVs0qKNXEUmsFYUuIdPBzjWilY8Uvf9nYU_xXd9CUAAXS5_i4aaWlHoTIf3zn8ZEINhDIU1DMauh5vhSWt7F0fkxODjtou-7PdIunuDtqyQm4steuNJc8";
@@ -0,0 +1,4 @@
1
+ import { StringSessionData } from '@mtcute/core/utils.js';
2
+ import { MtkrutoSession } from './types.js';
3
+ export declare function convertFromMtkrutoSession(session: MtkrutoSession | string): StringSessionData;
4
+ export declare function convertToMtkrutoSession(session: StringSessionData | string): string;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ export * from './convert.js';
2
+ export * from './parse.js';
3
+ export * from './serialize.js';
4
+ export * from './types.js';
@@ -0,0 +1,2 @@
1
+ import { MtkrutoSession } from './types.js';
2
+ export declare function parseMtkrutoSession(session: string): MtkrutoSession;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import { MtkrutoSession } from './types.js';
2
+ export declare function serializeMtkrutoSession(session: MtkrutoSession): string;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ export interface MtkrutoSession {
2
+ dcId: number;
3
+ isTest: boolean;
4
+ authKey: Uint8Array;
5
+ }
@@ -0,0 +1,75 @@
1
+ if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WARNED) {
2
+ globalThis._MTCUTE_CJS_DEPRECATION_WARNED = true;
3
+ console.warn("[mtcute-workspace] CommonJS support is deprecated and will be removed in 0.20.0. Please consider switching to ESM, it's " + (/* @__PURE__ */ new Date()).getFullYear() + " already.");
4
+ console.warn("[mtcute-workspace] Learn more about switching to ESM: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c");
5
+ }
6
+ "use strict";
7
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
8
+ const node_crypto = require("node:crypto");
9
+ const promises = require("node:fs/promises");
10
+ const node_module = require("node:module");
11
+ const node_zlib = require("node:zlib");
12
+ const utils_js = require("@mtcute/core/utils.js");
13
+ const index = require("../../../wasm/src/index.cjs");
14
+ var _documentCurrentScript = typeof document !== "undefined" ? document.currentScript : null;
15
+ class BaseNodeCryptoProvider extends utils_js.BaseCryptoProvider {
16
+ createAesCtr(key, iv) {
17
+ const cipher = node_crypto.createCipheriv(`aes-${key.length * 8}-ctr`, key, iv);
18
+ const update = (data) => cipher.update(data);
19
+ return {
20
+ process: update
21
+ };
22
+ }
23
+ pbkdf2(password, salt, iterations, keylen = 64, algo = "sha512") {
24
+ return new Promise(
25
+ (resolve, reject) => node_crypto.pbkdf2(password, salt, iterations, keylen, algo, (err, buf) => err !== null ? reject(err) : resolve(buf))
26
+ );
27
+ }
28
+ sha1(data) {
29
+ return node_crypto.createHash("sha1").update(data).digest();
30
+ }
31
+ sha256(data) {
32
+ return node_crypto.createHash("sha256").update(data).digest();
33
+ }
34
+ hmacSha256(data, key) {
35
+ return node_crypto.createHmac("sha256", key).update(data).digest();
36
+ }
37
+ gzip(data, maxSize) {
38
+ try {
39
+ return node_zlib.deflateSync(data, {
40
+ maxOutputLength: maxSize
41
+ });
42
+ } catch (e) {
43
+ if (e.code === "ERR_BUFFER_TOO_LARGE") {
44
+ return null;
45
+ }
46
+ throw e;
47
+ }
48
+ }
49
+ gunzip(data) {
50
+ return node_zlib.gunzipSync(data);
51
+ }
52
+ randomFill(buf) {
53
+ node_crypto.randomFillSync(buf);
54
+ }
55
+ }
56
+ class NodeCryptoProvider extends BaseNodeCryptoProvider {
57
+ async initialize() {
58
+ const require$1 = node_module.createRequire(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === "SCRIPT" && _documentCurrentScript.src || new URL("node/src/utils/crypto.cjs", document.baseURI).href);
59
+ const wasmFile = require$1.resolve("@mtcute/wasm/mtcute.wasm");
60
+ const wasm = await promises.readFile(wasmFile);
61
+ index.initSync(wasm);
62
+ }
63
+ createAesIge(key, iv) {
64
+ return {
65
+ encrypt(data) {
66
+ return index.ige256Encrypt(data, key, iv);
67
+ },
68
+ decrypt(data) {
69
+ return index.ige256Decrypt(data, key, iv);
70
+ }
71
+ };
72
+ }
73
+ }
74
+ exports.BaseNodeCryptoProvider = BaseNodeCryptoProvider;
75
+ exports.NodeCryptoProvider = NodeCryptoProvider;
@@ -0,0 +1,69 @@
1
+ import { createCipheriv, pbkdf2, createHash, createHmac, randomFillSync } from "node:crypto";
2
+ import { readFile } from "node:fs/promises";
3
+ import { createRequire } from "node:module";
4
+ import { deflateSync, gunzipSync } from "node:zlib";
5
+ import { BaseCryptoProvider } from "@mtcute/core/utils.js";
6
+ import { initSync, ige256Decrypt, ige256Encrypt } from "../../../wasm/src/index.js";
7
+ class BaseNodeCryptoProvider extends BaseCryptoProvider {
8
+ createAesCtr(key, iv) {
9
+ const cipher = createCipheriv(`aes-${key.length * 8}-ctr`, key, iv);
10
+ const update = (data) => cipher.update(data);
11
+ return {
12
+ process: update
13
+ };
14
+ }
15
+ pbkdf2(password, salt, iterations, keylen = 64, algo = "sha512") {
16
+ return new Promise(
17
+ (resolve, reject) => pbkdf2(password, salt, iterations, keylen, algo, (err, buf) => err !== null ? reject(err) : resolve(buf))
18
+ );
19
+ }
20
+ sha1(data) {
21
+ return createHash("sha1").update(data).digest();
22
+ }
23
+ sha256(data) {
24
+ return createHash("sha256").update(data).digest();
25
+ }
26
+ hmacSha256(data, key) {
27
+ return createHmac("sha256", key).update(data).digest();
28
+ }
29
+ gzip(data, maxSize) {
30
+ try {
31
+ return deflateSync(data, {
32
+ maxOutputLength: maxSize
33
+ });
34
+ } catch (e) {
35
+ if (e.code === "ERR_BUFFER_TOO_LARGE") {
36
+ return null;
37
+ }
38
+ throw e;
39
+ }
40
+ }
41
+ gunzip(data) {
42
+ return gunzipSync(data);
43
+ }
44
+ randomFill(buf) {
45
+ randomFillSync(buf);
46
+ }
47
+ }
48
+ class NodeCryptoProvider extends BaseNodeCryptoProvider {
49
+ async initialize() {
50
+ const require2 = createRequire(import.meta.url);
51
+ const wasmFile = require2.resolve("@mtcute/wasm/mtcute.wasm");
52
+ const wasm = await readFile(wasmFile);
53
+ initSync(wasm);
54
+ }
55
+ createAesIge(key, iv) {
56
+ return {
57
+ encrypt(data) {
58
+ return ige256Encrypt(data, key, iv);
59
+ },
60
+ decrypt(data) {
61
+ return ige256Decrypt(data, key, iv);
62
+ }
63
+ };
64
+ }
65
+ }
66
+ export {
67
+ BaseNodeCryptoProvider,
68
+ NodeCryptoProvider
69
+ };