@mtcute/core 0.1.0 → 0.1.1
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.
- package/cjs/base-client.js +4 -1
- package/cjs/base-client.js.map +1 -1
- package/cjs/network/auth-key.d.ts +3 -3
- package/cjs/network/auth-key.js +12 -12
- package/cjs/network/auth-key.js.map +1 -1
- package/cjs/network/authorization.js +62 -57
- package/cjs/network/authorization.js.map +1 -1
- package/cjs/network/mtproto-session.d.ts +2 -2
- package/cjs/network/mtproto-session.js +2 -2
- package/cjs/network/mtproto-session.js.map +1 -1
- package/cjs/network/multi-session-connection.d.ts +1 -1
- package/cjs/network/multi-session-connection.js +2 -2
- package/cjs/network/multi-session-connection.js.map +1 -1
- package/cjs/network/network-manager.d.ts +1 -0
- package/cjs/network/network-manager.js +21 -27
- package/cjs/network/network-manager.js.map +1 -1
- package/cjs/network/session-connection.d.ts +2 -1
- package/cjs/network/session-connection.js +48 -35
- package/cjs/network/session-connection.js.map +1 -1
- package/cjs/network/transports/intermediate.d.ts +3 -0
- package/cjs/network/transports/intermediate.js +5 -3
- package/cjs/network/transports/intermediate.js.map +1 -1
- package/cjs/network/transports/obfuscated.js +14 -18
- package/cjs/network/transports/obfuscated.js.map +1 -1
- package/cjs/storage/abstract.d.ts +23 -2
- package/cjs/storage/abstract.js.map +1 -1
- package/cjs/storage/json.js +8 -9
- package/cjs/storage/json.js.map +1 -1
- package/cjs/storage/localstorage.js +5 -2
- package/cjs/storage/localstorage.js.map +1 -1
- package/cjs/storage/memory.d.ts +7 -8
- package/cjs/storage/memory.js +7 -7
- package/cjs/storage/memory.js.map +1 -1
- package/cjs/utils/bigint-utils.d.ts +18 -11
- package/cjs/utils/bigint-utils.js +148 -47
- package/cjs/utils/bigint-utils.js.map +1 -1
- package/cjs/utils/buffer-utils.d.ts +7 -1
- package/cjs/utils/buffer-utils.js +16 -3
- package/cjs/utils/buffer-utils.js.map +1 -1
- package/cjs/utils/crypto/abstract.d.ts +16 -9
- package/cjs/utils/crypto/abstract.js +6 -9
- package/cjs/utils/crypto/abstract.js.map +1 -1
- package/cjs/utils/crypto/factorization.d.ts +2 -1
- package/cjs/utils/crypto/factorization.js +28 -36
- package/cjs/utils/crypto/factorization.js.map +1 -1
- package/cjs/utils/crypto/index.d.ts +4 -0
- package/cjs/utils/crypto/index.js +4 -0
- package/cjs/utils/crypto/index.js.map +1 -1
- package/cjs/utils/crypto/keys.d.ts +2 -2
- package/cjs/utils/crypto/keys.js +4 -4
- package/cjs/utils/crypto/keys.js.map +1 -1
- package/cjs/utils/crypto/miller-rabin.d.ts +2 -2
- package/cjs/utils/crypto/miller-rabin.js +20 -22
- package/cjs/utils/crypto/miller-rabin.js.map +1 -1
- package/cjs/utils/crypto/mtproto.d.ts +3 -3
- package/cjs/utils/crypto/mtproto.js +12 -12
- package/cjs/utils/crypto/mtproto.js.map +1 -1
- package/cjs/utils/crypto/node.d.ts +21 -0
- package/cjs/utils/crypto/node.js +74 -0
- package/cjs/utils/crypto/node.js.map +1 -0
- package/cjs/utils/crypto/password.js +22 -33
- package/cjs/utils/crypto/password.js.map +1 -1
- package/cjs/utils/crypto/wasm.d.ts +22 -0
- package/cjs/utils/crypto/wasm.js +41 -0
- package/cjs/utils/crypto/wasm.js.map +1 -0
- package/cjs/utils/crypto/web.d.ts +11 -0
- package/cjs/utils/crypto/web.js +40 -0
- package/cjs/utils/crypto/web.js.map +1 -0
- package/cjs/utils/deque.d.ts +1 -1
- package/cjs/utils/deque.js +9 -3
- package/cjs/utils/deque.js.map +1 -1
- package/cjs/utils/function-utils.d.ts +4 -1
- package/cjs/utils/function-utils.js +8 -1
- package/cjs/utils/function-utils.js.map +1 -1
- package/cjs/utils/index.d.ts +0 -1
- package/cjs/utils/index.js +0 -1
- package/cjs/utils/index.js.map +1 -1
- package/cjs/utils/logger.d.ts +0 -1
- package/cjs/utils/logger.js +3 -4
- package/cjs/utils/logger.js.map +1 -1
- package/cjs/utils/lru-map.d.ts +1 -0
- package/cjs/utils/lru-map.js +6 -0
- package/cjs/utils/lru-map.js.map +1 -1
- package/cjs/utils/peer-utils.js +5 -1
- package/cjs/utils/peer-utils.js.map +1 -1
- package/cjs/utils/platform/crypto.js +2 -2
- package/cjs/utils/platform/crypto.js.map +1 -1
- package/cjs/utils/platform/crypto.web.js +2 -2
- package/cjs/utils/platform/crypto.web.js.map +1 -1
- package/cjs/utils/string-session.js +4 -2
- package/cjs/utils/string-session.js.map +1 -1
- package/esm/base-client.js +4 -1
- package/esm/base-client.js.map +1 -1
- package/esm/network/auth-key.d.ts +3 -3
- package/esm/network/auth-key.js +13 -13
- package/esm/network/auth-key.js.map +1 -1
- package/esm/network/authorization.js +64 -59
- package/esm/network/authorization.js.map +1 -1
- package/esm/network/mtproto-session.d.ts +2 -2
- package/esm/network/mtproto-session.js +2 -2
- package/esm/network/mtproto-session.js.map +1 -1
- package/esm/network/multi-session-connection.d.ts +1 -1
- package/esm/network/multi-session-connection.js +2 -2
- package/esm/network/multi-session-connection.js.map +1 -1
- package/esm/network/network-manager.d.ts +1 -0
- package/esm/network/network-manager.js +21 -27
- package/esm/network/network-manager.js.map +1 -1
- package/esm/network/session-connection.d.ts +2 -1
- package/esm/network/session-connection.js +50 -37
- package/esm/network/session-connection.js.map +1 -1
- package/esm/network/transports/intermediate.d.ts +3 -0
- package/esm/network/transports/intermediate.js +6 -4
- package/esm/network/transports/intermediate.js.map +1 -1
- package/esm/network/transports/obfuscated.js +15 -19
- package/esm/network/transports/obfuscated.js.map +1 -1
- package/esm/storage/abstract.d.ts +23 -2
- package/esm/storage/abstract.js.map +1 -1
- package/esm/storage/json.js +8 -9
- package/esm/storage/json.js.map +1 -1
- package/esm/storage/localstorage.js +5 -2
- package/esm/storage/localstorage.js.map +1 -1
- package/esm/storage/memory.d.ts +7 -8
- package/esm/storage/memory.js +7 -7
- package/esm/storage/memory.js.map +1 -1
- package/esm/utils/bigint-utils.d.ts +18 -11
- package/esm/utils/bigint-utils.js +142 -44
- package/esm/utils/bigint-utils.js.map +1 -1
- package/esm/utils/buffer-utils.d.ts +7 -1
- package/esm/utils/buffer-utils.js +14 -1
- package/esm/utils/buffer-utils.js.map +1 -1
- package/esm/utils/crypto/abstract.d.ts +16 -9
- package/esm/utils/crypto/abstract.js +6 -9
- package/esm/utils/crypto/abstract.js.map +1 -1
- package/esm/utils/crypto/factorization.d.ts +2 -1
- package/esm/utils/crypto/factorization.js +29 -34
- package/esm/utils/crypto/factorization.js.map +1 -1
- package/esm/utils/crypto/index.d.ts +4 -0
- package/esm/utils/crypto/index.js +4 -0
- package/esm/utils/crypto/index.js.map +1 -1
- package/esm/utils/crypto/keys.d.ts +2 -2
- package/esm/utils/crypto/keys.js +4 -4
- package/esm/utils/crypto/keys.js.map +1 -1
- package/esm/utils/crypto/miller-rabin.d.ts +2 -2
- package/esm/utils/crypto/miller-rabin.js +21 -20
- package/esm/utils/crypto/miller-rabin.js.map +1 -1
- package/esm/utils/crypto/mtproto.d.ts +3 -3
- package/esm/utils/crypto/mtproto.js +12 -12
- package/esm/utils/crypto/mtproto.js.map +1 -1
- package/esm/utils/crypto/node.d.ts +21 -0
- package/esm/utils/crypto/node.js +69 -0
- package/esm/utils/crypto/node.js.map +1 -0
- package/esm/utils/crypto/password.js +24 -32
- package/esm/utils/crypto/password.js.map +1 -1
- package/esm/utils/crypto/wasm.d.ts +22 -0
- package/esm/utils/crypto/wasm.js +37 -0
- package/esm/utils/crypto/wasm.js.map +1 -0
- package/esm/utils/crypto/web.d.ts +11 -0
- package/esm/utils/crypto/web.js +36 -0
- package/esm/utils/crypto/web.js.map +1 -0
- package/esm/utils/deque.d.ts +1 -1
- package/esm/utils/deque.js +9 -3
- package/esm/utils/deque.js.map +1 -1
- package/esm/utils/function-utils.d.ts +4 -1
- package/esm/utils/function-utils.js +8 -1
- package/esm/utils/function-utils.js.map +1 -1
- package/esm/utils/index.d.ts +0 -1
- package/esm/utils/index.js +0 -1
- package/esm/utils/index.js.map +1 -1
- package/esm/utils/logger.d.ts +0 -1
- package/esm/utils/logger.js +3 -4
- package/esm/utils/logger.js.map +1 -1
- package/esm/utils/lru-map.d.ts +1 -0
- package/esm/utils/lru-map.js +6 -0
- package/esm/utils/lru-map.js.map +1 -1
- package/esm/utils/peer-utils.js +5 -1
- package/esm/utils/peer-utils.js.map +1 -1
- package/esm/utils/platform/crypto.js +1 -1
- package/esm/utils/platform/crypto.js.map +1 -1
- package/esm/utils/platform/crypto.web.js +2 -2
- package/esm/utils/platform/crypto.web.js.map +1 -1
- package/esm/utils/string-session.js +4 -2
- package/esm/utils/string-session.js.map +1 -1
- package/package.json +4 -5
- package/cjs/utils/crypto/common.d.ts +0 -12
- package/cjs/utils/crypto/common.js +0 -54
- package/cjs/utils/crypto/common.js.map +0 -1
- package/cjs/utils/crypto/node-crypto.d.ts +0 -10
- package/cjs/utils/crypto/node-crypto.js +0 -46
- package/cjs/utils/crypto/node-crypto.js.map +0 -1
- package/cjs/utils/crypto/subtle.d.ts +0 -12
- package/cjs/utils/crypto/subtle.js +0 -86
- package/cjs/utils/crypto/subtle.js.map +0 -1
- package/cjs/utils/flood-control.d.ts +0 -14
- package/cjs/utils/flood-control.js +0 -65
- package/cjs/utils/flood-control.js.map +0 -1
- package/cjs/utils/platform/random.d.ts +0 -1
- package/cjs/utils/platform/random.js +0 -7
- package/cjs/utils/platform/random.js.map +0 -1
- package/cjs/utils/platform/random.web.d.ts +0 -1
- package/cjs/utils/platform/random.web.js +0 -10
- package/cjs/utils/platform/random.web.js.map +0 -1
- package/esm/utils/crypto/common.d.ts +0 -12
- package/esm/utils/crypto/common.js +0 -50
- package/esm/utils/crypto/common.js.map +0 -1
- package/esm/utils/crypto/node-crypto.d.ts +0 -10
- package/esm/utils/crypto/node-crypto.js +0 -42
- package/esm/utils/crypto/node-crypto.js.map +0 -1
- package/esm/utils/crypto/subtle.d.ts +0 -12
- package/esm/utils/crypto/subtle.js +0 -59
- package/esm/utils/crypto/subtle.js.map +0 -1
- package/esm/utils/flood-control.d.ts +0 -14
- package/esm/utils/flood-control.js +0 -61
- package/esm/utils/flood-control.js.map +0 -1
- package/esm/utils/platform/random.d.ts +0 -1
- package/esm/utils/platform/random.js +0 -4
- package/esm/utils/platform/random.js.map +0 -1
- package/esm/utils/platform/random.web.d.ts +0 -1
- package/esm/utils/platform/random.web.js +0 -6
- package/esm/utils/platform/random.web.js.map +0 -1
package/cjs/base-client.js
CHANGED
|
@@ -32,8 +32,9 @@ class BaseTelegramClient extends events_1.default {
|
|
|
32
32
|
if (isNaN(apiId)) {
|
|
33
33
|
throw new Error('apiId must be a number or a numeric string!');
|
|
34
34
|
}
|
|
35
|
-
if (opts.logLevel)
|
|
35
|
+
if (opts.logLevel !== undefined) {
|
|
36
36
|
this.log.level = opts.logLevel;
|
|
37
|
+
}
|
|
37
38
|
this.crypto = (opts.crypto ?? index_js_2.defaultCryptoProviderFactory)();
|
|
38
39
|
this.storage = opts.storage ?? new index_js_1.MemoryStorage();
|
|
39
40
|
this._apiHash = opts.apiHash;
|
|
@@ -73,6 +74,7 @@ class BaseTelegramClient extends events_1.default {
|
|
|
73
74
|
useIpv6: Boolean(opts.useIpv6),
|
|
74
75
|
keepAliveAction: this._keepAliveAction.bind(this),
|
|
75
76
|
enableErrorReporting: opts.enableErrorReporting ?? false,
|
|
77
|
+
onUsable: () => this.emit('usable'),
|
|
76
78
|
...(opts.network ?? {}),
|
|
77
79
|
}, this._config);
|
|
78
80
|
this.storage.setup?.(this.log, this._readerMap, this._writerMap);
|
|
@@ -108,6 +110,7 @@ class BaseTelegramClient extends events_1.default {
|
|
|
108
110
|
return;
|
|
109
111
|
}
|
|
110
112
|
const promise = (this._connected = (0, index_js_2.createControllablePromise)());
|
|
113
|
+
await this.crypto.initialize?.();
|
|
111
114
|
await this._loadStorage();
|
|
112
115
|
const primaryDc = await this.storage.getDefaultDcs();
|
|
113
116
|
if (primaryDc !== null)
|
package/cjs/base-client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-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IAAI;qBACb,CAAC,CAAA;oBACF,MAAK;aACZ;SACJ;QAED,IAAI,KAAK,GAAG,CAAC,EAAE;YACX,MAAM,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,WAAW,CAAC,CAAA;YAC3C,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,iBAAiB,EAAE,KAAK,CAAC,CAAA;SAC3C;QAED,OAAO,MAAM,CAAA;IACjB,CAAC;IAED;;;;;;;;;;;;;;OAcG;IACH,KAAK,CAAC,aAAa;QACf,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,CAAA;QACrD,IAAI,CAAC,UAAU;YAAE,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAA;QAErD,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;QACpE,IAAI,CAAC,OAAO;YAAE,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAA;QAE1D,OAAO,IAAA,6BAAkB,EAAC,IAAI,CAAC,UAAU,EAAE;YACvC,OAAO,EAAE,CAAC;YACV,IAAI,EAAE,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE;YAClC,QAAQ,EAAE,IAAI,CAAC,SAAS;YACxB,UAAU;YACV,OAAO;SACV,CAAC,CAAA;IACN,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,aAAa,CAAC,OAAe,EAAE,KAAK,GAAG,KAAK;QACxC,IAAI,CAAC,WAAW,GAAG,OAAO,CAAA;QAC1B,IAAI,CAAC,YAAY,GAAG,KAAK,CAAA;IAC7B,CAAC;CACJ;AAvZD,gDAuZC","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport EventEmitter from 'events'\nimport Long from 'long'\n\nimport { tl } from '@mtcute/tl'\nimport { __tlReaderMap as defaultReaderMap } from '@mtcute/tl/binary/reader.js'\nimport { __tlWriterMap as defaultWriterMap } from '@mtcute/tl/binary/writer.js'\nimport { TlReaderMap, TlWriterMap } from '@mtcute/tl-runtime'\n\nimport { ConfigManager } from './network/config-manager.js'\nimport { ReconnectionStrategy, SessionConnection, TransportFactory } from './network/index.js'\nimport { NetworkManager, NetworkManagerExtraParams, RpcCallOptions } from './network/network-manager.js'\nimport { PersistentConnectionParams } from './network/persistent-connection.js'\nimport { ITelegramStorage, MemoryStorage } from './storage/index.js'\nimport { MustEqual } from './types/index.js'\nimport {\n ControllablePromise,\n createControllablePromise,\n CryptoProviderFactory,\n defaultCryptoProviderFactory,\n defaultProductionDc,\n defaultProductionIpv6Dc,\n defaultTestDc,\n defaultTestIpv6Dc,\n getAllPeersFrom,\n ICryptoProvider,\n LogManager,\n readStringSession,\n toggleChannelIdMark,\n writeStringSession,\n} from './utils/index.js'\n\n/** Options for {@link BaseTelegramClient} */\nexport interface BaseTelegramClientOptions {\n /**\n * API ID from my.telegram.org\n */\n apiId: number | string\n /**\n * API hash from my.telegram.org\n */\n apiHash: string\n\n /**\n * Telegram storage to use.\n * If omitted, {@link MemoryStorage} is used\n */\n storage?: ITelegramStorage\n\n /**\n * Cryptography provider factory to allow delegating\n * crypto to native addon, worker, etc.\n */\n crypto?: CryptoProviderFactory\n\n /**\n * Whether to use IPv6 datacenters\n * (IPv6 will be preferred when choosing a DC by id)\n * (default: false)\n */\n useIpv6?: boolean\n\n /**\n * Primary DC to use for initial connection.\n * This does not mean this will be the only DC used,\n * nor that this DC will actually be primary, this only\n * determines the first DC the library will try to connect to.\n * Can be used to connect to other networks (like test DCs).\n *\n * When session already contains primary DC, this parameter is ignored.\n *\n * @default Production DC 2.\n */\n defaultDcs?: ITelegramStorage.DcOptions\n\n /**\n * Whether to connect to test servers.\n *\n * If passed, {@link defaultDc} defaults to Test DC 2.\n *\n * **Must** be passed if using test servers, even if\n * you passed custom {@link defaultDc}\n */\n testMode?: boolean\n\n /**\n * Additional options for initConnection call.\n * `apiId` and `query` are not available and will be ignored.\n * Omitted values will be filled with defaults\n */\n initConnectionOptions?: Partial<Omit<tl.RawInitConnectionRequest, 'apiId' | 'query'>>\n\n /**\n * Transport factory to use in the client.\n *\n * @default platform-specific transport: WebSocket on the web, TCP in node\n */\n transport?: TransportFactory\n\n /**\n * Reconnection strategy.\n *\n * @default simple reconnection strategy: first 0ms, then up to 5s (increasing by 1s)\n */\n reconnectionStrategy?: ReconnectionStrategy<PersistentConnectionParams>\n\n /**\n * Maximum duration of a flood_wait that will be waited automatically.\n * Flood waits above this threshold will throw a FloodWaitError.\n * Set to 0 to disable. Can be overridden with `throwFlood` parameter in call() params\n *\n * @default 10000\n */\n floodSleepThreshold?: number\n\n /**\n * Maximum number of retries when calling RPC methods.\n * Call is retried when InternalError or FloodWaitError is encountered.\n * Can be set to Infinity.\n *\n * @default 5\n */\n maxRetryCount?: number\n\n /**\n * If true, every single API call will be wrapped with `tl.invokeWithoutUpdates`,\n * effectively disabling the server-sent events for the clients.\n * May be useful in some cases.\n *\n * Note that this only wraps calls made with `.call()` within the primary\n * connection. Additional connections and direct `.sendForResult()` calls\n * must be wrapped manually.\n *\n * @default false\n */\n disableUpdates?: boolean\n\n /**\n * mtcute can send all unknown RPC errors to [danog](https://github.com/danog)'s\n * [error reporting service](https://rpc.pwrtelegram.xyz/).\n *\n * This is fully anonymous (except maybe IP) and is only used to improve the library\n * and developer experience for everyone working with MTProto. This is fully opt-in,\n * and if you're too paranoid, you can disable it by manually passing `enableErrorReporting: false` to the client.\n *\n * @default false\n */\n enableErrorReporting?: boolean\n\n /**\n * If true, RPC errors will have a stack trace of the initial `.call()`\n * or `.sendForResult()` call position, which drastically improves\n * debugging experience.<br>\n * If false, they will have a stack trace of mtcute internals.\n *\n * Internally this creates a stack capture before every RPC call\n * and stores it until the result is received. This might\n * use a lot more memory than normal, thus can be disabled here.\n *\n * @default true\n */\n niceStacks?: boolean\n\n /**\n * Extra parameters for {@link NetworkManager}\n */\n network?: NetworkManagerExtraParams\n\n /**\n * Set logging level for the client.\n *\n * See static members of {@link LogManager} for possible values.\n */\n logLevel?: number\n\n /**\n * **EXPERT USE ONLY!**\n *\n * Override TL layer used for the connection.\n *\n * **Does not** change the schema used.\n */\n overrideLayer?: number\n\n /**\n * **EXPERT USE ONLY**\n *\n * Override reader map used for the connection.\n */\n readerMap?: TlReaderMap\n\n /**\n * **EXPERT USE ONLY**\n *\n * Override writer map used for the connection.\n */\n writerMap?: TlWriterMap\n}\n\n/**\n * Basic Telegram client that only implements the bare minimum\n * to make RPC calls and receive low-level updates.\n */\nexport class BaseTelegramClient extends EventEmitter {\n /**\n * Crypto provider taken from {@link BaseTelegramClientOptions.crypto}\n */\n readonly crypto: ICryptoProvider\n\n /**\n * Telegram storage taken from {@link BaseTelegramClientOptions.storage}\n */\n readonly storage: ITelegramStorage\n\n /**\n * API hash taken from {@link BaseTelegramClientOptions.apiHash}\n */\n protected readonly _apiHash: string\n\n /**\n * \"Use IPv6\" taken from {@link BaseTelegramClientOptions.useIpv6}\n */\n protected readonly _useIpv6: boolean\n\n /**\n * \"Test mode\" taken from {@link BaseTelegramClientOptions.testMode}\n */\n protected readonly _testMode: boolean\n\n /**\n * Primary DCs taken from {@link BaseTelegramClientOptions.defaultDcs},\n * loaded from session or changed by other means (like redirecting).\n */\n protected _defaultDcs: ITelegramStorage.DcOptions\n\n private _niceStacks: boolean\n /** TL layer used by the client */\n readonly _layer: number\n /** TL readers map used by the client */\n readonly _readerMap: TlReaderMap\n /** TL writers map used by the client */\n readonly _writerMap: TlWriterMap\n\n /** Unix timestamp when the last update was received */\n protected _lastUpdateTime = 0\n\n readonly _config = new ConfigManager(() => this.call({ _: 'help.getConfig' }))\n\n // not really connected, but rather \"connect() was called\"\n private _connected: ControllablePromise<void> | boolean = false\n\n private _onError?: (err: unknown, connection?: SessionConnection) => void\n\n private _importFrom?: string\n private _importForce?: boolean\n\n readonly log = new LogManager('client')\n readonly network: NetworkManager\n\n constructor(opts: BaseTelegramClientOptions) {\n super()\n\n const apiId = typeof opts.apiId === 'string' ? parseInt(opts.apiId) : opts.apiId\n\n if (isNaN(apiId)) {\n throw new Error('apiId must be a number or a numeric string!')\n }\n\n if (opts.logLevel) this.log.level = opts.logLevel\n\n this.crypto = (opts.crypto ?? defaultCryptoProviderFactory)()\n this.storage = opts.storage ?? new MemoryStorage()\n this._apiHash = opts.apiHash\n this._useIpv6 = Boolean(opts.useIpv6)\n this._testMode = Boolean(opts.testMode)\n\n let dc = opts.defaultDcs\n\n if (!dc) {\n if (this._testMode) {\n dc = this._useIpv6 ? defaultTestIpv6Dc : defaultTestDc\n } else {\n dc = this._useIpv6 ? defaultProductionIpv6Dc : defaultProductionDc\n }\n }\n\n this._defaultDcs = dc\n this._niceStacks = opts.niceStacks ?? true\n\n this._layer = opts.overrideLayer ?? tl.LAYER\n this._readerMap = opts.readerMap ?? defaultReaderMap\n this._writerMap = opts.writerMap ?? defaultWriterMap\n\n this.network = new NetworkManager(\n {\n apiId,\n crypto: this.crypto,\n disableUpdates: opts.disableUpdates ?? false,\n initConnectionOptions: opts.initConnectionOptions,\n layer: this._layer,\n log: this.log,\n readerMap: this._readerMap,\n writerMap: this._writerMap,\n reconnectionStrategy: opts.reconnectionStrategy,\n storage: this.storage,\n testMode: this._testMode,\n transport: opts.transport,\n _emitError: this._emitError.bind(this),\n floodSleepThreshold: opts.floodSleepThreshold ?? 10000,\n maxRetryCount: opts.maxRetryCount ?? 5,\n isPremium: false,\n useIpv6: Boolean(opts.useIpv6),\n keepAliveAction: this._keepAliveAction.bind(this),\n enableErrorReporting: opts.enableErrorReporting ?? false,\n ...(opts.network ?? {}),\n },\n this._config,\n )\n\n this.storage.setup?.(this.log, this._readerMap, this._writerMap)\n }\n\n protected _keepAliveAction(): void {\n this.emit('keep_alive')\n }\n\n protected async _loadStorage(): Promise<void> {\n await this.storage.load?.()\n }\n\n _beforeStorageSave: (() => Promise<void>)[] = []\n\n beforeStorageSave(cb: () => Promise<void>): void {\n this._beforeStorageSave.push(cb)\n }\n\n offBeforeStorageSave(cb: () => Promise<void>): void {\n this._beforeStorageSave = this._beforeStorageSave.filter((x) => x !== cb)\n }\n\n async saveStorage(): Promise<void> {\n for (const cb of this._beforeStorageSave) {\n await cb()\n }\n await this.storage.save?.()\n }\n\n /**\n * Initialize the connection to the primary DC.\n *\n * You shouldn't usually call this method directly as it is called\n * implicitly the first time you call {@link call}.\n */\n async connect(): Promise<void> {\n if (this._connected) {\n // avoid double-connect\n await this._connected\n\n return\n }\n\n const promise = (this._connected = createControllablePromise())\n\n await this._loadStorage()\n const primaryDc = await this.storage.getDefaultDcs()\n if (primaryDc !== null) this._defaultDcs = primaryDc\n\n const defaultDcAuthKey = await this.storage.getAuthKeyFor(this._defaultDcs.main.id)\n\n if ((this._importForce || !defaultDcAuthKey) && this._importFrom) {\n const data = readStringSession(this._readerMap, this._importFrom)\n\n if (data.testMode !== this._testMode) {\n throw new Error(\n 'This session string is not for the current backend. ' +\n `Session is ${data.testMode ? 'test' : 'prod'}, but the client is ${\n this._testMode ? 'test' : 'prod'\n }`,\n )\n }\n\n this._defaultDcs = data.primaryDcs\n await this.storage.setDefaultDcs(data.primaryDcs)\n\n if (data.self) {\n await this.storage.setSelf(data.self)\n }\n\n // await this.primaryConnection.setupKeys(data.authKey)\n await this.storage.setAuthKeyFor(data.primaryDcs.main.id, data.authKey)\n\n await this.saveStorage()\n }\n\n this.emit('before_connect')\n\n this.network\n .connect(this._defaultDcs)\n .then(() => {\n promise.resolve()\n this._connected = true\n })\n .catch((err: Error) => this._emitError(err))\n }\n\n /**\n * Close all connections and finalize the client.\n */\n async close(): Promise<void> {\n this.emit('before_close')\n\n this._config.destroy()\n this.network.destroy()\n\n await this.saveStorage()\n await this.storage.destroy?.()\n\n this.emit('closed')\n }\n\n /**\n * Make an RPC call to the primary DC.\n * This method handles DC migration, flood waits and retries automatically.\n *\n * If you want more low-level control, use\n * `primaryConnection.sendForResult()` (which is what this method wraps)\n *\n * This method is still quite low-level and you shouldn't use this\n * when using high-level API provided by `@mtcute/client`.\n *\n * @param message RPC method to call\n * @param params Additional call parameters\n */\n async call<T extends tl.RpcMethod>(\n message: MustEqual<T, tl.RpcMethod>,\n params?: RpcCallOptions,\n ): Promise<tl.RpcCallReturn[T['_']]> {\n if (this._connected !== true) {\n await this.connect()\n }\n\n const stack = this._niceStacks ? new Error().stack : undefined\n\n const res = await this.network.call(message, params, stack)\n await this._cachePeersFrom(res)\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return res\n }\n\n /**\n * Change transport for the client.\n *\n * Can be used, for example, to change proxy at runtime\n *\n * This effectively calls `changeTransport()` on\n * `primaryConnection` and all additional connections.\n *\n * @param factory New transport factory\n */\n changeTransport(factory: TransportFactory): void {\n this.network.changeTransport(factory)\n }\n\n /**\n * Register an error handler for the client\n *\n * @param handler\n * Error handler. Called with one or two parameters.\n * The first one is always the error, and the second is\n * the connection in which the error has occurred, in case\n * this was connection-related error.\n */\n onError(handler: (err: unknown, connection?: SessionConnection) => void): void {\n this._onError = handler\n }\n\n notifyLoggedIn(auth: tl.auth.RawAuthorization): void {\n this.network.notifyLoggedIn(auth)\n this.emit('logged_in', auth)\n }\n\n _emitError(err: unknown, connection?: SessionConnection): void {\n if (this._onError) {\n this._onError(err, connection)\n } else {\n console.error(err)\n }\n }\n\n /**\n * Adds all peers from a given object to entity cache in storage.\n *\n * @returns `true` if there were any `min` peers\n */\n async _cachePeersFrom(obj: object): Promise<boolean> {\n const parsedPeers: ITelegramStorage.PeerInfo[] = []\n\n let hadMin = false\n let count = 0\n\n for (const peer of getAllPeersFrom(obj as tl.TlObject)) {\n if ((peer as any).min) {\n // absolutely incredible min peer handling, courtesy of levlam.\n // see this thread: https://t.me/tdlibchat/15084\n hadMin = true\n this.log.debug('received min peer: %j', peer)\n continue\n }\n\n count += 1\n\n switch (peer._) {\n case 'user':\n if (!peer.accessHash) {\n this.log.warn('received user without access hash: %j', peer)\n continue\n }\n\n parsedPeers.push({\n id: peer.id,\n accessHash: peer.accessHash,\n username: peer.username?.toLowerCase(),\n phone: peer.phone,\n type: 'user',\n full: peer,\n })\n break\n case 'chat':\n case 'chatForbidden':\n parsedPeers.push({\n id: -peer.id,\n accessHash: Long.ZERO,\n type: 'chat',\n full: peer,\n })\n break\n case 'channel':\n case 'channelForbidden':\n if (!peer.accessHash) {\n this.log.warn('received user without access hash: %j', peer)\n continue\n }\n parsedPeers.push({\n id: toggleChannelIdMark(peer.id),\n accessHash: peer.accessHash,\n username: peer._ === 'channel' ? peer.username?.toLowerCase() : undefined,\n type: 'channel',\n full: peer,\n })\n break\n }\n }\n\n if (count > 0) {\n await this.storage.updatePeers(parsedPeers)\n this.log.debug('cached %d peers', count)\n }\n\n return hadMin\n }\n\n /**\n * Export current session to a single *LONG* string, containing\n * all the needed information.\n *\n * > **Warning!** Anyone with this string will be able\n * > to authorize as you and do anything. Treat this\n * > as your password, and never give it away!\n * >\n * > In case you have accidentally leaked this string,\n * > make sure to revoke this session in account settings:\n * > \"Privacy & Security\" > \"Active sessions\" >\n * > find the one containing `mtcute` > Revoke,\n * > or, in case this is a bot, revoke bot token\n * > with [@BotFather](//t.me/botfather)\n */\n async exportSession(): Promise<string> {\n const primaryDcs = await this.storage.getDefaultDcs()\n if (!primaryDcs) throw new Error('No default DC set')\n\n const authKey = await this.storage.getAuthKeyFor(primaryDcs.main.id)\n if (!authKey) throw new Error('Auth key is not ready yet')\n\n return writeStringSession(this._writerMap, {\n version: 2,\n self: await this.storage.getSelf(),\n testMode: this._testMode,\n primaryDcs,\n authKey,\n })\n }\n\n /**\n * Request the session to be imported from the given session string.\n *\n * Note that the string will not be parsed and imported right away,\n * instead, it will be imported when `connect()` is called\n *\n * Also note that the session will only be imported in case\n * the storage is missing authorization (i.e. does not contain\n * auth key for the primary DC), otherwise it will be ignored (unless `force).\n *\n * @param session Session string to import\n * @param force Whether to overwrite existing session\n */\n importSession(session: string, force = false): void {\n this._importFrom = session\n this._importForce = force\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"base-client.js","sour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no-explicit-any */\nimport EventEmitter from 'events'\nimport Long from 'long'\n\nimport { tl } from '@mtcute/tl'\nimport { __tlReaderMap as defaultReaderMap } from '@mtcute/tl/binary/reader.js'\nimport { __tlWriterMap as defaultWriterMap } from '@mtcute/tl/binary/writer.js'\nimport { TlReaderMap, TlWriterMap } from '@mtcute/tl-runtime'\n\nimport { ConfigManager } from './network/config-manager.js'\nimport { ReconnectionStrategy, SessionConnection, TransportFactory } from './network/index.js'\nimport { NetworkManager, NetworkManagerExtraParams, RpcCallOptions } from './network/network-manager.js'\nimport { PersistentConnectionParams } from './network/persistent-connection.js'\nimport { ITelegramStorage, MemoryStorage } from './storage/index.js'\nimport { MustEqual } from './types/index.js'\nimport {\n ControllablePromise,\n createControllablePromise,\n CryptoProviderFactory,\n defaultCryptoProviderFactory,\n defaultProductionDc,\n defaultProductionIpv6Dc,\n defaultTestDc,\n defaultTestIpv6Dc,\n getAllPeersFrom,\n ICryptoProvider,\n LogManager,\n readStringSession,\n toggleChannelIdMark,\n writeStringSession,\n} from './utils/index.js'\n\n/** Options for {@link BaseTelegramClient} */\nexport interface BaseTelegramClientOptions {\n /**\n * API ID from my.telegram.org\n */\n apiId: number | string\n /**\n * API hash from my.telegram.org\n */\n apiHash: string\n\n /**\n * Telegram storage to use.\n * If omitted, {@link MemoryStorage} is used\n */\n storage?: ITelegramStorage\n\n /**\n * Cryptography provider factory to allow delegating\n * crypto to native addon, worker, etc.\n */\n crypto?: CryptoProviderFactory\n\n /**\n * Whether to use IPv6 datacenters\n * (IPv6 will be preferred when choosing a DC by id)\n * (default: false)\n */\n useIpv6?: boolean\n\n /**\n * Primary DC to use for initial connection.\n * This does not mean this will be the only DC used,\n * nor that this DC will actually be primary, this only\n * determines the first DC the library will try to connect to.\n * Can be used to connect to other networks (like test DCs).\n *\n * When session already contains primary DC, this parameter is ignored.\n *\n * @default Production DC 2.\n */\n defaultDcs?: ITelegramStorage.DcOptions\n\n /**\n * Whether to connect to test servers.\n *\n * If passed, {@link defaultDc} defaults to Test DC 2.\n *\n * **Must** be passed if using test servers, even if\n * you passed custom {@link defaultDc}\n */\n testMode?: boolean\n\n /**\n * Additional options for initConnection call.\n * `apiId` and `query` are not available and will be ignored.\n * Omitted values will be filled with defaults\n */\n initConnectionOptions?: Partial<Omit<tl.RawInitConnectionRequest, 'apiId' | 'query'>>\n\n /**\n * Transport factory to use in the client.\n *\n * @default platform-specific transport: WebSocket on the web, TCP in node\n */\n transport?: TransportFactory\n\n /**\n * Reconnection strategy.\n *\n * @default simple reconnection strategy: first 0ms, then up to 5s (increasing by 1s)\n */\n reconnectionStrategy?: ReconnectionStrategy<PersistentConnectionParams>\n\n /**\n * Maximum duration of a flood_wait that will be waited automatically.\n * Flood waits above this threshold will throw a FloodWaitError.\n * Set to 0 to disable. Can be overridden with `throwFlood` parameter in call() params\n *\n * @default 10000\n */\n floodSleepThreshold?: number\n\n /**\n * Maximum number of retries when calling RPC methods.\n * Call is retried when InternalError or FloodWaitError is encountered.\n * Can be set to Infinity.\n *\n * @default 5\n */\n maxRetryCount?: number\n\n /**\n * If true, every single API call will be wrapped with `tl.invokeWithoutUpdates`,\n * effectively disabling the server-sent events for the clients.\n * May be useful in some cases.\n *\n * Note that this only wraps calls made with `.call()` within the primary\n * connection. Additional connections and direct `.sendForResult()` calls\n * must be wrapped manually.\n *\n * @default false\n */\n disableUpdates?: boolean\n\n /**\n * mtcute can send all unknown RPC errors to [danog](https://github.com/danog)'s\n * [error reporting service](https://rpc.pwrtelegram.xyz/).\n *\n * This is fully anonymous (except maybe IP) and is only used to improve the library\n * and developer experience for everyone working with MTProto. This is fully opt-in,\n * and if you're too paranoid, you can disable it by manually passing `enableErrorReporting: false` to the client.\n *\n * @default false\n */\n enableErrorReporting?: boolean\n\n /**\n * If true, RPC errors will have a stack trace of the initial `.call()`\n * or `.sendForResult()` call position, which drastically improves\n * debugging experience.<br>\n * If false, they will have a stack trace of mtcute internals.\n *\n * Internally this creates a stack capture before every RPC call\n * and stores it until the result is received. This might\n * use a lot more memory than normal, thus can be disabled here.\n *\n * @default true\n */\n niceStacks?: boolean\n\n /**\n * Extra parameters for {@link NetworkManager}\n */\n network?: NetworkManagerExtraParams\n\n /**\n * Set logging level for the client.\n *\n * See static members of {@link LogManager} for possible values.\n */\n logLevel?: number\n\n /**\n * **EXPERT USE ONLY!**\n *\n * Override TL layer used for the connection.\n *\n * **Does not** change the schema used.\n */\n overrideLayer?: number\n\n /**\n * **EXPERT USE ONLY**\n *\n * Override reader map used for the connection.\n */\n readerMap?: TlReaderMap\n\n /**\n * **EXPERT USE ONLY**\n *\n * Override writer map used for the connection.\n */\n writerMap?: TlWriterMap\n}\n\n/**\n * Basic Telegram client that only implements the bare minimum\n * to make RPC calls and receive low-level updates.\n */\nexport class BaseTelegramClient extends EventEmitter {\n /**\n * Crypto provider taken from {@link BaseTelegramClientOptions.crypto}\n */\n readonly crypto: ICryptoProvider\n\n /**\n * Telegram storage taken from {@link BaseTelegramClientOptions.storage}\n */\n readonly storage: ITelegramStorage\n\n /**\n * API hash taken from {@link BaseTelegramClientOptions.apiHash}\n */\n protected readonly _apiHash: string\n\n /**\n * \"Use IPv6\" taken from {@link BaseTelegramClientOptions.useIpv6}\n */\n protected readonly _useIpv6: boolean\n\n /**\n * \"Test mode\" taken from {@link BaseTelegramClientOptions.testMode}\n */\n protected readonly _testMode: boolean\n\n /**\n * Primary DCs taken from {@link BaseTelegramClientOptions.defaultDcs},\n * loaded from session or changed by other means (like redirecting).\n */\n protected _defaultDcs: ITelegramStorage.DcOptions\n\n private _niceStacks: boolean\n /** TL layer used by the client */\n readonly _layer: number\n /** TL readers map used by the client */\n readonly _readerMap: TlReaderMap\n /** TL writers map used by the client */\n readonly _writerMap: TlWriterMap\n\n /** Unix timestamp when the last update was received */\n protected _lastUpdateTime = 0\n\n readonly _config = new ConfigManager(() => this.call({ _: 'help.getConfig' }))\n\n // not really connected, but rather \"connect() was called\"\n private _connected: ControllablePromise<void> | boolean = false\n\n private _onError?: (err: unknown, connection?: SessionConnection) => void\n\n private _importFrom?: string\n private _importForce?: boolean\n\n readonly log = new LogManager('client')\n readonly network: NetworkManager\n\n constructor(opts: BaseTelegramClientOptions) {\n super()\n\n const apiId = typeof opts.apiId === 'string' ? parseInt(opts.apiId) : opts.apiId\n\n if (isNaN(apiId)) {\n throw new Error('apiId must be a number or a numeric string!')\n }\n\n if (opts.logLevel !== undefined) {\n this.log.level = opts.logLevel\n }\n\n this.crypto = (opts.crypto ?? defaultCryptoProviderFactory)()\n this.storage = opts.storage ?? new MemoryStorage()\n this._apiHash = opts.apiHash\n this._useIpv6 = Boolean(opts.useIpv6)\n this._testMode = Boolean(opts.testMode)\n\n let dc = opts.defaultDcs\n\n if (!dc) {\n if (this._testMode) {\n dc = this._useIpv6 ? defaultTestIpv6Dc : defaultTestDc\n } else {\n dc = this._useIpv6 ? defaultProductionIpv6Dc : defaultProductionDc\n }\n }\n\n this._defaultDcs = dc\n this._niceStacks = opts.niceStacks ?? true\n\n this._layer = opts.overrideLayer ?? tl.LAYER\n this._readerMap = opts.readerMap ?? defaultReaderMap\n this._writerMap = opts.writerMap ?? defaultWriterMap\n\n this.network = new NetworkManager(\n {\n apiId,\n crypto: this.crypto,\n disableUpdates: opts.disableUpdates ?? false,\n initConnectionOptions: opts.initConnectionOptions,\n layer: this._layer,\n log: this.log,\n readerMap: this._readerMap,\n writerMap: this._writerMap,\n reconnectionStrategy: opts.reconnectionStrategy,\n storage: this.storage,\n testMode: this._testMode,\n transport: opts.transport,\n _emitError: this._emitError.bind(this),\n floodSleepThreshold: opts.floodSleepThreshold ?? 10000,\n maxRetryCount: opts.maxRetryCount ?? 5,\n isPremium: false,\n useIpv6: Boolean(opts.useIpv6),\n keepAliveAction: this._keepAliveAction.bind(this),\n enableErrorReporting: opts.enableErrorReporting ?? false,\n onUsable: () => this.emit('usable'),\n ...(opts.network ?? {}),\n },\n this._config,\n )\n\n this.storage.setup?.(this.log, this._readerMap, this._writerMap)\n }\n\n protected _keepAliveAction(): void {\n this.emit('keep_alive')\n }\n\n protected async _loadStorage(): Promise<void> {\n await this.storage.load?.()\n }\n\n _beforeStorageSave: (() => Promise<void>)[] = []\n\n beforeStorageSave(cb: () => Promise<void>): void {\n this._beforeStorageSave.push(cb)\n }\n\n offBeforeStorageSave(cb: () => Promise<void>): void {\n this._beforeStorageSave = this._beforeStorageSave.filter((x) => x !== cb)\n }\n\n async saveStorage(): Promise<void> {\n for (const cb of this._beforeStorageSave) {\n await cb()\n }\n await this.storage.save?.()\n }\n\n /**\n * Initialize the connection to the primary DC.\n *\n * You shouldn't usually call this method directly as it is called\n * implicitly the first time you call {@link call}.\n */\n async connect(): Promise<void> {\n if (this._connected) {\n // avoid double-connect\n await this._connected\n\n return\n }\n\n const promise = (this._connected = createControllablePromise())\n\n await this.crypto.initialize?.()\n await this._loadStorage()\n const primaryDc = await this.storage.getDefaultDcs()\n if (primaryDc !== null) this._defaultDcs = primaryDc\n\n const defaultDcAuthKey = await this.storage.getAuthKeyFor(this._defaultDcs.main.id)\n\n if ((this._importForce || !defaultDcAuthKey) && this._importFrom) {\n const data = readStringSession(this._readerMap, this._importFrom)\n\n if (data.testMode !== this._testMode) {\n throw new Error(\n 'This session string is not for the current backend. ' +\n `Session is ${data.testMode ? 'test' : 'prod'}, but the client is ${\n this._testMode ? 'test' : 'prod'\n }`,\n )\n }\n\n this._defaultDcs = data.primaryDcs\n await this.storage.setDefaultDcs(data.primaryDcs)\n\n if (data.self) {\n await this.storage.setSelf(data.self)\n }\n\n // await this.primaryConnection.setupKeys(data.authKey)\n await this.storage.setAuthKeyFor(data.primaryDcs.main.id, data.authKey)\n\n await this.saveStorage()\n }\n\n this.emit('before_connect')\n\n this.network\n .connect(this._defaultDcs)\n .then(() => {\n promise.resolve()\n this._connected = true\n })\n .catch((err: Error) => this._emitError(err))\n }\n\n /**\n * Close all connections and finalize the client.\n */\n async close(): Promise<void> {\n this.emit('before_close')\n\n this._config.destroy()\n this.network.destroy()\n\n await this.saveStorage()\n await this.storage.destroy?.()\n\n this.emit('closed')\n }\n\n /**\n * Make an RPC call to the primary DC.\n * This method handles DC migration, flood waits and retries automatically.\n *\n * If you want more low-level control, use\n * `primaryConnection.sendForResult()` (which is what this method wraps)\n *\n * This method is still quite low-level and you shouldn't use this\n * when using high-level API provided by `@mtcute/client`.\n *\n * @param message RPC method to call\n * @param params Additional call parameters\n */\n async call<T extends tl.RpcMethod>(\n message: MustEqual<T, tl.RpcMethod>,\n params?: RpcCallOptions,\n ): Promise<tl.RpcCallReturn[T['_']]> {\n if (this._connected !== true) {\n await this.connect()\n }\n\n const stack = this._niceStacks ? new Error().stack : undefined\n\n const res = await this.network.call(message, params, stack)\n await this._cachePeersFrom(res)\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return res\n }\n\n /**\n * Change transport for the client.\n *\n * Can be used, for example, to change proxy at runtime\n *\n * This effectively calls `changeTransport()` on\n * `primaryConnection` and all additional connections.\n *\n * @param factory New transport factory\n */\n changeTransport(factory: TransportFactory): void {\n this.network.changeTransport(factory)\n }\n\n /**\n * Register an error handler for the client\n *\n * @param handler\n * Error handler. Called with one or two parameters.\n * The first one is always the error, and the second is\n * the connection in which the error has occurred, in case\n * this was connection-related error.\n */\n onError(handler: (err: unknown, connection?: SessionConnection) => void): void {\n this._onError = handler\n }\n\n notifyLoggedIn(auth: tl.auth.RawAuthorization): void {\n this.network.notifyLoggedIn(auth)\n this.emit('logged_in', auth)\n }\n\n _emitError(err: unknown, connection?: SessionConnection): void {\n if (this._onError) {\n this._onError(err, connection)\n } else {\n console.error(err)\n }\n }\n\n /**\n * Adds all peers from a given object to entity cache in storage.\n *\n * @returns `true` if there were any `min` peers\n */\n async _cachePeersFrom(obj: object): Promise<boolean> {\n const parsedPeers: ITelegramStorage.PeerInfo[] = []\n\n let hadMin = false\n let count = 0\n\n for (const peer of getAllPeersFrom(obj as tl.TlObject)) {\n if ((peer as any).min) {\n // absolutely incredible min peer handling, courtesy of levlam.\n // see this thread: https://t.me/tdlibchat/15084\n hadMin = true\n this.log.debug('received min peer: %j', peer)\n continue\n }\n\n count += 1\n\n switch (peer._) {\n case 'user':\n if (!peer.accessHash) {\n this.log.warn('received user without access hash: %j', peer)\n continue\n }\n\n parsedPeers.push({\n id: peer.id,\n accessHash: peer.accessHash,\n username: peer.username?.toLowerCase(),\n phone: peer.phone,\n type: 'user',\n full: peer,\n })\n break\n case 'chat':\n case 'chatForbidden':\n parsedPeers.push({\n id: -peer.id,\n accessHash: Long.ZERO,\n type: 'chat',\n full: peer,\n })\n break\n case 'channel':\n case 'channelForbidden':\n if (!peer.accessHash) {\n this.log.warn('received user without access hash: %j', peer)\n continue\n }\n parsedPeers.push({\n id: toggleChannelIdMark(peer.id),\n accessHash: peer.accessHash,\n username: peer._ === 'channel' ? peer.username?.toLowerCase() : undefined,\n type: 'channel',\n full: peer,\n })\n break\n }\n }\n\n if (count > 0) {\n await this.storage.updatePeers(parsedPeers)\n this.log.debug('cached %d peers', count)\n }\n\n return hadMin\n }\n\n /**\n * Export current session to a single *LONG* string, containing\n * all the needed information.\n *\n * > **Warning!** Anyone with this string will be able\n * > to authorize as you and do anything. Treat this\n * > as your password, and never give it away!\n * >\n * > In case you have accidentally leaked this string,\n * > make sure to revoke this session in account settings:\n * > \"Privacy & Security\" > \"Active sessions\" >\n * > find the one containing `mtcute` > Revoke,\n * > or, in case this is a bot, revoke bot token\n * > with [@BotFather](//t.me/botfather)\n */\n async exportSession(): Promise<string> {\n const primaryDcs = await this.storage.getDefaultDcs()\n if (!primaryDcs) throw new Error('No default DC set')\n\n const authKey = await this.storage.getAuthKeyFor(primaryDcs.main.id)\n if (!authKey) throw new Error('Auth key is not ready yet')\n\n return writeStringSession(this._writerMap, {\n version: 2,\n self: await this.storage.getSelf(),\n testMode: this._testMode,\n primaryDcs,\n authKey,\n })\n }\n\n /**\n * Request the session to be imported from the given session string.\n *\n * Note that the string will not be parsed and imported right away,\n * instead, it will be imported when `connect()` is called\n *\n * Also note that the session will only be imported in case\n * the storage is missing authorization (i.e. does not contain\n * auth key for the primary DC), otherwise it will be ignored (unless `force).\n *\n * @param session Session string to import\n * @param force Whether to overwrite existing session\n */\n importSession(session: string, force = false): void {\n this._importFrom = session\n this._importForce = force\n }\n}\n"]}
|
|
@@ -13,9 +13,9 @@ export declare class AuthKey {
|
|
|
13
13
|
serverSalt: Uint8Array;
|
|
14
14
|
constructor(_crypto: ICryptoProvider, log: Logger, _readerMap: TlReaderMap);
|
|
15
15
|
match(keyId: Uint8Array): boolean;
|
|
16
|
-
setup(authKey?: Uint8Array | null):
|
|
17
|
-
encryptMessage(message: Uint8Array, serverSalt: Long, sessionId: Long):
|
|
18
|
-
decryptMessage(data: Uint8Array, sessionId: Long, callback: (msgId: tl.Long, seqNo: number, data: TlBinaryReader) => void):
|
|
16
|
+
setup(authKey?: Uint8Array | null): void;
|
|
17
|
+
encryptMessage(message: Uint8Array, serverSalt: Long, sessionId: Long): Uint8Array;
|
|
18
|
+
decryptMessage(data: Uint8Array, sessionId: Long, callback: (msgId: tl.Long, seqNo: number, data: TlBinaryReader) => void): void;
|
|
19
19
|
copyFrom(authKey: AuthKey): void;
|
|
20
20
|
reset(): void;
|
|
21
21
|
}
|
package/cjs/network/auth-key.js
CHANGED
|
@@ -15,17 +15,17 @@ class AuthKey {
|
|
|
15
15
|
match(keyId) {
|
|
16
16
|
return this.ready && (0, index_js_1.buffersEqual)(keyId, this.id);
|
|
17
17
|
}
|
|
18
|
-
|
|
18
|
+
setup(authKey) {
|
|
19
19
|
if (!authKey)
|
|
20
20
|
return this.reset();
|
|
21
21
|
this.ready = true;
|
|
22
22
|
this.key = authKey;
|
|
23
23
|
this.clientSalt = authKey.subarray(88, 120);
|
|
24
24
|
this.serverSalt = authKey.subarray(96, 128);
|
|
25
|
-
this.id =
|
|
25
|
+
this.id = this._crypto.sha1(authKey).subarray(-8);
|
|
26
26
|
this.log.verbose('auth key set up, id = %h', this.id);
|
|
27
27
|
}
|
|
28
|
-
|
|
28
|
+
encryptMessage(message, serverSalt, sessionId) {
|
|
29
29
|
if (!this.ready)
|
|
30
30
|
throw new errors_js_1.MtcuteError('Keys are not set up!');
|
|
31
31
|
let padding = (16 /* header size */ + message.length + 12) /* min padding */ % 16;
|
|
@@ -37,13 +37,13 @@ class AuthKey {
|
|
|
37
37
|
dv.setInt32(8, sessionId.low, true);
|
|
38
38
|
dv.setInt32(12, sessionId.high, true);
|
|
39
39
|
buf.set(message, 16);
|
|
40
|
-
buf.
|
|
41
|
-
const messageKey =
|
|
42
|
-
const ige =
|
|
43
|
-
const encryptedData =
|
|
40
|
+
this._crypto.randomFill(buf.subarray(16 + message.length, 16 + message.length + padding));
|
|
41
|
+
const messageKey = this._crypto.sha256((0, index_js_1.concatBuffers)([this.clientSalt, buf])).subarray(8, 24);
|
|
42
|
+
const ige = (0, mtproto_js_1.createAesIgeForMessage)(this._crypto, this.key, messageKey, true);
|
|
43
|
+
const encryptedData = ige.encrypt(buf);
|
|
44
44
|
return (0, index_js_1.concatBuffers)([this.id, messageKey, encryptedData]);
|
|
45
45
|
}
|
|
46
|
-
|
|
46
|
+
decryptMessage(data, sessionId, callback) {
|
|
47
47
|
const messageKey = data.subarray(8, 24);
|
|
48
48
|
let encryptedData = data.subarray(24);
|
|
49
49
|
const mod16 = encryptedData.byteLength % 16;
|
|
@@ -53,12 +53,12 @@ class AuthKey {
|
|
|
53
53
|
// because padding size is not known, and transport level should not be aware of MTProto structure
|
|
54
54
|
encryptedData = encryptedData.subarray(0, encryptedData.byteLength - mod16);
|
|
55
55
|
}
|
|
56
|
-
const ige =
|
|
57
|
-
const innerData =
|
|
58
|
-
const msgKeySource =
|
|
56
|
+
const ige = (0, mtproto_js_1.createAesIgeForMessage)(this._crypto, this.key, messageKey, false);
|
|
57
|
+
const innerData = ige.decrypt(encryptedData);
|
|
58
|
+
const msgKeySource = this._crypto.sha256((0, index_js_1.concatBuffers)([this.serverSalt, innerData]));
|
|
59
59
|
const expectedMessageKey = msgKeySource.subarray(8, 24);
|
|
60
60
|
if (!(0, index_js_1.buffersEqual)(messageKey, expectedMessageKey)) {
|
|
61
|
-
this.log.warn('
|
|
61
|
+
this.log.warn('received message with invalid messageKey = %h (expected %h)', messageKey, expectedMessageKey);
|
|
62
62
|
return;
|
|
63
63
|
}
|
|
64
64
|
const innerReader = new tl_runtime_1.TlBinaryReader(this._readerMap, innerData);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-key.js","sourceRoot":"","sources":["../../../src/network/auth-key.ts"],"names":[],"mappings":";;;AAGA,mDAAgE;AAEhE,kDAAgD;AAChD,2DAAmE;AACnE,
|
|
1
|
+
{"version":3,"file":"auth-key.js","sourceRoot":"","sources":["../../../src/network/auth-key.ts"],"names":[],"mappings":";;;AAGA,mDAAgE;AAEhE,kDAAgD;AAChD,2DAAmE;AACnE,gDAA4G;AAE5G,MAAa,OAAO;IAQhB,YACa,OAAwB,EACxB,GAAW,EACX,UAAuB;QAFvB,YAAO,GAAP,OAAO,CAAiB;QACxB,QAAG,GAAH,GAAG,CAAQ;QACX,eAAU,GAAV,UAAU,CAAa;QAVpC,UAAK,GAAG,KAAK,CAAA;IAWV,CAAC;IAEJ,KAAK,CAAC,KAAiB;QACnB,OAAO,IAAI,CAAC,KAAK,IAAI,IAAA,uBAAY,EAAC,KAAK,EAAE,IAAI,CAAC,EAAE,CAAC,CAAA;IACrD,CAAC;IAED,KAAK,CAAC,OAA2B;QAC7B,IAAI,CAAC,OAAO;YAAE,OAAO,IAAI,CAAC,KAAK,EAAE,CAAA;QAEjC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAA;QACjB,IAAI,CAAC,GAAG,GAAG,OAAO,CAAA;QAClB,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,QAAQ,CAAC,EAAE,EAAE,GAAG,CAAC,CAAA;QAC3C,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,QAAQ,CAAC,EAAE,EAAE,GAAG,CAAC,CAAA;QAC3C,IAAI,CAAC,EA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,WAAW,GAAG,SAAS,CAAC,MAAM,GAAG,MAAM,GAAG,EAAE,CAAA,CAAC,cAAc;QAEjE,IAAI,WAAW,GAAG,EAAE,IAAI,WAAW,GAAG,IAAI,EAAE;YACxC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,gDAAgD,EAAE,WAAW,CAAC,CAAA;YAE5E,OAAM;SACT;QAED,QAAQ,CAAC,SAAS,EAAE,KAAK,EAAE,WAAW,CAAC,CAAA;IAC3C,CAAC;IAED,QAAQ,CAAC,OAAgB;QACrB,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAA;QAC1B,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAA;QACtB,IAAI,CAAC,EAAE,GAAG,OAAO,CAAC,EAAE,CAAA;QACpB,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAA;QACpC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAA;IACxC,CAAC;IAED,KAAK;QACD,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;IACtB,CAAC;CACJ;AAlID,0BAkIC","sourcesContent":["import Long from 'long'\n\nimport { tl } from '@mtcute/tl'\nimport { TlBinaryReader, TlReaderMap } from '@mtcute/tl-runtime'\n\nimport { MtcuteError } from '../types/errors.js'\nimport { createAesIgeForMessage } from '../utils/crypto/mtproto.js'\nimport { buffersEqual, concatBuffers, dataViewFromBuffer, ICryptoProvider, Logger } from '../utils/index.js'\n\nexport class AuthKey {\n ready = false\n\n key!: Uint8Array\n id!: Uint8Array\n clientSalt!: Uint8Array\n serverSalt!: Uint8Array\n\n constructor(\n readonly _crypto: ICryptoProvider,\n readonly log: Logger,\n readonly _readerMap: TlReaderMap,\n ) {}\n\n match(keyId: Uint8Array): boolean {\n return this.ready && buffersEqual(keyId, this.id)\n }\n\n setup(authKey?: Uint8Array | null): void {\n if (!authKey) return this.reset()\n\n this.ready = true\n this.key = authKey\n this.clientSalt = authKey.subarray(88, 120)\n this.serverSalt = authKey.subarray(96, 128)\n this.id = this._crypto.sha1(authKey).subarray(-8)\n\n this.log.verbose('auth key set up, id = %h', this.id)\n }\n\n encryptMessage(message: Uint8Array, serverSalt: Long, sessionId: Long): Uint8Array {\n if (!this.ready) throw new MtcuteError('Keys are not set up!')\n\n let padding = (16 /* header size */ + message.length + 12) /* min padding */ % 16\n padding = 12 + (padding ? 16 - padding : 0)\n\n const buf = new Uint8Array(16 + message.length + padding)\n const dv = dataViewFromBuffer(buf)\n\n dv.setInt32(0, serverSalt.low, true)\n dv.setInt32(4, serverSalt.high, true)\n dv.setInt32(8, sessionId.low, true)\n dv.setInt32(12, sessionId.high, true)\n buf.set(message, 16)\n this._crypto.randomFill(buf.subarray(16 + message.length, 16 + message.length + padding))\n\n const messageKey = this._crypto.sha256(concatBuffers([this.clientSalt, buf])).subarray(8, 24)\n const ige = createAesIgeForMessage(this._crypto, this.key, messageKey, true)\n const encryptedData = ige.encrypt(buf)\n\n return concatBuffers([this.id, messageKey, encryptedData])\n }\n\n decryptMessage(\n data: Uint8Array,\n sessionId: Long,\n callback: (msgId: tl.Long, seqNo: number, data: TlBinaryReader) => void,\n ): void {\n const messageKey = data.subarray(8, 24)\n let encryptedData = data.subarray(24)\n\n const mod16 = encryptedData.byteLength % 16\n\n if (mod16 !== 0) {\n // strip padding in case of padded transport.\n // i wish this could be done at transport level, but we can't properly align anything there\n // because padding size is not known, and transport level should not be aware of MTProto structure\n encryptedData = encryptedData.subarray(0, encryptedData.byteLength - mod16)\n }\n\n const ige = createAesIgeForMessage(this._crypto, this.key, messageKey, false)\n const innerData = ige.decrypt(encryptedData)\n\n const msgKeySource = this._crypto.sha256(concatBuffers([this.serverSalt, innerData]))\n const expectedMessageKey = msgKeySource.subarray(8, 24)\n\n if (!buffersEqual(messageKey, expectedMessageKey)) {\n this.log.warn('received message with invalid messageKey = %h (expected %h)', messageKey, expectedMessageKey)\n\n return\n }\n\n const innerReader = new TlBinaryReader(this._readerMap, innerData)\n innerReader.seek(8) // skip salt\n const sessionId_ = innerReader.long()\n const messageId = innerReader.long(true)\n\n if (sessionId_.neq(sessionId)) {\n this.log.warn('ignoring message with invalid sessionId = %h', sessionId_)\n\n return\n }\n\n const seqNo = innerReader.uint()\n const length = innerReader.uint()\n\n if (length > innerData.length - 32 /* header size */) {\n this.log.warn('ignoring message with invalid length: %d > %d', length, innerData.length - 32)\n\n return\n }\n\n if (length % 4 !== 0) {\n this.log.warn('ignoring message with invalid length: %d is not a multiple of 4', length)\n\n return\n }\n\n const paddingSize = innerData.length - length - 32 // header size\n\n if (paddingSize < 12 || paddingSize > 1024) {\n this.log.warn('ignoring message with invalid padding size: %d', paddingSize)\n\n return\n }\n\n callback(messageId, seqNo, innerReader)\n }\n\n copyFrom(authKey: AuthKey): void {\n this.ready = authKey.ready\n this.key = authKey.key\n this.id = authKey.id\n this.serverSalt = authKey.serverSalt\n this.clientSalt = authKey.clientSalt\n }\n\n reset(): void {\n this.ready = false\n }\n}\n"]}
|
|
@@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.doAuthorization = void 0;
|
|
7
|
-
const big_integer_1 = __importDefault(require("big-integer"));
|
|
8
7
|
const long_1 = __importDefault(require("long"));
|
|
9
8
|
const tl_1 = require("@mtcute/tl");
|
|
10
9
|
const tl_runtime_1 = require("@mtcute/tl-runtime");
|
|
@@ -18,25 +17,28 @@ const index_js_2 = require("../utils/index.js");
|
|
|
18
17
|
const type_assertions_js_1 = require("../utils/type-assertions.js");
|
|
19
18
|
// Heavily based on code from https://github.com/LonamiWebs/Telethon/blob/master/telethon/network/authenticator.py
|
|
20
19
|
// see https://core.telegram.org/mtproto/security_guidelines
|
|
21
|
-
const DH_SAFETY_RANGE =
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
|
|
20
|
+
// const DH_SAFETY_RANGE = bigInt[2].pow(2048 - 64)
|
|
21
|
+
const DH_SAFETY_RANGE = 2n ** (2048n - 64n);
|
|
22
|
+
const KNOWN_DH_PRIME =
|
|
23
|
+
// eslint-disable-next-line max-len
|
|
24
|
+
0xc71caeb9c6b1c9048e6c522f70f13f73980d40238e3e21c14934d037563d930f48198a0aa7c14058229493d22530f4dbfa336f6e0ac925139543aed44cce7c3720fd51f69458705ac68cd4fe6b6b13abdc9746512969328454f18faf8c595f642477fe96bb2a941d5bcd1d4ac8cc49880708fa9b378e3c4f3a9060bee67cf9a4a4a695811051907e162753b56b0f6b410dba74d8a84b2a14b3144e0ef1284754fd17ed950d5965b4b9dd46582db1178d169c6bc465b0d6ff9ca3928fef5b9ae4e418fc15e83ebea0f87fa9ff5eed70050ded2849f47bf959d956850ce929851f0d8115f635b105ee2e4e15d04b2454bf6f4fadf034b10403119cd8e3b92fcc5bn;
|
|
25
|
+
const TWO_POW_2047 = 2n ** 2047n;
|
|
26
|
+
const TWO_POW_2048 = 2n ** 2048n;
|
|
25
27
|
const checkedPrimesCache = [];
|
|
26
|
-
function checkDhPrime(log, dhPrime, g) {
|
|
27
|
-
if (KNOWN_DH_PRIME
|
|
28
|
+
function checkDhPrime(crypto, log, dhPrime, g) {
|
|
29
|
+
if (KNOWN_DH_PRIME === dhPrime) {
|
|
28
30
|
log.debug('server is using known dh prime, skipping validation');
|
|
29
31
|
return;
|
|
30
32
|
}
|
|
31
|
-
let checkedPrime = checkedPrimesCache.find((x) => x.prime
|
|
33
|
+
let checkedPrime = checkedPrimesCache.find((x) => x.prime === dhPrime);
|
|
32
34
|
if (!checkedPrime) {
|
|
33
|
-
if (dhPrime
|
|
35
|
+
if (dhPrime <= TWO_POW_2047 || dhPrime >= TWO_POW_2048) {
|
|
34
36
|
throw new index_js_1.MtSecurityError('Step 3: dh_prime is not in the 2048-bit range');
|
|
35
37
|
}
|
|
36
|
-
if (!(0, miller_rabin_js_1.millerRabin)(dhPrime)) {
|
|
38
|
+
if (!(0, miller_rabin_js_1.millerRabin)(crypto, dhPrime)) {
|
|
37
39
|
throw new index_js_1.MtSecurityError('Step 3: dh_prime is not prime');
|
|
38
40
|
}
|
|
39
|
-
if (!(0, miller_rabin_js_1.millerRabin)(dhPrime
|
|
41
|
+
if (!(0, miller_rabin_js_1.millerRabin)(crypto, (dhPrime - 1n) / 2n)) {
|
|
40
42
|
throw new index_js_1.MtSecurityError('Step 3: dh_prime is not a safe prime - (dh_prime-1)/2 is not prime');
|
|
41
43
|
}
|
|
42
44
|
log.debug('dh_prime is probably prime');
|
|
@@ -56,34 +58,34 @@ function checkDhPrime(log, dhPrime, g) {
|
|
|
56
58
|
}
|
|
57
59
|
switch (g) {
|
|
58
60
|
case 2:
|
|
59
|
-
if (dhPrime
|
|
61
|
+
if (dhPrime % 8n !== 7n) {
|
|
60
62
|
throw new index_js_1.MtSecurityError('Step 3: ivalid g - dh_prime mod 8 != 7');
|
|
61
63
|
}
|
|
62
64
|
break;
|
|
63
65
|
case 3:
|
|
64
|
-
if (dhPrime
|
|
66
|
+
if (dhPrime % 3n !== 2n) {
|
|
65
67
|
throw new index_js_1.MtSecurityError('Step 3: ivalid g - dh_prime mod 3 != 2');
|
|
66
68
|
}
|
|
67
69
|
break;
|
|
68
70
|
case 4:
|
|
69
71
|
break;
|
|
70
72
|
case 5: {
|
|
71
|
-
const mod = dhPrime
|
|
72
|
-
if (mod
|
|
73
|
+
const mod = dhPrime % 5n;
|
|
74
|
+
if (mod !== 1n && mod !== 4n) {
|
|
73
75
|
throw new index_js_1.MtSecurityError('Step 3: ivalid g - dh_prime mod 5 != 1 && dh_prime mod 5 != 4');
|
|
74
76
|
}
|
|
75
77
|
break;
|
|
76
78
|
}
|
|
77
79
|
case 6: {
|
|
78
|
-
const mod = dhPrime
|
|
79
|
-
if (mod
|
|
80
|
+
const mod = dhPrime % 24n;
|
|
81
|
+
if (mod !== 19n && mod !== 23n) {
|
|
80
82
|
throw new index_js_1.MtSecurityError('Step 3: ivalid g - dh_prime mod 24 != 19 && dh_prime mod 24 != 23');
|
|
81
83
|
}
|
|
82
84
|
break;
|
|
83
85
|
}
|
|
84
86
|
case 7: {
|
|
85
|
-
const mod = dhPrime
|
|
86
|
-
if (mod
|
|
87
|
+
const mod = dhPrime % 7n;
|
|
88
|
+
if (mod !== 3n && mod !== 5n && mod !== 6n) {
|
|
87
89
|
throw new index_js_1.MtSecurityError('Step 3: ivalid g - dh_prime mod 7 != 3 && dh_prime mod 7 != 5 && dh_prime mod 7 != 6');
|
|
88
90
|
}
|
|
89
91
|
break;
|
|
@@ -94,41 +96,44 @@ function checkDhPrime(log, dhPrime, g) {
|
|
|
94
96
|
checkedPrime.generators.push(g);
|
|
95
97
|
log.debug('g = %d is safe to use with dh_prime', g);
|
|
96
98
|
}
|
|
97
|
-
|
|
99
|
+
function rsaPad(data, crypto, key) {
|
|
98
100
|
// since Summer 2021, they use "version of RSA with a variant of OAEP+ padding explained below"
|
|
99
|
-
const keyModulus = (
|
|
100
|
-
const keyExponent = (
|
|
101
|
+
const keyModulus = BigInt(`0x${key.modulus}`);
|
|
102
|
+
const keyExponent = BigInt(`0x${key.exponent}`);
|
|
101
103
|
if (data.length > 144) {
|
|
102
104
|
throw new index_js_1.MtArgumentError('Failed to pad: too big data');
|
|
103
105
|
}
|
|
104
|
-
|
|
106
|
+
const dataPadded = new Uint8Array(192);
|
|
107
|
+
dataPadded.set(data, 0);
|
|
108
|
+
crypto.randomFill(dataPadded.subarray(data.length));
|
|
109
|
+
data = dataPadded;
|
|
105
110
|
for (;;) {
|
|
106
111
|
const aesIv = new Uint8Array(32);
|
|
107
|
-
const aesKey =
|
|
108
|
-
const dataWithHash = (0, buffer_utils_js_1.concatBuffers)([data,
|
|
112
|
+
const aesKey = crypto.randomBytes(32);
|
|
113
|
+
const dataWithHash = (0, buffer_utils_js_1.concatBuffers)([data, crypto.sha256((0, buffer_utils_js_1.concatBuffers)([aesKey, data]))]);
|
|
109
114
|
// we only need to reverse the data
|
|
110
115
|
dataWithHash.subarray(0, 192).reverse();
|
|
111
|
-
const aes =
|
|
112
|
-
const encrypted =
|
|
113
|
-
const encryptedHash =
|
|
116
|
+
const aes = crypto.createAesIge(aesKey, aesIv);
|
|
117
|
+
const encrypted = aes.encrypt(dataWithHash);
|
|
118
|
+
const encryptedHash = crypto.sha256(encrypted);
|
|
114
119
|
(0, utils_js_1.xorBufferInPlace)(aesKey, encryptedHash);
|
|
115
120
|
const decryptedData = (0, buffer_utils_js_1.concatBuffers)([aesKey, encrypted]);
|
|
116
121
|
const decryptedDataBigint = (0, index_js_2.bufferToBigInt)(decryptedData);
|
|
117
|
-
if (decryptedDataBigint
|
|
122
|
+
if (decryptedDataBigint >= keyModulus) {
|
|
118
123
|
continue;
|
|
119
124
|
}
|
|
120
|
-
const encryptedBigint =
|
|
125
|
+
const encryptedBigint = (0, index_js_2.bigIntModPow)(decryptedDataBigint, keyExponent, keyModulus);
|
|
121
126
|
return (0, index_js_2.bigIntToBuffer)(encryptedBigint, 256);
|
|
122
127
|
}
|
|
123
128
|
}
|
|
124
|
-
|
|
129
|
+
function rsaEncrypt(data, crypto, key) {
|
|
125
130
|
const toEncrypt = (0, buffer_utils_js_1.concatBuffers)([
|
|
126
|
-
|
|
131
|
+
crypto.sha1(data),
|
|
127
132
|
data,
|
|
128
133
|
// sha1 is always 20 bytes, so we're left with 255 - 20 - x padding
|
|
129
|
-
|
|
134
|
+
crypto.randomBytes(235 - data.length),
|
|
130
135
|
]);
|
|
131
|
-
const encryptedBigInt = (0, index_js_2.
|
|
136
|
+
const encryptedBigInt = (0, index_js_2.bigIntModPow)((0, index_js_2.bufferToBigInt)(toEncrypt), BigInt(`0x${key.exponent}`), BigInt(`0x${key.modulus}`));
|
|
132
137
|
return (0, index_js_2.bigIntToBuffer)(encryptedBigInt);
|
|
133
138
|
}
|
|
134
139
|
/**
|
|
@@ -160,7 +165,7 @@ async function doAuthorization(connection, crypto, expiresIn) {
|
|
|
160
165
|
}
|
|
161
166
|
if (expiresIn)
|
|
162
167
|
log.prefix = '[PFS] ';
|
|
163
|
-
const nonce =
|
|
168
|
+
const nonce = crypto.randomBytes(16);
|
|
164
169
|
// Step 1: PQ request
|
|
165
170
|
log.debug('starting PQ handshake (temp = %b), nonce = %h', expiresIn, nonce);
|
|
166
171
|
await sendPlainMessage({ _: 'mt_req_pq_multi', nonce });
|
|
@@ -179,7 +184,7 @@ async function doAuthorization(connection, crypto, expiresIn) {
|
|
|
179
184
|
log.debug('found server key, fp = %s, old = %s', publicKey.fingerprint, publicKey.old);
|
|
180
185
|
const [p, q] = await crypto.factorizePQ(resPq.pq);
|
|
181
186
|
log.debug('factorized PQ: PQ = %h, P = %h, Q = %h', resPq.pq, p, q);
|
|
182
|
-
const newNonce =
|
|
187
|
+
const newNonce = crypto.randomBytes(32);
|
|
183
188
|
let dcId = connection.params.dc.id;
|
|
184
189
|
if (connection.params.testMode)
|
|
185
190
|
dcId += 10000;
|
|
@@ -198,8 +203,8 @@ async function doAuthorization(connection, crypto, expiresIn) {
|
|
|
198
203
|
};
|
|
199
204
|
const pqInnerData = tl_runtime_1.TlBinaryWriter.serializeObject(writerMap, _pqInnerData);
|
|
200
205
|
const encryptedData = publicKey.old ?
|
|
201
|
-
|
|
202
|
-
|
|
206
|
+
rsaEncrypt(pqInnerData, crypto, publicKey) :
|
|
207
|
+
rsaPad(pqInnerData, crypto, publicKey);
|
|
203
208
|
log.debug('requesting DH params');
|
|
204
209
|
await sendPlainMessage({
|
|
205
210
|
_: 'mt_req_DH_params',
|
|
@@ -223,13 +228,13 @@ async function doAuthorization(connection, crypto, expiresIn) {
|
|
|
223
228
|
throw new index_js_1.MtSecurityError('Step 2: AES block size is invalid');
|
|
224
229
|
}
|
|
225
230
|
// Step 3: complete DH exchange
|
|
226
|
-
const [key, iv] =
|
|
227
|
-
const ige =
|
|
228
|
-
const plainTextAnswer =
|
|
231
|
+
const [key, iv] = (0, mtproto_js_1.generateKeyAndIvFromNonce)(crypto, resPq.serverNonce, newNonce);
|
|
232
|
+
const ige = crypto.createAesIge(key, iv);
|
|
233
|
+
const plainTextAnswer = ige.decrypt(serverDhParams.encryptedAnswer);
|
|
229
234
|
const innerDataHash = plainTextAnswer.subarray(0, 20);
|
|
230
235
|
const serverDhInnerReader = new tl_runtime_1.TlBinaryReader(readerMap, plainTextAnswer, 20);
|
|
231
236
|
const serverDhInner = serverDhInnerReader.object();
|
|
232
|
-
if (!(0, buffer_utils_js_1.buffersEqual)(innerDataHash,
|
|
237
|
+
if (!(0, buffer_utils_js_1.buffersEqual)(innerDataHash, crypto.sha1(plainTextAnswer.subarray(20, serverDhInnerReader.pos)))) {
|
|
233
238
|
throw new index_js_1.MtSecurityError('Step 3: invalid inner data hash');
|
|
234
239
|
}
|
|
235
240
|
(0, type_assertions_js_1.mtpAssertTypeIs)('auth step 3', serverDhInner, 'mt_server_DH_inner_data');
|
|
@@ -241,30 +246,30 @@ async function doAuthorization(connection, crypto, expiresIn) {
|
|
|
241
246
|
}
|
|
242
247
|
const dhPrime = (0, index_js_2.bufferToBigInt)(serverDhInner.dhPrime);
|
|
243
248
|
const timeOffset = Math.floor(Date.now() / 1000) - serverDhInner.serverTime;
|
|
244
|
-
const g = (
|
|
249
|
+
const g = BigInt(serverDhInner.g);
|
|
245
250
|
const gA = (0, index_js_2.bufferToBigInt)(serverDhInner.gA);
|
|
246
|
-
checkDhPrime(log, dhPrime, serverDhInner.g);
|
|
251
|
+
checkDhPrime(crypto, log, dhPrime, serverDhInner.g);
|
|
247
252
|
let retryId = long_1.default.ZERO;
|
|
248
253
|
const serverSalt = (0, utils_js_1.xorBuffer)(newNonce.subarray(0, 8), resPq.serverNonce.subarray(0, 8));
|
|
249
254
|
for (;;) {
|
|
250
|
-
const b = (0, index_js_2.bufferToBigInt)(
|
|
251
|
-
const gB =
|
|
252
|
-
const authKey = (0, index_js_2.bigIntToBuffer)(
|
|
253
|
-
const authKeyAuxHash =
|
|
255
|
+
const b = (0, index_js_2.bufferToBigInt)(crypto.randomBytes(256));
|
|
256
|
+
const gB = (0, index_js_2.bigIntModPow)(g, b, dhPrime);
|
|
257
|
+
const authKey = (0, index_js_2.bigIntToBuffer)((0, index_js_2.bigIntModPow)(gA, b, dhPrime));
|
|
258
|
+
const authKeyAuxHash = crypto.sha1(authKey).subarray(0, 8);
|
|
254
259
|
// validate DH params
|
|
255
|
-
if (g
|
|
260
|
+
if (g <= 1 || g >= dhPrime - 1n) {
|
|
256
261
|
throw new index_js_1.MtSecurityError('g is not within (1, dh_prime - 1)');
|
|
257
262
|
}
|
|
258
|
-
if (gA
|
|
263
|
+
if (gA <= 1 || gA >= dhPrime - 1n) {
|
|
259
264
|
throw new index_js_1.MtSecurityError('g_a is not within (1, dh_prime - 1)');
|
|
260
265
|
}
|
|
261
|
-
if (gB
|
|
266
|
+
if (gB <= 1 || gB >= dhPrime - 1n) {
|
|
262
267
|
throw new index_js_1.MtSecurityError('g_b is not within (1, dh_prime - 1)');
|
|
263
268
|
}
|
|
264
|
-
if (gA
|
|
269
|
+
if (gA <= DH_SAFETY_RANGE || gA >= dhPrime - DH_SAFETY_RANGE) {
|
|
265
270
|
throw new index_js_1.MtSecurityError('g_a is not within (2^{2048-64}, dh_prime - 2^{2048-64})');
|
|
266
271
|
}
|
|
267
|
-
if (gB
|
|
272
|
+
if (gB <= DH_SAFETY_RANGE || gB >= dhPrime - DH_SAFETY_RANGE) {
|
|
268
273
|
throw new index_js_1.MtSecurityError('g_b is not within (2^{2048-64}, dh_prime - 2^{2048-64})');
|
|
269
274
|
}
|
|
270
275
|
const gB_ = (0, index_js_2.bigIntToBuffer)(gB, 0, false);
|
|
@@ -283,11 +288,11 @@ async function doAuthorization(connection, crypto, expiresIn) {
|
|
|
283
288
|
const clientDhInnerWriter = tl_runtime_1.TlBinaryWriter.alloc(writerMap, innerLength);
|
|
284
289
|
clientDhInnerWriter.pos = 20;
|
|
285
290
|
clientDhInnerWriter.object(clientDhInner);
|
|
286
|
-
const clientDhInnerHash =
|
|
291
|
+
const clientDhInnerHash = crypto.sha1(clientDhInnerWriter.uint8View.subarray(20, clientDhInnerWriter.pos));
|
|
287
292
|
clientDhInnerWriter.pos = 0;
|
|
288
293
|
clientDhInnerWriter.raw(clientDhInnerHash);
|
|
289
294
|
log.debug('sending client DH (timeOffset = %d)', timeOffset);
|
|
290
|
-
const clientDhEncrypted =
|
|
295
|
+
const clientDhEncrypted = ige.encrypt(clientDhInnerWriter.uint8View);
|
|
291
296
|
await sendPlainMessage({
|
|
292
297
|
_: 'mt_set_client_DH_params',
|
|
293
298
|
nonce,
|
|
@@ -310,7 +315,7 @@ async function doAuthorization(connection, crypto, expiresIn) {
|
|
|
310
315
|
throw new index_js_1.MtTypeAssertionError('auth step 4', '!mt_dh_gen_fail', dhGen._);
|
|
311
316
|
}
|
|
312
317
|
if (dhGen._ === 'mt_dh_gen_retry') {
|
|
313
|
-
const expectedHash =
|
|
318
|
+
const expectedHash = crypto.sha1((0, buffer_utils_js_1.concatBuffers)([newNonce, new Uint8Array([2]), authKeyAuxHash]));
|
|
314
319
|
if (!(0, buffer_utils_js_1.buffersEqual)(expectedHash.subarray(4, 20), dhGen.newNonceHash2)) {
|
|
315
320
|
throw Error('Step 4: invalid retry nonce hash from server');
|
|
316
321
|
}
|
|
@@ -319,7 +324,7 @@ async function doAuthorization(connection, crypto, expiresIn) {
|
|
|
319
324
|
}
|
|
320
325
|
if (dhGen._ !== 'mt_dh_gen_ok')
|
|
321
326
|
throw new Error(); // unreachable
|
|
322
|
-
const expectedHash =
|
|
327
|
+
const expectedHash = crypto.sha1((0, buffer_utils_js_1.concatBuffers)([newNonce, new Uint8Array([1]), authKeyAuxHash]));
|
|
323
328
|
if (!(0, buffer_utils_js_1.buffersEqual)(expectedHash.subarray(4, 20), dhGen.newNonceHash1)) {
|
|
324
329
|
throw Error('Step 4: invalid nonce hash from server');
|
|
325
330
|
}
|