-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnect-wallet-menu.js
129 lines (110 loc) · 3.4 KB
/
connect-wallet-menu.js
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
const ton = require('@tonconnect/sdk')
const qr = require("qrcode")
const _bot = require("./bot");
const _wallet = require("./ton/wallets");
const _ctr = require("./ton/connector")
const utils = require("./utils")
const fs = require("fs")
const walletMenuCallbacks = {
chose_wallet: onChooseWalletClick,
select_wallet: onWalletClick,
universal_qr: onOpenUniversalQRClick
};
async function onChooseWalletClick(query) {
const wallets = await _wallet.getWallets();
await _bot.editMessageReplyMarkup(
{
inline_keyboard: [
wallets.map(wallet => ({
text: wallet.name,
callback_data: JSON.stringify({ method: 'select_wallet', data: wallet.appName })
})),
[
{
text: '« Back',
callback_data: JSON.stringify({
method: 'universal_qr'
})
}
]
]
},
{
message_id: query.message?.message_id,
chat_id: query.message?.chat.id
}
);
}
async function onOpenUniversalQRClick(query){
const chatId = query.message?.chat.id;
const wallets = await _wallet.getWallets();
const connector = _ctr.getConnector(chatId);
const link = connector.connect(wallets);
await editQR(query.message, link);
const keyboard = await utils.buildUniversalKeyboard(link, wallets);
await _bot.editMessageReplyMarkup(
{
inline_keyboard: [keyboard]
},
{
message_id: query.message?.message_id,
chat_id: query.message?.chat.id
}
);
}
async function onWalletClick(query, data){
const chatId = query.message?.chat.id;
const connector = _ctr.getConnector(chatId);
const selectedWallet = await _wallet.getWalletInfo(data);
if (!selectedWallet) {
return;
}
let buttonLink = connector.connect({
bridgeUrl: selectedWallet.bridgeUrl,
universalLink: selectedWallet.universalLink
});
let qrLink = buttonLink;
if (ton.isTelegramUrl(selectedWallet.universalLink)) {
buttonLink = utils.addTGReturnStrategy(buttonLink, process.env.TELEGRAM__bot_LINK);
qrLink = utils.addTGReturnStrategy(qrLink, 'none');
}
await editQR(query.message, qrLink);
await _bot.editMessageReplyMarkup(
{
inline_keyboard: [
[
{
text: '« Back',
callback_data: JSON.stringify({ method: 'chose_wallet' })
},
{
text: `Open ${selectedWallet.name}`,
url: buttonLink
}
]
]
},
{
message_id: query.message?.message_id,
chat_id: chatId
}
);
}
async function editQR(message, link){
const fileName = 'QR-code-' + Math.round(Math.random() * 10000000000);
await qr.toFile(`./${fileName}`, link);
await _bot.editMessageMedia(
{
type: 'photo',
media: `attach://${fileName}`
},
{
message_id: message?.message_id,
chat_id: message?.chat.id
}
);
await new Promise(r => fs.rm(`./${fileName}`, r));
}
module.exports = {
walletMenuCallbacks
}