@mtkruto/node 0.0.79 → 0.0.820

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.
@@ -98,11 +98,11 @@ export class Client extends ClientAbstract {
98
98
  writable: true,
99
99
  value: void 0
100
100
  });
101
- Object.defineProperty(this, "sessionLoaded", {
101
+ Object.defineProperty(this, "shouldLoadSession", {
102
102
  enumerable: true,
103
103
  configurable: true,
104
104
  writable: true,
105
- value: false
105
+ value: true
106
106
  });
107
107
  this.appVersion = params?.appVersion ?? DEFAULT_APP_VERSION;
108
108
  this.deviceModel = params?.deviceModel ?? DEFAULT_DEVICE_MODEL;
@@ -115,13 +115,16 @@ export class Client extends ClientAbstract {
115
115
  if (this.session.dc != dc) {
116
116
  this.session.dc = dc;
117
117
  this.session.authKey = null;
118
+ if (this.shouldLoadSession) {
119
+ this.shouldLoadSession = false;
120
+ }
118
121
  }
119
122
  super.setDc(dc);
120
123
  }
121
124
  async connect() {
122
- if (!this.sessionLoaded) {
125
+ if (this.shouldLoadSession) {
123
126
  await this.session.load();
124
- this.sessionLoaded = true;
127
+ this.shouldLoadSession = false;
125
128
  }
126
129
  if (this.session.authKey == null) {
127
130
  const plain = new ClientPlain(this.transportProvider);
@@ -173,7 +176,10 @@ export class Client extends ClientAbstract {
173
176
  }
174
177
  }
175
178
  try {
176
- if (typeof params == "object") {
179
+ if (params instanceof types.AuthExportedAuthorization) {
180
+ await this.invoke(new functions.AuthImportAuthorization({ id: params.id, bytes: params.bytes }));
181
+ }
182
+ else if (typeof params == "object") {
177
183
  throw new Error("Not implemented");
178
184
  }
179
185
  else {
@@ -223,7 +229,7 @@ export class Client extends ClientAbstract {
223
229
  }
224
230
  let decrypted;
225
231
  try {
226
- decrypted = await decryptMessage(buffer, this.session.authKey, this.sessionId);
232
+ decrypted = await decryptMessage(buffer, this.session.authKey, (await this.session.authKeyId), this.sessionId);
227
233
  }
228
234
  catch (_err) {
229
235
  // logger().error(`Failed to decrypt message: ${err}`);
@@ -297,7 +303,7 @@ export class Client extends ClientAbstract {
297
303
  this.state.seqNo++;
298
304
  }
299
305
  const message = new Message(getMessageId(), seqNo, function_);
300
- await this.transport.send(await encryptMessage(message, this.session.authKey, this.state.salt, this.sessionId));
306
+ await this.transport.send(await encryptMessage(message, this.session.authKey, (await this.session.authKeyId), this.state.salt, this.sessionId));
301
307
  // logger().debug(`Invoked ${function_.constructor.name}`);
302
308
  if (noWait) {
303
309
  return;
@@ -4,6 +4,7 @@ import { bigIntFromBuffer, getRandomBigInt, modExp } from "../utilities/0_bigint
4
4
  import { bufferFromBigInt, concat } from "../utilities/0_buffer.js";
5
5
  import { sha1 } from "../utilities/0_hash.js";
6
6
  import { rsaPad } from "../utilities/1_auth.js";
7
+ import { serialize } from "../tl/1_tl_object.js";
7
8
  import { packUnencryptedMessage, unpackUnencryptedMessage } from "../utilities/1_message.js";
8
9
  import { ClientDHInnerData, DHGenOK, PQInnerDataDC, ResPQ, ServerDHInnerData, ServerDHParamsOK } from "../tl/2_types.js";
9
10
  import { ReqDHParams, ReqPQMulti, SetClientDHParams } from "../tl/3_functions.js";
@@ -11,7 +12,7 @@ import { TLReader } from "../tl/3_tl_reader.js";
11
12
  import { ClientAbstract } from "./client_abstract.js";
12
13
  export class ClientPlain extends ClientAbstract {
13
14
  async invoke(function_) {
14
- await this.transport.send(packUnencryptedMessage(function_.serialize()));
15
+ await this.transport.send(packUnencryptedMessage(function_[serialize]()));
15
16
  const buffer = await this.transport.receive();
16
17
  if (buffer.length == 4) {
17
18
  const int = bigIntFromBuffer(buffer, true, true);
@@ -60,7 +61,7 @@ export class ClientPlain extends ClientAbstract {
60
61
  newNonce,
61
62
  nonce,
62
63
  serverNonce,
63
- }).serialize(), publicKey);
64
+ })[serialize](), publicKey);
64
65
  const dhParams = await this.invoke(new ReqDHParams({
65
66
  nonce,
66
67
  serverNonce,
@@ -88,7 +89,7 @@ export class ClientPlain extends ClientAbstract {
88
89
  serverNonce,
89
90
  retryId: 0n,
90
91
  gB: bufferFromBigInt(gB, 256, false, false),
91
- }).serialize();
92
+ })[serialize]();
92
93
  let dataWithHash = concat(await sha1(data), data);
93
94
  while (dataWithHash.length % 16 != 0) {
94
95
  dataWithHash = concat(dataWithHash, new Uint8Array(1));
@@ -36,7 +36,9 @@ export class ConnectionWebSocket {
36
36
  this.webSocket.onmessage = async (e) => {
37
37
  // deno-lint-ignore no-explicit-any
38
38
  const data = e.data instanceof Blob ? new Uint8Array(await e.data.arrayBuffer()) : new Uint8Array(e.data);
39
- this.buffer.push(...Array.from(data));
39
+ for (const byte of data) {
40
+ this.buffer.push(byte);
41
+ }
40
42
  if (this.nextResolve != null && this.buffer.length >= this.nextResolve[0]) {
41
43
  this.nextResolve[1]();
42
44
  this.nextResolve = null;
package/esm/mod.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { checkPassword } from "./utilities/1_password.js";
2
2
  import { getRandomId } from "./utilities/0_bigint.js";
3
3
  export const utils = { checkPassword, getRandomId };
4
+ export { as } from "./tl/1_tl_object.js";
4
5
  export * as types from "./tl/2_types.js";
5
6
  export * as functions from "./tl/3_functions.js";
6
7
  export * from "./tl/4_rpc_result.js";
@@ -1,3 +1,5 @@
1
+ import { sha1 } from "../utilities/0_hash.js";
2
+ import { bigIntFromBuffer } from "../utilities/0_bigint.js";
1
3
  export class Session {
2
4
  constructor() {
3
5
  Object.defineProperty(this, "dc", {
@@ -6,11 +8,29 @@ export class Session {
6
8
  writable: true,
7
9
  value: null
8
10
  });
9
- Object.defineProperty(this, "authKey", {
11
+ Object.defineProperty(this, "_authKeyId", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: Promise.resolve(null)
16
+ });
17
+ Object.defineProperty(this, "_authKey", {
10
18
  enumerable: true,
11
19
  configurable: true,
12
20
  writable: true,
13
21
  value: null
14
22
  });
15
23
  }
24
+ get authKeyId() {
25
+ return this._authKeyId;
26
+ }
27
+ set authKey(authKey) {
28
+ this._authKey = authKey;
29
+ if (authKey != null) {
30
+ this._authKeyId = sha1(authKey).then((hash) => bigIntFromBuffer(hash.slice(-8), true, false));
31
+ }
32
+ }
33
+ get authKey() {
34
+ return this._authKey;
35
+ }
16
36
  }
@@ -5,6 +5,8 @@ export const id = Symbol("id");
5
5
  export const params = Symbol("params");
6
6
  export const paramDesc = Symbol("paramDesc");
7
7
  export const length = Symbol("length");
8
+ export const serialize = Symbol();
9
+ export const as = Symbol();
8
10
  export function isOptionalParam(ntype) {
9
11
  return ntype.includes("?");
10
12
  }
@@ -22,7 +24,7 @@ function serializeSingleParam(writer, value, type, ntype) {
22
24
  if (isTLObjectConstructor(type)) {
23
25
  if ((type.name == "TypeX" && value instanceof TLObject) ||
24
26
  value instanceof type) {
25
- writer.write(value.serialize());
27
+ writer.write(value[serialize]());
26
28
  return;
27
29
  }
28
30
  else {
@@ -87,10 +89,7 @@ function serializeSingleParam(writer, value, type, ntype) {
87
89
  }
88
90
  break;
89
91
  case "true":
90
- if (value === true) {
91
- writer.writeInt32(0x997275B5);
92
- }
93
- else {
92
+ if (value !== true) {
94
93
  throw new TypeError("Expected true");
95
94
  }
96
95
  }
@@ -101,9 +100,9 @@ export class TLObject {
101
100
  return [];
102
101
  }
103
102
  get [length]() {
104
- return this.serialize().byteLength;
103
+ return this[serialize]().byteLength;
105
104
  }
106
- serialize() {
105
+ [serialize]() {
107
106
  const writer = new TLRawWriter();
108
107
  writer.writeInt32(this[id], false);
109
108
  for (const [value, type, ntype] of this[params]) {
@@ -142,6 +141,14 @@ export class TLObject {
142
141
  }
143
142
  return writer.buffer;
144
143
  }
144
+ [as](constructor) {
145
+ if (this instanceof constructor) {
146
+ return this;
147
+ }
148
+ else {
149
+ throw new TypeError(`Expected ${constructor.name}, got ${this.constructor.name}`);
150
+ }
151
+ }
145
152
  }
146
153
  export function isTLObjectConstructor(t) {
147
154
  // deno-lint-ignore no-explicit-any
@@ -1,7 +1,7 @@
1
1
  import { TLRawReader } from "./0_tl_raw_reader.js";
2
2
  import { paramDesc } from "./1_tl_object.js";
3
3
  import { map } from "./2_types.js";
4
- import { deserialize } from "./3_tl_object_deserializer.js";
4
+ import { deserialize } from "./3_deserialize.js";
5
5
  export class TLReader extends TLRawReader {
6
6
  readObject(id) {
7
7
  if (!id) {
@@ -1,5 +1,6 @@
1
1
  import { VECTOR_CONSTRUCTOR } from "../constants.js";
2
2
  import { TLRawWriter } from "./0_tl_raw_writer.js";
3
+ import { serialize } from "./1_tl_object.js";
3
4
  export class TLWriter extends TLRawWriter {
4
5
  writeObject(object) {
5
6
  if (Array.isArray(object)) {
@@ -10,7 +11,7 @@ export class TLWriter extends TLRawWriter {
10
11
  }
11
12
  }
12
13
  else {
13
- this.write(object.serialize());
14
+ this.write(object[serialize]());
14
15
  }
15
16
  return this;
16
17
  }
@@ -1,4 +1,4 @@
1
- import { id } from "./1_tl_object.js";
1
+ import { id, serialize } from "./1_tl_object.js";
2
2
  import { TLReader } from "./3_tl_reader.js";
3
3
  import { TLWriter } from "./3_tl_writer.js";
4
4
  import { RPCResult } from "./4_rpc_result.js";
@@ -13,7 +13,7 @@ function calculateLength(object) {
13
13
  }
14
14
  }
15
15
  else {
16
- length += object.serialize().length;
16
+ length += object[serialize]().length;
17
17
  }
18
18
  return length;
19
19
  }
@@ -1,5 +1,10 @@
1
1
  export function concat(...buffers) {
2
- return new Uint8Array(buffers.map((v) => Array.from(v)).flat());
2
+ const bytes = new Array();
3
+ for (const buffer of buffers) {
4
+ bytes.push(...buffer);
5
+ }
6
+ const buffer = new Uint8Array(bytes);
7
+ return buffer;
3
8
  }
4
9
  const bufferFromHexString = (hexString) => Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
5
10
  export function bufferFromBigInt(bigIntVar, bytesNumber, little = true, signed = false) {
@@ -1,17 +1,13 @@
1
1
  import { assertEquals, ige256Decrypt, ige256Encrypt } from "../deps.js";
2
2
  import { TLRawReader } from "../tl/0_tl_raw_reader.js";
3
3
  import { TLRawWriter } from "../tl/0_tl_raw_writer.js";
4
- import { id } from "../tl/1_tl_object.js";
4
+ import { id, serialize } from "../tl/1_tl_object.js";
5
5
  import { TLReader } from "../tl/3_tl_reader.js";
6
6
  import { RPCResult } from "../tl/4_rpc_result.js";
7
7
  import { Message } from "../tl/5_message.js";
8
8
  import { MessageContainer } from "../tl/6_message_container.js";
9
- import { bigIntFromBuffer } from "./0_bigint.js";
10
9
  import { bufferFromBigInt, concat } from "./0_buffer.js";
11
- import { sha1, sha256 } from "./0_hash.js";
12
- async function getAuthKeyId(authKey) {
13
- return bigIntFromBuffer((await sha1(authKey)).slice(-8), true, false);
14
- }
10
+ import { sha256 } from "./0_hash.js";
15
11
  let lastMsgId = 0n;
16
12
  export function getMessageId() {
17
13
  const now = new Date().getTime() / 1000 + 0;
@@ -37,9 +33,8 @@ export function unpackUnencryptedMessage(buffer) {
37
33
  const message = reader.read(messageLength);
38
34
  return { messageId, message };
39
35
  }
40
- export async function encryptMessage(message, authKey, salt, sessionId) {
41
- const encoded = message.body.serialize();
42
- const authKeyId = await getAuthKeyId(authKey);
36
+ export async function encryptMessage(message, authKey, authKeyId, salt, sessionId) {
37
+ const encoded = message.body[serialize]();
43
38
  const payloadWriter = new TLRawWriter();
44
39
  payloadWriter.writeInt64(salt);
45
40
  payloadWriter.writeInt64(sessionId);
@@ -66,8 +61,7 @@ export async function encryptMessage(message, authKey, salt, sessionId) {
66
61
  messageWriter.write(ige256Encrypt(payload, aesKey, aesIV));
67
62
  return messageWriter.buffer;
68
63
  }
69
- export async function decryptMessage(buffer, authKey, _sessionId) {
70
- const authKeyId = await getAuthKeyId(authKey);
64
+ export async function decryptMessage(buffer, authKey, authKeyId, _sessionId) {
71
65
  const reader = new TLReader(buffer);
72
66
  assertEquals(reader.readInt64(false), authKeyId);
73
67
  const messageKey_ = reader.readInt128();
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "main": "./script/mod.js",
4
4
  "types": "./types/mod.d.ts",
5
5
  "name": "@mtkruto/node",
6
- "version": "0.0.79",
6
+ "version": "0.0.820",
7
7
  "description": "MTKruto for Node.js",
8
8
  "author": "Roj <rojvv@icloud.com>",
9
9
  "license": "LGPL-3.0-or-later",
@@ -124,11 +124,11 @@ class Client extends client_abstract_js_1.ClientAbstract {
124
124
  writable: true,
125
125
  value: void 0
126
126
  });
127
- Object.defineProperty(this, "sessionLoaded", {
127
+ Object.defineProperty(this, "shouldLoadSession", {
128
128
  enumerable: true,
129
129
  configurable: true,
130
130
  writable: true,
131
- value: false
131
+ value: true
132
132
  });
133
133
  this.appVersion = params?.appVersion ?? constants_js_1.DEFAULT_APP_VERSION;
134
134
  this.deviceModel = params?.deviceModel ?? constants_js_1.DEFAULT_DEVICE_MODEL;
@@ -141,13 +141,16 @@ class Client extends client_abstract_js_1.ClientAbstract {
141
141
  if (this.session.dc != dc) {
142
142
  this.session.dc = dc;
143
143
  this.session.authKey = null;
144
+ if (this.shouldLoadSession) {
145
+ this.shouldLoadSession = false;
146
+ }
144
147
  }
145
148
  super.setDc(dc);
146
149
  }
147
150
  async connect() {
148
- if (!this.sessionLoaded) {
151
+ if (this.shouldLoadSession) {
149
152
  await this.session.load();
150
- this.sessionLoaded = true;
153
+ this.shouldLoadSession = false;
151
154
  }
152
155
  if (this.session.authKey == null) {
153
156
  const plain = new client_plain_js_1.ClientPlain(this.transportProvider);
@@ -199,7 +202,10 @@ class Client extends client_abstract_js_1.ClientAbstract {
199
202
  }
200
203
  }
201
204
  try {
202
- if (typeof params == "object") {
205
+ if (params instanceof types.AuthExportedAuthorization) {
206
+ await this.invoke(new functions.AuthImportAuthorization({ id: params.id, bytes: params.bytes }));
207
+ }
208
+ else if (typeof params == "object") {
203
209
  throw new Error("Not implemented");
204
210
  }
205
211
  else {
@@ -249,7 +255,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
249
255
  }
250
256
  let decrypted;
251
257
  try {
252
- decrypted = await (0, _1_message_js_1.decryptMessage)(buffer, this.session.authKey, this.sessionId);
258
+ decrypted = await (0, _1_message_js_1.decryptMessage)(buffer, this.session.authKey, (await this.session.authKeyId), this.sessionId);
253
259
  }
254
260
  catch (_err) {
255
261
  // logger().error(`Failed to decrypt message: ${err}`);
@@ -323,7 +329,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
323
329
  this.state.seqNo++;
324
330
  }
325
331
  const message = new _5_message_js_1.Message((0, _1_message_js_1.getMessageId)(), seqNo, function_);
326
- await this.transport.send(await (0, _1_message_js_1.encryptMessage)(message, this.session.authKey, this.state.salt, this.sessionId));
332
+ await this.transport.send(await (0, _1_message_js_1.encryptMessage)(message, this.session.authKey, (await this.session.authKeyId), this.state.salt, this.sessionId));
327
333
  // logger().debug(`Invoked ${function_.constructor.name}`);
328
334
  if (noWait) {
329
335
  return;
@@ -7,6 +7,7 @@ const _0_bigint_js_1 = require("../utilities/0_bigint.js");
7
7
  const _0_buffer_js_1 = require("../utilities/0_buffer.js");
8
8
  const _0_hash_js_1 = require("../utilities/0_hash.js");
9
9
  const _1_auth_js_1 = require("../utilities/1_auth.js");
10
+ const _1_tl_object_js_1 = require("../tl/1_tl_object.js");
10
11
  const _1_message_js_1 = require("../utilities/1_message.js");
11
12
  const _2_types_js_1 = require("../tl/2_types.js");
12
13
  const _3_functions_js_1 = require("../tl/3_functions.js");
@@ -14,7 +15,7 @@ const _3_tl_reader_js_1 = require("../tl/3_tl_reader.js");
14
15
  const client_abstract_js_1 = require("./client_abstract.js");
15
16
  class ClientPlain extends client_abstract_js_1.ClientAbstract {
16
17
  async invoke(function_) {
17
- await this.transport.send((0, _1_message_js_1.packUnencryptedMessage)(function_.serialize()));
18
+ await this.transport.send((0, _1_message_js_1.packUnencryptedMessage)(function_[_1_tl_object_js_1.serialize]()));
18
19
  const buffer = await this.transport.receive();
19
20
  if (buffer.length == 4) {
20
21
  const int = (0, _0_bigint_js_1.bigIntFromBuffer)(buffer, true, true);
@@ -63,7 +64,7 @@ class ClientPlain extends client_abstract_js_1.ClientAbstract {
63
64
  newNonce,
64
65
  nonce,
65
66
  serverNonce,
66
- }).serialize(), publicKey);
67
+ })[_1_tl_object_js_1.serialize](), publicKey);
67
68
  const dhParams = await this.invoke(new _3_functions_js_1.ReqDHParams({
68
69
  nonce,
69
70
  serverNonce,
@@ -91,7 +92,7 @@ class ClientPlain extends client_abstract_js_1.ClientAbstract {
91
92
  serverNonce,
92
93
  retryId: 0n,
93
94
  gB: (0, _0_buffer_js_1.bufferFromBigInt)(gB, 256, false, false),
94
- }).serialize();
95
+ })[_1_tl_object_js_1.serialize]();
95
96
  let dataWithHash = (0, _0_buffer_js_1.concat)(await (0, _0_hash_js_1.sha1)(data), data);
96
97
  while (dataWithHash.length % 16 != 0) {
97
98
  dataWithHash = (0, _0_buffer_js_1.concat)(dataWithHash, new Uint8Array(1));
@@ -62,7 +62,9 @@ class ConnectionWebSocket {
62
62
  this.webSocket.onmessage = async (e) => {
63
63
  // deno-lint-ignore no-explicit-any
64
64
  const data = e.data instanceof Blob ? new Uint8Array(await e.data.arrayBuffer()) : new Uint8Array(e.data);
65
- this.buffer.push(...Array.from(data));
65
+ for (const byte of data) {
66
+ this.buffer.push(byte);
67
+ }
66
68
  if (this.nextResolve != null && this.buffer.length >= this.nextResolve[0]) {
67
69
  this.nextResolve[1]();
68
70
  this.nextResolve = null;
package/script/mod.js CHANGED
@@ -26,10 +26,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
26
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.LAYER = exports.DEFAULT_SYSTEM_VERSION = exports.DEFAULT_SYSTEM_LANG_CODE = exports.DEFAULT_LANG_PACK = exports.DEFAULT_LANG_CODE = exports.DEFAULT_INITIAL_DC = exports.DEFAULT_DEVICE_MODEL = exports.DEFAULT_APP_VERSION = exports.functions = exports.types = exports.utils = void 0;
29
+ exports.LAYER = exports.DEFAULT_SYSTEM_VERSION = exports.DEFAULT_SYSTEM_LANG_CODE = exports.DEFAULT_LANG_PACK = exports.DEFAULT_LANG_CODE = exports.DEFAULT_INITIAL_DC = exports.DEFAULT_DEVICE_MODEL = exports.DEFAULT_APP_VERSION = exports.functions = exports.types = exports.as = exports.utils = void 0;
30
30
  const _1_password_js_1 = require("./utilities/1_password.js");
31
31
  const _0_bigint_js_1 = require("./utilities/0_bigint.js");
32
32
  exports.utils = { checkPassword: _1_password_js_1.checkPassword, getRandomId: _0_bigint_js_1.getRandomId };
33
+ var _1_tl_object_js_1 = require("./tl/1_tl_object.js");
34
+ Object.defineProperty(exports, "as", { enumerable: true, get: function () { return _1_tl_object_js_1.as; } });
33
35
  exports.types = __importStar(require("./tl/2_types.js"));
34
36
  exports.functions = __importStar(require("./tl/3_functions.js"));
35
37
  __exportStar(require("./tl/4_rpc_result.js"), exports);
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Session = void 0;
4
+ const _0_hash_js_1 = require("../utilities/0_hash.js");
5
+ const _0_bigint_js_1 = require("../utilities/0_bigint.js");
4
6
  class Session {
5
7
  constructor() {
6
8
  Object.defineProperty(this, "dc", {
@@ -9,12 +11,30 @@ class Session {
9
11
  writable: true,
10
12
  value: null
11
13
  });
12
- Object.defineProperty(this, "authKey", {
14
+ Object.defineProperty(this, "_authKeyId", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: Promise.resolve(null)
19
+ });
20
+ Object.defineProperty(this, "_authKey", {
13
21
  enumerable: true,
14
22
  configurable: true,
15
23
  writable: true,
16
24
  value: null
17
25
  });
18
26
  }
27
+ get authKeyId() {
28
+ return this._authKeyId;
29
+ }
30
+ set authKey(authKey) {
31
+ this._authKey = authKey;
32
+ if (authKey != null) {
33
+ this._authKeyId = (0, _0_hash_js_1.sha1)(authKey).then((hash) => (0, _0_bigint_js_1.bigIntFromBuffer)(hash.slice(-8), true, false));
34
+ }
35
+ }
36
+ get authKey() {
37
+ return this._authKey;
38
+ }
19
39
  }
20
40
  exports.Session = Session;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isTLObjectConstructor = exports.TLObject = exports.analyzeOptionalParam = exports.isOptionalParam = exports.length = exports.paramDesc = exports.params = exports.id = exports.flags = void 0;
3
+ exports.isTLObjectConstructor = exports.TLObject = exports.analyzeOptionalParam = exports.isOptionalParam = exports.as = exports.serialize = exports.length = exports.paramDesc = exports.params = exports.id = exports.flags = void 0;
4
4
  const deps_js_1 = require("../deps.js");
5
5
  const _0_tl_raw_writer_js_1 = require("./0_tl_raw_writer.js");
6
6
  exports.flags = Symbol("flags");
@@ -8,6 +8,8 @@ exports.id = Symbol("id");
8
8
  exports.params = Symbol("params");
9
9
  exports.paramDesc = Symbol("paramDesc");
10
10
  exports.length = Symbol("length");
11
+ exports.serialize = Symbol();
12
+ exports.as = Symbol();
11
13
  function isOptionalParam(ntype) {
12
14
  return ntype.includes("?");
13
15
  }
@@ -27,7 +29,7 @@ function serializeSingleParam(writer, value, type, ntype) {
27
29
  if (isTLObjectConstructor(type)) {
28
30
  if ((type.name == "TypeX" && value instanceof TLObject) ||
29
31
  value instanceof type) {
30
- writer.write(value.serialize());
32
+ writer.write(value[exports.serialize]());
31
33
  return;
32
34
  }
33
35
  else {
@@ -92,10 +94,7 @@ function serializeSingleParam(writer, value, type, ntype) {
92
94
  }
93
95
  break;
94
96
  case "true":
95
- if (value === true) {
96
- writer.writeInt32(0x997275B5);
97
- }
98
- else {
97
+ if (value !== true) {
99
98
  throw new TypeError("Expected true");
100
99
  }
101
100
  }
@@ -106,9 +105,9 @@ class TLObject {
106
105
  return [];
107
106
  }
108
107
  get [exports.length]() {
109
- return this.serialize().byteLength;
108
+ return this[exports.serialize]().byteLength;
110
109
  }
111
- serialize() {
110
+ [exports.serialize]() {
112
111
  const writer = new _0_tl_raw_writer_js_1.TLRawWriter();
113
112
  writer.writeInt32(this[exports.id], false);
114
113
  for (const [value, type, ntype] of this[exports.params]) {
@@ -147,6 +146,14 @@ class TLObject {
147
146
  }
148
147
  return writer.buffer;
149
148
  }
149
+ [exports.as](constructor) {
150
+ if (this instanceof constructor) {
151
+ return this;
152
+ }
153
+ else {
154
+ throw new TypeError(`Expected ${constructor.name}, got ${this.constructor.name}`);
155
+ }
156
+ }
150
157
  }
151
158
  exports.TLObject = TLObject;
152
159
  function isTLObjectConstructor(t) {
@@ -4,7 +4,7 @@ exports.TLReader = void 0;
4
4
  const _0_tl_raw_reader_js_1 = require("./0_tl_raw_reader.js");
5
5
  const _1_tl_object_js_1 = require("./1_tl_object.js");
6
6
  const _2_types_js_1 = require("./2_types.js");
7
- const _3_tl_object_deserializer_js_1 = require("./3_tl_object_deserializer.js");
7
+ const _3_deserialize_js_1 = require("./3_deserialize.js");
8
8
  class TLReader extends _0_tl_raw_reader_js_1.TLRawReader {
9
9
  readObject(id) {
10
10
  if (!id) {
@@ -20,7 +20,7 @@ class TLReader extends _0_tl_raw_reader_js_1.TLRawReader {
20
20
  }
21
21
  const constructor = _2_types_js_1.map.get(id);
22
22
  if (constructor) {
23
- return (0, _3_tl_object_deserializer_js_1.deserialize)(this, constructor[_1_tl_object_js_1.paramDesc], constructor);
23
+ return (0, _3_deserialize_js_1.deserialize)(this, constructor[_1_tl_object_js_1.paramDesc], constructor);
24
24
  }
25
25
  throw new Error(`Unknown constructor ${id.toString(16)}`);
26
26
  }
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TLWriter = void 0;
4
4
  const constants_js_1 = require("../constants.js");
5
5
  const _0_tl_raw_writer_js_1 = require("./0_tl_raw_writer.js");
6
+ const _1_tl_object_js_1 = require("./1_tl_object.js");
6
7
  class TLWriter extends _0_tl_raw_writer_js_1.TLRawWriter {
7
8
  writeObject(object) {
8
9
  if (Array.isArray(object)) {
@@ -13,7 +14,7 @@ class TLWriter extends _0_tl_raw_writer_js_1.TLRawWriter {
13
14
  }
14
15
  }
15
16
  else {
16
- this.write(object.serialize());
17
+ this.write(object[_1_tl_object_js_1.serialize]());
17
18
  }
18
19
  return this;
19
20
  }
@@ -16,7 +16,7 @@ function calculateLength(object) {
16
16
  }
17
17
  }
18
18
  else {
19
- length += object.serialize().length;
19
+ length += object[_1_tl_object_js_1.serialize]().length;
20
20
  }
21
21
  return length;
22
22
  }
@@ -2,7 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.bufferFromBigInt = exports.concat = void 0;
4
4
  function concat(...buffers) {
5
- return new Uint8Array(buffers.map((v) => Array.from(v)).flat());
5
+ const bytes = new Array();
6
+ for (const buffer of buffers) {
7
+ bytes.push(...buffer);
8
+ }
9
+ const buffer = new Uint8Array(bytes);
10
+ return buffer;
6
11
  }
7
12
  exports.concat = concat;
8
13
  const bufferFromHexString = (hexString) => Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
@@ -9,12 +9,8 @@ const _3_tl_reader_js_1 = require("../tl/3_tl_reader.js");
9
9
  const _4_rpc_result_js_1 = require("../tl/4_rpc_result.js");
10
10
  const _5_message_js_1 = require("../tl/5_message.js");
11
11
  const _6_message_container_js_1 = require("../tl/6_message_container.js");
12
- const _0_bigint_js_1 = require("./0_bigint.js");
13
12
  const _0_buffer_js_1 = require("./0_buffer.js");
14
13
  const _0_hash_js_1 = require("./0_hash.js");
15
- async function getAuthKeyId(authKey) {
16
- return (0, _0_bigint_js_1.bigIntFromBuffer)((await (0, _0_hash_js_1.sha1)(authKey)).slice(-8), true, false);
17
- }
18
14
  let lastMsgId = 0n;
19
15
  function getMessageId() {
20
16
  const now = new Date().getTime() / 1000 + 0;
@@ -43,9 +39,8 @@ function unpackUnencryptedMessage(buffer) {
43
39
  return { messageId, message };
44
40
  }
45
41
  exports.unpackUnencryptedMessage = unpackUnencryptedMessage;
46
- async function encryptMessage(message, authKey, salt, sessionId) {
47
- const encoded = message.body.serialize();
48
- const authKeyId = await getAuthKeyId(authKey);
42
+ async function encryptMessage(message, authKey, authKeyId, salt, sessionId) {
43
+ const encoded = message.body[_1_tl_object_js_1.serialize]();
49
44
  const payloadWriter = new _0_tl_raw_writer_js_1.TLRawWriter();
50
45
  payloadWriter.writeInt64(salt);
51
46
  payloadWriter.writeInt64(sessionId);
@@ -73,8 +68,7 @@ async function encryptMessage(message, authKey, salt, sessionId) {
73
68
  return messageWriter.buffer;
74
69
  }
75
70
  exports.encryptMessage = encryptMessage;
76
- async function decryptMessage(buffer, authKey, _sessionId) {
77
- const authKeyId = await getAuthKeyId(authKey);
71
+ async function decryptMessage(buffer, authKey, authKeyId, _sessionId) {
78
72
  const reader = new _3_tl_reader_js_1.TLReader(buffer);
79
73
  (0, deps_js_1.assertEquals)(reader.readInt64(false), authKeyId);
80
74
  const messageKey_ = reader.readInt128();