teleproto 1.214.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 (222) hide show
  1. package/CryptoFile.d.ts +2 -0
  2. package/CryptoFile.js +37 -0
  3. package/Helpers.d.ts +150 -0
  4. package/Helpers.js +477 -0
  5. package/LICENSE +23 -0
  6. package/Password.d.ts +14 -0
  7. package/Password.js +270 -0
  8. package/README.md +98 -0
  9. package/Utils.d.ts +227 -0
  10. package/Utils.js +1247 -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 +108 -0
  15. package/client/TelegramClient.d.ts +1043 -0
  16. package/client/TelegramClient.js +1181 -0
  17. package/client/account.d.ts +0 -0
  18. package/client/account.js +1 -0
  19. package/client/auth.d.ts +92 -0
  20. package/client/auth.js +388 -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 +45 -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 +156 -0
  30. package/client/downloads.js +614 -0
  31. package/client/fs.d.ts +1 -0
  32. package/client/fs.js +17 -0
  33. package/client/index.d.ts +15 -0
  34. package/client/index.js +64 -0
  35. package/client/messageParse.d.ts +17 -0
  36. package/client/messageParse.js +191 -0
  37. package/client/messages.d.ts +263 -0
  38. package/client/messages.js +813 -0
  39. package/client/os.d.ts +2 -0
  40. package/client/os.js +37 -0
  41. package/client/path.d.ts +2 -0
  42. package/client/path.js +7 -0
  43. package/client/telegramBaseClient.d.ts +237 -0
  44. package/client/telegramBaseClient.js +368 -0
  45. package/client/updates.d.ts +33 -0
  46. package/client/updates.js +260 -0
  47. package/client/uploads.d.ts +136 -0
  48. package/client/uploads.js +514 -0
  49. package/client/users.d.ts +29 -0
  50. package/client/users.js +490 -0
  51. package/crypto/AuthKey.d.ts +19 -0
  52. package/crypto/AuthKey.js +76 -0
  53. package/crypto/CTR.d.ts +5 -0
  54. package/crypto/CTR.js +16 -0
  55. package/crypto/Factorizator.d.ts +19 -0
  56. package/crypto/Factorizator.js +74 -0
  57. package/crypto/IGE.d.ts +11 -0
  58. package/crypto/IGE.js +115 -0
  59. package/crypto/RSA.d.ts +14 -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 +19 -0
  64. package/crypto/crypto.js +58 -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 +71 -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 +32 -0
  78. package/events/Album.d.ts +36 -0
  79. package/events/Album.js +104 -0
  80. package/events/CallbackQuery.d.ts +73 -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 +99 -0
  97. package/extensions/BinaryReader.js +242 -0
  98. package/extensions/BinaryWriter.d.ts +6 -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 +54 -0
  103. package/extensions/Logger.js +128 -0
  104. package/extensions/MessagePacker.d.ts +21 -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 +24 -0
  109. package/extensions/PromisedNetSockets.js +192 -0
  110. package/extensions/PromisedWebSockets.d.ts +18 -0
  111. package/extensions/PromisedWebSockets.js +124 -0
  112. package/extensions/html.d.ts +5 -0
  113. package/extensions/html.js +228 -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 +17 -0
  122. package/extensions/socks.d.ts +1 -0
  123. package/extensions/socks.js +17 -0
  124. package/index.d.ts +14 -0
  125. package/index.js +62 -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 +192 -0
  130. package/network/MTProtoPlainSender.d.ts +19 -0
  131. package/network/MTProtoPlainSender.js +74 -0
  132. package/network/MTProtoSender.d.ts +290 -0
  133. package/network/MTProtoSender.js +873 -0
  134. package/network/MTProtoState.d.ts +102 -0
  135. package/network/MTProtoState.js +267 -0
  136. package/network/RequestState.d.ts +18 -0
  137. package/network/RequestState.js +35 -0
  138. package/network/connection/Connection.d.ts +69 -0
  139. package/network/connection/Connection.js +162 -0
  140. package/network/connection/TCPAbridged.d.ts +19 -0
  141. package/network/connection/TCPAbridged.js +58 -0
  142. package/network/connection/TCPFull.d.ts +16 -0
  143. package/network/connection/TCPFull.js +61 -0
  144. package/network/connection/TCPMTProxy.d.ts +49 -0
  145. package/network/connection/TCPMTProxy.js +121 -0
  146. package/network/connection/TCPObfuscated.d.ts +18 -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 +50 -0
  153. package/requestIter.d.ts +24 -0
  154. package/requestIter.js +109 -0
  155. package/sessions/Abstract.d.ts +103 -0
  156. package/sessions/Abstract.js +6 -0
  157. package/sessions/Memory.d.ts +38 -0
  158. package/sessions/Memory.js +272 -0
  159. package/sessions/StoreSession.d.ts +14 -0
  160. package/sessions/StoreSession.js +77 -0
  161. package/sessions/StringSession.d.ts +32 -0
  162. package/sessions/StringSession.js +116 -0
  163. package/sessions/index.d.ts +4 -0
  164. package/sessions/index.js +11 -0
  165. package/sessions/localStorage.d.ts +1 -0
  166. package/sessions/localStorage.js +4 -0
  167. package/tl/AllTLObjects.d.ts +3 -0
  168. package/tl/AllTLObjects.js +17 -0
  169. package/tl/MTProtoRequest.d.ts +18 -0
  170. package/tl/MTProtoRequest.js +38 -0
  171. package/tl/api.d.ts +32488 -0
  172. package/tl/api.js +507 -0
  173. package/tl/apiTl.d.ts +2 -0
  174. package/tl/apiTl.js +2209 -0
  175. package/tl/core/GZIPPacked.d.ts +15 -0
  176. package/tl/core/GZIPPacked.js +51 -0
  177. package/tl/core/MessageContainer.d.ts +12 -0
  178. package/tl/core/MessageContainer.js +42 -0
  179. package/tl/core/RPCResult.d.ts +14 -0
  180. package/tl/core/RPCResult.js +32 -0
  181. package/tl/core/TLMessage.d.ts +10 -0
  182. package/tl/core/TLMessage.js +14 -0
  183. package/tl/core/index.d.ts +6 -0
  184. package/tl/core/index.js +16 -0
  185. package/tl/custom/button.d.ts +24 -0
  186. package/tl/custom/button.js +78 -0
  187. package/tl/custom/chatGetter.d.ts +29 -0
  188. package/tl/custom/chatGetter.js +116 -0
  189. package/tl/custom/dialog.d.ts +30 -0
  190. package/tl/custom/dialog.js +40 -0
  191. package/tl/custom/draft.d.ts +21 -0
  192. package/tl/custom/draft.js +48 -0
  193. package/tl/custom/file.d.ts +21 -0
  194. package/tl/custom/file.js +68 -0
  195. package/tl/custom/forward.d.ts +15 -0
  196. package/tl/custom/forward.js +47 -0
  197. package/tl/custom/index.d.ts +1 -0
  198. package/tl/custom/index.js +5 -0
  199. package/tl/custom/inlineResult.d.ts +32 -0
  200. package/tl/custom/inlineResult.js +87 -0
  201. package/tl/custom/inlineResults.d.ts +20 -0
  202. package/tl/custom/inlineResults.js +26 -0
  203. package/tl/custom/message.d.ts +427 -0
  204. package/tl/custom/message.js +716 -0
  205. package/tl/custom/messageButton.d.ts +54 -0
  206. package/tl/custom/messageButton.js +152 -0
  207. package/tl/custom/senderGetter.d.ts +28 -0
  208. package/tl/custom/senderGetter.js +55 -0
  209. package/tl/generateModule.d.ts +1 -0
  210. package/tl/generateModule.js +17 -0
  211. package/tl/generationHelpers.d.ts +11 -0
  212. package/tl/generationHelpers.js +289 -0
  213. package/tl/index.d.ts +3 -0
  214. package/tl/index.js +10 -0
  215. package/tl/patched/index.d.ts +2 -0
  216. package/tl/patched/index.js +76 -0
  217. package/tl/schemaTl.d.ts +2 -0
  218. package/tl/schemaTl.js +64 -0
  219. package/tl/types-generator/generate.d.ts +1 -0
  220. package/tl/types-generator/generate.js +84 -0
  221. package/tl/types-generator/template.d.ts +6 -0
  222. package/tl/types-generator/template.js +257 -0
