@mtkruto/node 0.0.71 → 0.0.73

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,15 @@ 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;
63
+ await this.session.save();
77
64
  }
78
- this.auth = { key, id };
79
65
  if (this.session.dc != null) {
80
66
  const { connection, transport, dcId } = this.transportProvider({ dc: this.session.dc, cdn: false });
81
67
  this.connection = connection;
@@ -88,7 +74,7 @@ export class Client extends ClientAbstract {
88
74
  this.pingLoop();
89
75
  }
90
76
  async receiveLoop() {
91
- if (!this.auth) {
77
+ if (!this.session.authKey) {
92
78
  throw new Error("Not connected");
93
79
  }
94
80
  while (this.connected) {
@@ -99,7 +85,7 @@ export class Client extends ClientAbstract {
99
85
  const buffer = await this.transport.receive();
100
86
  let decrypted;
101
87
  try {
102
- decrypted = await decryptMessage(buffer, this.auth.key, this.auth.id, this.sessionId);
88
+ decrypted = await decryptMessage(buffer, this.session.authKey, this.sessionId);
103
89
  }
104
90
  catch (_err) {
105
91
  // logger().error(`Failed to decrypt message: ${err}`);
@@ -164,7 +150,7 @@ export class Client extends ClientAbstract {
164
150
  }
165
151
  }
166
152
  async invoke(function_, noWait) {
167
- if (!this.auth) {
153
+ if (!this.session.authKey) {
168
154
  throw new Error("Not connected");
169
155
  }
170
156
  let seqNo = this.state.seqNo * 2;
@@ -173,7 +159,7 @@ export class Client extends ClientAbstract {
173
159
  this.state.seqNo++;
174
160
  }
175
161
  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));
162
+ await this.transport.send(await encryptMessage(message, this.session.authKey, this.state.salt, this.sessionId));
177
163
  // logger().debug(`Invoked ${function_.constructor.name}`);
178
164
  if (noWait) {
179
165
  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.73",
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,15 @@ 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;
89
+ await this.session.save();
103
90
  }
104
- this.auth = { key, id };
105
91
  if (this.session.dc != null) {
106
92
  const { connection, transport, dcId } = this.transportProvider({ dc: this.session.dc, cdn: false });
107
93
  this.connection = connection;
@@ -114,7 +100,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
114
100
  this.pingLoop();
115
101
  }
116
102
  async receiveLoop() {
117
- if (!this.auth) {
103
+ if (!this.session.authKey) {
118
104
  throw new Error("Not connected");
119
105
  }
120
106
  while (this.connected) {
@@ -125,7 +111,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
125
111
  const buffer = await this.transport.receive();
126
112
  let decrypted;
127
113
  try {
128
- decrypted = await (0, _1_message_js_1.decryptMessage)(buffer, this.auth.key, this.auth.id, this.sessionId);
114
+ decrypted = await (0, _1_message_js_1.decryptMessage)(buffer, this.session.authKey, this.sessionId);
129
115
  }
130
116
  catch (_err) {
131
117
  // logger().error(`Failed to decrypt message: ${err}`);
@@ -190,7 +176,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
190
176
  }
191
177
  }
192
178
  async invoke(function_, noWait) {
193
- if (!this.auth) {
179
+ if (!this.session.authKey) {
194
180
  throw new Error("Not connected");
195
181
  }
196
182
  let seqNo = this.state.seqNo * 2;
@@ -199,7 +185,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
199
185
  this.state.seqNo++;
200
186
  }
201
187
  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));
188
+ await this.transport.send(await (0, _1_message_js_1.encryptMessage)(message, this.session.authKey, this.state.salt, this.sessionId));
203
189
  // logger().debug(`Invoked ${function_.constructor.name}`);
204
190
  if (noWait) {
205
191
  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>;