This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: telegram webapps support (#158)
- Loading branch information
1 parent
132748d
commit bdcdeab
Showing
23 changed files
with
571 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,6 @@ | |
"web3.storage": "4.5.4" | ||
}, | ||
"devDependencies": { | ||
"@vercel/node": "2.10.3" | ||
"@vercel/node": "2.12.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { webcrypto } from 'crypto'; | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
type Data = { ok: boolean } | { error: string }; | ||
|
||
async function isHashValid(data: Record<string, string>, botToken: string) { | ||
const encoder = new TextEncoder(); | ||
|
||
const checkString = Object.keys(data) | ||
.filter((key) => key !== `hash`) | ||
.map((key) => `${key}=${data[key]}`) | ||
.sort() | ||
.join(`\n`); | ||
|
||
const secretKey = await webcrypto.subtle.importKey( | ||
`raw`, | ||
encoder.encode(`WebAppData`), | ||
{ name: `HMAC`, hash: `SHA-256` }, | ||
true, | ||
[`sign`], | ||
); | ||
|
||
const secret = await webcrypto.subtle.sign( | ||
`HMAC`, | ||
secretKey, | ||
encoder.encode(botToken), | ||
); | ||
|
||
const signatureKey = await webcrypto.subtle.importKey( | ||
`raw`, | ||
secret, | ||
{ name: `HMAC`, hash: `SHA-256` }, | ||
true, | ||
[`sign`], | ||
); | ||
|
||
const signature = await webcrypto.subtle.sign( | ||
`HMAC`, | ||
signatureKey, | ||
encoder.encode(checkString), | ||
); | ||
|
||
const hex = Buffer.from(signature).toString(`hex`); | ||
|
||
return data.hash === hex; | ||
} | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse<Data>, | ||
) { | ||
if (req.method !== `POST`) { | ||
return res.status(405).json({ error: `Method not allowed` }); | ||
} | ||
|
||
if (!req.body.hash) { | ||
return res.status(400).json({ | ||
error: `Missing required field hash`, | ||
}); | ||
} | ||
|
||
if (!process.env.BOT_TOKEN) { | ||
return res.status(500).json({ error: `Internal server error` }); | ||
} | ||
|
||
const data = Object.fromEntries(new URLSearchParams(req.body.hash)); | ||
const isValid = await isHashValid(data, process.env.BOT_TOKEN); | ||
|
||
if (isValid) { | ||
return res.status(200).json({ ok: true }); | ||
} | ||
|
||
return res.status(403).json({ error: `Invalid hash` }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
bdcdeab
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deploy preview for algoworld-swapper ready!
✅ Preview
https://algoworld-swapper-mequat0b9-algoworldexplorer.vercel.app
Built with commit bdcdeab.
This pull request is being automatically deployed with vercel-action