@@ -0,0 +1,74 @@
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.Factorizator = void 0;
7
+ const big_integer_1 = __importDefault(require("big-integer"));
8
+ const Helpers_1 = require("../Helpers");
9
+ class Factorizator {
10
+ /**
11
+ * Calculates the greatest common divisor
12
+ * @param a {BigInteger}
13
+ * @param b {BigInteger}
14
+ * @returns {BigInteger}
15
+ */
16
+ static gcd(a, b) {
17
+ while (b.neq(big_integer_1.default.zero)) {
18
+ const temp = b;
19
+ b = a.remainder(b);
20
+ a = temp;
21
+ }
22
+ return a;
23
+ }
24
+ /**
25
+ * Factorizes the given number and returns both the divisor and the number divided by the divisor
26
+ * @param pq {BigInteger}
27
+ * @returns {{p: *, q: *}}
28
+ */
29
+ static factorize(pq) {
30
+ if (pq.remainder(2).equals(big_integer_1.default.zero)) {
31
+ return { p: (0, big_integer_1.default)(2), q: pq.divide((0, big_integer_1.default)(2)) };
32
+ }
33
+ let y = big_integer_1.default.randBetween((0, big_integer_1.default)(1), pq.minus(1));
34
+ const c = big_integer_1.default.randBetween((0, big_integer_1.default)(1), pq.minus(1));
35
+ const m = big_integer_1.default.randBetween((0, big_integer_1.default)(1), pq.minus(1));
36
+ let g = big_integer_1.default.one;
37
+ let r = big_integer_1.default.one;
38
+ let q = big_integer_1.default.one;
39
+ let x = big_integer_1.default.zero;
40
+ let ys = big_integer_1.default.zero;
41
+ let k;
42
+ while (g.eq(big_integer_1.default.one)) {
43
+ x = y;
44
+ for (let i = 0; (0, big_integer_1.default)(i).lesser(r); i++) {
45
+ y = (0, Helpers_1.modExp)(y, (0, big_integer_1.default)(2), pq).add(c).remainder(pq);
46
+ }
47
+ k = big_integer_1.default.zero;
48
+ while (k.lesser(r) && g.eq(big_integer_1.default.one)) {
49
+ ys = y;
50
+ const condition = big_integer_1.default.min(m, r.minus(k));
51
+ for (let i = 0; (0, big_integer_1.default)(i).lesser(condition); i++) {
52
+ y = (0, Helpers_1.modExp)(y, (0, big_integer_1.default)(2), pq).add(c).remainder(pq);
53
+ q = q.multiply(x.minus(y).abs()).remainder(pq);
54
+ }
55
+ g = Factorizator.gcd(q, pq);
56
+ k = k.add(m);
57
+ }
58
+ r = r.multiply(2);
59
+ }
60
+ if (g.eq(pq)) {
61
+ while (true) {
62
+ ys = (0, Helpers_1.modExp)(ys, (0, big_integer_1.default)(2), pq).add(c).remainder(pq);
63
+ g = Factorizator.gcd(x.minus(ys).abs(), pq);
64
+ if (g.greater(1)) {
65
+ break;
66
+ }
67
+ }
68
+ }
69
+ const p = g;
70
+ q = pq.divide(g);
71
+ return p < q ? { p: p, q: q } : { p: q, q: p };
72
+ }
73
+ }
74
+ exports.Factorizator = Factorizator;
@@ -0,0 +1,11 @@
1
+ declare class IGE {
2
+ private key;
3
+ private iv;
4
+ constructor(key: Buffer, iv: Buffer);
5
+ private xorBuffers;
6
+ private aesEncryptBlock;
7
+ private aesDecryptBlock;
8
+ encryptIge(plainText: Buffer): Buffer;
9
+ decryptIge(cipherText: Buffer): Buffer;
10
+ }
11
+ export { IGE };
package/crypto/IGE.js ADDED
@@ -0,0 +1,115 @@
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 () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.IGE = void 0;
40
+ const node_crypto_1 = __importDefault(require("node:crypto"));
41
+ const Helpers = __importStar(require("../Helpers"));
42
+ class IGE {
43
+ constructor(key, iv) {
44
+ if (key.length !== 32)
45
+ throw new Error("Key must be 32 bytes (AES-256)");
46
+ if (iv.length !== 32)
47
+ throw new Error("IV must be 32 bytes (2 * block size)");
48
+ this.key = key;
49
+ this.iv = iv;
50
+ }
51
+ xorBuffers(a, b) {
52
+ const res = Buffer.alloc(a.length);
53
+ for (let i = 0; i < a.length; i++) {
54
+ res[i] = a[i] ^ b[i];
55
+ }
56
+ return res;
57
+ }
58
+ aesEncryptBlock(block) {
59
+ const cipher = node_crypto_1.default.createCipheriv("aes-256-ecb", this.key, null);
60
+ cipher.setAutoPadding(false);
61
+ return Buffer.concat([cipher.update(block), cipher.final()]);
62
+ }
63
+ aesDecryptBlock(block) {
64
+ const decipher = node_crypto_1.default.createDecipheriv("aes-256-ecb", this.key, null);
65
+ decipher.setAutoPadding(false);
66
+ return Buffer.concat([decipher.update(block), decipher.final()]);
67
+ }
68
+ encryptIge(plainText) {
69
+ const blockSize = 16;
70
+ const padding = plainText.length % blockSize;
71
+ if (padding !== 0) {
72
+ plainText = Buffer.concat([
73
+ plainText,
74
+ Helpers.generateRandomBytes(blockSize - padding),
75
+ ]);
76
+ }
77
+ const iv1 = this.iv.subarray(0, blockSize);
78
+ const iv2 = this.iv.subarray(blockSize, 2 * blockSize);
79
+ let prevCipher = iv1;
80
+ let prevPlain = iv2;
81
+ const output = Buffer.alloc(plainText.length);
82
+ for (let i = 0; i < plainText.length; i += blockSize) {
83
+ const plainBlock = plainText.subarray(i, i + blockSize);
84
+ const xored = this.xorBuffers(plainBlock, prevCipher);
85
+ const encrypted = this.aesEncryptBlock(xored);
86
+ const cipherBlock = this.xorBuffers(encrypted, prevPlain);
87
+ cipherBlock.copy(output, i);
88
+ prevCipher = cipherBlock;
89
+ prevPlain = plainBlock;
90
+ }
91
+ return output;
92
+ }
93
+ decryptIge(cipherText) {
94
+ const blockSize = 16;
95
+ if (cipherText.length % blockSize !== 0) {
96
+ throw new Error("Cipher text must be multiple of 16 bytes");
97
+ }
98
+ const iv1 = this.iv.subarray(0, blockSize);
99
+ const iv2 = this.iv.subarray(blockSize, 2 * blockSize);
100
+ let prevCipher = iv1;
101
+ let prevPlain = iv2;
102
+ const output = Buffer.alloc(cipherText.length);
103
+ for (let i = 0; i < cipherText.length; i += blockSize) {
104
+ const cipherBlock = cipherText.subarray(i, i + blockSize);
105
+ const xored = this.xorBuffers(cipherBlock, prevPlain);
106
+ const decrypted = this.aesDecryptBlock(xored);
107
+ const plainBlock = this.xorBuffers(decrypted, prevCipher);
108
+ plainBlock.copy(output, i);
109
+ prevCipher = cipherBlock;
110
+ prevPlain = plainBlock;
111
+ }
112
+ return output;
113
+ }
114
+ }
115
+ exports.IGE = IGE;
@@ -0,0 +1,14 @@
1
+ import bigInt from "big-integer";
2
+ export declare const _serverKeys: Map<string, {
3
+ n: bigInt.BigInteger;
4
+ e: number;
5
+ }>;
6
+ /**
7
+ * Encrypts the given data known the fingerprint to be used
8
+ * in the way Telegram requires us to do so (sha1(data) + data + padding)
9
+
10
+ * @param fingerprint the fingerprint of the RSA key.
11
+ * @param data the data to be encrypted.
12
+ * @returns {Buffer|*|undefined} the cipher text, or undefined if no key matching this fingerprint is found.
13
+ */
14
+ export declare function encrypt(fingerprint: bigInt.BigInteger, data: Buffer): Promise<Buffer<ArrayBufferLike> | undefined>;
package/crypto/RSA.js ADDED
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports._serverKeys = void 0;
18
+ exports.encrypt = encrypt;
19
+ const big_integer_1 = __importDefault(require("big-integer"));
20
+ const Helpers_1 = require("../Helpers");
21
+ const PUBLIC_KEYS = [
22
+ {
23
+ fingerprint: (0, big_integer_1.default)("-3414540481677951611"),
24
+ n: (0, big_integer_1.default)("2937959817066933702298617714945612856538843112005886376816255642404751219133084745514657634448776440866" +
25
+ "1701890505066208632169112269581063774293102577308490531282748465986139880977280302242772832972539403531" +
26
+ "3160108704012876427630091361567343395380424193887227773571344877461690935390938502512438971889287359033" +
27
+ "8945177273024525306296338410881284207988753897636046529094613963869149149606209957083647645485599631919" +
28
+ "2747663615955633778034897140982517446405334423701359108810182097749467210509584293428076654573384828809" +
29
+ "574217079944388301239431309115013843331317877374435868468779972014486325557807783825502498215169806323"),
30
+ e: 65537,
31
+ },
32
+ {
33
+ fingerprint: (0, big_integer_1.default)("-5595554452916591101"),
34
+ n: (0, big_integer_1.default)("2534288944884041556497168959071347320689884775908477905258202659454602246385394058588521595116849196570822" +
35
+ "26493991806038180742006204637761354248846321625124031637930839216416315647409595294193595958529411668489405859523" +
36
+ "37613333022396096584117954892216031229237302943701877588456738335398602461675225081791820393153757504952636234951" +
37
+ "32323782003654358104782690612092797248736680529211579223142368426126233039432475078545094258975175539015664775146" +
38
+ "07193514399690599495696153028090507215003302390050778898553239175099482557220816446894421272976054225797071426466" +
39
+ "60768825302832201908302295573257427896031830742328565032949"),
40
+ e: 65537,
41
+ },
42
+ ];
43
+ exports._serverKeys = new Map();
44
+ PUBLIC_KEYS.forEach((_a) => {
45
+ var { fingerprint } = _a, keyInfo = __rest(_a, ["fingerprint"]);
46
+ exports._serverKeys.set(fingerprint.toString(), keyInfo);
47
+ });
48
+ /**
49
+ * Encrypts the given data known the fingerprint to be used
50
+ * in the way Telegram requires us to do so (sha1(data) + data + padding)
51
+
52
+ * @param fingerprint the fingerprint of the RSA key.
53
+ * @param data the data to be encrypted.
54
+ * @returns {Buffer|*|undefined} the cipher text, or undefined if no key matching this fingerprint is found.
55
+ */
56
+ async function encrypt(fingerprint, data) {
57
+ const key = exports._serverKeys.get(fingerprint.toString());
58
+ if (!key) {
59
+ return undefined;
60
+ }
61
+ // len(sha1.digest) is always 20, so we're left with 255 - 20 - x padding
62
+ const rand = (0, Helpers_1.generateRandomBytes)(235 - data.length);
63
+ const toEncrypt = Buffer.concat([await (0, Helpers_1.sha1)(data), data, rand]);
64
+ // rsa module rsa.encrypt adds 11 bits for padding which we don't want
65
+ // rsa module uses rsa.transform.bytes2int(to_encrypt), easier way:
66
+ const payload = (0, Helpers_1.readBigIntFromBuffer)(toEncrypt, false);
67
+ const encrypted = (0, Helpers_1.modExp)(payload, (0, big_integer_1.default)(key.e), key.n);
68
+ // rsa module uses transform.int2bytes(encrypted, keylength), easier:
69
+ return (0, Helpers_1.readBufferFromBigInt)(encrypted, 256, false);
70
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Uint32Array -> ArrayBuffer (low-endian os)
3
+ */
4
+ export declare function i2abLow(buf: Uint32Array): ArrayBuffer;
5
+ /**
6
+ * Uint32Array -> ArrayBuffer (big-endian os)
7
+ */
8
+ export declare function i2abBig(buf: Uint32Array): ArrayBuffer;
9
+ /**
10
+ * ArrayBuffer -> Uint32Array (low-endian os)
11
+ */
12
+ export declare function ab2iLow(ab: ArrayBuffer | SharedArrayBuffer | Uint8Array): Uint32Array;
13
+ /**
14
+ * ArrayBuffer -> Uint32Array (big-endian os)
15
+ */
16
+ export declare function ab2iBig(ab: ArrayBuffer | SharedArrayBuffer | Uint8Array): Uint32Array;
17
+ export declare const isBigEndian: boolean;
18
+ export declare const i2ab: typeof i2abBig;
19
+ export declare const ab2i: typeof ab2iBig;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ab2i = exports.i2ab = exports.isBigEndian = void 0;
4
+ exports.i2abLow = i2abLow;
5
+ exports.i2abBig = i2abBig;
6
+ exports.ab2iLow = ab2iLow;
7
+ exports.ab2iBig = ab2iBig;
8
+ /**
9
+ * Uint32Array -> ArrayBuffer (low-endian os)
10
+ */
11
+ function i2abLow(buf) {
12
+ const uint8 = new Uint8Array(buf.length * 4);
13
+ let i = 0;
14
+ for (let j = 0; j < buf.length; j++) {
15
+ const int = buf[j];
16
+ uint8[i++] = int >>> 24;
17
+ uint8[i++] = (int >> 16) & 0xff;
18
+ uint8[i++] = (int >> 8) & 0xff;
19
+ uint8[i++] = int & 0xff;
20
+ }
21
+ return uint8.buffer;
22
+ }
23
+ /**
24
+ * Uint32Array -> ArrayBuffer (big-endian os)
25
+ */
26
+ function i2abBig(buf) {
27
+ return buf.buffer;
28
+ }
29
+ /**
30
+ * ArrayBuffer -> Uint32Array (low-endian os)
31
+ */
32
+ function ab2iLow(ab) {
33
+ const uint8 = new Uint8Array(ab);
34
+ const buf = new Uint32Array(uint8.length / 4);
35
+ for (let i = 0; i < uint8.length; i += 4) {
36
+ buf[i / 4] =
37
+ (uint8[i] << 24) ^
38
+ (uint8[i + 1] << 16) ^
39
+ (uint8[i + 2] << 8) ^
40
+ uint8[i + 3];
41
+ }
42
+ return buf;
43
+ }
44
+ /**
45
+ * ArrayBuffer -> Uint32Array (big-endian os)
46
+ */
47
+ function ab2iBig(ab) {
48
+ return new Uint32Array(ab);
49
+ }
50
+ exports.isBigEndian = new Uint8Array(new Uint32Array([0x01020304]))[0] === 0x01;
51
+ exports.i2ab = exports.isBigEndian ? i2abBig : i2abLow;
52
+ exports.ab2i = exports.isBigEndian ? ab2iBig : ab2iLow;
@@ -0,0 +1,19 @@
1
+ export declare class CTR {
2
+ private cipher;
3
+ private decipher;
4
+ constructor(key: Buffer, iv: Buffer, algorithm: string);
5
+ update(plainText: Buffer): any;
6
+ encrypt(plainText: Buffer): any;
7
+ decrypt(cipherText: Buffer): any;
8
+ }
9
+ export declare function createDecipher(algorithm: string, key: Buffer, iv: Buffer): CTR;
10
+ export declare function createCipher(algorithm: string, key: Buffer, iv: Buffer): CTR;
11
+ export declare function randomBytes(count: number): Buffer<ArrayBufferLike>;
12
+ export declare class Hash {
13
+ private readonly hash;
14
+ constructor(algorithm: string);
15
+ update(data: Buffer): void;
16
+ digest(): any;
17
+ }
18
+ export declare function pbkdf2Sync(password: any, salt: any, iterations: any, keylen: any, digest: any): Buffer<ArrayBufferLike>;
19
+ export declare function createHash(algorithm: string): Hash;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Hash = exports.CTR = void 0;
4
+ exports.createDecipher = createDecipher;
5
+ exports.createCipher = createCipher;
6
+ exports.randomBytes = randomBytes;
7
+ exports.pbkdf2Sync = pbkdf2Sync;
8
+ exports.createHash = createHash;
9
+ const node_crypto_1 = require("node:crypto");
10
+ class CTR {
11
+ constructor(key, iv, algorithm) {
12
+ this.cipher = (0, node_crypto_1.createCipheriv)(algorithm, key, iv);
13
+ this.decipher = (0, node_crypto_1.createDecipheriv)(algorithm, key, iv);
14
+ }
15
+ update(plainText) {
16
+ return this.encrypt(plainText);
17
+ }
18
+ encrypt(plainText) {
19
+ return this.cipher.update(plainText);
20
+ }
21
+ decrypt(cipherText) {
22
+ return this.decipher.update(cipherText);
23
+ }
24
+ }
25
+ exports.CTR = CTR;
26
+ function createDecipher(algorithm, key, iv) {
27
+ if (algorithm.includes("ECB")) {
28
+ throw new Error("ECB mode is not supported");
29
+ }
30
+ return new CTR(key, iv, algorithm);
31
+ }
32
+ function createCipher(algorithm, key, iv) {
33
+ if (algorithm.includes("ECB")) {
34
+ throw new Error("ECB mode is not supported");
35
+ }
36
+ return new CTR(key, iv, algorithm);
37
+ }
38
+ function randomBytes(count) {
39
+ return (0, node_crypto_1.randomBytes)(count);
40
+ }
41
+ class Hash {
42
+ constructor(algorithm) {
43
+ this.hash = (0, node_crypto_1.createHash)(algorithm);
44
+ }
45
+ update(data) {
46
+ this.hash.update(data);
47
+ }
48
+ digest() {
49
+ return this.hash.digest();
50
+ }
51
+ }
52
+ exports.Hash = Hash;
53
+ function pbkdf2Sync(password, salt, iterations, keylen, digest) {
54
+ return (0, node_crypto_1.pbkdf2Sync)(password, salt, iterations, keylen, digest);
55
+ }
56
+ function createHash(algorithm) {
57
+ return new Hash(algorithm);
58
+ }
@@ -0,0 +1,6 @@
1
+ export declare function s2i(str: string, pos: number): number;
2
+ /**
3
+ * Helper function for transforming string key to Uint32Array
4
+ */
5
+ export declare function getWords(key: string | Uint8Array | Uint32Array): Uint32Array<ArrayBufferLike>;
6
+ export declare function xor(left: Uint32Array, right: Uint32Array, to?: Uint32Array<ArrayBufferLike>): void;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ /*
3
+ * Imported from https://github.com/spalt08/cryptography/blob/master/packages/aes/src/utils/words.ts
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.s2i = s2i;
7
+ exports.getWords = getWords;
8
+ exports.xor = xor;
9
+ function s2i(str, pos) {
10
+ return ((str.charCodeAt(pos) << 24) ^
11
+ (str.charCodeAt(pos + 1) << 16) ^
12
+ (str.charCodeAt(pos + 2) << 8) ^
13
+ str.charCodeAt(pos + 3));
14
+ }
15
+ /**
16
+ * Helper function for transforming string key to Uint32Array
17
+ */
18
+ function getWords(key) {
19
+ if (key instanceof Uint32Array) {
20
+ return key;
21
+ }
22
+ if (typeof key === "string") {
23
+ if (key.length % 4 !== 0)
24
+ for (let i = key.length % 4; i <= 4; i++)
25
+ key += "\0x00";
26
+ const buf = new Uint32Array(key.length / 4);
27
+ for (let i = 0; i < key.length; i += 4)
28
+ buf[i / 4] = s2i(key, i);
29
+ return buf;
30
+ }
31
+ if (key instanceof Uint8Array) {
32
+ const buf = new Uint32Array(key.length / 4);
33
+ for (let i = 0; i < key.length; i += 4) {
34
+ buf[i / 4] =
35
+ (key[i] << 24) ^
36
+ (key[i + 1] << 16) ^
37
+ (key[i + 2] << 8) ^
38
+ key[i + 3];
39
+ }
40
+ return buf;
41
+ }
42
+ throw new Error("Unable to create 32-bit words");
43
+ }
44
+ function xor(left, right, to = left) {
45
+ for (let i = 0; i < left.length; i++)
46
+ to[i] = left[i] ^ right[i];
47
+ }
package/define.d.ts ADDED
@@ -0,0 +1,77 @@
1
+ import type { Button } from "./tl/custom/button";
2
+ import { Api } from "./tl";
3
+ import type { CustomFile } from "./client/uploads";
4
+ import TypeUser = Api.TypeUser;
5
+ import TypeChat = Api.TypeChat;
6
+ import TypeInputUser = Api.TypeInputUser;
7
+ import TypeInputChannel = Api.TypeInputChannel;
8
+ import bigInt from "big-integer";
9
+ import { WriteStream } from "fs";
10
+
11
+ type ValueOf<T> = T[keyof T];
12
+ type Phone = string;
13
+ type Username = string;
14
+ type PeerID = number;
15
+ type Entity = Api.User | Api.Chat | Api.Channel | TypeUser | TypeChat;
16
+ type FullEntity =
17
+ | Api.UserFull
18
+ | Api.messages.ChatFull
19
+ | Api.ChatFull
20
+ | Api.ChannelFull;
21
+ type PeerLike = Api.TypePeer | Api.TypeInputPeer | Entity | FullEntity;
22
+ type EntityLike =
23
+ | bigInt.BigInteger
24
+ | Phone
25
+ | Username
26
+ | PeerID
27
+ | Api.TypePeer
28
+ | Api.TypeInputPeer
29
+ | Entity
30
+ | FullEntity
31
+ | TypeUser
32
+ | TypeChat
33
+ | TypeInputChannel
34
+ | TypeInputUser;
35
+
36
+ type EntitiesLike = EntityLike[];
37
+ type MessageIDLike =
38
+ | number
39
+ | Api.Message
40
+ | Api.MessageService
41
+ | Api.TypeInputMessage;
42
+ type MessageLike = string | Api.Message;
43
+
44
+ type LocalPath = string;
45
+ type ExternalUrl = string;
46
+ type BotFileID = string;
47
+
48
+ type FileLike =
49
+ | LocalPath
50
+ | ExternalUrl
51
+ | BotFileID
52
+ | Buffer
53
+ | Api.TypeMessageMedia
54
+ | Api.TypeInputMedia
55
+ | Api.TypeInputFile
56
+ | Api.TypeInputFileLocation
57
+ | File
58
+ | Api.TypePhoto
59
+ | Api.TypeDocument
60
+ | CustomFile;
61
+ type OutFile =
62
+ | string
63
+ | Buffer
64
+ | WriteStream
65
+ | { write: Function; close?: Function };
66
+ type ProgressCallback = (
67
+ downloaded: bigInt.BigInteger,
68
+ total: bigInt.BigInteger
69
+ ) => void;
70
+ type ButtonLike = Api.TypeKeyboardButton | Button;
71
+
72
+ type MarkupLike =
73
+ | Api.TypeReplyMarkup
74
+ | ButtonLike
75
+ | ButtonLike[]
76
+ | ButtonLike[][];
77
+ type DateLike = number;
@@ -0,0 +1,7 @@
1
+ import bigInt from "big-integer";
2
+ export declare class EntityCache {
3
+ private cacheMap;
4
+ constructor();
5
+ add(entities: any): void;
6
+ get(item: bigInt.BigInteger | string | undefined): any;
7
+ }
package/entityCache.js ADDED
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ // Which updates have the following fields?
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.EntityCache = void 0;
8
+ const Utils_1 = require("./Utils");
9
+ const Helpers_1 = require("./Helpers");
10
+ const tl_1 = require("./tl");
11
+ const big_integer_1 = __importDefault(require("big-integer"));
12
+ class EntityCache {
13
+ constructor() {
14
+ this.cacheMap = new Map();
15
+ }
16
+ add(entities) {
17
+ const temp = [];
18
+ if (!(0, Helpers_1.isArrayLike)(entities)) {
19
+ if (entities != undefined) {
20
+ if (typeof entities == "object") {
21
+ if ("chats" in entities) {
22
+ temp.push(...entities.chats);
23
+ }
24
+ if ("users" in entities) {
25
+ temp.push(...entities.users);
26
+ }
27
+ if ("user" in entities) {
28
+ temp.push(entities.user);
29
+ }
30
+ }
31
+ }
32
+ if (temp.length) {
33
+ entities = temp;
34
+ }
35
+ else {
36
+ return;
37
+ }
38
+ }
39
+ for (const entity of entities) {
40
+ try {
41
+ const pid = (0, Utils_1.getPeerId)(entity);
42
+ if (!this.cacheMap.has(pid.toString())) {
43
+ this.cacheMap.set(pid.toString(), (0, Utils_1.getInputPeer)(entity));
44
+ }
45
+ }
46
+ catch (e) { }
47
+ }
48
+ }
49
+ get(item) {
50
+ if (item == undefined) {
51
+ throw new Error("No cached entity for the given key");
52
+ }
53
+ item = (0, Helpers_1.returnBigInt)(item);
54
+ if (item.lesser(big_integer_1.default.zero)) {
55
+ let res;
56
+ try {
57
+ res = this.cacheMap.get((0, Utils_1.getPeerId)(item).toString());
58
+ if (res) {
59
+ return res;
60
+ }
61
+ }
62
+ catch (e) {
63
+ throw new Error("Invalid key will not have entity");
64
+ }
65
+ }
66
+ for (const cls of [tl_1.Api.PeerUser, tl_1.Api.PeerChat, tl_1.Api.PeerChannel]) {
67
+ const result = this.cacheMap.get((0, Utils_1.getPeerId)(new cls({
68
+ userId: item,
69
+ chatId: item,
70
+ channelId: item,
71
+ })).toString());
72
+ if (result) {
73
+ return result;
74
+ }
75
+ }
76
+ throw new Error("No cached entity for the given key");
77
+ }
78
+ }
79
+ exports.EntityCache = EntityCache;