@mtkruto/node 0.0.71 → 0.0.72

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.
@@ -1,7 +1,6 @@
1
1
  import { gunzip } from "../deps.js";
2
2
  import { ackThreshold } from "../constants.js";
3
- import { bigIntFromBuffer, getRandomBigInt } from "../utilities/0_bigint.js";
4
- import { sha1 } from "../utilities/0_hash.js";
3
+ import { getRandomBigInt } from "../utilities/0_bigint.js";
5
4
  import { decryptMessage, encryptMessage, getMessageId } from "../utilities/1_message.js";
6
5
  import * as types from "../tl/2_types.js";
7
6
  import * as functions from "../tl/3_functions.js";
@@ -27,12 +26,6 @@ export class Client extends ClientAbstract {
27
26
  writable: true,
28
27
  value: getRandomBigInt(8, true, false)
29
28
  });
30
- Object.defineProperty(this, "auth", {
31
- enumerable: true,
32
- configurable: true,
33
- writable: true,
34
- value: void 0
35
- });
36
29
  Object.defineProperty(this, "state", {
37
30
  enumerable: true,
38
31
  configurable: true,
@@ -60,22 +53,14 @@ export class Client extends ClientAbstract {
60
53
  }
61
54
  async connect() {
62
55
  await this.session.load();
63
- let key;
64
- let id;
65
- if (this.session.authKey != null) {
66
- key = this.session.authKey;
67
- id = bigIntFromBuffer((await sha1(key)).slice(-8), true, false);
68
- }
69
- else {
56
+ if (this.session.authKey == null) {
70
57
  const plain = new ClientPlain(this.transportProvider);
71
58
  await plain.connect();
72
- const { authKey, authKeyId, salt } = await plain.createAuthKey();
59
+ const { authKey, salt } = await plain.createAuthKey();
73
60
  await plain.disconnect();
74
- key = authKey;
75
- id = authKeyId;
76
61
  this.state.salt = salt;
62
+ this.session.authKey = authKey;
77
63
  }
78
- this.auth = { key, id };
79
64
  if (this.session.dc != null) {
80
65
  const { connection, transport, dcId } = this.transportProvider({ dc: this.session.dc, cdn: false });
81
66
  this.connection = connection;
@@ -88,7 +73,7 @@ export class Client extends ClientAbstract {
88
73
  this.pingLoop();
89
74
  }
90
75
  async receiveLoop() {
91
- if (!this.auth) {
76
+ if (!this.session.authKey) {
92
77
  throw new Error("Not connected");
93
78
  }
94
79
  while (this.connected) {
@@ -99,7 +84,7 @@ export class Client extends ClientAbstract {
99
84
  const buffer = await this.transport.receive();
100
85
  let decrypted;
101
86
  try {
102
- decrypted = await decryptMessage(buffer, this.auth.key, this.auth.id, this.sessionId);
87
+ decrypted = await decryptMessage(buffer, this.session.authKey, this.sessionId);
103
88
  }
104
89
  catch (_err) {
105
90
  // logger().error(`Failed to decrypt message: ${err}`);
@@ -164,7 +149,7 @@ export class Client extends ClientAbstract {
164
149
  }
165
150
  }
166
151
  async invoke(function_, noWait) {
167
- if (!this.auth) {
152
+ if (!this.session.authKey) {
168
153
  throw new Error("Not connected");
169
154
  }
170
155
  let seqNo = this.state.seqNo * 2;
@@ -173,7 +158,7 @@ export class Client extends ClientAbstract {
173
158
  this.state.seqNo++;
174
159
  }
175
160
  const message = new Message(getMessageId(), seqNo, function_);
176
- await this.transport.send(await encryptMessage(message, this.auth.key, this.auth.id, this.state.salt, this.sessionId));
161
+ await this.transport.send(await encryptMessage(message, this.session.authKey, this.state.salt, this.sessionId));
177
162
  // logger().debug(`Invoked ${function_.constructor.name}`);
178
163
  if (noWait) {
179
164
  return;
@@ -101,11 +101,9 @@ export class ClientPlain extends ClientAbstract {
101
101
  const salt = newNonce_.slice(0, 0 + 8).map((v, i) => v ^ serverNonceSlice[i]);
102
102
  const authKey_ = modExp(gA, b, dhPrime);
103
103
  const authKey = bufferFromBigInt(authKey_, 256, false, false);
104
- const authKeyId = (await sha1(authKey)).slice(-8);
105
104
  // logger().debug("Auth key created");
106
105
  return {
107
106
  authKey,
108
- authKeyId: bigIntFromBuffer(authKeyId, true, false),
109
107
  salt: bigIntFromBuffer(salt, true, false),
110
108
  };
111
109
  }
@@ -6,8 +6,12 @@ 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";
9
10
  import { bufferFromBigInt, concat } from "./0_buffer.js";
10
- import { sha256 } from "./0_hash.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
+ }
11
15
  let lastMsgId = 0n;
12
16
  export function getMessageId() {
13
17
  const now = new Date().getTime() / 1000 + 0;
@@ -33,8 +37,9 @@ export function unpackUnencryptedMessage(buffer) {
33
37
  const message = reader.read(messageLength);
34
38
  return { messageId, message };
35
39
  }
36
- export async function encryptMessage(message, authKey, authKeyId, salt, sessionId) {
40
+ export async function encryptMessage(message, authKey, salt, sessionId) {
37
41
  const encoded = message.body.serialize();
42
+ const authKeyId = await getAuthKeyId(authKey);
38
43
  const payloadWriter = new TLRawWriter();
39
44
  payloadWriter.writeInt64(salt);
40
45
  payloadWriter.writeInt64(sessionId);
@@ -61,7 +66,8 @@ export async function encryptMessage(message, authKey, authKeyId, salt, sessionI
61
66
  messageWriter.write(ige256Encrypt(payload, aesKey, aesIV));
62
67
  return messageWriter.buffer;
63
68
  }
64
- export async function decryptMessage(buffer, authKey, authKeyId, _sessionId) {
69
+ export async function decryptMessage(buffer, authKey, _sessionId) {
70
+ const authKeyId = await getAuthKeyId(authKey);
65
71
  const reader = new TLReader(buffer);
66
72
  assertEquals(reader.readInt64(false), authKeyId);
67
73
  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.71",
6
+ "version": "0.0.72",
7
7
  "description": "MTKruto for Node.js",
8
8
  "author": "Roj <rojvv@icloud.com>",
9
9
  "license": "LGPL-3.0-or-later",
@@ -27,7 +27,6 @@ exports.Client = void 0;
27
27
  const deps_js_1 = require("../deps.js");
28
28
  const constants_js_1 = require("../constants.js");
29
29
  const _0_bigint_js_1 = require("../utilities/0_bigint.js");
30
- const _0_hash_js_1 = require("../utilities/0_hash.js");
31
30
  const _1_message_js_1 = require("../utilities/1_message.js");
32
31
  const types = __importStar(require("../tl/2_types.js"));
33
32
  const functions = __importStar(require("../tl/3_functions.js"));
@@ -53,12 +52,6 @@ class Client extends client_abstract_js_1.ClientAbstract {
53
52
  writable: true,
54
53
  value: (0, _0_bigint_js_1.getRandomBigInt)(8, true, false)
55
54
  });
56
- Object.defineProperty(this, "auth", {
57
- enumerable: true,
58
- configurable: true,
59
- writable: true,
60
- value: void 0
61
- });
62
55
  Object.defineProperty(this, "state", {
63
56
  enumerable: true,
64
57
  configurable: true,
@@ -86,22 +79,14 @@ class Client extends client_abstract_js_1.ClientAbstract {
86
79
  }
87
80
  async connect() {
88
81
  await this.session.load();
89
- let key;
90
- let id;
91
- if (this.session.authKey != null) {
92
- key = this.session.authKey;
93
- id = (0, _0_bigint_js_1.bigIntFromBuffer)((await (0, _0_hash_js_1.sha1)(key)).slice(-8), true, false);
94
- }
95
- else {
82
+ if (this.session.authKey == null) {
96
83
  const plain = new client_plain_js_1.ClientPlain(this.transportProvider);
97
84
  await plain.connect();
98
- const { authKey, authKeyId, salt } = await plain.createAuthKey();
85
+ const { authKey, salt } = await plain.createAuthKey();
99
86
  await plain.disconnect();
100
- key = authKey;
101
- id = authKeyId;
102
87
  this.state.salt = salt;
88
+ this.session.authKey = authKey;
103
89
  }
104
- this.auth = { key, id };
105
90
  if (this.session.dc != null) {
106
91
  const { connection, transport, dcId } = this.transportProvider({ dc: this.session.dc, cdn: false });
107
92
  this.connection = connection;
@@ -114,7 +99,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
114
99
  this.pingLoop();
115
100
  }
116
101
  async receiveLoop() {
117
- if (!this.auth) {
102
+ if (!this.session.authKey) {
118
103
  throw new Error("Not connected");
119
104
  }
120
105
  while (this.connected) {
@@ -125,7 +110,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
125
110
  const buffer = await this.transport.receive();
126
111
  let decrypted;
127
112
  try {
128
- decrypted = await (0, _1_message_js_1.decryptMessage)(buffer, this.auth.key, this.auth.id, this.sessionId);
113
+ decrypted = await (0, _1_message_js_1.decryptMessage)(buffer, this.session.authKey, this.sessionId);
129
114
  }
130
115
  catch (_err) {
131
116
  // logger().error(`Failed to decrypt message: ${err}`);
@@ -190,7 +175,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
190
175
  }
191
176
  }
192
177
  async invoke(function_, noWait) {
193
- if (!this.auth) {
178
+ if (!this.session.authKey) {
194
179
  throw new Error("Not connected");
195
180
  }
196
181
  let seqNo = this.state.seqNo * 2;
@@ -199,7 +184,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
199
184
  this.state.seqNo++;
200
185
  }
201
186
  const message = new _5_message_js_1.Message((0, _1_message_js_1.getMessageId)(), seqNo, function_);
202
- await this.transport.send(await (0, _1_message_js_1.encryptMessage)(message, this.auth.key, this.auth.id, this.state.salt, this.sessionId));
187
+ await this.transport.send(await (0, _1_message_js_1.encryptMessage)(message, this.session.authKey, this.state.salt, this.sessionId));
203
188
  // logger().debug(`Invoked ${function_.constructor.name}`);
204
189
  if (noWait) {
205
190
  return;
@@ -104,11 +104,9 @@ class ClientPlain extends client_abstract_js_1.ClientAbstract {
104
104
  const salt = newNonce_.slice(0, 0 + 8).map((v, i) => v ^ serverNonceSlice[i]);
105
105
  const authKey_ = (0, _0_bigint_js_1.modExp)(gA, b, dhPrime);
106
106
  const authKey = (0, _0_buffer_js_1.bufferFromBigInt)(authKey_, 256, false, false);
107
- const authKeyId = (await (0, _0_hash_js_1.sha1)(authKey)).slice(-8);
108
107
  // logger().debug("Auth key created");
109
108
  return {
110
109
  authKey,
111
- authKeyId: (0, _0_bigint_js_1.bigIntFromBuffer)(authKeyId, true, false),
112
110
  salt: (0, _0_bigint_js_1.bigIntFromBuffer)(salt, true, false),
113
111
  };
114
112
  }
@@ -9,8 +9,12 @@ 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");
12
13
  const _0_buffer_js_1 = require("./0_buffer.js");
13
14
  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
+ }
14
18
  let lastMsgId = 0n;
15
19
  function getMessageId() {
16
20
  const now = new Date().getTime() / 1000 + 0;
@@ -39,8 +43,9 @@ function unpackUnencryptedMessage(buffer) {
39
43
  return { messageId, message };
40
44
  }
41
45
  exports.unpackUnencryptedMessage = unpackUnencryptedMessage;
42
- async function encryptMessage(message, authKey, authKeyId, salt, sessionId) {
46
+ async function encryptMessage(message, authKey, salt, sessionId) {
43
47
  const encoded = message.body.serialize();
48
+ const authKeyId = await getAuthKeyId(authKey);
44
49
  const payloadWriter = new _0_tl_raw_writer_js_1.TLRawWriter();
45
50
  payloadWriter.writeInt64(salt);
46
51
  payloadWriter.writeInt64(sessionId);
@@ -68,7 +73,8 @@ async function encryptMessage(message, authKey, authKeyId, salt, sessionId) {
68
73
  return messageWriter.buffer;
69
74
  }
70
75
  exports.encryptMessage = encryptMessage;
71
- async function decryptMessage(buffer, authKey, authKeyId, _sessionId) {
76
+ async function decryptMessage(buffer, authKey, _sessionId) {
77
+ const authKeyId = await getAuthKeyId(authKey);
72
78
  const reader = new _3_tl_reader_js_1.TLReader(buffer);
73
79
  (0, deps_js_1.assertEquals)(reader.readInt64(false), authKeyId);
74
80
  const messageKey_ = reader.readInt128();
@@ -8,7 +8,6 @@ export type UpdatesHandler = null | ((client: Client, update: types.Updates) =>
8
8
  export declare class Client extends ClientAbstract {
9
9
  readonly session: Session;
10
10
  private sessionId;
11
- private auth?;
12
11
  private state;
13
12
  private promises;
14
13
  private toAcknowledge;
@@ -4,7 +4,6 @@ export declare class ClientPlain extends ClientAbstract {
4
4
  invoke<T extends Function<unknown>>(function_: T): Promise<T["__R"]>;
5
5
  createAuthKey(): Promise<{
6
6
  authKey: Uint8Array;
7
- authKeyId: bigint;
8
7
  salt: bigint;
9
8
  }>;
10
9
  }
@@ -6,5 +6,5 @@ export declare function unpackUnencryptedMessage(buffer: Uint8Array): {
6
6
  messageId: bigint;
7
7
  message: Uint8Array;
8
8
  };
9
- export declare function encryptMessage(message: Message, authKey: Uint8Array, authKeyId: bigint, salt: bigint, sessionId: bigint): Promise<Uint8Array>;
10
- export declare function decryptMessage(buffer: Uint8Array, authKey: Uint8Array, authKeyId: bigint, _sessionId: bigint): Promise<Message | MessageContainer>;
9
+ export declare function encryptMessage(message: Message, authKey: Uint8Array, salt: bigint, sessionId: bigint): Promise<Uint8Array>;
10
+ export declare function decryptMessage(buffer: Uint8Array, authKey: Uint8Array, _sessionId: bigint): Promise<Message | MessageContainer>;