'use strict'; var node_crypto = require('node:crypto'); function parseInitData(query) { const searchParams = new URLSearchParams(query); const userData = searchParams.get("user"); const userParsedData = userData ? JSON.parse(userData) : void 0; const receiverData = searchParams.get("receiver"); const receiverParsedData = receiverData ? JSON.parse(receiverData) : void 0; const chatData = searchParams.get("chat"); const chatParsedData = chatData ? JSON.parse(chatData) : void 0; const optionalData = { queryId: searchParams.get("query_id") ?? void 0, user: userParsedData ? { id: userParsedData.id, firstName: userParsedData.first_name, lastName: userParsedData.last_name, username: userParsedData.username, isPremium: userParsedData.is_premium, addedToAttachmentMenu: userParsedData.added_to_attachment_menu, allowsWriteToPm: userParsedData.allows_write_to_pm, photoUrl: userParsedData.photoUrl, languageCode: userParsedData.language_code } : void 0, receiver: receiverParsedData ? { id: receiverParsedData.id, firstName: receiverParsedData.first_name, lastName: receiverParsedData.last_name, username: receiverParsedData.username, isPremium: receiverParsedData.is_premium, addedToAttachmentMenu: receiverParsedData.added_to_attachment_menu, allowsWriteToPm: receiverParsedData.allows_write_to_pm, photoUrl: receiverParsedData.photoUrl, isBot: receiverParsedData.is_bot } : void 0, chat: chatParsedData ? { id: chatParsedData.id, type: chatParsedData.type, title: chatParsedData.title, username: chatParsedData.username, photoUrl: chatParsedData.photo_url } : void 0, chatType: searchParams.get("chat_type") ?? void 0, chatInstance: searchParams.get("chat_instance") ?? void 0, startParam: searchParams.get("start_param") ?? void 0, canSendAfter: searchParams.get("can_send_after") ? Number(searchParams.get("can_send_after")) : void 0, authDate: searchParams.get("auth_date") ? Number(searchParams.get("auth_date")) : void 0, hash: searchParams.get("hash") ?? void 0 }; if (!optionalData.authDate || !optionalData.hash) throw new Error("Invalid data at parseInitData"); return optionalData; } function validateInitData(webAppInitData, token) { const { hash, authDate, ...data } = Object.fromEntries( new URLSearchParams(webAppInitData) ); const sortedData = Object.keys(data).sort().reduce( (acc, key) => { acc[key] = data[key]; return acc; }, {} ); const dataCheckString = Object.entries(sortedData).map(([key, value]) => `${key}=${value}`).join("\n"); const secretKey = node_crypto.createHmac("sha256", "WebAppData").update(token).digest(); const calculatedHash = node_crypto.createHmac("sha256", secretKey).update(dataCheckString).digest("hex"); if (hash !== calculatedHash) return false; return true; } function validateAndParseInitData(query, token) { const result = validateInitData(query, token); if (!result) return false; return parseInitData(query); } exports.parseInitData = parseInitData; exports.validateAndParseInitData = validateAndParseInitData; exports.validateInitData = validateInitData;