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,490 @@
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.invoke = invoke;
7
+ exports.getMe = getMe;
8
+ exports.isBot = isBot;
9
+ exports.isUserAuthorized = isUserAuthorized;
10
+ exports.getEntity = getEntity;
11
+ exports.getInputEntity = getInputEntity;
12
+ exports._getEntityFromString = _getEntityFromString;
13
+ exports.getPeerId = getPeerId;
14
+ exports._getPeer = _getPeer;
15
+ exports._getInputDialog = _getInputDialog;
16
+ exports._getInputNotify = _getInputNotify;
17
+ exports._selfId = _selfId;
18
+ const tl_1 = require("../tl");
19
+ const Utils_1 = require("../Utils");
20
+ const Helpers_1 = require("../Helpers");
21
+ const __1 = require("..");
22
+ const big_integer_1 = __importDefault(require("big-integer"));
23
+ const Logger_1 = require("../extensions/Logger");
24
+ const RequestState_1 = require("../network/RequestState");
25
+ // UserMethods {
26
+ // region Invoking Telegram request
27
+ /** @hidden */
28
+ async function invoke(client, request, dcId, otherSender) {
29
+ if (request.classType !== "request") {
30
+ throw new Error("You can only invoke MTProtoRequests");
31
+ }
32
+ let sender = client._sender;
33
+ if (dcId) {
34
+ sender = await client.getSender(dcId);
35
+ }
36
+ if (otherSender != undefined) {
37
+ sender = otherSender;
38
+ }
39
+ if (sender == undefined) {
40
+ throw new Error("Cannot send requests while disconnected. You need to call .connect()");
41
+ }
42
+ if (sender.userDisconnected) {
43
+ throw new Error("Cannot send requests while disconnected. Please reconnect.");
44
+ }
45
+ await client._connectedDeferred.promise;
46
+ await request.resolve(client, __1.utils);
47
+ client._lastRequest = new Date().getTime();
48
+ const state = new RequestState_1.RequestState(request);
49
+ let attempt = 0;
50
+ for (attempt = 0; attempt < client._requestRetries; attempt++) {
51
+ sender.addStateToQueue(state);
52
+ try {
53
+ const result = await state.promise;
54
+ state.finished.resolve();
55
+ client.session.processEntities(result);
56
+ client._entityCache.add(result);
57
+ return result;
58
+ }
59
+ catch (e) {
60
+ if (e instanceof __1.errors.ServerError ||
61
+ e.errorMessage === "RPC_CALL_FAIL" ||
62
+ e.errorMessage === "RPC_MCGET_FAIL") {
63
+ client._log.warn(`Telegram is having internal issues ${e.constructor.name}`);
64
+ await (0, Helpers_1.sleep)(2000);
65
+ }
66
+ else if (e instanceof __1.errors.FloodWaitError ||
67
+ e instanceof __1.errors.FloodTestPhoneWaitError) {
68
+ if (e.seconds <= client.floodSleepThreshold) {
69
+ client._log.info(`Sleeping for ${e.seconds}s on flood wait (Caused by ${request.className})`);
70
+ await (0, Helpers_1.sleep)(e.seconds * 1000);
71
+ }
72
+ else {
73
+ state.finished.resolve();
74
+ throw e;
75
+ }
76
+ }
77
+ else if (e instanceof __1.errors.PhoneMigrateError ||
78
+ e instanceof __1.errors.NetworkMigrateError ||
79
+ e instanceof __1.errors.UserMigrateError) {
80
+ client._log.info(`Phone migrated to ${e.newDc}`);
81
+ const shouldRaise = e instanceof __1.errors.PhoneMigrateError ||
82
+ e instanceof __1.errors.NetworkMigrateError;
83
+ if (shouldRaise && (await client.isUserAuthorized())) {
84
+ state.finished.resolve();
85
+ throw e;
86
+ }
87
+ await client._switchDC(e.newDc);
88
+ sender =
89
+ dcId === undefined
90
+ ? client._sender
91
+ : await client.getSender(dcId);
92
+ }
93
+ else if (e instanceof __1.errors.MsgWaitError) {
94
+ // We need to resend this after the old one was confirmed.
95
+ await state.isReady();
96
+ state.after = undefined;
97
+ }
98
+ else if (e.message === "CONNECTION_NOT_INITED") {
99
+ await client.disconnect();
100
+ await (0, Helpers_1.sleep)(2000);
101
+ await client.connect();
102
+ }
103
+ else {
104
+ state.finished.resolve();
105
+ throw e;
106
+ }
107
+ }
108
+ state.resetPromise();
109
+ }
110
+ throw new Error(`Request was unsuccessful ${attempt} time(s)`);
111
+ }
112
+ /** @hidden */
113
+ async function getMe(client, inputPeer) {
114
+ if (inputPeer && client._selfInputPeer) {
115
+ return client._selfInputPeer;
116
+ }
117
+ const me = (await client.invoke(new tl_1.Api.users.GetUsers({ id: [new tl_1.Api.InputUserSelf()] })))[0];
118
+ client._bot = me.bot;
119
+ if (!client._selfInputPeer) {
120
+ client._selfInputPeer = __1.utils.getInputPeer(me, false);
121
+ }
122
+ return inputPeer
123
+ ? client._selfInputPeer
124
+ : me;
125
+ }
126
+ /** @hidden */
127
+ async function isBot(client) {
128
+ if (client._bot === undefined) {
129
+ const me = await client.getMe();
130
+ if (me) {
131
+ return !(me instanceof tl_1.Api.InputPeerUser) ? me.bot : undefined;
132
+ }
133
+ }
134
+ return client._bot;
135
+ }
136
+ /** @hidden */
137
+ async function isUserAuthorized(client) {
138
+ try {
139
+ await client.invoke(new tl_1.Api.updates.GetState());
140
+ return true;
141
+ }
142
+ catch (e) {
143
+ return false;
144
+ }
145
+ }
146
+ /** @hidden */
147
+ async function getEntity(client, entity) {
148
+ const single = !(0, Helpers_1.isArrayLike)(entity);
149
+ let entityArray = [];
150
+ if ((0, Helpers_1.isArrayLike)(entity)) {
151
+ entityArray = entity;
152
+ }
153
+ else {
154
+ entityArray.push(entity);
155
+ }
156
+ const inputs = [];
157
+ for (const x of entityArray) {
158
+ if (typeof x === "string") {
159
+ const valid = (0, Utils_1.parseID)(x);
160
+ if (valid) {
161
+ inputs.push(await client.getInputEntity(valid));
162
+ }
163
+ else {
164
+ inputs.push(x);
165
+ }
166
+ }
167
+ else {
168
+ inputs.push(await client.getInputEntity(x));
169
+ }
170
+ }
171
+ const lists = new Map([
172
+ [Helpers_1._EntityType.USER, []],
173
+ [Helpers_1._EntityType.CHAT, []],
174
+ [Helpers_1._EntityType.CHANNEL, []],
175
+ ]);
176
+ for (const x of inputs) {
177
+ try {
178
+ lists.get((0, Helpers_1._entityType)(x)).push(x);
179
+ }
180
+ catch (e) { }
181
+ }
182
+ let users = lists.get(Helpers_1._EntityType.USER);
183
+ let chats = lists.get(Helpers_1._EntityType.CHAT);
184
+ let channels = lists.get(Helpers_1._EntityType.CHANNEL);
185
+ if (users.length) {
186
+ users = await client.invoke(new tl_1.Api.users.GetUsers({
187
+ id: users,
188
+ }));
189
+ }
190
+ if (chats.length) {
191
+ const chatIds = chats.map((x) => x.chatId);
192
+ chats = (await client.invoke(new tl_1.Api.messages.GetChats({ id: chatIds }))).chats;
193
+ }
194
+ if (channels.length) {
195
+ channels = (await client.invoke(new tl_1.Api.channels.GetChannels({ id: channels }))).chats;
196
+ }
197
+ const idEntity = new Map();
198
+ for (const user of users) {
199
+ idEntity.set((0, Utils_1.getPeerId)(user), user);
200
+ }
201
+ for (const channel of channels) {
202
+ idEntity.set((0, Utils_1.getPeerId)(channel), channel);
203
+ }
204
+ for (const chat of chats) {
205
+ idEntity.set((0, Utils_1.getPeerId)(chat), chat);
206
+ }
207
+ const result = [];
208
+ for (const x of inputs) {
209
+ if (typeof x === "string") {
210
+ result.push(await _getEntityFromString(client, x));
211
+ }
212
+ else if (!(x instanceof tl_1.Api.InputPeerSelf)) {
213
+ result.push(idEntity.get((0, Utils_1.getPeerId)(x)));
214
+ }
215
+ else {
216
+ for (const [key, u] of idEntity.entries()) {
217
+ if (u instanceof tl_1.Api.User && u.self) {
218
+ result.push(u);
219
+ break;
220
+ }
221
+ }
222
+ }
223
+ }
224
+ return single ? result[0] : result;
225
+ }
226
+ /** @hidden */
227
+ async function getInputEntity(client, peer) {
228
+ // Short-circuit if the input parameter directly maps to an InputPeer
229
+ try {
230
+ return __1.utils.getInputPeer(peer);
231
+ // eslint-disable-next-line no-empty
232
+ }
233
+ catch (e) { }
234
+ // Next in priority is having a peer (or its ID) cached in-memory
235
+ try {
236
+ if (typeof peer == "string") {
237
+ const valid = (0, Utils_1.parseID)(peer);
238
+ if (valid) {
239
+ const res = client._entityCache.get(peer);
240
+ if (res) {
241
+ return res;
242
+ }
243
+ }
244
+ }
245
+ if (typeof peer === "number" ||
246
+ typeof peer === "bigint" ||
247
+ big_integer_1.default.isInstance(peer)) {
248
+ const res = client._entityCache.get(peer.toString());
249
+ if (res) {
250
+ return res;
251
+ }
252
+ }
253
+ // 0x2d45687 == crc32(b'Peer')
254
+ if (typeof peer == "object" &&
255
+ !big_integer_1.default.isInstance(peer) &&
256
+ peer.SUBCLASS_OF_ID === 0x2d45687) {
257
+ const res = client._entityCache.get(__1.utils.getPeerId(peer));
258
+ if (res) {
259
+ return res;
260
+ }
261
+ }
262
+ // eslint-disable-next-line no-empty
263
+ }
264
+ catch (e) { }
265
+ // Then come known strings that take precedence
266
+ if (typeof peer == "string") {
267
+ if (["me", "this", "self"].includes(peer)) {
268
+ return new tl_1.Api.InputPeerSelf();
269
+ }
270
+ }
271
+ // No InputPeer, cached peer, or known string. Fetch from disk cache
272
+ try {
273
+ if (peer != undefined) {
274
+ return client.session.getInputEntity(peer);
275
+ }
276
+ // eslint-disable-next-line no-empty
277
+ }
278
+ catch (e) { }
279
+ // Only network left to try
280
+ if (typeof peer === "string") {
281
+ return __1.utils.getInputPeer(await _getEntityFromString(client, peer));
282
+ }
283
+ // If we're a bot and the user has messaged us privately users.getUsers
284
+ // will work with accessHash = 0. Similar for channels.getChannels.
285
+ // If we're not a bot but the user is in our contacts, it seems to work
286
+ // regardless. These are the only two special-cased requests.
287
+ if (typeof peer === "number") {
288
+ peer = (0, Helpers_1.returnBigInt)(peer);
289
+ }
290
+ peer = __1.utils.getPeer(peer);
291
+ if (peer instanceof tl_1.Api.PeerUser) {
292
+ const users = await client.invoke(new tl_1.Api.users.GetUsers({
293
+ id: [
294
+ new tl_1.Api.InputUser({
295
+ userId: peer.userId,
296
+ accessHash: big_integer_1.default.zero,
297
+ }),
298
+ ],
299
+ }));
300
+ if (users.length && !(users[0] instanceof tl_1.Api.UserEmpty)) {
301
+ // If the user passed a valid ID they expect to work for
302
+ // channels but would be valid for users, we get UserEmpty.
303
+ // Avoid returning the invalid empty input peer for that.
304
+ //
305
+ // We *could* try to guess if it's a channel first, and if
306
+ // it's not, work as a chat and try to validate it through
307
+ // another request, but that becomes too much work.
308
+ return __1.utils.getInputPeer(users[0]);
309
+ }
310
+ }
311
+ else if (peer instanceof tl_1.Api.PeerChat) {
312
+ return new tl_1.Api.InputPeerChat({
313
+ chatId: peer.chatId,
314
+ });
315
+ }
316
+ else if (peer instanceof tl_1.Api.PeerChannel) {
317
+ try {
318
+ const channels = await client.invoke(new tl_1.Api.channels.GetChannels({
319
+ id: [
320
+ new tl_1.Api.InputChannel({
321
+ channelId: peer.channelId,
322
+ accessHash: big_integer_1.default.zero,
323
+ }),
324
+ ],
325
+ }));
326
+ return __1.utils.getInputPeer(channels.chats[0]);
327
+ }
328
+ catch (e) {
329
+ if (client._errorHandler) {
330
+ await client._errorHandler(e);
331
+ }
332
+ if (client._log.canSend(Logger_1.LogLevel.ERROR)) {
333
+ console.error(e);
334
+ }
335
+ }
336
+ }
337
+ throw new Error(`Could not find the input entity for ${JSON.stringify(peer)}.
338
+ Please read https://` +
339
+ "docs.telethon.dev/en/stable/concepts/entities.html to" +
340
+ " find out more details.");
341
+ }
342
+ /** @hidden */
343
+ async function _getEntityFromString(client, string) {
344
+ const phone = __1.utils.parsePhone(string);
345
+ if (phone) {
346
+ try {
347
+ const result = await client.invoke(new tl_1.Api.contacts.GetContacts({
348
+ hash: big_integer_1.default.zero,
349
+ }));
350
+ if (!(result instanceof tl_1.Api.contacts.ContactsNotModified)) {
351
+ for (const user of result.users) {
352
+ if (user instanceof tl_1.Api.User && user.phone === phone) {
353
+ return user;
354
+ }
355
+ }
356
+ }
357
+ }
358
+ catch (e) {
359
+ if (e.errorMessage === "BOT_METHOD_INVALID") {
360
+ throw new Error("Cannot get entity by phone number as a " +
361
+ "bot (try using integer IDs, not strings)");
362
+ }
363
+ throw e;
364
+ }
365
+ }
366
+ const id = __1.utils.parseID(string);
367
+ if (id != undefined) {
368
+ return getInputEntity(client, id);
369
+ }
370
+ else if (["me", "this"].includes(string.toLowerCase())) {
371
+ return client.getMe();
372
+ }
373
+ else {
374
+ const { username, isInvite } = __1.utils.parseUsername(string);
375
+ if (isInvite) {
376
+ const invite = await client.invoke(new tl_1.Api.messages.CheckChatInvite({
377
+ hash: username,
378
+ }));
379
+ if (invite instanceof tl_1.Api.ChatInvite) {
380
+ throw new Error("Cannot get entity from a channel (or group) " +
381
+ "that you are not part of. Join the group and retry");
382
+ }
383
+ else if (invite instanceof tl_1.Api.ChatInviteAlready) {
384
+ return invite.chat;
385
+ }
386
+ }
387
+ else if (username) {
388
+ try {
389
+ const result = await client.invoke(new tl_1.Api.contacts.ResolveUsername({ username: username }));
390
+ const pid = __1.utils.getPeerId(result.peer, false);
391
+ if (result.peer instanceof tl_1.Api.PeerUser) {
392
+ for (const x of result.users) {
393
+ if ((0, Helpers_1.returnBigInt)(x.id).equals((0, Helpers_1.returnBigInt)(pid))) {
394
+ return x;
395
+ }
396
+ }
397
+ }
398
+ else {
399
+ for (const x of result.chats) {
400
+ if ((0, Helpers_1.returnBigInt)(x.id).equals((0, Helpers_1.returnBigInt)(pid))) {
401
+ return x;
402
+ }
403
+ }
404
+ }
405
+ }
406
+ catch (e) {
407
+ if (e.errorMessage === "USERNAME_NOT_OCCUPIED") {
408
+ throw new Error(`No user has "${username}" as username`);
409
+ }
410
+ throw e;
411
+ }
412
+ }
413
+ }
414
+ throw new Error(`Cannot find any entity corresponding to "${string}"`);
415
+ }
416
+ /** @hidden */
417
+ async function getPeerId(client, peer, addMark = true) {
418
+ if (typeof peer == "string") {
419
+ const valid = (0, Utils_1.parseID)(peer);
420
+ if (valid) {
421
+ return __1.utils.getPeerId(peer, addMark);
422
+ }
423
+ else {
424
+ peer = await client.getInputEntity(peer);
425
+ }
426
+ }
427
+ if (typeof peer == "number" ||
428
+ typeof peer == "bigint" ||
429
+ big_integer_1.default.isInstance(peer)) {
430
+ return __1.utils.getPeerId(peer, addMark);
431
+ }
432
+ if (peer.SUBCLASS_OF_ID == 0x2d45687 || peer.SUBCLASS_OF_ID == 0xc91c90b6) {
433
+ peer = await client.getInputEntity(peer);
434
+ }
435
+ if (peer instanceof tl_1.Api.InputPeerSelf) {
436
+ peer = await client.getMe(true);
437
+ }
438
+ return __1.utils.getPeerId(peer, addMark);
439
+ }
440
+ /** @hidden */
441
+ async function _getPeer(client, peer) {
442
+ if (!peer) {
443
+ return undefined;
444
+ }
445
+ const [i, cls] = __1.utils.resolveId((0, Helpers_1.returnBigInt)(await client.getPeerId(peer)));
446
+ return new cls({
447
+ userId: i,
448
+ channelId: i,
449
+ chatId: i,
450
+ });
451
+ }
452
+ /** @hidden */
453
+ async function _getInputDialog(client, dialog) {
454
+ try {
455
+ if (dialog.SUBCLASS_OF_ID == 0xa21c9795) {
456
+ // crc32(b'InputDialogPeer')
457
+ dialog.peer = await client.getInputEntity(dialog.peer);
458
+ return dialog;
459
+ }
460
+ else if (dialog.SUBCLASS_OF_ID == 0xc91c90b6) {
461
+ //crc32(b'InputPeer')
462
+ return new tl_1.Api.InputDialogPeer({
463
+ peer: dialog,
464
+ });
465
+ }
466
+ }
467
+ catch (e) { }
468
+ return new tl_1.Api.InputDialogPeer({
469
+ peer: dialog,
470
+ });
471
+ }
472
+ /** @hidden */
473
+ async function _getInputNotify(client, notify) {
474
+ try {
475
+ if (notify.SUBCLASS_OF_ID == 0x58981615) {
476
+ if (notify instanceof tl_1.Api.InputNotifyPeer) {
477
+ notify.peer = await client.getInputEntity(notify.peer);
478
+ }
479
+ return notify;
480
+ }
481
+ }
482
+ catch (e) { }
483
+ return new tl_1.Api.InputNotifyPeer({
484
+ peer: await client.getInputEntity(notify),
485
+ });
486
+ }
487
+ /** @hidden */
488
+ function _selfId(client) {
489
+ return client._selfInputPeer ? client._selfInputPeer.userId : undefined;
490
+ }
@@ -0,0 +1,19 @@
1
+ import bigInt from "big-integer";
2
+ export declare class AuthKey {
3
+ private _key?;
4
+ private _hash?;
5
+ private auxHash?;
6
+ keyId?: bigInt.BigInteger;
7
+ constructor(value?: Buffer, hash?: Buffer);
8
+ setKey(value?: Buffer | AuthKey): Promise<void>;
9
+ waitForKey(): Promise<void>;
10
+ getKey(): Buffer<ArrayBufferLike> | undefined;
11
+ /**
12
+ * Calculates the new nonce hash based on the current class fields' values
13
+ * @param newNonce
14
+ * @param number
15
+ * @returns {bigInt.BigInteger}
16
+ */
17
+ calcNewNonceHash(newNonce: bigInt.BigInteger, number: number): Promise<bigInt.BigInteger>;
18
+ equals(other: AuthKey): boolean | undefined;
19
+ }
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthKey = void 0;
4
+ const Helpers_1 = require("../Helpers");
5
+ const extensions_1 = require("../extensions");
6
+ const Helpers_2 = require("../Helpers");
7
+ class AuthKey {
8
+ constructor(value, hash) {
9
+ if (!hash || !value) {
10
+ return;
11
+ }
12
+ this._key = value;
13
+ this._hash = hash;
14
+ const reader = new extensions_1.BinaryReader(hash);
15
+ this.auxHash = reader.readLong(false);
16
+ reader.read(4);
17
+ this.keyId = reader.readLong(false);
18
+ }
19
+ async setKey(value) {
20
+ if (!value) {
21
+ this._key = this.auxHash = this.keyId = this._hash = undefined;
22
+ return;
23
+ }
24
+ if (value instanceof AuthKey) {
25
+ this._key = value._key;
26
+ this.auxHash = value.auxHash;
27
+ this.keyId = value.keyId;
28
+ this._hash = value._hash;
29
+ return;
30
+ }
31
+ this._key = value;
32
+ this._hash = await (0, Helpers_1.sha1)(this._key);
33
+ const reader = new extensions_1.BinaryReader(this._hash);
34
+ this.auxHash = reader.readLong(false);
35
+ reader.read(4);
36
+ this.keyId = reader.readLong(false);
37
+ }
38
+ async waitForKey() {
39
+ while (!this.keyId) {
40
+ await (0, Helpers_2.sleep)(20);
41
+ }
42
+ }
43
+ getKey() {
44
+ return this._key;
45
+ }
46
+ // TODO : This doesn't really fit here, it's only used in authentication
47
+ /**
48
+ * Calculates the new nonce hash based on the current class fields' values
49
+ * @param newNonce
50
+ * @param number
51
+ * @returns {bigInt.BigInteger}
52
+ */
53
+ async calcNewNonceHash(newNonce, number) {
54
+ if (this.auxHash) {
55
+ const nonce = (0, Helpers_1.toSignedLittleBuffer)(newNonce, 32);
56
+ const n = Buffer.alloc(1);
57
+ n.writeUInt8(number, 0);
58
+ const data = Buffer.concat([
59
+ nonce,
60
+ Buffer.concat([n, (0, Helpers_1.readBufferFromBigInt)(this.auxHash, 8, true)]),
61
+ ]);
62
+ // Calculates the message key from the given data
63
+ const shaData = (await (0, Helpers_1.sha1)(data)).slice(4, 20);
64
+ return (0, Helpers_1.readBigIntFromBuffer)(shaData, true, true);
65
+ }
66
+ throw new Error("Auth key not set");
67
+ }
68
+ equals(other) {
69
+ var _a;
70
+ return (other instanceof this.constructor &&
71
+ this._key &&
72
+ Buffer.isBuffer(other.getKey()) &&
73
+ ((_a = other.getKey()) === null || _a === void 0 ? void 0 : _a.equals(this._key)));
74
+ }
75
+ }
76
+ exports.AuthKey = AuthKey;
@@ -0,0 +1,5 @@
1
+ export declare class CTR {
2
+ private cipher;
3
+ constructor(key: Buffer, iv: Buffer);
4
+ encrypt(data: any): Buffer<any>;
5
+ }
package/crypto/CTR.js ADDED
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CTR = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ class CTR {
6
+ constructor(key, iv) {
7
+ if (!Buffer.isBuffer(key) || !Buffer.isBuffer(iv) || iv.length !== 16) {
8
+ throw new Error("Key and iv need to be a buffer");
9
+ }
10
+ this.cipher = (0, node_crypto_1.createCipheriv)("AES-256-CTR", key, iv);
11
+ }
12
+ encrypt(data) {
13
+ return Buffer.from(this.cipher.update(data));
14
+ }
15
+ }
16
+ exports.CTR = CTR;
@@ -0,0 +1,19 @@
1
+ import bigInt from "big-integer";
2
+ export declare class Factorizator {
3
+ /**
4
+ * Calculates the greatest common divisor
5
+ * @param a {BigInteger}
6
+ * @param b {BigInteger}
7
+ * @returns {BigInteger}
8
+ */
9
+ static gcd(a: bigInt.BigInteger, b: bigInt.BigInteger): bigInt.BigInteger;
10
+ /**
11
+ * Factorizes the given number and returns both the divisor and the number divided by the divisor
12
+ * @param pq {BigInteger}
13
+ * @returns {{p: *, q: *}}
14
+ */
15
+ static factorize(pq: bigInt.BigInteger): {
16
+ p: bigInt.BigInteger;
17
+ q: bigInt.BigInteger;
18
+ };
19
+ }