teleproto 203.0.0

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.
Files changed (224) hide show
  1. package/CryptoFile.d.ts +2 -0
  2. package/CryptoFile.js +23 -0
  3. package/Helpers.d.ts +151 -0
  4. package/Helpers.js +477 -0
  5. package/LICENSE +23 -0
  6. package/Password.d.ts +15 -0
  7. package/Password.js +271 -0
  8. package/README.md +111 -0
  9. package/Utils.d.ts +228 -0
  10. package/Utils.js +1248 -0
  11. package/Version.d.ts +1 -0
  12. package/Version.js +4 -0
  13. package/client/2fa.d.ts +48 -0
  14. package/client/2fa.js +109 -0
  15. package/client/TelegramClient.d.ts +1042 -0
  16. package/client/TelegramClient.js +1167 -0
  17. package/client/account.d.ts +0 -0
  18. package/client/account.js +1 -0
  19. package/client/auth.d.ts +93 -0
  20. package/client/auth.js +375 -0
  21. package/client/bots.d.ts +6 -0
  22. package/client/bots.js +24 -0
  23. package/client/buttons.d.ts +4 -0
  24. package/client/buttons.js +87 -0
  25. package/client/chats.d.ts +46 -0
  26. package/client/chats.js +350 -0
  27. package/client/dialogs.d.ts +52 -0
  28. package/client/dialogs.js +174 -0
  29. package/client/downloads.d.ts +157 -0
  30. package/client/downloads.js +598 -0
  31. package/client/fs.d.ts +1 -0
  32. package/client/fs.js +13 -0
  33. package/client/index.d.ts +15 -0
  34. package/client/index.js +50 -0
  35. package/client/messageParse.d.ts +17 -0
  36. package/client/messageParse.js +191 -0
  37. package/client/messages.d.ts +259 -0
  38. package/client/messages.js +804 -0
  39. package/client/os.d.ts +2 -0
  40. package/client/os.js +23 -0
  41. package/client/path.d.ts +2 -0
  42. package/client/path.js +7 -0
  43. package/client/telegramBaseClient.d.ts +238 -0
  44. package/client/telegramBaseClient.js +368 -0
  45. package/client/updates.d.ts +31 -0
  46. package/client/updates.js +254 -0
  47. package/client/uploads.d.ts +137 -0
  48. package/client/uploads.js +514 -0
  49. package/client/users.d.ts +29 -0
  50. package/client/users.js +491 -0
  51. package/crypto/AuthKey.d.ts +20 -0
  52. package/crypto/AuthKey.js +76 -0
  53. package/crypto/CTR.d.ts +6 -0
  54. package/crypto/CTR.js +35 -0
  55. package/crypto/Factorizator.d.ts +19 -0
  56. package/crypto/Factorizator.js +74 -0
  57. package/crypto/IGE.d.ts +18 -0
  58. package/crypto/IGE.js +34 -0
  59. package/crypto/RSA.d.ts +15 -0
  60. package/crypto/RSA.js +70 -0
  61. package/crypto/converters.d.ts +19 -0
  62. package/crypto/converters.js +52 -0
  63. package/crypto/crypto.d.ts +27 -0
  64. package/crypto/crypto.js +117 -0
  65. package/crypto/words.d.ts +6 -0
  66. package/crypto/words.js +48 -0
  67. package/define.d.ts +77 -0
  68. package/entityCache.d.ts +7 -0
  69. package/entityCache.js +79 -0
  70. package/errors/Common.d.ts +72 -0
  71. package/errors/Common.js +122 -0
  72. package/errors/RPCBaseErrors.d.ts +84 -0
  73. package/errors/RPCBaseErrors.js +134 -0
  74. package/errors/RPCErrorList.d.ts +37 -0
  75. package/errors/RPCErrorList.js +117 -0
  76. package/errors/index.d.ts +11 -0
  77. package/errors/index.js +29 -0
  78. package/events/Album.d.ts +36 -0
  79. package/events/Album.js +104 -0
  80. package/events/CallbackQuery.d.ts +74 -0
  81. package/events/CallbackQuery.js +193 -0
  82. package/events/DeletedMessage.d.ts +45 -0
  83. package/events/DeletedMessage.js +63 -0
  84. package/events/EditedMessage.d.ts +30 -0
  85. package/events/EditedMessage.js +41 -0
  86. package/events/NewMessage.d.ts +89 -0
  87. package/events/NewMessage.js +182 -0
  88. package/events/Raw.d.ts +29 -0
  89. package/events/Raw.js +43 -0
  90. package/events/common.d.ts +66 -0
  91. package/events/common.js +141 -0
  92. package/events/index.d.ts +2 -0
  93. package/events/index.js +8 -0
  94. package/extensions/AsyncQueue.d.ts +10 -0
  95. package/extensions/AsyncQueue.js +32 -0
  96. package/extensions/BinaryReader.d.ts +100 -0
  97. package/extensions/BinaryReader.js +242 -0
  98. package/extensions/BinaryWriter.d.ts +7 -0
  99. package/extensions/BinaryWriter.js +15 -0
  100. package/extensions/Deferred.d.ts +8 -0
  101. package/extensions/Deferred.js +16 -0
  102. package/extensions/Logger.d.ts +56 -0
  103. package/extensions/Logger.js +107 -0
  104. package/extensions/MessagePacker.d.ts +22 -0
  105. package/extensions/MessagePacker.js +154 -0
  106. package/extensions/PendingState.d.ts +12 -0
  107. package/extensions/PendingState.js +29 -0
  108. package/extensions/PromisedNetSockets.d.ts +25 -0
  109. package/extensions/PromisedNetSockets.js +178 -0
  110. package/extensions/PromisedWebSockets.d.ts +19 -0
  111. package/extensions/PromisedWebSockets.js +124 -0
  112. package/extensions/html.d.ts +5 -0
  113. package/extensions/html.js +213 -0
  114. package/extensions/index.d.ts +7 -0
  115. package/extensions/index.js +17 -0
  116. package/extensions/markdown.d.ts +5 -0
  117. package/extensions/markdown.js +76 -0
  118. package/extensions/markdownv2.d.ts +5 -0
  119. package/extensions/markdownv2.js +51 -0
  120. package/extensions/net.d.ts +1 -0
  121. package/extensions/net.js +13 -0
  122. package/extensions/socks.d.ts +1 -0
  123. package/extensions/socks.js +13 -0
  124. package/index.d.ts +14 -0
  125. package/index.js +48 -0
  126. package/inspect.d.ts +1 -0
  127. package/inspect.js +5 -0
  128. package/network/Authenticator.d.ts +12 -0
  129. package/network/Authenticator.js +193 -0
  130. package/network/MTProtoPlainSender.d.ts +19 -0
  131. package/network/MTProtoPlainSender.js +74 -0
  132. package/network/MTProtoSender.d.ts +291 -0
  133. package/network/MTProtoSender.js +854 -0
  134. package/network/MTProtoState.d.ts +103 -0
  135. package/network/MTProtoState.js +267 -0
  136. package/network/RequestState.d.ts +19 -0
  137. package/network/RequestState.js +35 -0
  138. package/network/connection/Connection.d.ts +71 -0
  139. package/network/connection/Connection.js +157 -0
  140. package/network/connection/TCPAbridged.d.ts +20 -0
  141. package/network/connection/TCPAbridged.js +58 -0
  142. package/network/connection/TCPFull.d.ts +17 -0
  143. package/network/connection/TCPFull.js +61 -0
  144. package/network/connection/TCPMTProxy.d.ts +50 -0
  145. package/network/connection/TCPMTProxy.js +121 -0
  146. package/network/connection/TCPObfuscated.d.ts +19 -0
  147. package/network/connection/TCPObfuscated.js +78 -0
  148. package/network/connection/index.d.ts +4 -0
  149. package/network/connection/index.js +11 -0
  150. package/network/index.d.ts +11 -0
  151. package/network/index.js +23 -0
  152. package/package.json +69 -0
  153. package/requestIter.d.ts +24 -0
  154. package/requestIter.js +107 -0
  155. package/sessions/Abstract.d.ts +103 -0
  156. package/sessions/Abstract.js +6 -0
  157. package/sessions/CacheApiSession.d.ts +18 -0
  158. package/sessions/CacheApiSession.js +99 -0
  159. package/sessions/Memory.d.ts +38 -0
  160. package/sessions/Memory.js +272 -0
  161. package/sessions/StoreSession.d.ts +14 -0
  162. package/sessions/StoreSession.js +77 -0
  163. package/sessions/StringSession.d.ts +33 -0
  164. package/sessions/StringSession.js +116 -0
  165. package/sessions/index.d.ts +4 -0
  166. package/sessions/index.js +13 -0
  167. package/sessions/localStorage.d.ts +1 -0
  168. package/sessions/localStorage.js +4 -0
  169. package/tl/AllTLObjects.d.ts +3 -0
  170. package/tl/AllTLObjects.js +17 -0
  171. package/tl/MTProtoRequest.d.ts +19 -0
  172. package/tl/MTProtoRequest.js +38 -0
  173. package/tl/api.d.ts +31425 -0
  174. package/tl/api.js +507 -0
  175. package/tl/apiTl.d.ts +2 -0
  176. package/tl/apiTl.js +2142 -0
  177. package/tl/core/GZIPPacked.d.ts +16 -0
  178. package/tl/core/GZIPPacked.js +51 -0
  179. package/tl/core/MessageContainer.d.ts +12 -0
  180. package/tl/core/MessageContainer.js +42 -0
  181. package/tl/core/RPCResult.d.ts +15 -0
  182. package/tl/core/RPCResult.js +32 -0
  183. package/tl/core/TLMessage.d.ts +10 -0
  184. package/tl/core/TLMessage.js +14 -0
  185. package/tl/core/index.d.ts +6 -0
  186. package/tl/core/index.js +16 -0
  187. package/tl/custom/button.d.ts +25 -0
  188. package/tl/custom/button.js +78 -0
  189. package/tl/custom/chatGetter.d.ts +30 -0
  190. package/tl/custom/chatGetter.js +114 -0
  191. package/tl/custom/dialog.d.ts +31 -0
  192. package/tl/custom/dialog.js +40 -0
  193. package/tl/custom/draft.d.ts +22 -0
  194. package/tl/custom/draft.js +48 -0
  195. package/tl/custom/file.d.ts +22 -0
  196. package/tl/custom/file.js +68 -0
  197. package/tl/custom/forward.d.ts +16 -0
  198. package/tl/custom/forward.js +47 -0
  199. package/tl/custom/index.d.ts +1 -0
  200. package/tl/custom/index.js +5 -0
  201. package/tl/custom/inlineResult.d.ts +33 -0
  202. package/tl/custom/inlineResult.js +87 -0
  203. package/tl/custom/inlineResults.d.ts +21 -0
  204. package/tl/custom/inlineResults.js +26 -0
  205. package/tl/custom/message.d.ts +428 -0
  206. package/tl/custom/message.js +702 -0
  207. package/tl/custom/messageButton.d.ts +55 -0
  208. package/tl/custom/messageButton.js +152 -0
  209. package/tl/custom/senderGetter.d.ts +29 -0
  210. package/tl/custom/senderGetter.js +55 -0
  211. package/tl/generateModule.d.ts +1 -0
  212. package/tl/generateModule.js +17 -0
  213. package/tl/generationHelpers.d.ts +12 -0
  214. package/tl/generationHelpers.js +289 -0
  215. package/tl/index.d.ts +3 -0
  216. package/tl/index.js +10 -0
  217. package/tl/patched/index.d.ts +2 -0
  218. package/tl/patched/index.js +77 -0
  219. package/tl/schemaTl.d.ts +2 -0
  220. package/tl/schemaTl.js +64 -0
  221. package/tl/types-generator/generate.d.ts +1 -0
  222. package/tl/types-generator/generate.js +84 -0
  223. package/tl/types-generator/template.d.ts +6 -0
  224. package/tl/types-generator/template.js +257 -0
