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
package/Password.js ADDED
@@ -0,0 +1,270 @@
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.computeCheck = computeCheck;
7
+ exports.computeDigest = computeDigest;
8
+ const tl_1 = require("./tl");
9
+ const Helpers_1 = require("./Helpers");
10
+ const big_integer_1 = __importDefault(require("big-integer"));
11
+ const CryptoFile_1 = __importDefault(require("./CryptoFile"));
12
+ const SIZE_FOR_HASH = 256;
13
+ /**
14
+ *
15
+ *
16
+ * @param prime{BigInteger}
17
+ * @param g{BigInteger}
18
+ */
19
+ /*
20
+ We don't support changing passwords yet
21
+ function checkPrimeAndGoodCheck(prime, g) {
22
+ console.error('Unsupported function `checkPrimeAndGoodCheck` call. Arguments:', prime, g)
23
+
24
+ const goodPrimeBitsCount = 2048
25
+ if (prime < 0 || prime.bitLength() !== goodPrimeBitsCount) {
26
+ throw new Error(`bad prime count ${prime.bitLength()},expected ${goodPrimeBitsCount}`)
27
+ }
28
+ // TODO this is kinda slow
29
+ if (Factorizator.factorize(prime)[0] !== 1) {
30
+ throw new Error('give "prime" is not prime')
31
+ }
32
+ if (g.eq(bigInt(2))) {
33
+ if ((prime.remainder(bigInt(8))).neq(bigInt(7))) {
34
+ throw new Error(`bad g ${g}, mod8 ${prime % 8}`)
35
+ }
36
+ } else if (g.eq(bigInt(3))) {
37
+ if ((prime.remainder(bigInt(3))).neq(bigInt(2))) {
38
+ throw new Error(`bad g ${g}, mod3 ${prime % 3}`)
39
+ }
40
+ // eslint-disable-next-line no-empty
41
+ } else if (g.eq(bigInt(4))) {
42
+
43
+ } else if (g.eq(bigInt(5))) {
44
+ if (!([ bigInt(1), bigInt(4) ].includes(prime.remainder(bigInt(5))))) {
45
+ throw new Error(`bad g ${g}, mod8 ${prime % 5}`)
46
+ }
47
+ } else if (g.eq(bigInt(6))) {
48
+ if (!([ bigInt(19), bigInt(23) ].includes(prime.remainder(bigInt(24))))) {
49
+ throw new Error(`bad g ${g}, mod8 ${prime % 24}`)
50
+ }
51
+ } else if (g.eq(bigInt(7))) {
52
+ if (!([ bigInt(3), bigInt(5), bigInt(6) ].includes(prime.remainder(bigInt(7))))) {
53
+ throw new Error(`bad g ${g}, mod8 ${prime % 7}`)
54
+ }
55
+ } else {
56
+ throw new Error(`bad g ${g}`)
57
+ }
58
+ const primeSub1Div2 = (prime.subtract(bigInt(1))).divide(bigInt(2))
59
+ if (Factorizator.factorize(primeSub1Div2)[0] !== 1) {
60
+ throw new Error('(prime - 1) // 2 is not prime')
61
+ }
62
+ }
63
+ */
64
+ /**
65
+ *
66
+ * @param primeBytes{Buffer}
67
+ * @param g{number}
68
+ */
69
+ function checkPrimeAndGood(primeBytes, g) {
70
+ const goodPrime = Buffer.from([
71
+ 0xc7, 0x1c, 0xae, 0xb9, 0xc6, 0xb1, 0xc9, 0x04, 0x8e, 0x6c, 0x52, 0x2f,
72
+ 0x70, 0xf1, 0x3f, 0x73, 0x98, 0x0d, 0x40, 0x23, 0x8e, 0x3e, 0x21, 0xc1,
73
+ 0x49, 0x34, 0xd0, 0x37, 0x56, 0x3d, 0x93, 0x0f, 0x48, 0x19, 0x8a, 0x0a,
74
+ 0xa7, 0xc1, 0x40, 0x58, 0x22, 0x94, 0x93, 0xd2, 0x25, 0x30, 0xf4, 0xdb,
75
+ 0xfa, 0x33, 0x6f, 0x6e, 0x0a, 0xc9, 0x25, 0x13, 0x95, 0x43, 0xae, 0xd4,
76
+ 0x4c, 0xce, 0x7c, 0x37, 0x20, 0xfd, 0x51, 0xf6, 0x94, 0x58, 0x70, 0x5a,
77
+ 0xc6, 0x8c, 0xd4, 0xfe, 0x6b, 0x6b, 0x13, 0xab, 0xdc, 0x97, 0x46, 0x51,
78
+ 0x29, 0x69, 0x32, 0x84, 0x54, 0xf1, 0x8f, 0xaf, 0x8c, 0x59, 0x5f, 0x64,
79
+ 0x24, 0x77, 0xfe, 0x96, 0xbb, 0x2a, 0x94, 0x1d, 0x5b, 0xcd, 0x1d, 0x4a,
80
+ 0xc8, 0xcc, 0x49, 0x88, 0x07, 0x08, 0xfa, 0x9b, 0x37, 0x8e, 0x3c, 0x4f,
81
+ 0x3a, 0x90, 0x60, 0xbe, 0xe6, 0x7c, 0xf9, 0xa4, 0xa4, 0xa6, 0x95, 0x81,
82
+ 0x10, 0x51, 0x90, 0x7e, 0x16, 0x27, 0x53, 0xb5, 0x6b, 0x0f, 0x6b, 0x41,
83
+ 0x0d, 0xba, 0x74, 0xd8, 0xa8, 0x4b, 0x2a, 0x14, 0xb3, 0x14, 0x4e, 0x0e,
84
+ 0xf1, 0x28, 0x47, 0x54, 0xfd, 0x17, 0xed, 0x95, 0x0d, 0x59, 0x65, 0xb4,
85
+ 0xb9, 0xdd, 0x46, 0x58, 0x2d, 0xb1, 0x17, 0x8d, 0x16, 0x9c, 0x6b, 0xc4,
86
+ 0x65, 0xb0, 0xd6, 0xff, 0x9c, 0xa3, 0x92, 0x8f, 0xef, 0x5b, 0x9a, 0xe4,
87
+ 0xe4, 0x18, 0xfc, 0x15, 0xe8, 0x3e, 0xbe, 0xa0, 0xf8, 0x7f, 0xa9, 0xff,
88
+ 0x5e, 0xed, 0x70, 0x05, 0x0d, 0xed, 0x28, 0x49, 0xf4, 0x7b, 0xf9, 0x59,
89
+ 0xd9, 0x56, 0x85, 0x0c, 0xe9, 0x29, 0x85, 0x1f, 0x0d, 0x81, 0x15, 0xf6,
90
+ 0x35, 0xb1, 0x05, 0xee, 0x2e, 0x4e, 0x15, 0xd0, 0x4b, 0x24, 0x54, 0xbf,
91
+ 0x6f, 0x4f, 0xad, 0xf0, 0x34, 0xb1, 0x04, 0x03, 0x11, 0x9c, 0xd8, 0xe3,
92
+ 0xb9, 0x2f, 0xcc, 0x5b,
93
+ ]);
94
+ if (goodPrime.equals(primeBytes)) {
95
+ if ([3, 4, 5, 7].includes(g)) {
96
+ return; // It's good
97
+ }
98
+ }
99
+ throw new Error("Changing passwords unsupported");
100
+ //checkPrimeAndGoodCheck(readBigIntFromBuffer(primeBytes, false), g)
101
+ }
102
+ /**
103
+ *
104
+ * @param number{BigInteger}
105
+ * @param p{BigInteger}
106
+ * @returns {boolean}
107
+ */
108
+ function isGoodLarge(number, p) {
109
+ return number.greater((0, big_integer_1.default)(0)) && p.subtract(number).greater((0, big_integer_1.default)(0));
110
+ }
111
+ /**
112
+ *
113
+ * @param number {Buffer}
114
+ * @returns {Buffer}
115
+ */
116
+ function numBytesForHash(number) {
117
+ return Buffer.concat([Buffer.alloc(SIZE_FOR_HASH - number.length), number]);
118
+ }
119
+ /**
120
+ *
121
+ * @param g {bigInt}
122
+ * @returns {Buffer}
123
+ */
124
+ function bigNumForHash(g) {
125
+ return (0, Helpers_1.readBufferFromBigInt)(g, SIZE_FOR_HASH, false);
126
+ }
127
+ /**
128
+ *
129
+ * @param modexp {BigInteger}
130
+ * @param prime {BigInteger}
131
+ * @returns {Boolean}
132
+ */
133
+ function isGoodModExpFirst(modexp, prime) {
134
+ const diff = prime.subtract(modexp);
135
+ const minDiffBitsCount = 2048 - 64;
136
+ const maxModExpSize = 256;
137
+ return !(diff.lesser((0, big_integer_1.default)(0)) ||
138
+ diff.bitLength().toJSNumber() < minDiffBitsCount ||
139
+ modexp.bitLength().toJSNumber() < minDiffBitsCount ||
140
+ Math.floor((modexp.bitLength().toJSNumber() + 7) / 8) > maxModExpSize);
141
+ }
142
+ function xor(a, b) {
143
+ const length = Math.min(a.length, b.length);
144
+ for (let i = 0; i < length; i++) {
145
+ a[i] = a[i] ^ b[i];
146
+ }
147
+ return a;
148
+ }
149
+ /**
150
+ *
151
+ * @param password{Buffer}
152
+ * @param salt{Buffer}
153
+ * @param iterations{number}
154
+ * @returns {*}
155
+ */
156
+ function pbkdf2sha512(password, salt, iterations) {
157
+ return CryptoFile_1.default.pbkdf2Sync(password, salt, iterations, 64, "sha512");
158
+ }
159
+ /**
160
+ *
161
+ * @param algo {constructors.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow}
162
+ * @param password
163
+ * @returns {Buffer|*}
164
+ */
165
+ async function computeHash(algo, password) {
166
+ const hash1 = await (0, Helpers_1.sha256)(Buffer.concat([algo.salt1, Buffer.from(password, "utf-8"), algo.salt1]));
167
+ const hash2 = await (0, Helpers_1.sha256)(Buffer.concat([algo.salt2, hash1, algo.salt2]));
168
+ const hash3 = await pbkdf2sha512(hash2, algo.salt1, 100000);
169
+ return (0, Helpers_1.sha256)(Buffer.concat([algo.salt2, hash3, algo.salt2]));
170
+ }
171
+ /**
172
+ *
173
+ * @param algo {constructors.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow}
174
+ * @param password
175
+ */
176
+ async function computeDigest(algo, password) {
177
+ try {
178
+ checkPrimeAndGood(algo.p, algo.g);
179
+ }
180
+ catch (e) {
181
+ throw new Error("bad p/g in password");
182
+ }
183
+ const value = (0, Helpers_1.modExp)((0, big_integer_1.default)(algo.g), (0, Helpers_1.readBigIntFromBuffer)(await computeHash(algo, password), false), (0, Helpers_1.readBigIntFromBuffer)(algo.p, false));
184
+ return bigNumForHash(value);
185
+ }
186
+ /**
187
+ *
188
+ * @param request {constructors.account.Password}
189
+ * @param password {string}
190
+ */
191
+ async function computeCheck(request, password) {
192
+ const algo = request.currentAlgo;
193
+ if (!(algo instanceof
194
+ tl_1.Api.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow)) {
195
+ throw new Error(`Unsupported password algorithm ${algo === null || algo === void 0 ? void 0 : algo.className}`);
196
+ }
197
+ const srp_B = request.srp_B;
198
+ const srpId = request.srpId;
199
+ if (!srp_B || !srpId) {
200
+ throw new Error(`Undefined srp_b ${request}`);
201
+ }
202
+ const pwHash = await computeHash(algo, password);
203
+ const p = (0, Helpers_1.readBigIntFromBuffer)(algo.p, false);
204
+ const g = algo.g;
205
+ const B = (0, Helpers_1.readBigIntFromBuffer)(srp_B, false);
206
+ try {
207
+ checkPrimeAndGood(algo.p, g);
208
+ }
209
+ catch (e) {
210
+ throw new Error("bad /g in password");
211
+ }
212
+ if (!isGoodLarge(B, p)) {
213
+ throw new Error("bad b in check");
214
+ }
215
+ const x = (0, Helpers_1.readBigIntFromBuffer)(pwHash, false);
216
+ const pForHash = numBytesForHash(algo.p);
217
+ const gForHash = bigNumForHash((0, big_integer_1.default)(g));
218
+ const bForHash = numBytesForHash(srp_B);
219
+ const gX = (0, Helpers_1.modExp)((0, big_integer_1.default)(g), x, p);
220
+ const k = (0, Helpers_1.readBigIntFromBuffer)(await (0, Helpers_1.sha256)(Buffer.concat([pForHash, gForHash])), false);
221
+ const kgX = (0, Helpers_1.bigIntMod)(k.multiply(gX), p);
222
+ const generateAndCheckRandom = async () => {
223
+ const randomSize = 256;
224
+ // eslint-disable-next-line no-constant-condition
225
+ while (true) {
226
+ const random = (0, Helpers_1.generateRandomBytes)(randomSize);
227
+ const a = (0, Helpers_1.readBigIntFromBuffer)(random, false);
228
+ const A = (0, Helpers_1.modExp)((0, big_integer_1.default)(g), a, p);
229
+ if (isGoodModExpFirst(A, p)) {
230
+ const aForHash = bigNumForHash(A);
231
+ const u = (0, Helpers_1.readBigIntFromBuffer)(await (0, Helpers_1.sha256)(Buffer.concat([aForHash, bForHash])), false);
232
+ if (u.greater((0, big_integer_1.default)(0))) {
233
+ return {
234
+ a: a,
235
+ aForHash: aForHash,
236
+ u: u,
237
+ };
238
+ }
239
+ }
240
+ }
241
+ };
242
+ const { a, aForHash, u } = await generateAndCheckRandom();
243
+ const gB = (0, Helpers_1.bigIntMod)(B.subtract(kgX), p);
244
+ if (!isGoodModExpFirst(gB, p)) {
245
+ throw new Error("bad gB");
246
+ }
247
+ const ux = u.multiply(x);
248
+ const aUx = a.add(ux);
249
+ const S = (0, Helpers_1.modExp)(gB, aUx, p);
250
+ const [K, pSha, gSha, salt1Sha, salt2Sha] = await Promise.all([
251
+ (0, Helpers_1.sha256)(bigNumForHash(S)),
252
+ (0, Helpers_1.sha256)(pForHash),
253
+ (0, Helpers_1.sha256)(gForHash),
254
+ (0, Helpers_1.sha256)(algo.salt1),
255
+ (0, Helpers_1.sha256)(algo.salt2),
256
+ ]);
257
+ const M1 = await (0, Helpers_1.sha256)(Buffer.concat([
258
+ xor(pSha, gSha),
259
+ salt1Sha,
260
+ salt2Sha,
261
+ aForHash,
262
+ bForHash,
263
+ K,
264
+ ]));
265
+ return new tl_1.Api.InputCheckPasswordSRP({
266
+ srpId: srpId,
267
+ A: Buffer.from(aForHash),
268
+ M1: M1,
269
+ });
270
+ }
package/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # teleproto
2
+
3
+ A modern Telegram client library written in TypeScript for Node.js, forked from [GramJS](https://github.com/gram-js/gramjs) with performance and size improvements.
4
+
5
+ ## Quick Start
6
+
7
+ Here's how to get started with teleproto:
8
+
9
+ ### Installation
10
+
11
+ ```bash
12
+ $ npm i teleproto
13
+ ```
14
+
15
+ ### Authentication Setup
16
+
17
+ 1. Login to your [Telegram account](https://my.telegram.org/)
18
+ 2. Click "API development tools" and create an application
19
+ 3. Save your API ID and hash (never share these with anyone)
20
+
21
+ ### Basic Usage
22
+
23
+ ```javascript
24
+ import { TelegramClient } from "teleproto";
25
+ import { StringSession } from "teleproto/sessions";
26
+ import readline from "readline";
27
+
28
+ const apiId = 123456; // Replace with your API ID
29
+ const apiHash = "123456abcdefg"; // Replace with your API Hash
30
+ const stringSession = new StringSession(""); // Save the string session for later use
31
+
32
+ const rl = readline.createInterface({
33
+ input: process.stdin,
34
+ output: process.stdout,
35
+ });
36
+
37
+ async function main() {
38
+ console.log("Starting teleproto client...");
39
+ const client = new TelegramClient(stringSession, apiId, apiHash, {
40
+ connectionRetries: 5,
41
+ });
42
+
43
+ await client.start({
44
+ phoneNumber: async () =>
45
+ await new Promise(resolve => rl.question("Phone number: ", resolve)),
46
+ password: async () =>
47
+ await new Promise(resolve => rl.question("Password: ", resolve)),
48
+ phoneCode: async () =>
49
+ await new Promise(resolve => rl.question("Verification code: ", resolve)),
50
+ onError: (err) => console.error(err),
51
+ });
52
+
53
+ console.log("Connected successfully!");
54
+ console.log("Session string:", client.session.save()); // Save this to avoid login next time
55
+
56
+ // Send a message to yourself
57
+ await client.sendMessage("me", { message: "Hello from teleproto!" });
58
+
59
+ // Disconnect when done
60
+ await client.disconnect();
61
+ rl.close();
62
+ }
63
+
64
+ main();
65
+ ```
66
+
67
+ You can also use `StoreSession` to save auth data to a folder instead of a string:
68
+
69
+ ```javascript
70
+ import { StoreSession } from "teleproto/sessions";
71
+ const storeSession = new StoreSession("session_folder");
72
+ const client = new TelegramClient(storeSession, apiId, apiHash, {});
73
+ ```
74
+
75
+ ## API Usage
76
+
77
+ ### Calling Raw API Methods
78
+
79
+ ```javascript
80
+ await client.invoke(new Api.RequestClass({ param1: "value1" }));
81
+ ```
82
+
83
+ ### Event Handling
84
+
85
+ ```javascript
86
+ import { NewMessage } from "teleproto/events";
87
+
88
+ client.addEventHandler(async (event) => {
89
+ console.log("New message received:", event.message.text);
90
+
91
+ if (event.message.text === "Hello") {
92
+ await event.message.reply("Hi there!");
93
+ }
94
+ }, new NewMessage({}));
95
+ ```
96
+
97
+ # Ask a question
98
+ If you have any questions or need help, feel free to join our [Telegram group](https://t.me/TeleprotoChat) or open an issue on GitHub
package/Utils.d.ts ADDED
@@ -0,0 +1,227 @@
1
+ import bigInt from "big-integer";
2
+ import type { ParseInterface } from "./client/messageParse";
3
+ import { CustomFile } from "./client/uploads";
4
+ import type { Entity, EntityLike, MessageIDLike } from "./define";
5
+ import { EntityCache } from "./entityCache";
6
+ import { Api } from "./tl";
7
+ export declare function getFileInfo(fileLocation: Api.Message | Api.MessageMediaDocument | Api.MessageMediaPhoto | Api.TypeInputFileLocation): {
8
+ dcId?: number;
9
+ location: Api.TypeInputFileLocation;
10
+ size?: bigInt.BigInteger;
11
+ };
12
+ /**
13
+ * Turns the given iterable into chunks of the specified size,
14
+ * which is 100 by default since that's what Telegram uses the most.
15
+ */
16
+ export declare function chunks<T>(arr: T[], size?: number): Generator<T[]>;
17
+ import TypeInputFile = Api.TypeInputFile;
18
+ /**
19
+ Gets the input peer for the given "entity" (user, chat or channel).
20
+
21
+ A ``TypeError`` is raised if the given entity isn't a supported type
22
+ or if ``check_hash is True`` but the entity's ``accessHash is None``
23
+ *or* the entity contains ``min`` information. In this case, the hash
24
+ cannot be used for general purposes, and thus is not returned to avoid
25
+ any issues which can derive from invalid access hashes.
26
+
27
+ Note that ``checkHash`` **is ignored** if an input peer is already
28
+ passed since in that case we assume the user knows what they're doing.
29
+ This is key to getting entities by explicitly passing ``hash = 0``.
30
+
31
+ * @param entity
32
+ * @param allowSelf
33
+ * @param checkHash
34
+ */
35
+ export declare function getInputPeer(entity: any, allowSelf?: boolean, checkHash?: boolean): Api.TypeInputPeer;
36
+ export declare function _photoSizeByteCount(size: Api.TypePhotoSize): number | undefined;
37
+ export declare function _getEntityPair(entityId: string, entities: Map<string, Entity>, cache: EntityCache, getInputPeerFunction?: any): [Entity?, Api.TypeInputPeer?];
38
+ export declare function getInnerText(text: string, entities: Api.TypeMessageEntity[]): string[];
39
+ /**
40
+ Similar to :meth:`get_input_peer`, but for :tl:`InputChannel`'s alone.
41
+
42
+ .. important::
43
+
44
+ This method does not validate for invalid general-purpose access
45
+ hashes, unlike `get_input_peer`. Consider using instead:
46
+ ``get_input_channel(get_input_peer(channel))``.
47
+
48
+ * @param entity
49
+ * @returns {InputChannel|*}
50
+ */
51
+ export declare function getInputChannel(entity: EntityLike): Api.InputChannelEmpty | Api.InputChannel | Api.InputChannelFromMessage;
52
+ /**
53
+ Similar to :meth:`getInputPeer`, but for :tl:`InputUser`'s alone.
54
+
55
+ .. important::
56
+
57
+ This method does not validate for invalid general-purpose access
58
+ hashes, unlike `get_input_peer`. Consider using instead:
59
+ ``get_input_channel(get_input_peer(channel))``.
60
+
61
+ * @param entity
62
+ */
63
+ export declare function getInputUser(entity: EntityLike): Api.TypeInputUser;
64
+ /**
65
+ Similar to :meth:`get_input_peer`, but for dialogs
66
+ * @param dialog
67
+ */
68
+ /**
69
+ * Similar to :meth:`get_input_peer`, but for input messages.
70
+ */
71
+ export declare function getInputMessage(message: any): Api.InputMessageID;
72
+ /**
73
+ * Similar to :meth:`get_input_peer`, but for input messages.
74
+ */
75
+ export declare function getInputChatPhoto(photo: any): Api.TypeInputChatPhoto;
76
+ /**
77
+ * Adds the JPG header and footer to a stripped image.
78
+ * Ported from https://github.com/telegramdesktop/tdesktop/blob/bec39d89e19670eb436dc794a8f20b657cb87c71/Telegram/SourceFiles/ui/image/image.cpp#L225
79
+
80
+ * @param stripped{Buffer}
81
+ * @returns {Buffer}
82
+ */
83
+ export declare function strippedPhotoToJpg(stripped: Buffer): Buffer<ArrayBufferLike>;
84
+ /**
85
+ * Similar to :meth:`get_input_peer`, but for photos
86
+ */
87
+ export declare function getInputPhoto(photo: any): Api.TypeInputPhoto;
88
+ /**
89
+ * Similar to :meth:`get_input_peer`, but for documents
90
+ */
91
+ export declare function getInputDocument(document: any): Api.InputDocument | Api.InputDocumentEmpty;
92
+ interface GetAttributesParams {
93
+ attributes?: any;
94
+ mimeType?: string;
95
+ forceDocument?: boolean;
96
+ voiceNote?: boolean;
97
+ videoNote?: boolean;
98
+ supportsStreaming?: boolean;
99
+ thumb?: any;
100
+ }
101
+ /**
102
+ * Returns `True` if the file has an audio mime type.
103
+ */
104
+ export declare function isAudio(file: any): boolean;
105
+ /**
106
+ * Returns `True` if the file has an image mime type.
107
+ */
108
+ export declare function isImage(file: any): boolean;
109
+ export declare function getExtension(media: any): string;
110
+ /**
111
+ Get a list of attributes for the given file and
112
+ the mime type as a tuple ([attribute], mime_type).
113
+ */
114
+ export declare function getAttributes(file: File | CustomFile | TypeInputFile | string, { attributes, mimeType, forceDocument, voiceNote, videoNote, supportsStreaming, thumb, }: GetAttributesParams): {
115
+ attrs: Api.TypeDocumentAttribute[];
116
+ mimeType: string;
117
+ };
118
+ /**
119
+ * Similar to :meth:`get_input_peer`, but for geo points
120
+ */
121
+ export declare function getInputGeo(geo: any): Api.TypeInputGeoPoint;
122
+ export interface GetInputMediaInterface {
123
+ isPhoto?: boolean;
124
+ attributes?: Api.TypeDocumentAttribute[];
125
+ forceDocument?: boolean;
126
+ voiceNote?: boolean;
127
+ videoNote?: boolean;
128
+ supportsStreaming?: boolean;
129
+ }
130
+ /**
131
+ *
132
+ Similar to :meth:`get_input_peer`, but for media.
133
+
134
+ If the media is :tl:`InputFile` and ``is_photo`` is known to be `True`,
135
+ it will be treated as an :tl:`InputMediaUploadedPhoto`. Else, the rest
136
+ of parameters will indicate how to treat it.
137
+ * @param media
138
+ * @param isPhoto - whether it's a photo or not
139
+ * @param attributes
140
+ * @param forceDocument
141
+ * @param voiceNote
142
+ * @param videoNote
143
+ * @param supportsStreaming
144
+ */
145
+ export declare function getInputMedia(media: any, { isPhoto, attributes, forceDocument, voiceNote, videoNote, supportsStreaming, }?: GetInputMediaInterface): Api.TypeInputMedia;
146
+ /**
147
+ * Gets the appropriated part size when uploading or downloading files,
148
+ * given an initial file size.
149
+ * @param fileSize
150
+ * @returns {Number}
151
+ */
152
+ export declare function getAppropriatedPartSize(fileSize: bigInt.BigInteger): 256 | 512 | 128;
153
+ export declare function getPeer(peer: EntityLike | any): any;
154
+ export declare function sanitizeParseMode(mode: string | ParseInterface): ParseInterface;
155
+ /**
156
+ Convert the given peer into its marked ID by default.
157
+
158
+ This "mark" comes from the "bot api" format, and with it the peer type
159
+ can be identified back. User ID is left unmodified, chat ID is negated,
160
+ and channel ID is prefixed with -100:
161
+
162
+ * ``userId``
163
+ * ``-chatId``
164
+ * ``-100channel_id``
165
+
166
+ The original ID and the peer type class can be returned with
167
+ a call to :meth:`resolve_id(marked_id)`.
168
+ * @param peer
169
+ * @param addMark
170
+ */
171
+ export declare function getPeerId(peer: EntityLike, addMark?: boolean): string;
172
+ /**
173
+ * Given a marked ID, returns the original ID and its :tl:`Peer` type.
174
+ * @param markedId
175
+ */
176
+ export declare function resolveId(markedId: bigInt.BigInteger): [
177
+ bigInt.BigInteger,
178
+ typeof Api.PeerUser | typeof Api.PeerChannel | typeof Api.PeerChat
179
+ ];
180
+ /**
181
+ * returns an entity pair
182
+ * @param entityId
183
+ * @param entities
184
+ * @param cache
185
+ * @param getInputPeer
186
+ * @returns {{inputEntity: *, entity: *}}
187
+ * @private
188
+ */
189
+ export declare function getMessageId(message: number | Api.TypeMessage | MessageIDLike | undefined): number | undefined;
190
+ /**
191
+ * Parses the given phone, or returns `undefined` if it's invalid.
192
+ * @param phone
193
+ */
194
+ export declare function parsePhone(phone: string): string | undefined;
195
+ /**
196
+ * Parses a string ID into a big int
197
+ * @param id
198
+ */
199
+ export declare function parseID(id: string): bigInt.BigInteger | undefined;
200
+ export declare function resolveInviteLink(link: string): [number, number, number];
201
+ /**
202
+ Parses the given username or channel access hash, given
203
+ a string, username or URL. Returns a tuple consisting of
204
+ both the stripped, lowercase username and whether it is
205
+ a joinchat/ hash (in which case is not lowercase'd).
206
+
207
+ Returns ``(undefined, false)`` if the ``username`` or link is not valid.
208
+
209
+ * @param username {string}
210
+ */
211
+ export declare function parseUsername(username: string): {
212
+ username?: string;
213
+ isInvite: boolean;
214
+ };
215
+ export declare function rtrim(s: string, mask: string): string;
216
+ /**
217
+ * Gets the display name for the given :tl:`User`,
218
+ :tl:`Chat` or :tl:`Channel`. Returns an empty string otherwise
219
+ * @param entity
220
+ */
221
+ export declare function getDisplayName(entity: EntityLike): string;
222
+ export {};
223
+ /**
224
+ * check if a given item is an array like or not
225
+ * @param item
226
+ * @returns {boolean}
227
+ */