telegram-tghub-better-new 0.0.3

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