@mtcute/convert 0.17.0 → 0.18.0-rc.5

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 (62) 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/index.cjs +41 -488
  42. package/index.d.ts +1 -0
  43. package/index.js +20 -467
  44. package/node/src/utils/crypto.cjs +75 -0
  45. package/node/src/utils/crypto.js +69 -0
  46. package/node/src/utils.cjs +34 -0
  47. package/node/src/utils.js +7 -0
  48. package/package.json +15 -12
  49. package/tdesktop/convert.d.ts +5 -0
  50. package/tdesktop/index.d.ts +5 -0
  51. package/tdesktop/qt-bundle.d.ts +3 -0
  52. package/tdesktop/qt-reader.d.ts +6 -0
  53. package/tdesktop/qt-writer.d.ts +6 -0
  54. package/tdesktop/tdata.d.ts +65 -0
  55. package/tdesktop/tdata.test.d.ts +1 -0
  56. package/tdesktop/types.d.ts +22 -0
  57. package/utils/crypto.d.ts +9 -0
  58. package/utils/fs.d.ts +11 -0
  59. package/wasm/src/index.cjs +64 -0
  60. package/wasm/src/index.js +59 -0
  61. package/index.d.cts +0 -5
  62. package/utils/ip.d.ts +0 -3
package/package.json CHANGED
@@ -1,11 +1,21 @@
1
1
  {
2
2
  "name": "@mtcute/convert",
3
3
  "type": "module",
4
- "version": "0.17.0",
4
+ "version": "0.18.0-rc.5",
5
5
  "description": "Cross-library session conversion utilities",
6
- "author": "alina sireneva <alina@tei.su>",
7
6
  "license": "MIT",
8
- "sideEffects": false,
7
+ "homepage": "https://mtcute.dev",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/mtcute/mtcute.git"
11
+ },
12
+ "scripts": {},
13
+ "dependencies": {
14
+ "@mtcute/core": "0.18.0-rc.5",
15
+ "@fuman/utils": "0.0.3",
16
+ "@fuman/net": "0.0.3",
17
+ "@fuman/io": "0.0.3"
18
+ },
9
19
  "exports": {
10
20
  ".": {
11
21
  "import": {
@@ -18,13 +28,6 @@
18
28
  }
19
29
  }
20
30
  },
21
- "scripts": {},
22
- "dependencies": {
23
- "@mtcute/core": "^0.17.0"
24
- },
25
- "homepage": "https://mtcute.dev",
26
- "repository": {
27
- "type": "git",
28
- "url": "https://github.com/mtcute/mtcute"
29
- }
31
+ "author": "alina sireneva <alina@tei.su>",
32
+ "sideEffects": false
30
33
  }
