generated from mauriciobraz/next.js-telegram-webapp
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge commit '4d2706e3ffe74a6d01deb880ac865a1ede9f11ac' into dev
- Loading branch information
Showing
13 changed files
with
1,031 additions
and
221 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 |
---|---|---|
|
@@ -11,4 +11,4 @@ else | |
# Don't build | ||
echo "🛑 - Build cancelled" | ||
exit 0; | ||
fi | ||
fi |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
function canUseStorage(version: string) { | ||
return versionCompare(version, '6.9') >= 0; | ||
} | ||
|
||
function versionCompare(v1: string, v2: string): number { | ||
if (typeof v1 !== 'string') v1 = ''; | ||
if (typeof v2 !== 'string') v2 = ''; | ||
const _v1 = v1.replace(/^\s+|\s+$/g, '').split('.'); | ||
const _v2 = v2.replace(/^\s+|\s+$/g, '').split('.'); | ||
let a = Math.max(_v1.length, _v2.length), | ||
i, | ||
p1, | ||
p2; | ||
for (i = 0; i < a; i++) { | ||
p1 = parseInt(_v1[i], 10) || 0; | ||
p2 = parseInt(_v2[i], 10) || 0; | ||
if (p1 === p2) continue; | ||
if (p1 > p2) return 1; | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
export function saveToTelegramStorage(window: Window, key: string, value: string) { | ||
if (canUseStorage(window.Telegram.WebApp.version)) { | ||
window.Telegram.WebApp.CloudStorage.setItem(key, value); | ||
} else { | ||
localStorage.setItem(key, value); | ||
} | ||
} | ||
|
||
export function getFromTelegramStorage(window: Window, key: string): string | null { | ||
if (canUseStorage(window.Telegram.WebApp.version)) { | ||
return window.Telegram.WebApp.CloudStorage.getItem(key); | ||
} | ||
return localStorage.getItem(key); | ||
} | ||
|
||
export function isUseApi(): boolean { | ||
const type = getFromTelegramStorage(window, 'dataStorageType'); | ||
return type === 'backend+ton' || type === 'backend'; | ||
} | ||
|
||
export function isUseTon(): boolean { | ||
const type = getFromTelegramStorage(window, 'dataStorageType'); | ||
return type === 'backend+ton' || type === 'ton'; | ||
} | ||
|
||
export function getUserTonPrivateKey(): string | null { | ||
return getFromTelegramStorage(window, 'privateKey'); | ||
} |
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,32 @@ | ||
import { KEYUTIL, KJUR, RSAKey } from 'jsrsasign'; | ||
|
||
export function generatePrivateKey(): string { | ||
// Generate an RSA key pair | ||
const rsaKey = KEYUTIL.generateKeypair('RSA', 1024); | ||
|
||
// Export the keys to PEM format | ||
return KEYUTIL.getPEM(rsaKey.prvKeyObj, 'PKCS8PRV'); | ||
} | ||
|
||
export function derivePublicKey(privateKeyPEM: string) { | ||
const privKey = KEYUTIL.getKey(privateKeyPEM) as RSAKey; | ||
const privKeyJWK = KEYUTIL.getJWKFromKey(privKey); | ||
let pubKeyJWK; | ||
{ | ||
const { e, kty, n } = privKeyJWK; | ||
pubKeyJWK = { e, kty, n }; | ||
} | ||
const pubKey = KEYUTIL.getKey(pubKeyJWK); | ||
const pubKeyPEMText = KEYUTIL.getPEM(pubKey); | ||
return pubKeyPEMText; | ||
} | ||
|
||
export function encryptData(data: string, publicKeyPEM: string): string { | ||
const publicKey = KEYUTIL.getKey(publicKeyPEM) as RSAKey; | ||
return KJUR.crypto.Cipher.encrypt(data, publicKey, 'RSAOAEP'); | ||
} | ||
|
||
export function decryptData(data: string, privateKeyPEM: string): string { | ||
const privateKey = KEYUTIL.getKey(privateKeyPEM) as RSAKey; | ||
return KJUR.crypto.Cipher.decrypt(data, privateKey, 'RSAOAEP'); | ||
} |
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.