-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
150 lines (138 loc) · 4.08 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { useEffect, useState } from "react";
import { useSDK } from "@metamask/sdk-react";
import "./App.css";
import litLogo from "./assets/lit.png";
import {
getSessionSignatures,
connectToLitNodes,
mintNewPkp,
} from "./litConnections";
import {
isRecent,
verifyInitData,
} from "./telegramAuthHelpers";
interface TelegramWebApp {
ready: () => void;
showPopup: (params: {
title?: string;
message: string;
buttons: Array<{ text: string; type: string }>;
}) => void;
initData: string;
}
declare global {
interface Window {
Telegram?: {
WebApp?: TelegramWebApp;
};
}
}
function App() {
const [webApp, setWebApp] = useState<TelegramWebApp | null>(null);
const [account, setAccount] = useState<string | null>(null);
const [sessionSignatures, setSessionSignatures] = useState<any | null>(null);
const [valid, setValid] = useState<boolean | null>(null);
const [recent, setRecent] = useState<boolean | null>(null);
const [data, setData] = useState<any | null>(null);
const [pkp, setPkp] = useState<{
tokenId: any;
publicKey: string;
ethAddress: string;
} | null>(null);
const { sdk, connected, provider } = useSDK();
useEffect(() => {
const tgApp = window.Telegram?.WebApp;
if (tgApp) {
tgApp.ready();
setWebApp(tgApp);
setData(tgApp.initData);
isRecent(tgApp.initData).then((isRecent) => {
setRecent(isRecent);
});
verifyInitData(tgApp.initData, import.meta.env.VITE_TELEGRAM_BOT_SECRET)
.then(( isVerified ) => {
setValid(isVerified);
})
.catch((error) => {
console.error("Error verifying init data:", error);
});
}
}, []);
const connect = async () => {
try {
const accounts = await sdk?.connect();
setAccount(accounts?.[0]);
webApp!.showPopup({
title: "Connected",
message: `Connected to MetaMask with account: ${accounts[0]}`,
buttons: [{ text: "Close", type: "close" }],
});
} catch (err) {
console.warn("failed to connect..", err);
}
};
const mintPkp = async () => {
const pkp = await mintNewPkp(provider);
setPkp(pkp);
};
const getSessionSigs = async () => {
const litNodeClient = await connectToLitNodes();
const sessionSignatures = await getSessionSignatures(
litNodeClient,
pkp,
provider,
data
);
setSessionSignatures(sessionSignatures);
};
return (
<div className="App">
<header className="App-header">
<img src={litLogo} className="App-logo" alt="logo" />
<h1>Telegram Mini App</h1>
</header>
{
<div>
<h3>Telegram User Data Validity:</h3>
<pre> Recent: {JSON.stringify(recent, null, 2)}</pre>
<pre> Valid: {JSON.stringify(valid, null, 2)}</pre>
</div>
}
<button style={{ padding: 10, margin: 10 }} onClick={connect}>
{connected ? "Connect to MetaMask" : "Connected"}
</button>
{connected && <div>{account && `Connected account: ${account}`}</div>}
{connected && (
<button style={{ padding: 10, margin: 10 }} onClick={getSessionSigs}>
Get Session Signatures
</button>
)}
{sessionSignatures && (
<div className="boxed-code-display">
<div className="boxed-code-display__container">
<h2 className="boxed-code-display__title">Session Signatures:</h2>
<pre className="boxed-code-display__content">
{JSON.stringify(sessionSignatures, null, 2)}
</pre>
</div>
</div>
)}
{connected && (
<button style={{ padding: 10, margin: 10 }} onClick={mintPkp}>
Mint PKP
</button>
)}
{pkp && (
<div className="boxed-code-display">
<div className="boxed-code-display__container">
<h2 className="boxed-code-display__title">PKP:</h2>
<pre className="boxed-code-display__content">
{JSON.stringify(pkp, null, 2)}
</pre>
</div>
</div>
)}
</div>
);
}
export default App;