@@ -0,0 +1,5 @@
1
+ import { MaybeArray } from '@fuman/utils';
2
+ import { TdataOptions, Tdata } from './tdata.js';
3
+ import { StringSessionData } from '@mtcute/core/utils.js';
4
+ export declare function convertFromTdata(tdata: Tdata | TdataOptions, accountIdx?: number): Promise<StringSessionData>;
5
+ export declare function convertToTdata(sessions: MaybeArray<StringSessionData | string>, tdata: Tdata | TdataOptions): Promise<void>;
@@ -0,0 +1,5 @@
1
+ import * as qt from './qt-bundle.js';
2
+ export { qt };
3
+ export * from './convert.js';
4
+ export * from './tdata.js';
5
+ export * from './types.js';
@@ -0,0 +1,3 @@
1
+ import * as read from './qt-reader.js';
2
+ import * as write from './qt-writer.js';
3
+ export { read, write };
@@ -0,0 +1,6 @@
1
+ import { ISyncReadable } from '@fuman/io';
2
+ import { Long } from '@mtcute/core';
3
+ export declare function readQByteArray(readable: ISyncReadable): Uint8Array;
4
+ export declare function readLong(readable: ISyncReadable): Long;
5
+ export declare function readCharArray(readable: ISyncReadable): Uint8Array;
6
+ export declare function readQString(readable: ISyncReadable): string;
@@ -0,0 +1,6 @@
1
+ import { ISyncWritable } from '@fuman/io';
2
+ import { Long } from '@mtcute/core';
3
+ export declare function writeQByteArray(into: ISyncWritable, buf: Uint8Array): void;
4
+ export declare function writeLong(into: ISyncWritable, long: Long): void;
5
+ export declare function writeCharArray(into: ISyncWritable, buf: Uint8Array): void;
6
+ export declare function writeQString(into: ISyncWritable, str: string): void;
@@ -0,0 +1,65 @@
1
+ import { INodeFsLike } from '../utils/fs.js';
2
+ import { InputTdKeyData, TdKeyData, TdMtpAuthorization } from './types.js';
3
+ import { IExtendedCryptoProvider } from '../utils/crypto.js';
4
+ export interface TdataOptions {
5
+ /** Full path to the tdata directory */
6
+ path: string;
7
+ /**
8
+ * File system to use for reading/writing.
9
+ *
10
+ * @default `import('node:fs/promises')`
11
+ */
12
+ fs?: INodeFsLike;
13
+ /**
14
+ * Crypto functions to use for encryption/decryption.
15
+ *
16
+ * @default `node:crypto`-based implementation
17
+ */
18
+ crypto?: IExtendedCryptoProvider;
19
+ /**
20
+ * Whether to ignore TDF version mismatch.
21
+ * If set to `true`, the version will be ignored and the file will be read as is,
22
+ * however the probability of errors is higher.
23
+ */
24
+ ignoreVersion?: boolean;
25
+ /**
26
+ * Whether the host machine has LE processor (default true, try changing in case of errors)
27
+ */
28
+ le?: boolean;
29
+ /**
30
+ * Value of -key cli parameter.
31
+ * Defaults to `data`
32
+ */
33
+ dataKey?: string;
34
+ /**
35
+ * Local passcode
36
+ */
37
+ passcode?: string;
38
+ }
39
+ export declare class Tdata {
40
+ #private;
41
+ readonly options: TdataOptions;
42
+ readonly fs: INodeFsLike;
43
+ readonly crypto: IExtendedCryptoProvider;
44
+ private constructor();
45
+ readonly keyData: TdKeyData;
46
+ static open(options: TdataOptions): Promise<Tdata>;
47
+ static create(options: TdataOptions & {
48
+ keyData: InputTdKeyData;
49
+ }): Promise<Tdata>;
50
+ getDataName(idx: number): string;
51
+ readFile(filename: string): Promise<[number, Uint8Array]>;
52
+ writeFile(filename: string, data: Uint8Array, mkdir?: boolean): Promise<void>;
53
+ createLocalKey(salt: Uint8Array, passcode?: string): Promise<Uint8Array>;
54
+ decryptLocal(encrypted: Uint8Array, key: Uint8Array): Promise<Uint8Array>;
55
+ encryptLocal(data: Uint8Array, key: Uint8Array): Promise<Uint8Array>;
56
+ readKeyData(): Promise<TdKeyData>;
57
+ writeKeyData(keyData: TdKeyData): Promise<void>;
58
+ computeDataNameKey(accountIdx: number): Promise<Uint8Array>;
59
+ computeDataNameKeyHex(accountIdx: number): Promise<string>;
60
+ readEncryptedFile(filename: string): Promise<[number, Uint8Array]>;
61
+ writeEncryptedFile(filename: string, data: Uint8Array, mkdir?: boolean): Promise<void>;
62
+ readMtpAuthorization(accountIdx?: number): Promise<TdMtpAuthorization>;
63
+ writeMtpAuthorization(auth: TdMtpAuthorization, accountIdx?: number): Promise<void>;
64
+ writeEmptyMapFile(accountIdx: number): Promise<void>;
65
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,22 @@
1
+ import { Long } from '@mtcute/core';
2
+ export interface TdAuthKey {
3
+ dcId: number;
4
+ key: Uint8Array;
5
+ }
6
+ export interface TdMtpAuthorization {
7
+ userId: Long;
8
+ mainDcId: number;
9
+ authKeys: TdAuthKey[];
10
+ authKeysToDestroy: TdAuthKey[];
11
+ }
12
+ export interface InputTdKeyData {
13
+ localKey?: Uint8Array;
14
+ version?: number;
15
+ count: number;
16
+ order: number[];
17
+ active: number;
18
+ }
19
+ export interface TdKeyData extends InputTdKeyData {
20
+ version: number;
21
+ localKey: Uint8Array;
22
+ }
@@ -0,0 +1,9 @@
1
+ import { MaybePromise } from '@fuman/utils';
2
+ import { ICryptoProvider } from '@mtcute/core/utils.js';
3
+ export interface IExtendedCryptoProvider extends ICryptoProvider {
4
+ createHash(algorithm: 'md5' | 'sha512'): MaybePromise<{
5
+ update(data: Uint8Array): MaybePromise<void>;
6
+ digest(): MaybePromise<Uint8Array>;
7
+ }>;
8
+ }
9
+ export declare function getDefaultCryptoProvider(): Promise<IExtendedCryptoProvider>;
package/utils/fs.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ export interface INodeFsLike {
2
+ readFile(path: string): Promise<Uint8Array>;
3
+ writeFile(path: string, data: Uint8Array): Promise<void>;
4
+ mkdir(path: string, options?: {
5
+ recursive?: boolean;
6
+ }): Promise<void>;
7
+ stat(path: string): Promise<{
8
+ size: number;
9
+ lastModified: number;
10
+ }>;
11
+ }
@@ -0,0 +1,64 @@
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
+ let wasm;
9
+ let sharedKeyPtr;
10
+ let sharedIvPtr;
11
+ let cachedUint8Memory = null;
12
+ function initCommon() {
13
+ wasm.libdeflate_alloc_compressor(6);
14
+ wasm.libdeflate_alloc_decompressor();
15
+ wasm.__get_shared_out();
16
+ sharedKeyPtr = wasm.__get_shared_key_buffer();
17
+ sharedIvPtr = wasm.__get_shared_iv_buffer();
18
+ }
19
+ function getUint8Memory() {
20
+ if (cachedUint8Memory === null || cachedUint8Memory.byteLength === 0) {
21
+ cachedUint8Memory = new Uint8Array(wasm.memory.buffer);
22
+ }
23
+ return cachedUint8Memory;
24
+ }
25
+ function initSync(module2) {
26
+ if (wasm !== void 0) return;
27
+ if (!(module2 instanceof WebAssembly.Instance)) {
28
+ if (!(module2 instanceof WebAssembly.Module)) {
29
+ module2 = new WebAssembly.Module(module2);
30
+ }
31
+ module2 = new WebAssembly.Instance(module2);
32
+ }
33
+ wasm = module2.exports;
34
+ initCommon();
35
+ }
36
+ function ige256Encrypt(data, key, iv) {
37
+ const ptr = wasm.__malloc(data.length + data.length);
38
+ const inputPtr = ptr;
39
+ const outputPtr = inputPtr + data.length;
40
+ const mem = getUint8Memory();
41
+ mem.set(data, inputPtr);
42
+ mem.set(key, sharedKeyPtr);
43
+ mem.set(iv, sharedIvPtr);
44
+ wasm.ige256_encrypt(inputPtr, data.length, outputPtr);
45
+ const result = mem.slice(outputPtr, outputPtr + data.length);
46
+ wasm.__free(ptr);
47
+ return result;
48
+ }
49
+ function ige256Decrypt(data, key, iv) {
50
+ const ptr = wasm.__malloc(data.length + data.length);
51
+ const inputPtr = ptr;
52
+ const outputPtr = inputPtr + data.length;
53
+ const mem = getUint8Memory();
54
+ mem.set(data, inputPtr);
55
+ mem.set(key, sharedKeyPtr);
56
+ mem.set(iv, sharedIvPtr);
57
+ wasm.ige256_decrypt(inputPtr, data.length, outputPtr);
58
+ const result = mem.slice(outputPtr, outputPtr + data.length);
59
+ wasm.__free(ptr);
60
+ return result;
61
+ }
62
+ exports.ige256Decrypt = ige256Decrypt;
63
+ exports.ige256Encrypt = ige256Encrypt;
64
+ exports.initSync = initSync;
@@ -0,0 +1,59 @@
1
+ let wasm;
2
+ let sharedKeyPtr;
3
+ let sharedIvPtr;
4
+ let cachedUint8Memory = null;
5
+ function initCommon() {
6
+ wasm.libdeflate_alloc_compressor(6);
7
+ wasm.libdeflate_alloc_decompressor();
8
+ wasm.__get_shared_out();
9
+ sharedKeyPtr = wasm.__get_shared_key_buffer();
10
+ sharedIvPtr = wasm.__get_shared_iv_buffer();
11
+ }
12
+ function getUint8Memory() {
13
+ if (cachedUint8Memory === null || cachedUint8Memory.byteLength === 0) {
14
+ cachedUint8Memory = new Uint8Array(wasm.memory.buffer);
15
+ }
16
+ return cachedUint8Memory;
17
+ }
18
+ function initSync(module) {
19
+ if (wasm !== void 0) return;
20
+ if (!(module instanceof WebAssembly.Instance)) {
21
+ if (!(module instanceof WebAssembly.Module)) {
22
+ module = new WebAssembly.Module(module);
23
+ }
24
+ module = new WebAssembly.Instance(module);
25
+ }
26
+ wasm = module.exports;
27
+ initCommon();
28
+ }
29
+ function ige256Encrypt(data, key, iv) {
30
+ const ptr = wasm.__malloc(data.length + data.length);
31
+ const inputPtr = ptr;
32
+ const outputPtr = inputPtr + data.length;
33
+ const mem = getUint8Memory();
34
+ mem.set(data, inputPtr);
35
+ mem.set(key, sharedKeyPtr);
36
+ mem.set(iv, sharedIvPtr);
37
+ wasm.ige256_encrypt(inputPtr, data.length, outputPtr);
38
+ const result = mem.slice(outputPtr, outputPtr + data.length);
39
+ wasm.__free(ptr);
40
+ return result;
41
+ }
42
+ function ige256Decrypt(data, key, iv) {
43
+ const ptr = wasm.__malloc(data.length + data.length);
44
+ const inputPtr = ptr;
45
+ const outputPtr = inputPtr + data.length;
46
+ const mem = getUint8Memory();
47
+ mem.set(data, inputPtr);
48
+ mem.set(key, sharedKeyPtr);
49
+ mem.set(iv, sharedIvPtr);
50
+ wasm.ige256_decrypt(inputPtr, data.length, outputPtr);
51
+ const result = mem.slice(outputPtr, outputPtr + data.length);
52
+ wasm.__free(ptr);
53
+ return result;
54
+ }
55
+ export {
56
+ ige256Decrypt,
57
+ ige256Encrypt,
58
+ initSync
59
+ };
package/index.d.cts DELETED
@@ -1,5 +0,0 @@
1
- export * from './dcs.js';
2
- export * from './gramjs/index.js';
3
- export * from './mtkruto/index.js';
4
- export * from './pyrogram/index.js';
5
- export * from './telethon/index.js';
package/utils/ip.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export declare function parseIpFromBytes(data: Uint8Array): string;
2
- export declare function serializeIpv4ToBytes(ip: string, buf: Uint8Array): void;
3
- export declare function serializeIpv6ToBytes(ip: string, buf: Uint8Array): void;