UNPKG

@ton-hard-core/nest-twa

Version:
41 lines 1.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TwaService = void 0; const tslib_1 = require("tslib"); const crypto_1 = require("crypto"); const common_1 = require("@nestjs/common"); const domain_1 = require("./domain"); const twa_options_1 = require("./twa.options"); let TwaService = class TwaService { options; constructor(options) { this.options = options; } validateData(dataBase64) { const data = domain_1.TelegramDataRaw.fromToken(dataBase64); const { hash, ...other } = data; if (!hash) { return false; } const originalHash = Buffer.from(hash, 'hex'); const checkString = Object.keys(other) .sort() // eslint-disable-next-line @typescript-eslint/ban-ts-comment -- todo // @ts-ignore .map(key => `${key}=${other[key]}`) .join('\n'); const hmacKey = (0, crypto_1.createHmac)('sha256', 'WebAppData') .update(Buffer.from(this.options.token, 'utf8')) .digest(); const hmac = (0, crypto_1.createHmac)('sha256', hmacKey); hmac.update(checkString); const computedHash = hmac.digest(); return (0, crypto_1.timingSafeEqual)(computedHash, originalHash); } }; exports.TwaService = TwaService; exports.TwaService = TwaService = tslib_1.__decorate([ (0, common_1.Injectable)(), tslib_1.__metadata("design:paramtypes", [twa_options_1.TwaOptions]) ], TwaService); //# sourceMappingURL=twa.service.js.map