@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.
- package/esm/client/client.js +13 -7
- package/esm/client/client_plain.js +4 -3
- package/esm/connection/connection_web_socket.js +3 -1
- package/esm/mod.js +1 -0
- package/esm/session/session.js +21 -1
- package/esm/tl/1_tl_object.js +14 -7
- package/esm/tl/3_tl_reader.js +1 -1
- package/esm/tl/3_tl_writer.js +2 -1
- package/esm/tl/5_message.js +2 -2
- package/esm/utilities/0_buffer.js +6 -1
- package/esm/utilities/1_message.js +5 -11
- package/package.json +1 -1
- package/script/client/client.js +13 -7
- package/script/client/client_plain.js +4 -3
- package/script/connection/connection_web_socket.js +3 -1
- package/script/mod.js +3 -1
- package/script/session/session.js +21 -1
- package/script/tl/1_tl_object.js +15 -8
- package/script/tl/3_tl_reader.js +2 -2
- package/script/tl/3_tl_writer.js +2 -1
- package/script/tl/5_message.js +1 -1
- package/script/utilities/0_buffer.js +6 -1
- package/script/utilities/1_message.js +3 -9
- package/types/client/client.d.ts +2 -2
- package/types/mod.d.ts +1 -0
- package/types/session/session.d.ts +5 -1
- package/types/tl/1_tl_object.d.ts +5 -2
- package/types/tl/3_functions.d.ts +216 -216
- package/types/utilities/1_message.d.ts +2 -2
- /package/esm/tl/{3_tl_object_deserializer.js → 3_deserialize.js} +0 -0
- /package/script/tl/{3_tl_object_deserializer.js → 3_deserialize.js} +0 -0
- /package/types/tl/{3_tl_object_deserializer.d.ts → 3_deserialize.d.ts} +0 -0
package/esm/client/client.js
CHANGED
|
@@ -98,11 +98,11 @@ export class Client extends ClientAbstract {
|
|
|
98
98
|
writable: true,
|
|
99
99
|
value: void 0
|
|
100
100
|
});
|
|
101
|
-
Object.defineProperty(this, "
|
|
101
|
+
Object.defineProperty(this, "shouldLoadSession", {
|
|
102
102
|
enumerable: true,
|
|
103
103
|
configurable: true,
|
|
104
104
|
writable: true,
|
|
105
|
-
value:
|
|
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 (
|
|
125
|
+
if (this.shouldLoadSession) {
|
|
123
126
|
await this.session.load();
|
|
124
|
-
this.
|
|
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 (
|
|
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_
|
|
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
|
-
})
|
|
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
|
-
})
|
|
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
|
-
|
|
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";
|
package/esm/session/session.js
CHANGED
|
@@ -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, "
|
|
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
|
}
|
package/esm/tl/1_tl_object.js
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
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
|
package/esm/tl/3_tl_reader.js
CHANGED
|
@@ -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 "./
|
|
4
|
+
import { deserialize } from "./3_deserialize.js";
|
|
5
5
|
export class TLReader extends TLRawReader {
|
|
6
6
|
readObject(id) {
|
|
7
7
|
if (!id) {
|
package/esm/tl/3_tl_writer.js
CHANGED
|
@@ -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
|
|
14
|
+
this.write(object[serialize]());
|
|
14
15
|
}
|
|
15
16
|
return this;
|
|
16
17
|
}
|
package/esm/tl/5_message.js
CHANGED
|
@@ -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
|
|
16
|
+
length += object[serialize]().length;
|
|
17
17
|
}
|
|
18
18
|
return length;
|
|
19
19
|
}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
export function concat(...buffers) {
|
|
2
|
-
|
|
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 {
|
|
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
|
|
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
package/script/client/client.js
CHANGED
|
@@ -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, "
|
|
127
|
+
Object.defineProperty(this, "shouldLoadSession", {
|
|
128
128
|
enumerable: true,
|
|
129
129
|
configurable: true,
|
|
130
130
|
writable: true,
|
|
131
|
-
value:
|
|
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 (
|
|
151
|
+
if (this.shouldLoadSession) {
|
|
149
152
|
await this.session.load();
|
|
150
|
-
this.
|
|
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 (
|
|
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
|
-
|
|
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, "
|
|
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;
|
package/script/tl/1_tl_object.js
CHANGED
|
@@ -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
|
|
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) {
|
package/script/tl/3_tl_reader.js
CHANGED
|
@@ -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
|
|
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,
|
|
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
|
}
|
package/script/tl/3_tl_writer.js
CHANGED
|
@@ -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
|
}
|
package/script/tl/5_message.js
CHANGED
|
@@ -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
|
-
|
|
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();
|