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