@@ -0,0 +1,2 @@
1
+ import * as crypto from "crypto";
2
+ export default crypto;
package/CryptoFile.js ADDED
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ const crypto = __importStar(require("crypto"));
23
+ exports.default = crypto;
package/Helpers.d.ts ADDED
@@ -0,0 +1,151 @@
1
+ /// <reference types="node" />
2
+ import bigInt from "big-integer";
3
+ import type { EntityLike } from "./define";
4
+ import type { Api } from "./tl";
5
+ /**
6
+ * converts a buffer to big int
7
+ * @param buffer
8
+ * @param little
9
+ * @param signed
10
+ * @returns {bigInt.BigInteger}
11
+ */
12
+ export declare function readBigIntFromBuffer(buffer: Buffer, little?: boolean, signed?: boolean): bigInt.BigInteger;
13
+ export declare function generateRandomBigInt(): bigInt.BigInteger;
14
+ export declare function escapeRegex(string: string): string;
15
+ export declare function groupBy(list: any[], keyGetter: Function): Map<any, any>;
16
+ /**
17
+ * Outputs the object in a better way by hiding all the private methods/attributes.
18
+ * @param object - the class to use
19
+ */
20
+ export declare function betterConsoleLog(object: {
21
+ [key: string]: any;
22
+ }): {
23
+ [key: string]: any;
24
+ };
25
+ /**
26
+ * Helper to find if a given object is an array (or similar)
27
+ */
28
+ export declare const isArrayLike: <T>(x: any) => x is T[];
29
+ /**
30
+ * Special case signed little ints
31
+ * @param big
32
+ * @param number
33
+ * @returns {Buffer}
34
+ */
35
+ export declare function toSignedLittleBuffer(big: bigInt.BigInteger | string | number, number?: number): Buffer;
36
+ /**
37
+ * converts a big int to a buffer
38
+ * @param bigIntVar {BigInteger}
39
+ * @param bytesNumber
40
+ * @param little
41
+ * @param signed
42
+ * @returns {Buffer}
43
+ */
44
+ export declare function readBufferFromBigInt(bigIntVar: bigInt.BigInteger, bytesNumber: number, little?: boolean, signed?: boolean): Buffer;
45
+ /**
46
+ * Generates a random long integer (8 bytes), which is optionally signed
47
+ * @returns {BigInteger}
48
+ */
49
+ export declare function generateRandomLong(signed?: boolean): bigInt.BigInteger;
50
+ /**
51
+ * .... really javascript
52
+ * @param n {number}
53
+ * @param m {number}
54
+ * @returns {number}
55
+ */
56
+ export declare function mod(n: number, m: number): number;
57
+ /**
58
+ * returns a positive bigInt
59
+ * @param n {bigInt.BigInteger}
60
+ * @param m {bigInt.BigInteger}
61
+ * @returns {bigInt.BigInteger}
62
+ */
63
+ export declare function bigIntMod(n: bigInt.BigInteger, m: bigInt.BigInteger): bigInt.BigInteger;
64
+ /**
65
+ * Generates a random bytes array
66
+ * @param count
67
+ * @returns {Buffer}
68
+ */
69
+ export declare function generateRandomBytes(count: number): Buffer;
70
+ /**
71
+ * Calculate the key based on Telegram guidelines, specifying whether it's the client or not
72
+ * @param sharedKey
73
+ * @param msgKey
74
+ * @param client
75
+ * @returns {{iv: Buffer, key: Buffer}}
76
+ */
77
+ export declare function stripText(text: string, entities: Api.TypeMessageEntity[]): string;
78
+ /**
79
+ * Generates the key data corresponding to the given nonces
80
+ * @param serverNonceBigInt
81
+ * @param newNonceBigInt
82
+ * @returns {{key: Buffer, iv: Buffer}}
83
+ */
84
+ export declare function generateKeyDataFromNonce(serverNonceBigInt: bigInt.BigInteger, newNonceBigInt: bigInt.BigInteger): Promise<{
85
+ key: Buffer;
86
+ iv: Buffer;
87
+ }>;
88
+ export declare function convertToLittle(buf: Buffer): Buffer;
89
+ /**
90
+ * Calculates the SHA1 digest for the given data
91
+ * @param data
92
+ * @returns {Promise}
93
+ */
94
+ export declare function sha1(data: Buffer): Promise<Buffer>;
95
+ /**
96
+ * Calculates the SHA256 digest for the given data
97
+ * @param data
98
+ * @returns {Promise}
99
+ */
100
+ export declare function sha256(data: Buffer): Promise<Buffer>;
101
+ /**
102
+ * Fast mod pow for RSA calculation. a^b % n
103
+ * @param a
104
+ * @param b
105
+ * @param n
106
+ * @returns {bigInt.BigInteger}
107
+ */
108
+ export declare function modExp(a: bigInt.BigInteger, b: bigInt.BigInteger, n: bigInt.BigInteger): bigInt.BigInteger;
109
+ /**
110
+ * Gets the arbitrary-length byte array corresponding to the given integer
111
+ * @param integer {number,BigInteger}
112
+ * @param signed {boolean}
113
+ * @returns {Buffer}
114
+ */
115
+ export declare function getByteArray(integer: bigInt.BigInteger | number, signed?: boolean): Buffer;
116
+ export declare function returnBigInt(num: bigInt.BigInteger | string | number | bigint): bigInt.BigInteger;
117
+ /**
118
+ * Helper function to return the smaller big int in an array
119
+ * @param arrayOfBigInts
120
+ */
121
+ export declare function getMinBigInt(arrayOfBigInts: (bigInt.BigInteger | string)[]): bigInt.BigInteger;
122
+ /**
123
+ * returns a random int from min (inclusive) and max (inclusive)
124
+ * @param min
125
+ * @param max
126
+ * @returns {number}
127
+ */
128
+ export declare function getRandomInt(min: number, max: number): number;
129
+ /**
130
+ * Sleeps a specified amount of time
131
+ * @param ms time in milliseconds
132
+ * @param isUnref make a timer unref'ed
133
+ * @returns {Promise}
134
+ */
135
+ export declare const sleep: (ms: number, isUnref?: boolean) => Promise<unknown>;
136
+ /**
137
+ * Helper to export two buffers of same length
138
+ * @returns {Buffer}
139
+ */
140
+ export declare function bufferXor(a: Buffer, b: Buffer): Buffer;
141
+ export declare function crc32(buf: Buffer | string): number;
142
+ export declare class TotalList<T> extends Array<T> {
143
+ total?: number;
144
+ constructor();
145
+ }
146
+ export declare const _EntityType: {
147
+ USER: number;
148
+ CHAT: number;
149
+ CHANNEL: number;
150
+ };
151
+ export declare function _entityType(entity: EntityLike): number;
package/Helpers.js ADDED
@@ -0,0 +1,477 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports._entityType = exports._EntityType = exports.TotalList = exports.crc32 = exports.bufferXor = exports.sleep = exports.getRandomInt = exports.getMinBigInt = exports.returnBigInt = exports.getByteArray = exports.modExp = exports.sha256 = exports.sha1 = exports.convertToLittle = exports.generateKeyDataFromNonce = exports.stripText = exports.generateRandomBytes = exports.bigIntMod = exports.mod = exports.generateRandomLong = exports.readBufferFromBigInt = exports.toSignedLittleBuffer = exports.isArrayLike = exports.betterConsoleLog = exports.groupBy = exports.escapeRegex = exports.generateRandomBigInt = exports.readBigIntFromBuffer = void 0;
7
+ const big_integer_1 = __importDefault(require("big-integer"));
8
+ const CryptoFile_1 = __importDefault(require("./CryptoFile"));
9
+ /**
10
+ * converts a buffer to big int
11
+ * @param buffer
12
+ * @param little
13
+ * @param signed
14
+ * @returns {bigInt.BigInteger}
15
+ */
16
+ function readBigIntFromBuffer(buffer, little = true, signed = false) {
17
+ let randBuffer = Buffer.from(buffer);
18
+ const bytesNumber = randBuffer.length;
19
+ if (little) {
20
+ randBuffer = randBuffer.reverse();
21
+ }
22
+ let bigIntVar = (0, big_integer_1.default)(randBuffer.toString("hex"), 16);
23
+ if (signed && Math.floor(bigIntVar.toString(2).length / 8) >= bytesNumber) {
24
+ bigIntVar = bigIntVar.subtract((0, big_integer_1.default)(2).pow((0, big_integer_1.default)(bytesNumber * 8)));
25
+ }
26
+ return bigIntVar;
27
+ }
28
+ exports.readBigIntFromBuffer = readBigIntFromBuffer;
29
+ function generateRandomBigInt() {
30
+ return readBigIntFromBuffer(generateRandomBytes(8), false);
31
+ }
32
+ exports.generateRandomBigInt = generateRandomBigInt;
33
+ function escapeRegex(string) {
34
+ return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
35
+ }
36
+ exports.escapeRegex = escapeRegex;
37
+ function groupBy(list, keyGetter) {
38
+ const map = new Map();
39
+ list.forEach((item) => {
40
+ const key = keyGetter(item);
41
+ const collection = map.get(key);
42
+ if (!collection) {
43
+ map.set(key, [item]);
44
+ }
45
+ else {
46
+ collection.push(item);
47
+ }
48
+ });
49
+ return map;
50
+ }
51
+ exports.groupBy = groupBy;
52
+ /**
53
+ * Outputs the object in a better way by hiding all the private methods/attributes.
54
+ * @param object - the class to use
55
+ */
56
+ function betterConsoleLog(object) {
57
+ const toPrint = {};
58
+ for (const key in object) {
59
+ if (object.hasOwnProperty(key)) {
60
+ if (!key.startsWith("_") && key != "originalArgs") {
61
+ toPrint[key] = object[key];
62
+ }
63
+ }
64
+ }
65
+ return toPrint;
66
+ }
67
+ exports.betterConsoleLog = betterConsoleLog;
68
+ /**
69
+ * Helper to find if a given object is an array (or similar)
70
+ */
71
+ const isArrayLike = (x) => x &&
72
+ typeof x.length === "number" &&
73
+ typeof x !== "function" &&
74
+ typeof x !== "string";
75
+ exports.isArrayLike = isArrayLike;
76
+ /*
77
+ export function addSurrogate(text: string) {
78
+ let temp = "";
79
+ for (const letter of text) {
80
+ const t = letter.charCodeAt(0);
81
+ if (0x1000 < t && t < 0x10FFFF) {
82
+ const b = Buffer.from(letter, "utf16le");
83
+ const r = String.fromCharCode(b.readUInt16LE(0)) + String.fromCharCode(b.readUInt16LE(2));
84
+ temp += r;
85
+ } else {
86
+ text += letter;
87
+ }
88
+ }
89
+ return temp;
90
+ }
91
+
92
+ */
93
+ /**
94
+ * Special case signed little ints
95
+ * @param big
96
+ * @param number
97
+ * @returns {Buffer}
98
+ */
99
+ function toSignedLittleBuffer(big, number = 8) {
100
+ const bigNumber = returnBigInt(big);
101
+ const byteArray = [];
102
+ for (let i = 0; i < number; i++) {
103
+ byteArray[i] = bigNumber.shiftRight(8 * i).and(255);
104
+ }
105
+ // smh hacks
106
+ return Buffer.from(byteArray);
107
+ }
108
+ exports.toSignedLittleBuffer = toSignedLittleBuffer;
109
+ /**
110
+ * converts a big int to a buffer
111
+ * @param bigIntVar {BigInteger}
112
+ * @param bytesNumber
113
+ * @param little
114
+ * @param signed
115
+ * @returns {Buffer}
116
+ */
117
+ function readBufferFromBigInt(bigIntVar, bytesNumber, little = true, signed = false) {
118
+ bigIntVar = (0, big_integer_1.default)(bigIntVar);
119
+ const bitLength = bigIntVar.bitLength().toJSNumber();
120
+ const bytes = Math.ceil(bitLength / 8);
121
+ if (bytesNumber < bytes) {
122
+ throw new Error("OverflowError: int too big to convert");
123
+ }
124
+ if (!signed && bigIntVar.lesser((0, big_integer_1.default)(0))) {
125
+ throw new Error("Cannot convert to unsigned");
126
+ }
127
+ if (signed && bigIntVar.lesser((0, big_integer_1.default)(0))) {
128
+ bigIntVar = (0, big_integer_1.default)(2)
129
+ .pow((0, big_integer_1.default)(bytesNumber).multiply(8))
130
+ .add(bigIntVar);
131
+ }
132
+ const hex = bigIntVar.toString(16).padStart(bytesNumber * 2, "0");
133
+ let buffer = Buffer.from(hex, "hex");
134
+ if (little) {
135
+ buffer = buffer.reverse();
136
+ }
137
+ return buffer;
138
+ }
139
+ exports.readBufferFromBigInt = readBufferFromBigInt;
140
+ /**
141
+ * Generates a random long integer (8 bytes), which is optionally signed
142
+ * @returns {BigInteger}
143
+ */
144
+ function generateRandomLong(signed = true) {
145
+ return readBigIntFromBuffer(generateRandomBytes(8), true, signed);
146
+ }
147
+ exports.generateRandomLong = generateRandomLong;
148
+ /**
149
+ * .... really javascript
150
+ * @param n {number}
151
+ * @param m {number}
152
+ * @returns {number}
153
+ */
154
+ function mod(n, m) {
155
+ return ((n % m) + m) % m;
156
+ }
157
+ exports.mod = mod;
158
+ /**
159
+ * returns a positive bigInt
160
+ * @param n {bigInt.BigInteger}
161
+ * @param m {bigInt.BigInteger}
162
+ * @returns {bigInt.BigInteger}
163
+ */
164
+ function bigIntMod(n, m) {
165
+ return n.remainder(m).add(m).remainder(m);
166
+ }
167
+ exports.bigIntMod = bigIntMod;
168
+ /**
169
+ * Generates a random bytes array
170
+ * @param count
171
+ * @returns {Buffer}
172
+ */
173
+ function generateRandomBytes(count) {
174
+ return Buffer.from(CryptoFile_1.default.randomBytes(count));
175
+ }
176
+ exports.generateRandomBytes = generateRandomBytes;
177
+ /**
178
+ * Calculate the key based on Telegram guidelines, specifying whether it's the client or not
179
+ * @param sharedKey
180
+ * @param msgKey
181
+ * @param client
182
+ * @returns {{iv: Buffer, key: Buffer}}
183
+ */
184
+ /*CONTEST
185
+ this is mtproto 1 (mostly used for secret chats)
186
+ async function calcKey(sharedKey, msgKey, client) {
187
+ const x = client === true ? 0 : 8
188
+ const [sha1a, sha1b, sha1c, sha1d] = await Promise.all([
189
+ sha1(Buffer.concat([msgKey, sharedKey.slice(x, x + 32)])),
190
+ sha1(Buffer.concat([sharedKey.slice(x + 32, x + 48), msgKey, sharedKey.slice(x + 48, x + 64)])),
191
+ sha1(Buffer.concat([sharedKey.slice(x + 64, x + 96), msgKey])),
192
+ sha1(Buffer.concat([msgKey, sharedKey.slice(x + 96, x + 128)]))
193
+ ])
194
+ const key = Buffer.concat([sha1a.slice(0, 8), sha1b.slice(8, 20), sha1c.slice(4, 16)])
195
+ const iv = Buffer.concat([sha1a.slice(8, 20), sha1b.slice(0, 8), sha1c.slice(16, 20), sha1d.slice(0, 8)])
196
+ return {
197
+ key,
198
+ iv
199
+ }
200
+ }
201
+
202
+ */
203
+ function stripText(text, entities) {
204
+ if (!entities || !entities.length) {
205
+ return text.trim();
206
+ }
207
+ while (text && text[text.length - 1].trim() === "") {
208
+ const e = entities[entities.length - 1];
209
+ if (e.offset + e.length == text.length) {
210
+ if (e.length == 1) {
211
+ entities.pop();
212
+ if (!entities.length) {
213
+ return text.trim();
214
+ }
215
+ }
216
+ else {
217
+ e.length -= 1;
218
+ }
219
+ }
220
+ text = text.slice(0, -1);
221
+ }
222
+ while (text && text[0].trim() === "") {
223
+ for (let i = 0; i < entities.length; i++) {
224
+ const e = entities[i];
225
+ if (e.offset != 0) {
226
+ e.offset--;
227
+ continue;
228
+ }
229
+ if (e.length == 1) {
230
+ entities.shift();
231
+ if (!entities.length) {
232
+ return text.trimLeft();
233
+ }
234
+ }
235
+ else {
236
+ e.length -= 1;
237
+ }
238
+ }
239
+ text = text.slice(1);
240
+ }
241
+ return text;
242
+ }
243
+ exports.stripText = stripText;
244
+ /**
245
+ * Generates the key data corresponding to the given nonces
246
+ * @param serverNonceBigInt
247
+ * @param newNonceBigInt
248
+ * @returns {{key: Buffer, iv: Buffer}}
249
+ */
250
+ async function generateKeyDataFromNonce(serverNonceBigInt, newNonceBigInt) {
251
+ const serverNonce = toSignedLittleBuffer(serverNonceBigInt, 16);
252
+ const newNonce = toSignedLittleBuffer(newNonceBigInt, 32);
253
+ const [hash1, hash2, hash3] = await Promise.all([
254
+ sha1(Buffer.concat([newNonce, serverNonce])),
255
+ sha1(Buffer.concat([serverNonce, newNonce])),
256
+ sha1(Buffer.concat([newNonce, newNonce])),
257
+ ]);
258
+ const keyBuffer = Buffer.concat([hash1, hash2.slice(0, 12)]);
259
+ const ivBuffer = Buffer.concat([
260
+ hash2.slice(12, 20),
261
+ hash3,
262
+ newNonce.slice(0, 4),
263
+ ]);
264
+ return {
265
+ key: keyBuffer,
266
+ iv: ivBuffer,
267
+ };
268
+ }
269
+ exports.generateKeyDataFromNonce = generateKeyDataFromNonce;
270
+ function convertToLittle(buf) {
271
+ const correct = Buffer.alloc(buf.length * 4);
272
+ for (let i = 0; i < buf.length; i++) {
273
+ correct.writeUInt32BE(buf[i], i * 4);
274
+ }
275
+ return correct;
276
+ }
277
+ exports.convertToLittle = convertToLittle;
278
+ /**
279
+ * Calculates the SHA1 digest for the given data
280
+ * @param data
281
+ * @returns {Promise}
282
+ */
283
+ function sha1(data) {
284
+ const shaSum = CryptoFile_1.default.createHash("sha1");
285
+ shaSum.update(data);
286
+ // @ts-ignore
287
+ return shaSum.digest();
288
+ }
289
+ exports.sha1 = sha1;
290
+ /**
291
+ * Calculates the SHA256 digest for the given data
292
+ * @param data
293
+ * @returns {Promise}
294
+ */
295
+ function sha256(data) {
296
+ const shaSum = CryptoFile_1.default.createHash("sha256");
297
+ shaSum.update(data);
298
+ // @ts-ignore
299
+ return shaSum.digest();
300
+ }
301
+ exports.sha256 = sha256;
302
+ /**
303
+ * Fast mod pow for RSA calculation. a^b % n
304
+ * @param a
305
+ * @param b
306
+ * @param n
307
+ * @returns {bigInt.BigInteger}
308
+ */
309
+ function modExp(a, b, n) {
310
+ a = a.remainder(n);
311
+ let result = big_integer_1.default.one;
312
+ let x = a;
313
+ while (b.greater(big_integer_1.default.zero)) {
314
+ const leastSignificantBit = b.remainder((0, big_integer_1.default)(2));
315
+ b = b.divide((0, big_integer_1.default)(2));
316
+ if (leastSignificantBit.eq(big_integer_1.default.one)) {
317
+ result = result.multiply(x);
318
+ result = result.remainder(n);
319
+ }
320
+ x = x.multiply(x);
321
+ x = x.remainder(n);
322
+ }
323
+ return result;
324
+ }
325
+ exports.modExp = modExp;
326
+ /**
327
+ * Gets the arbitrary-length byte array corresponding to the given integer
328
+ * @param integer {number,BigInteger}
329
+ * @param signed {boolean}
330
+ * @returns {Buffer}
331
+ */
332
+ function getByteArray(integer, signed = false) {
333
+ const bits = integer.toString(2).length;
334
+ const byteLength = Math.floor((bits + 8 - 1) / 8);
335
+ return readBufferFromBigInt(typeof integer == "number" ? (0, big_integer_1.default)(integer) : integer, byteLength, false, signed);
336
+ }
337
+ exports.getByteArray = getByteArray;
338
+ function returnBigInt(num) {
339
+ if (big_integer_1.default.isInstance(num)) {
340
+ return num;
341
+ }
342
+ if (typeof num == "number") {
343
+ return (0, big_integer_1.default)(num);
344
+ }
345
+ if (typeof num == "bigint") {
346
+ return (0, big_integer_1.default)(num);
347
+ }
348
+ return (0, big_integer_1.default)(num);
349
+ }
350
+ exports.returnBigInt = returnBigInt;
351
+ /**
352
+ * Helper function to return the smaller big int in an array
353
+ * @param arrayOfBigInts
354
+ */
355
+ function getMinBigInt(arrayOfBigInts) {
356
+ if (arrayOfBigInts.length == 0) {
357
+ return big_integer_1.default.zero;
358
+ }
359
+ if (arrayOfBigInts.length == 1) {
360
+ return returnBigInt(arrayOfBigInts[0]);
361
+ }
362
+ let smallest = returnBigInt(arrayOfBigInts[0]);
363
+ for (let i = 1; i < arrayOfBigInts.length; i++) {
364
+ if (returnBigInt(arrayOfBigInts[i]).lesser(smallest)) {
365
+ smallest = returnBigInt(arrayOfBigInts[i]);
366
+ }
367
+ }
368
+ return smallest;
369
+ }
370
+ exports.getMinBigInt = getMinBigInt;
371
+ /**
372
+ * returns a random int from min (inclusive) and max (inclusive)
373
+ * @param min
374
+ * @param max
375
+ * @returns {number}
376
+ */
377
+ function getRandomInt(min, max) {
378
+ min = Math.ceil(min);
379
+ max = Math.floor(max);
380
+ return Math.floor(Math.random() * (max - min + 1)) + min;
381
+ }
382
+ exports.getRandomInt = getRandomInt;
383
+ /**
384
+ * Sleeps a specified amount of time
385
+ * @param ms time in milliseconds
386
+ * @param isUnref make a timer unref'ed
387
+ * @returns {Promise}
388
+ */
389
+ const sleep = (ms, isUnref = false) => new Promise((resolve) => isUnref ? setTimeout(resolve, ms).unref() : setTimeout(resolve, ms));
390
+ exports.sleep = sleep;
391
+ /**
392
+ * Helper to export two buffers of same length
393
+ * @returns {Buffer}
394
+ */
395
+ function bufferXor(a, b) {
396
+ const res = [];
397
+ for (let i = 0; i < a.length; i++) {
398
+ res.push(a[i] ^ b[i]);
399
+ }
400
+ return Buffer.from(res);
401
+ }
402
+ exports.bufferXor = bufferXor;
403
+ // Taken from https://stackoverflow.com/questions/18638900/javascript-crc32/18639999#18639999
404
+ function makeCRCTable() {
405
+ let c;
406
+ const crcTable = [];
407
+ for (let n = 0; n < 256; n++) {
408
+ c = n;
409
+ for (let k = 0; k < 8; k++) {
410
+ c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
411
+ }
412
+ crcTable[n] = c;
413
+ }
414
+ return crcTable;
415
+ }
416
+ let crcTable = undefined;
417
+ function crc32(buf) {
418
+ if (!crcTable) {
419
+ crcTable = makeCRCTable();
420
+ }
421
+ if (!Buffer.isBuffer(buf)) {
422
+ buf = Buffer.from(buf);
423
+ }
424
+ let crc = -1;
425
+ for (let index = 0; index < buf.length; index++) {
426
+ const byte = buf[index];
427
+ crc = crcTable[(crc ^ byte) & 0xff] ^ (crc >>> 8);
428
+ }
429
+ return (crc ^ -1) >>> 0;
430
+ }
431
+ exports.crc32 = crc32;
432
+ class TotalList extends Array {
433
+ constructor() {
434
+ super();
435
+ this.total = 0;
436
+ }
437
+ }
438
+ exports.TotalList = TotalList;
439
+ exports._EntityType = {
440
+ USER: 0,
441
+ CHAT: 1,
442
+ CHANNEL: 2,
443
+ };
444
+ Object.freeze(exports._EntityType);
445
+ function _entityType(entity) {
446
+ if (typeof entity !== "object" || !("SUBCLASS_OF_ID" in entity)) {
447
+ throw new Error(`${entity} is not a TLObject, cannot determine entity type`);
448
+ }
449
+ if (![
450
+ 0x2d45687,
451
+ 0xc91c90b6,
452
+ 0xe669bf46,
453
+ 0x40f202fd,
454
+ 0x2da17977,
455
+ 0xc5af5d94,
456
+ 0x1f4661b9,
457
+ 0xd49a2697, // crc32('ChatFull')
458
+ ].includes(entity.SUBCLASS_OF_ID)) {
459
+ throw new Error(`${entity} does not have any entity type`);
460
+ }
461
+ const name = entity.className;
462
+ if (name.includes("User")) {
463
+ return exports._EntityType.USER;
464
+ }
465
+ else if (name.includes("Chat")) {
466
+ return exports._EntityType.CHAT;
467
+ }
468
+ else if (name.includes("Channel")) {
469
+ return exports._EntityType.CHANNEL;
470
+ }
471
+ else if (name.includes("Self")) {
472
+ return exports._EntityType.USER;
473
+ }
474
+ // 'Empty' in name or not found, we don't care, not a valid entity.
475
+ throw new Error(`${entity} does not have any entity type`);
476
+ }
477
+ exports._entityType = _entityType;
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Alexander Grigoryev
4
+ Based on work originally licensed under MIT by GramJS (c) 2019
5
+ https://github.com/gram-js/gramjs
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
package/Password.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ /// <reference types="node" />
2
+ import { Api } from "./tl";
3
+ /**
4
+ *
5
+ * @param algo {constructors.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow}
6
+ * @param password
7
+ */
8
+ declare function computeDigest(algo: Api.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow, password: string): Promise<Buffer>;
9
+ /**
10
+ *
11
+ * @param request {constructors.account.Password}
12
+ * @param password {string}
13
+ */
14
+ declare function computeCheck(request: Api.account.Password, password: string): Promise<Api.InputCheckPasswordSRP>;
15
+ export { computeCheck, computeDigest };