@mtkruto/node 0.0.79 → 0.0.801

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;
@@ -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
  }
@@ -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) {
@@ -6,12 +6,8 @@ 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) {
36
+ export async function encryptMessage(message, authKey, authKeyId, salt, sessionId) {
41
37
  const encoded = message.body.serialize();
42
- const authKeyId = await getAuthKeyId(authKey);
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.801",
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;
@@ -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;
@@ -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) {
42
+ async function encryptMessage(message, authKey, authKeyId, salt, sessionId) {
47
43
  const encoded = message.body.serialize();
48
- const authKeyId = await getAuthKeyId(authKey);
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();
@@ -38,10 +38,10 @@ export declare class Client extends ClientAbstract {
38
38
  systemLangCode?: string;
39
39
  systemVersion?: string;
40
40
  });
41
+ private shouldLoadSession;
41
42
  setDc(dc: DC): void;
42
- private sessionLoaded;
43
43
  connect(): Promise<void>;
44
- authorize(params: string | AuthorizeUserParams): Promise<void>;
44
+ authorize(params: string | types.AuthExportedAuthorization | AuthorizeUserParams): Promise<void>;
45
45
  private receiveLoop;
46
46
  private pingLoop;
47
47
  invoke<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T): Promise<T extends functions.Function<unknown> ? T["__R"] : void>;
@@ -2,7 +2,11 @@ import { MaybePromise } from "../types.js";
2
2
  import { DC } from "../transport/transport_provider.js";
3
3
  export declare abstract class Session {
4
4
  dc: DC | null;
5
- authKey: Uint8Array | null;
5
+ private _authKeyId;
6
+ private _authKey;
6
7
  abstract load(): MaybePromise<void>;
7
8
  abstract save(): MaybePromise<void>;
9
+ get authKeyId(): Promise<bigint | null>;
10
+ set authKey(authKey: Uint8Array | null);
11
+ get authKey(): Uint8Array | null;
8
12
  }